|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--lava.util.FifoQueue
A FifoQueue
is a first-in, first-out Object
queue. Unlike queues in the real world, this FifoQueue expands as needed and cannot overflow (except where Java runs out of memory, of course).
Objects are dequeued off the front of the queue using the nextElement
method. Objects are enqueued to the back of the queue by the addElement
method. Additional methods are also available for searching and manipulating the queue.
Queue expansion:
Whenever the queue must grow, its internal storage area simply doubles in size. In most situations, this will provide relatively efficient queue growth. If, however, you are able to predict ahead of time how large this queue must eventually be, you may optimize the the queue by passing in an "initial queue capacity" parameter during construction. You can also force queue capacity expansion by calling the ensureCapacity
method.
FifoQueue does not automatically reduce its capacity when objects are dequeued. This may result in an undesirable waste of memory, especially if you are certain that the queue is not going to grow again. At any time, you can minimize the storage requirements of an active queue by calling its trimToSize
method.
A note on design:
FifoQueue
is essentially a sliding vector. Its first implementation was originally just a subclass of java.util.Vector
. Unfortunately, the internal mechanisms of Sun's Vector
class prevents it from supporting FifoQueue
s very efficiently. Due to this limitation, Vector
is no longer used as a superclass.*
Because of the similarity between the two ideas, however, the names of FifoQueue
's public methods have been chosen to resemble those of the Vector
class. Thus, if you're already familiar with Vector
, then you already know how to use FifoQueue. They are completely interchangeable.
*FifoQueue uses a circular Object
array, which is significantly more efficient than the linear array used in Sun's Vector class if you intend to remove elements from the front or near the front of the queue. This is because in a linear array, such as is used by java.util.Vector, all the values in the array must be shifted every time the element at the head of the queue is removed.
Constructor Summary | |
FifoQueue()
Initializes an empty FifoQueue , using a default initial capacity. |
|
FifoQueue(int dc)
Prepares an empty FifoQueue with the specified initial capacity. |
Method Summary | |
void |
addElement(java.lang.Object object)
Adds an element to the end of this queue. |
int |
capacity()
Returns the current capacity of this queue. |
java.lang.Object |
clone()
Clones this queue. |
boolean |
contains(java.lang.Object obj)
Tells if this queue contains the specified object. |
void |
copyInto(java.lang.Object[] dest)
Copies the elements of this queue into a destination array. |
java.lang.Object |
elementAt(int index)
Returns the element at the specified index in the queue. |
void |
ensureCapacity(int capacity)
Guarantees that this queue will be able to buffer at least the indicated number of elements. |
java.lang.Object |
firstElement()
Returns the next element in the queue without removing it. |
boolean |
hasMoreElements()
Determines whether this queue contains more elements. |
int |
indexOf(java.lang.Object obj)
See indexOf(Object,int) . |
int |
indexOf(java.lang.Object obj,
int index)
Searches this queue for an object equal to the given object. |
void |
insertElementAt(java.lang.Object obj,
int index)
Inserts an object into the queue at the specified index. |
boolean |
isEmpty()
Determines if this queue is empty. |
java.lang.Object |
lastElement()
Returns the last element in this queue without removing it. |
int |
lastIndexOf(java.lang.Object obj)
See lastIndexOf(Object,int) . |
int |
lastIndexOf(java.lang.Object obj,
int index)
Searches this queue for an object equal to the given object. |
java.lang.Object |
nextElement()
Removes the element at the head of the queue and returns it. |
void |
removeAllElements()
Empties the queue. |
boolean |
removeElement(java.lang.Object obj)
Removes the specified object from the queue. |
void |
removeElementAt(int index)
Deletes the element at the specified index. |
void |
setElementAt(java.lang.Object obj,
int index)
Replaces the element at the specified index with the given object. |
void |
setSize(int size)
Sets the number of elements contained in this queue. |
int |
size()
Returns the number of elements currently buffered in this queue. |
java.lang.String |
toString()
Generates a string representation of this queue. |
void |
trimToSize()
Minimizes the amount of memory used to store this queue. |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public FifoQueue(int dc)
FifoQueue
with the specified initial capacity.dc
- the initial capacitypublic FifoQueue()
FifoQueue
, using a default initial capacity.Method Detail |
public boolean isEmpty()
public boolean hasMoreElements()
hasMoreElements
in interface java.util.Enumeration
public java.lang.Object nextElement()
nextElement
in interface java.util.Enumeration
public void addElement(java.lang.Object object)
object
- the element to addpublic final int capacity()
public final int size()
public int indexOf(java.lang.Object obj, int index)
obj
is null
, the search is for an element whose value is also null
; otherwise the object's equals
method is used to determine equality. Searching begins with the object specified by index, and precedes forwards.obj
- the object to search forindex
- the index to begin searching from (Default value
: 0)public final int indexOf(java.lang.Object obj)
indexOf(Object,int)
.public int lastIndexOf(java.lang.Object obj, int index)
obj
is null
, the search is for an element whose value is also null
; otherwise the object's equals
method is used to determine equality. Searching begins with the object specified by index, and precedes backwards.obj
- the object to search forindex
- the index to begin searching from (Default value
: tail, where tail is the index of the last element)public final int lastIndexOf(java.lang.Object obj)
lastIndexOf(Object,int)
.public java.lang.Object elementAt(int index)
index
- the index of the elementpublic java.lang.Object firstElement()
java.util.NoSuchElementException
- if the queue is emptypublic java.lang.Object clone()
Object
s.clone
in class java.lang.Object
public boolean contains(java.lang.Object obj)
obj
- the object to search fortrue
if the object is in this queue, false
otherwisepublic void insertElementAt(java.lang.Object obj, int index)
obj
- the object to insertindex
- the index at which to perform the insertionjava.lang.ArrayIndexOutOfBoundsException
- if (index < 0 || index > size()
)public java.lang.Object lastElement()
java.util.NoSuchElementException
- if this queue is emptypublic void removeAllElements()
public void removeElementAt(int index)
index
- the indexpublic boolean removeElement(java.lang.Object obj)
indexOf(Object)
method, and then deleting the match if one is found. If no match is found, the queue is unchanged.
Overrideable methods employed:
obj
- the objecttrue
if the object was found and removed, false
otherwisepublic final void setElementAt(java.lang.Object obj, int index)
obj
- the new objectindex
- the indexpublic final void setSize(int size)
size
- the new sizepublic final java.lang.String toString()
toString
method returned by each queued object, or "null" for elements that are null
.toString
in class java.lang.Object
public final void trimToSize()
public final void ensureCapacity(int capacity)
capacity
- the minimum capacity of this queuepublic final void copyInto(java.lang.Object[] dest)
dest
- the destination array
|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |