LimeWire Collection Component API

org.limewire.collection
Class Buffer<E>

java.lang.Object
  extended by org.limewire.collection.Buffer<E>
All Implemented Interfaces:
Cloneable, Iterable<E>
Direct Known Subclasses:
NumericBuffer

public class Buffer<E>
extends Object
implements Cloneable, Iterable<E>

Provides a simple fixed-size double-ended queue, a circular buffer. The fixed size is intentional for performance; use Buffer when you want to use a fix amount of resources. For a minimal amount of efficiency, the internal buffer is only allocated on the first insertion or retrieval, allowing lots of Buffers to be created that may not be used.

This class is not thread-safe.


Field Summary
protected  E[] buf
           
 
Constructor Summary
Buffer(Buffer<? extends E> other)
          "Copy constructor": constructs a new shallow copy of other.
Buffer(int size)
           
 
Method Summary
 E add(E x)
          Same as addFirst(x).
 E addFirst(E x)
           
 E addLast(E x)
           
 void clear()
           
 Buffer<E> clone()
          Returns a shallow copy of this, of type Buffer
 boolean contains(Object x)
          Returns true if the input object x is in the buffer.
protected  E[] createArray(int size)
           
 E first()
          Returns the head of this, or throws NoSuchElementException if this is empty.
 E get(int i)
          If i<0 or i>=getSize(), throws IndexOutOfBoundsException.
 int getCapacity()
          Returns the number of elements that this can hold, i.e., the max size that was passed to the constructor.
 int getSize()
          Returns the number of elements in this.
protected  void initialize()
          Initializes the internal buf if necessary.
 boolean isEmpty()
          Returns true if and only if this is empty.
 boolean isFull()
          Returns true if and only if this is full, e.g., adding another element would force another out.
 Iterator<E> iterator()
           
 E last()
          Returns the tail of this, or throws NoSuchElementException if this is empty.
 E remove(int i)
           
 boolean remove(Object x)
           
 boolean removeAll(Object x)
           
 E removeFirst()
           
 E removeLast()
           
 void set(int i, E o)
           
 int size()
          Same as getSize().
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

protected E[] buf
Constructor Detail

Buffer

public Buffer(int size)

Buffer

public Buffer(Buffer<? extends E> other)
"Copy constructor": constructs a new shallow copy of other.

Method Detail

createArray

protected E[] createArray(int size)

initialize

protected void initialize()
Initializes the internal buf if necessary.


isEmpty

public boolean isEmpty()
Returns true if and only if this is empty.


isFull

public boolean isFull()
Returns true if and only if this is full, e.g., adding another element would force another out.


size

public final int size()
Same as getSize().


getSize

public int getSize()
Returns the number of elements in this. Note that this never exceeds the value returned by getCapacity.


getCapacity

public int getCapacity()
Returns the number of elements that this can hold, i.e., the max size that was passed to the constructor.


get

public E get(int i)
      throws IndexOutOfBoundsException
If i<0 or i>=getSize(), throws IndexOutOfBoundsException. Else returns this[i]

Throws:
IndexOutOfBoundsException

set

public void set(int i,
                E o)
         throws IndexOutOfBoundsException
Throws:
IndexOutOfBoundsException

add

public E add(E x)
Same as addFirst(x).


addFirst

public E addFirst(E x)

addLast

public E addLast(E x)

contains

public boolean contains(Object x)
Returns true if the input object x is in the buffer.


first

public E first()
        throws NoSuchElementException
Returns the head of this, or throws NoSuchElementException if this is empty.

Throws:
NoSuchElementException

last

public E last()
       throws NoSuchElementException
Returns the tail of this, or throws NoSuchElementException if this is empty.

Throws:
NoSuchElementException

removeFirst

public E removeFirst()
              throws NoSuchElementException
Throws:
NoSuchElementException

removeLast

public E removeLast()
             throws NoSuchElementException
Throws:
NoSuchElementException

remove

public E remove(int i)
         throws IndexOutOfBoundsException
Throws:
IndexOutOfBoundsException

remove

public boolean remove(Object x)

removeAll

public boolean removeAll(Object x)

clear

public void clear()

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>

clone

public Buffer<E> clone()
                throws CloneNotSupportedException
Returns a shallow copy of this, of type Buffer

Overrides:
clone in class Object
Throws:
CloneNotSupportedException

toString

public String toString()
Overrides:
toString in class Object

LimeWire Collection Component API

Copyright © 2009 Lime Wire LLC. All Rights Reserved.