net.sf.btw.commons
Class Queue

java.lang.Object
  extended by net.sf.btw.commons.Queue
All Implemented Interfaces:
Enumeration

public final class Queue
extends Object
implements Enumeration

Circular buffer (FIFO) of a fixed length. Not thread safe. You must NOT use the queue as an Enumeration instance: if you want Enumeration please use getEnumeration().

Author:
Martin Vysny

Field Summary
 int bufferSize
          The size of the buffer.
 
Constructor Summary
Queue(int bufferSize)
          Creates new buffer.
Queue(Queue other)
          Clones given queue.
 
Method Summary
 void clear()
          Removes all objects from the queue.
 boolean equals(Object obj)
           
 Enumeration getEnumeration()
          Returns enumeration of items in this queue.
 int hashCode()
           
 boolean hasMoreElements()
           
 boolean isEmpty()
          Checks if this queue is empty.
 boolean isFull()
          Checks if this queue is full.
 Object nextElement()
           
 int occupiedSlots()
          Returns number of occupied slots in circular buffer array.
 boolean offer(Object o)
          Inserts the specified element into this queue, if possible.
 boolean offerAll(Enumeration other)
          Inserts specified elements into this queue, if possible.
 Object peek()
          Peeks at the head of the queue and returns head item without removing it from the queue.
 Object peek(int index)
          Peeks at the head of the queue and returns index-th object beneath the head.
 Object poll()
          Retrieves and removes the head of this queue, or null if this queue is empty.
 Object removeLast()
          Removes last item offered to the queue.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

bufferSize

public final int bufferSize
The size of the buffer.

Constructor Detail

Queue

public Queue(int bufferSize)
Creates new buffer.

Parameters:
bufferSize - fixed size of the buffer. The buffer can contain no more items.

Queue

public Queue(Queue other)
Clones given queue.

Parameters:
other - clones this queue.
Method Detail

occupiedSlots

public int occupiedSlots()
Returns number of occupied slots in circular buffer array.

Returns:
if zero then no slots are occupied.

poll

public Object poll()
Retrieves and removes the head of this queue, or null if this queue is empty.

Returns:
Retrieves and removes the head of this queue, or null if this queue is empty.

offer

public boolean offer(Object o)
Inserts the specified element into this queue, if possible.

Parameters:
o - the element to insert.
Returns:
true if it was possible to add the element to this queue, else false

offerAll

public boolean offerAll(Enumeration other)
Inserts specified elements into this queue, if possible.

Parameters:
other - the elements to insert.
Returns:
true if it was possible to add all element to this queue, false if some or all elements have not been added.
Throws:
IllegalArgumentException - if trying to add self.

isFull

public boolean isFull()
Checks if this queue is full.

Returns:
true if the queue is full, false if it has some room left.

isEmpty

public boolean isEmpty()
Checks if this queue is empty.

Returns:
true if the queue is empty, false if it contains some elements.

getEnumeration

public Enumeration getEnumeration()
Returns enumeration of items in this queue. Enumeration is a snapshot of this queue and is further independent.

Returns:
enumerator instance.

hasMoreElements

public boolean hasMoreElements()
Specified by:
hasMoreElements in interface Enumeration

nextElement

public Object nextElement()
Specified by:
nextElement in interface Enumeration

clear

public void clear()
Removes all objects from the queue.


equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

peek

public Object peek()
Peeks at the head of the queue and returns head item without removing it from the queue.

Returns:
head item or null if the queue is empty.

peek

public Object peek(int index)
Peeks at the head of the queue and returns index-th object beneath the head.

Parameters:
index - the index of the item to retrieve.
Returns:
item at required position or null if no such item exists.

removeLast

public Object removeLast()
Removes last item offered to the queue. This allows to use the queue as a LIFO stack aswell.

Returns:
last offered item or null if the queue is empty.


Copyright © 2007 Moto a Judas. All Rights Reserved.