public void remove() { if (lastReturnedIndex == -1) throw new IllegalStateException(); // First element can be removed quickly if (lastReturnedIndex == m_start) { BoundedFifoBuffer.this.remove(); lastReturnedIndex = -1; return; } // Other elements require us to shift the subsequent elements int i = lastReturnedIndex + 1; while (i != m_end) { if (i >= maxElements) { m_elements[i - 1] = m_elements[0]; i = 0; } else { m_elements[i - 1] = m_elements[i]; i++; } } lastReturnedIndex = -1; m_end = decrement(m_end); m_elements[m_end] = null; m_full = false; index = decrement(index); }
/** * Returns true if this buffer is empty; false otherwise. * * @return true if this buffer is empty */ public boolean isEmpty() { return size() == 0; }
/** * 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 m_elements[m_start]; }
final BoundedFifoBuffer buffer = new BoundedFifoBuffer( Math.max( m_components.size(), 1 ) ); getLogger().warn( cause, e.getCause() ); buffer.add( e ); getLogger().warn( message, e ); buffer.add( e ); getLogger().warn( message, le ); buffer.add( le ); if ( buffer.size() > 0 ) throw new CompositeException( (Exception[]) buffer.toArray( new Exception[0] ), "unable to instantiate one or more components" );
/** * 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()); }
/** * Returns an empty BoundedFifoBuffer that won't overflow. * * @return an empty BoundedFifoBuffer */ public Collection makeCollection() { return new BoundedFifoBuffer(100); }
/** * 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 m_elements[m_start]; }
/** * 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()); }
/** * Returns a BoundedFifoBuffer that's filled to capacity. * Any attempt to add to the returned buffer will result in a * BufferOverflowException. * * @return a full BoundedFifoBuffer */ public Collection makeFullCollection() { return new BoundedFifoBuffer(Arrays.asList(getFullElements())); }
/** * 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; }
/** * Removes the least recently inserted element from this buffer. * * @return the least recently inserted element * @throws BufferUnderflowException if the buffer is empty */ public Object remove() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } Object element = m_elements[m_start]; if (null != element) { m_elements[m_start++] = null; if (m_start >= maxElements) { m_start = 0; } m_full = false; } return element; }
public void remove() { if (lastReturnedIndex == -1) throw new IllegalStateException(); // First element can be removed quickly if (lastReturnedIndex == m_start) { BoundedFifoBuffer.this.remove(); lastReturnedIndex = -1; return; } // Other elements require us to shift the subsequent elements int i = lastReturnedIndex + 1; while (i != m_end) { if (i >= maxElements) { m_elements[i - 1] = m_elements[0]; i = 0; } else { m_elements[i - 1] = m_elements[i]; i++; } } lastReturnedIndex = -1; m_end = decrement(m_end); m_elements[m_end] = null; m_full = false; index = decrement(index); }
/** * 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); }
/** * Tests that the constructor correctly throws an exception. */ public void testConstructorException2() { try { new BoundedFifoBuffer(-20); } catch (IllegalArgumentException ex) { return; } fail(); }
/** * 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; }
/** * Removes the least recently inserted element from this buffer. * * @return the least recently inserted element * @throws BufferUnderflowException if the buffer is empty */ public Object remove() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } Object element = m_elements[m_start]; if (null != element) { m_elements[m_start++] = null; if (m_start >= maxElements) { m_start = 0; } m_full = false; } return element; }
/** * 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 } }
/** * 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); }
/** * Tests that the constructor correctly throws an exception. */ public void testConstructorException3() { try { new BoundedFifoBuffer(null); } catch (NullPointerException ex) { return; } fail(); } }