uk.org.ogsadai.common
Class FIFOQueue

java.lang.Object
  |
  +--uk.org.ogsadai.common.FIFOQueue
All Implemented Interfaces:
Queue

public class FIFOQueue
extends java.lang.Object
implements Queue

Array-based implementation of the uk.org.ogsadai.common.Queue interface, using a first-in-first-out (FIFO) ordering of elements.

Author:
The OGSA-DAI Project team
See Also:
Queue

Nested Class Summary
protected  class FIFOQueue.LocalIterator
           
 
Field Summary
private static java.lang.String COPYRIGHT_NOTICE
           
static int DEFAULT_CAPACITY
          Default fixed capacity.
protected  int mCapacity
          Capacity of the queue.
protected  java.lang.Object[] mData
          Array containing elements of the queue.
private  int mHeadIndex
           
private  long mModificationCount
           
private  int mSize
           
private  int mTailIndex
           
 
Constructor Summary
FIFOQueue()
          Constructs a queue with a default capacity of DEFAULT_CAPACITY elements.
FIFOQueue(int capacity)
          Constructs a queue of given, fixed capacity.
 
Method Summary
private  int dec(int index)
          Circularly decrease indices into data array
 java.lang.Object element()
          Retrieves the head of the queue without removing it.
private  int inc(int index)
          Circularly increase indices into data array.
 java.util.Iterator iterator()
          Returns an iterator over the elements in this queue.
 boolean offer(java.lang.Object obj)
          Inserts, if possible, an object into the queue.
 java.lang.Object peek()
          Retrieves the head of the queue without removing it.
 java.lang.Object poll()
          Retrieves and removes the head of the queue.
 java.lang.Object remove()
          Retrieves and removes the head of the queue.
 void remove(java.lang.Object item)
          Removes the specified item from the queue.
 int size()
          Returns the number of elements in this queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT_NOTICE

private static final java.lang.String COPYRIGHT_NOTICE
See Also:
Constant Field Values

DEFAULT_CAPACITY

public static final int DEFAULT_CAPACITY
Default fixed capacity.

See Also:
Constant Field Values

mData

protected final java.lang.Object[] mData
Array containing elements of the queue.


mCapacity

protected int mCapacity
Capacity of the queue.


mSize

private int mSize

mHeadIndex

private int mHeadIndex

mTailIndex

private int mTailIndex

mModificationCount

private long mModificationCount
Constructor Detail

FIFOQueue

public FIFOQueue(int capacity)
          throws java.lang.IllegalArgumentException
Constructs a queue of given, fixed capacity.

Parameters:
capacity - The desired capacity, which must be greater than 1.
Throws:
java.lang.IllegalArgumentException - If the supplied capacity argument is less than 1.

FIFOQueue

public FIFOQueue()
Constructs a queue with a default capacity of DEFAULT_CAPACITY elements.

Method Detail

offer

public boolean offer(java.lang.Object obj)
              throws java.lang.IllegalArgumentException
Description copied from interface: Queue
Inserts, if possible, an object into the queue.

Specified by:
offer in interface Queue
Parameters:
obj - The item to insert. Must not be null.
Returns:
true if it was possible to add the element to this queue, otherwise false.
java.lang.IllegalArgumentException

poll

public java.lang.Object poll()
Description copied from interface: Queue
Retrieves and removes the head of the queue.

Specified by:
poll in interface Queue
Returns:
the head of the queue, or null if the queue is empty.

remove

public java.lang.Object remove()
                        throws java.util.NoSuchElementException
Description copied from interface: Queue
Retrieves and removes the head of the queue. Unlike poll, which performs a similar task, this method throws an exception if called on an empty queue.

Specified by:
remove in interface Queue
Returns:
the head of the queue.
Throws:
java.util.NoSuchElementException - if this queue is empty.

remove

public void remove(java.lang.Object item)
            throws java.util.NoSuchElementException
Description copied from interface: Queue
Removes the specified item from the queue.

Specified by:
remove in interface Queue
Parameters:
item - The item to remove.
Throws:
java.util.NoSuchElementException - if this queue is empty.

peek

public java.lang.Object peek()
Description copied from interface: Queue
Retrieves the head of the queue without removing it.

Specified by:
peek in interface Queue
Returns:
the head of this queue, or null if the queue is empty.

element

public java.lang.Object element()
                         throws java.util.NoSuchElementException
Description copied from interface: Queue
Retrieves the head of the queue without removing it. Unlike peek, which performs a similar task, this method throws an exception if called on an empty queue.

Specified by:
element in interface Queue
Returns:
the head of this queue.
Throws:
java.util.NoSuchElementException - if this queue is empty.

size

public int size()
Description copied from interface: Queue
Returns the number of elements in this queue.

Specified by:
size in interface Queue
Returns:
the number of elements in the queue.

iterator

public java.util.Iterator iterator()
Description copied from interface: Queue
Returns an iterator over the elements in this queue. The order of iteration is the same as the order of the elements in the queue at the time this method is called.

Specified by:
iterator in interface Queue
Returns:
an iterator over the elements in the queue.

inc

private final int inc(int index)
Circularly increase indices into data array.

Parameters:
index - Update index. If it reaches capacity then circle back to the beginning of the queue.
Returns:
new index.

dec

private final int dec(int index)
Circularly decrease indices into data array

Parameters:
index - Update index. If it reaches 0 then circle on to the end of the queue.
Returns:
new index.