sclass SynchronizedCircularFifoBuffer is Iterable, IntSize { int capacity; long base; ArrayDeque coreStorage; // Unsynched core list Cl storage; // synched coreStorage *(int capacity) { this.capacity = capacity; coreStorage = new ArrayDeque(capacity); storage = synchronizedCollection(coreStorage); } /** * 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(); coreStorage.addLast(e); } } /** * Removes and returns the least recently inserted element from this buffer. */ public E remove() { synchronized(storage) { ++base; return coreStorage.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 Cl getBackingStore() { ret storage; } public Iterator iterator() { return storage.iterator(); } public E last() { synchronized(storage) { ret coreStorage.peekLast(); } } public void clear { synchronized(storage) { coreStorage.clear(); base = 0; } } // how many elements were discarded synchronized long getBase() { ret base; } synchronized int getCapacity() { ret capacity; } }