/** * Constructs a new <code>BoundedFifoBuffer</code> big enough to hold all * of the elements in the specified collection. That collection's * elements will also be added to the buffer. * * @param coll the collection whose elements to add, may not be null * @throws NullPointerException if the collection is null */ public BoundedFifoBuffer(Collection coll) { this(coll.size()); addAll(coll); }
/** * Returns the least recently inserted element in this buffer. * * @return the least recently inserted element * @throws BufferUnderflowException if the buffer is empty */ public Object get() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } return elements[start]; }
/** * Write the buffer out using a custom routine. * * @param out the output stream * @throws IOException */ private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(size()); for (Iterator it = iterator(); it.hasNext();) { out.writeObject(it.next()); } }
BoundedFifoBuffer apiCallHistory = new BoundedFifoBuffer(20); for(int i =1 ; i < 25; i++){ if(apiCallHistory.isFull()){ System.out.println("removing :: "+apiCallHistory.remove()); } apiCallHistory.add(i); }
/** * Tests is full */ public void testIsFull() { resetFull(); assertEquals(true, ((BoundedCollection) collection).isFull()); ((BoundedFifoBuffer) collection).remove(); assertEquals(false, ((BoundedCollection) collection).isFull()); ((BoundedFifoBuffer) collection).add("jj"); assertEquals(true, ((BoundedCollection) collection).isFull()); }
BoundedFifoBuffer.this.remove(); lastReturnedIndex = -1; return;
/** * If the buffer is full, the least recently added element is discarded so * that a new element can be inserted. * * @param element the element to add * @return true, always */ public boolean add(Object element) { if (isFull()) { remove(); } return super.add(element); }
/** * Returns true if this buffer is empty; false otherwise. * * @return true if this buffer is empty */ public boolean isEmpty() { return size() == 0; }
/** * Write the buffer out using a custom routine. * * @param out the output stream * @throws IOException */ private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(size()); for (Iterator it = iterator(); it.hasNext();) { out.writeObject(it.next()); } }
/** * Tests max size */ public void testMaxSize() { resetFull(); assertEquals(getFullElements().length, ((BoundedCollection) collection).maxSize()); ((BoundedFifoBuffer) collection).remove(); assertEquals(getFullElements().length, ((BoundedCollection) collection).maxSize()); ((BoundedFifoBuffer) collection).add("jj"); assertEquals(getFullElements().length, ((BoundedCollection) collection).maxSize()); }
BoundedFifoBuffer.this.remove(); lastReturnedIndex = -1; return;
/** * If the buffer is full, the least recently added element is discarded so * that a new element can be inserted. * * @param element the element to add * @return true, always */ public boolean add(Object element) { if (isFull()) { remove(); } return super.add(element); }
/** * Returns true if this collection is full and no new elements can be added. * * @return <code>true</code> if the collection is full */ public boolean isFull() { return size() == maxElements; }
public void testIsFull() { Set set = new HashSet(); set.add("1"); set.add("2"); set.add("3"); try { CollectionUtils.isFull(null); fail(); } catch (NullPointerException ex) {} assertEquals(false, CollectionUtils.isFull(set)); BoundedFifoBuffer buf = new BoundedFifoBuffer(set); assertEquals(true, CollectionUtils.isFull(buf)); buf.remove("2"); assertEquals(false, CollectionUtils.isFull(buf)); buf.add("2"); assertEquals(true, CollectionUtils.isFull(buf)); Buffer buf2 = BufferUtils.synchronizedBuffer(buf); assertEquals(true, CollectionUtils.isFull(buf2)); buf2.remove("2"); assertEquals(false, CollectionUtils.isFull(buf2)); buf2.add("2"); assertEquals(true, CollectionUtils.isFull(buf2)); }
/** * Write the buffer out using a custom routine. * * @param out the output stream * @throws IOException */ private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(size()); for (Iterator it = iterator(); it.hasNext();) { out.writeObject(it.next()); } }
/** * Tests that the removal operation actually removes the first element. */ public void testBoundedFifoBufferRemove() { resetFull(); int size = confirmed.size(); for (int i = 0; i < size; i++) { Object o1 = ((BoundedFifoBuffer)collection).remove(); Object o2 = ((ArrayList)confirmed).remove(0); assertEquals("Removed objects should be equal", o1, o2); verify(); } try { ((BoundedFifoBuffer)collection).remove(); fail("Empty buffer should raise Underflow."); } catch (BufferUnderflowException e) { // expected } }
/** * If the buffer is full, the least recently added element is discarded so * that a new element can be inserted. * * @param element the element to add * @return true, always */ public boolean add(Object element) { if (isFull()) { remove(); } return super.add(element); }