OpMath

com.opmath.mail
Class CircularQueue

java.lang.Object
  |
  +--com.opmath.mail.CircularQueue

public class CircularQueue
extends Object

A queue data structure, complete with synchronized access methods. Queue elements are of type Object.

This class should only be used in a multi-threaded environment.

Copyright: (c) 2002 The Open Math Company Limited

The Terms of Distribution of this software are stipulated in the javadoc Overview for this project.

Since:
J2SE 1.3
Version:
1.0
Author:
Bruno Beloff bruno.beloff@opmath.com

Constructor Summary
CircularQueue(int size)
          Constructs a new CircularQueue instance.
 
Method Summary
 Object get()
          Removes an element from the front of the queue.
 boolean isEmpty()
          Returns true if the queue is empty.
 void put(Object element)
          Appends a new element on to the end of the queue.
 String toString()
          Returns a String describing the queue.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CircularQueue

public CircularQueue(int size)
Constructs a new CircularQueue instance. The queue will be of the given size. The size of the queue cannot be changed once it is created.

Parameters:
size - the int size of the queue.
Since:
JDK 1.2.2
Method Detail

put

public void put(Object element)
         throws QueueOverflowException
Appends a new element on to the end of the queue. If the queue is full, put(Object) throws a QueueOverflowException. If the element has been enqueued successfully, put(Object) raises a notify() to the adjacent thread.

NOTE: In the case where an exception is raised, put(Object) actually has placed the element on the queue - it is signaling that there is no room for further elements.

Parameters:
size - the int size of the queue.
Throws:
QueueOverflowException - An exception thrown if the queue was full.
Since:
JDK 1.2.2

get

public Object get()
Removes an element from the front of the queue. If there are no elements on the queue, get() waits until notified by put(Object).

WARNING: There is no timeout on the wait - thus, get() will wait forever if nothing is placed on the queue.

Returns:
the Object that had been at the front of the queue.
Since:
JDK 1.2.2

isEmpty

public boolean isEmpty()
Returns true if the queue is empty.

This method is essential if the queue is being used in a single-threaded environment: in such a case, it is essential to test whether there are any elements on the queue before doing a get().

Returns:
a boolean reporting the state of the queue.
Since:
JDK 1.2.2

toString

public String toString()
Returns a String describing the queue.

Overrides:
toString in class Object
Returns:
a String reporting the state of the queue.
Since:
JDK 1.2.2

OpMath

Submit a bug or feature to Open Math
Copyright © 2002 Open Math Company Limited, Brighton, UK