goldman.collection.positional
Class Buffer<E>

java.lang.Object
  extended by goldman.collection.positional.Buffer<E>

public class Buffer<E>
extends Object

For many applications, elements need only be added or removed from the front or back end of the collection. Data structures designed for such settings can gain efficiency by limiting access to the ends. The buffer is the most general ADT for this category. A buffer allows the user to insert or remove an element from either end of the collection.


Constructor Summary
Buffer()
          Creates an unbounded, untracked buffer with a default initial capacity
Buffer(int capacity)
          Creates an unbounded, untracked buffer with the given initial capacity
Buffer(int capacity, boolean bounded)
          Creates an untracked buffer with the specified parameters
Buffer(int capacity, boolean bounded, boolean tracked)
          Creates a buffer satisfying the specification of the given parameters
 
Method Summary
 void addFirst(E element)
           
 void addLast(E element)
           
 void clear()
          Removes all elements from the buffer.
 boolean contains(E value)
           
 E getFirst()
           
 E getLast()
           
 int getSize()
           
 boolean isEmpty()
           
 PositionalCollectionLocator<E> iterator()
          Creates a new locator that starts just before the first item in the buffer.
 E removeFirst()
          Removes the first element in the buffer,
 E removeLast()
          Removes the last element in the buffer
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Buffer

public Buffer(int capacity,
              boolean bounded,
              boolean tracked)
Creates a buffer satisfying the specification of the given parameters

Parameters:
capacity - the initial capacity for the underlying positional collection
bounded - a boolean which is true if and only if the buffer should be bounded at the given capacity
tracked - a boolean which is true if and only if the buffer should support a tracker

Buffer

public Buffer()
Creates an unbounded, untracked buffer with a default initial capacity


Buffer

public Buffer(int capacity)
Creates an unbounded, untracked buffer with the given initial capacity

Parameters:
capacity - the initial capacity for the underlying positional collection

Buffer

public Buffer(int capacity,
              boolean bounded)
Creates an untracked buffer with the specified parameters

Parameters:
capacity - the initial capacity for the underlying positional collection
bounded - a boolean which is true if and only if the buffer should be bounded
Method Detail

isEmpty

public boolean isEmpty()
Returns:
true if and only if no elements are stored in the collection

getSize

public int getSize()
Returns:
the number of elements stored in the buffer

toString

public String toString()
Overrides:
toString in class Object

contains

public boolean contains(E value)
Parameters:
value - the target
Returns:
true if and only if an equivalent value exists in the buffer

getFirst

public E getFirst()
Returns:
the first element in the buffer, leaving the buffer unchanged

getLast

public E getLast()
Returns:
the last element in the buffer, leaving the buffer unchanged

addFirst

public void addFirst(E element)
Parameters:
element - the element that is to be inserted at the front of the buffer.
Throws:
AtCapacityException - a bounded buffer is full

addLast

public void addLast(E element)
Parameters:
element - the element that is to be inserted at the end of the buffer.
Throws:
AtCapacityException - a bounded buffer is full

removeFirst

public E removeFirst()
Removes the first element in the buffer,

Returns:
the removed element
Throws:
NoSuchElementException - the buffer is empty

removeLast

public E removeLast()
Removes the last element in the buffer

Returns:
the element that was removed.
Throws:
NoSuchElementException - the buffer is empty

clear

public void clear()
Removes all elements from the buffer.


iterator

public PositionalCollectionLocator<E> iterator()
Creates a new locator that starts just before the first item in the buffer.