FIFO Circular List. Once the size is reached, it will overwrite previous entries
| 26 | * <p>Once the size is reached, it will overwrite previous entries |
| 27 | */ |
| 28 | public class CircularList<T> implements Iterable<T> { |
| 29 | private T[] data; |
| 30 | private int head = 0; |
| 31 | private int tail = 0; |
| 32 | private int size = 0; |
| 33 | |
| 34 | @SuppressWarnings("unchecked") |
| 35 | public CircularList(int size) { |
| 36 | data = (T[]) new Object[size]; |
| 37 | } |
| 38 | |
| 39 | @SuppressWarnings("unchecked") |
| 40 | public synchronized void resize(int newsize) { |
| 41 | if (newsize == this.size) return; |
| 42 | |
| 43 | T[] vals = (T[]) new Object[newsize]; |
| 44 | int i = 0; |
| 45 | if (newsize > size) { |
| 46 | for (i = 0; i < size; i++) { |
| 47 | vals[i] = data[convert(i)]; |
| 48 | } |
| 49 | } else { |
| 50 | int off = size - newsize; |
| 51 | for (i = 0; i < newsize; i++) { |
| 52 | vals[i] = data[convert(i + off)]; |
| 53 | } |
| 54 | } |
| 55 | data = vals; |
| 56 | head = 0; |
| 57 | tail = i; |
| 58 | } |
| 59 | |
| 60 | private int convert(int index) { |
| 61 | return (index + head) % data.length; |
| 62 | } |
| 63 | |
| 64 | public boolean isEmpty() { |
| 65 | return head == tail; // or size == 0 |
| 66 | } |
| 67 | |
| 68 | public int size() { |
| 69 | return size; |
| 70 | } |
| 71 | |
| 72 | public int getBufferSize() { |
| 73 | return data.length; |
| 74 | } |
| 75 | |
| 76 | private void checkIndex(int index) { |
| 77 | if (index >= size || index < 0) |
| 78 | throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); |
| 79 | } |
| 80 | |
| 81 | public T get(int index) { |
| 82 | checkIndex(index); |
| 83 | return data[convert(index)]; |
| 84 | } |
| 85 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…