Coverage Summary for Class: QueueFIFO (com.mygdx.utils)

Class Class, % Method, % Line, %
QueueFIFO 100% (1/1) 95.8% (23/24) 98.5% (64/65)


1 package com.mygdx.utils; 2  3 import java.lang.UnsupportedOperationException; 4  5 import java.util.*; 6  7 /** 8  * A First in first out queue 9  * 10  * @param <T> Data type to store 11  */ 12 public class QueueFIFO<T> implements Queue<T> { 13  private ArrayList<T> data; 14  private int topIndex; 15  16  /** 17  * Initialize all properties 18  */ 19  public QueueFIFO() { 20  topIndex = -1; 21  data = new ArrayList<>(); 22  } 23  24  //Implemented for Testing 25  public void set(ArrayList<T> setThis) { 26  data = setThis; 27  topIndex = size() -1; 28  } 29  30  public ArrayList<T> get() { 31  return data; } 32  33  public int getI() { 34  return topIndex; 35  } 36  //End of Testing Functions 37  38  @Override 39  public int size() { 40  return data.size(); 41  } 42  43  @Override 44  public boolean isEmpty() { 45  return data.isEmpty(); 46  } 47  48  @Override 49  public boolean contains(Object o) { 50  return data.contains(o); 51  } 52  53  @Override 54  public Iterator<T> iterator() { 55  return data.iterator(); 56  } 57  58  @Override 59  public Object[] toArray() { 60  return data.toArray(); 61  } 62  63  @Override 64  public <T1> T1[] toArray(T1[] a) { 65  Object[] queue = toArray(); 66  int newLength = a.length + queue.length; 67  68  T1[] arrNew = (T1[]) new Object[newLength]; 69  for(int i = 0; i < a.length; i++) { 70  arrNew[i] = a[i]; 71  } 72  73  for(int i = a.length; i < arrNew.length; i++) { 74  arrNew[i] = (T1) queue[i-a.length]; 75  } 76  77  return arrNew; 78  } 79  80  @Override 81  public boolean add(T t) { 82  topIndex++; 83  return data.add(t); 84  } 85  86  @Override 87  public boolean remove(Object o) { 88  if (isEmpty()) { 89  return false; 90  } 91  int i = data.indexOf(o); 92  if (i == -1) { 93  return false; 94  } 95  topIndex--; 96  data.remove(i); 97  return true; 98  } 99  100  public void remove(int index) { 101  if (isEmpty()) { 102  return; 103  } 104  topIndex--; 105  data.remove(index); 106  } 107  108  @Override 109  public boolean containsAll(Collection<?> c) { 110  return data.containsAll(c); 111  } 112  113  @Override 114  public boolean addAll(Collection<? extends T> c) { 115  boolean suc = data.addAll(c); 116  if (suc) { 117  topIndex = data.size() -1; 118  } 119  return suc; 120  } 121  122  @Override 123  public boolean removeAll(Collection<?> c) { 124  boolean suc = data.removeAll(c); 125  if (suc) { 126  topIndex = data.size() -1; 127  } 128  return suc; 129  } 130  131  @Override 132  public boolean retainAll(Collection<?> c) { 133  boolean suc = data.retainAll(c); 134  if (suc) { 135  topIndex = data.size() -1; 136  } 137  return suc; 138  } 139  140  @Override 141  public void clear() { 142  data.clear(); 143  topIndex = -1; 144  145  } 146  147  /** 148  * Not implemented 149  */ 150  @Override 151  public boolean offer(T t) { 152  throw new UnsupportedOperationException(); 153  } 154  155  @Override 156  public T remove() throws RuntimeException{ 157  if (isEmpty()) { 158  throw new RuntimeException("Queue is empty"); 159  } 160  T t = data.remove(topIndex); 161  topIndex--; 162  return t; 163  } 164  165  public T pop() { 166  return remove(); 167  } 168  169  @Override 170  public T poll() { 171  if (isEmpty()) { 172  return null; 173  } 174  topIndex--; 175  return data.remove(topIndex + 1); 176  } 177  178  @Override 179  public T element() throws RuntimeException{ 180  if (isEmpty()) { 181  throw new RuntimeException("Queue is empty"); 182  } 183  return data.get(topIndex); 184  } 185  186  @Override 187  public T peek() { 188  if (isEmpty()) { 189  return null; 190  } 191  return data.get(topIndex); 192  } 193 }