sclass SynchronizedCircularFifoBuffer is Iterable, IntSize { int capacity; long base; Cl storage; // synched ArrayDeque public CircularFifoBuffer(int capacity) { this.capacity = capacity; storage = synchronizedCollection(new ArrayDeque(capacity)); } /** * Adds the given element to the buffer. If the buffer is full, the least recently added element is discarded so * that a new element can be inserted. */ public void add(E e) { synchronized(storage) { if (isFull()) remove(); storage.addLast(e); } } /** * Removes and returns the least recently inserted element from this buffer. */ public E remove() { synchronized(storage) { ++base; return storage.removeFirst(); } } /** * Returns true iff the buffers remaining capacity is 0. */ public boolean isFull() { return storage.size() == capacity; } /** * Returns true iff the buffers size is 0. */ public boolean isEmpty() { return storage.isEmpty(); } /** * Returns the number of elements in the buffer. */ public int size() { return storage.size(); } public Object[] toArray() { return storage.toArray(); } public T[] toArray(T[] a) { return storage.toArray(a); } public List asList() { return new ArrayList(storage); } public ArrayDeque getBackingStore() { return storage; } public Iterator iterator() { return storage.iterator(); } // how many elements were discarded synchronized long getBase() { ret base; } synchronized int getCapacity() { ret capacity; } }