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

Class Class, % Method, % Line, %
QueueFIFO 100% (1/1) 76.2% (16/21) 75.5% (40/53)


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