public void testInternalStateIteratorRemoveWithWrap1() { UnboundedFifoBuffer test = new UnboundedFifoBuffer(3); test.add("A"); test.add("B"); test.add("C"); test.remove("A"); test.remove("B"); test.add("D"); test.add("E"); assertEquals(4, test.buffer.length); assertEquals(2, test.head); assertEquals(1, test.tail); Iterator it = test.iterator(); assertEquals("C", it.next()); it.remove(); assertEquals(4, test.buffer.length); assertEquals(3, test.head); assertEquals(1, test.tail); }
/** * Removes the next object from the buffer * * @return the removed object * @throws BufferUnderflowException if this buffer is empty */ public Object remove() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } Object element = buffer[head]; if (element != null) { buffer[head] = null; head = increment(head); } return element; }
/** * 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()); } }
/** * Adds the given element to this buffer. * * @param obj the element to add * @return true, always * @throws NullPointerException if the given element is null */ public boolean add(final Object obj) { if (obj == null) { throw new NullPointerException("Attempted to add null object to buffer"); } if (size() + 1 >= buffer.length) { // copy contents to a new buffer array Object[] tmp = new Object[((buffer.length - 1) * 2) + 1]; int j = 0; // move head to element zero in the new array for (int i = head; i != tail;) { tmp[j] = buffer[i]; buffer[i] = null; j++; i = increment(i); } buffer = tmp; head = 0; tail = j; } buffer[tail] = obj; tail = increment(tail); return true; }
/** * Returns an empty UnboundedFifoBuffer with a small capacity. * * @return an empty UnboundedFifoBuffer */ public Collection makeCollection() { return new UnboundedFifoBuffer(5); }
public void remove() { if (lastReturnedIndex == -1) { throw new IllegalStateException(); } // First element can be removed quickly if (lastReturnedIndex == head) { UnboundedFifoBuffer.this.remove(); lastReturnedIndex = -1; return; } // Other elements require us to shift the subsequent elements int i = increment(lastReturnedIndex); while (i != tail) { buffer[decrement(i)] = buffer[i]; i = increment(i); } lastReturnedIndex = -1; tail = decrement(tail); buffer[tail] = null; index = decrement(index); }
public void testInternalStateRemove2() { UnboundedFifoBuffer test = new UnboundedFifoBuffer(4); test.add("A"); test.add("B"); test.add("C"); assertEquals(5, test.buffer.length); assertEquals(0, test.head); assertEquals(3, test.tail); test.remove("B"); assertEquals(5, test.buffer.length); assertEquals(0, test.head); assertEquals(2, test.tail); test.add("D"); assertEquals(5, test.buffer.length); assertEquals(0, test.head); assertEquals(3, test.tail); }
public void testInternalStateIteratorRemove2() { UnboundedFifoBuffer test = new UnboundedFifoBuffer(4); test.add("A"); test.add("B"); test.add("C"); Iterator it = test.iterator(); it.next(); it.next(); it.remove(); assertEquals(5, test.buffer.length); assertEquals(0, test.head); assertEquals(2, test.tail); test.add("D"); assertEquals(5, test.buffer.length); assertEquals(0, test.head); assertEquals(3, test.tail); }
/** * Returns the next object in the buffer. * * @return the next object in the buffer * @throws BufferUnderflowException if this buffer is empty */ public Object get() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } return buffer[head]; }
/** * Returns true if this buffer is empty; false otherwise. * * @return true if this buffer is empty */ public boolean isEmpty() { return (size() == 0); }
public void testInternalStateAdd() { UnboundedFifoBuffer test = new UnboundedFifoBuffer(2); assertEquals(3, test.buffer.length); assertEquals(0, test.head); assertEquals(0, test.tail); test.add("A"); assertEquals(3, test.buffer.length); assertEquals(0, test.head); assertEquals(1, test.tail); test.add("B"); assertEquals(3, test.buffer.length); assertEquals(0, test.head); assertEquals(2, test.tail); test.add("C"); // forces buffer increase assertEquals(5, test.buffer.length); assertEquals(0, test.head); assertEquals(3, test.tail); test.add("D"); assertEquals(5, test.buffer.length); assertEquals(0, test.head); assertEquals(4, test.tail); }
/** * Adds the given element to this buffer. * * @param obj the element to add * @return true, always * @throws NullPointerException if the given element is null */ public boolean add(final Object obj) { if (obj == null) { throw new NullPointerException("Attempted to add null object to buffer"); } if (size() + 1 >= buffer.length) { // copy contents to a new buffer array Object[] tmp = new Object[((buffer.length - 1) * 2) + 1]; int j = 0; // move head to element zero in the new array for (int i = head; i != tail;) { tmp[j] = buffer[i]; buffer[i] = null; j++; i = increment(i); } buffer = tmp; head = 0; tail = j; } buffer[tail] = obj; tail = increment(tail); return true; }
/** * Tests that the constructor correctly throws an exception. */ public void testConstructorException1() { try { new UnboundedFifoBuffer(0); } catch (IllegalArgumentException ex) { return; } fail(); }
public void remove() { if (lastReturnedIndex == -1) { throw new IllegalStateException(); } // First element can be removed quickly if (lastReturnedIndex == head) { UnboundedFifoBuffer.this.remove(); lastReturnedIndex = -1; return; } // Other elements require us to shift the subsequent elements int i = increment(lastReturnedIndex); while (i != tail) { buffer[decrement(i)] = buffer[i]; i = increment(i); } lastReturnedIndex = -1; tail = decrement(tail); buffer[tail] = null; index = decrement(index); }
public void testInternalStateRemove1() { UnboundedFifoBuffer test = new UnboundedFifoBuffer(4); test.add("A"); test.add("B"); test.add("C"); assertEquals(5, test.buffer.length); assertEquals(0, test.head); assertEquals(3, test.tail); test.remove("A"); assertEquals(5, test.buffer.length); assertEquals(1, test.head); assertEquals(3, test.tail); test.add("D"); assertEquals(5, test.buffer.length); assertEquals(1, test.head); assertEquals(4, test.tail); }
public void testInternalStateIteratorRemove1() { UnboundedFifoBuffer test = new UnboundedFifoBuffer(4); test.add("A"); test.add("B"); test.add("C"); assertEquals(5, test.buffer.length); assertEquals(0, test.head); assertEquals(3, test.tail); Iterator it = test.iterator(); it.next(); it.remove(); assertEquals(5, test.buffer.length); assertEquals(1, test.head); assertEquals(3, test.tail); test.add("D"); assertEquals(5, test.buffer.length); assertEquals(1, test.head); assertEquals(4, test.tail); }
/** * Returns the next object in the buffer. * * @return the next object in the buffer * @throws BufferUnderflowException if this buffer is empty */ public Object get() { if (isEmpty()) { throw new BufferUnderflowException("The buffer is already empty"); } return buffer[head]; }
/** * Returns true if this buffer is empty; false otherwise. * * @return true if this buffer is empty */ public boolean isEmpty() { return (size() == 0); }
public void testInternalStateIteratorRemoveWithTailAtEnd1() { UnboundedFifoBuffer test = new UnboundedFifoBuffer(3); test.add("A"); test.add("B"); test.add("C"); test.remove("A"); test.add("D"); assertEquals(4, test.buffer.length); assertEquals(1, test.head); assertEquals(0, test.tail); Iterator it = test.iterator(); assertEquals("B", it.next()); it.remove(); assertEquals(4, test.buffer.length); assertEquals(2, test.head); assertEquals(0, test.tail); }
/** * 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()); } }