Queue
| Kind of class: | public class |
|---|---|
| Package: | com.bourre.collection |
| Inherits from: | none |
| Implements: | |
| Author: | Romain Flacher, Cédric Néhémie |
| Classpath: | com.bourre.collection.Queue |
| File last modified: | Monday, 24 November 2008, 11:36:49 |
Besides basic
Collection operations,queues provide additional insertion, extraction, and inspection
operations.
The Queue class allow as many occurrences as you
want. In a same way, null values are allowed in
the queue.
Elements in a Queue are orderered according
to a FIFO (first-in-first-out) order.
- Using an untyped
Queuevar queue : Queue = new Queue ();
queue.add( 20 )
queue.add( "20" );
queue.add( 80 );
trace ( queue.size() ); // 3
trace ( queue.poll() ); // 80
trace ( queue.peek() ); // "20"
trace ( queue.size() ); // 2
Using a typedQueuevar queue : Queue = new Queue ( Number );
queue.add( 20 )
try
{
queue.add( "20" ); // throws an error because "20" is not a Number
queue.add( 80 );
}
catch ( e : Error ) {}
trace ( queue.size() ); // 2
trace ( queue.poll() ); // 80
trace ( queue.peek() ); // 20
trace ( queue.size() ); // 1
Summary
- Queue (type:Class = null)
- Create an empty Queue object.
- add (o:Object) : Boolean
- Adds the specified element to this queue.
- remove (o:Object) : Boolean
- Removes a single instance of the specified element from this
- contains (o:Object) : Boolean
- Returns true if this queue contains at least
- isEmpty : Boolean
- Returns true if this queue contains no elements.
- clear : void
- Removes all of the elements from this queue
- iterator : Iterator
- Returns an iterator over the elements in this queue.
- addAll (c:Collection) : Boolean
- Adds all of the elements in the specified Collection
- removeAll (c:Collection) : Boolean
- Removes from this queue all of its elements that are contained
- containsAll (c:Collection) : Boolean
- Returns true if this queue contains
- retainAll (c:Collection) : Boolean
- Retains only the elements in this queue that are contained
- size : uint
- Returns the number of elements in this queue (its cardinality).
- peek : Object
- Retrieves, but does not remove, the head of this queue,
- poll : Object
- Retrieves and removes the head of this queue,
- matchType (o:*) : Boolean
- Verify that the passed-in object type match the current
- isTyped : Boolean
- Returns true if this queue perform a verification
- getType : Class
- Return the class type of elements in this queue object.
- isValidCollection (c:Collection) : Boolean
- Verify that the passed-in Collection is a valid
- isValidType (o:Object) : Boolean
- Verify that the passed-in object type match the current
- toArray : Array
- Returns an array containing all the elements in this queue.
- toString : String
- Returns the String representation of
Constructor
Queue
Queue object. Instance methods
add
The object is added as the top of the queue.
If the current queue object is typed and if the passed-in object's
type prevents it to be added in this queue, the function throws
an ClassCastException.
-
trueif this queue have changed at
the end of the call
- {VISDOC_LINK_0}
ClassCastException— If the object's
type prevents it to be added into this queue
- Adding elements to an untyped queue
var queue : Queue = new Queue();
queue.add( 50 );
queue.add( "foo" );
trace( queue.add( "foo" ) );
trace( queue.size() );
Adding elements to a typed queue
var queue : Queue = new Queue( String );
queue.add( "foo" );
trace( queue.add( "foo" ) );
try
{
queue.add( 50 );
}
catch( e : ClassCastException )
{
trace ( e );
}
trace( queue.size() );
addAll
Collection to this queue. The behavior of this operation is
unspecified if the specified collection is modified while the
operation is in progress.
The rules which govern collaboration between typed and untyped Collectionare described in the isValidCollection descrition, all rules described
there are supported by the addAll method.
Collection whose elements are to be added to this queue.-
trueif this queue changed as a result of the call.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this queue - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
- How the
Queue.addAllmethod works with typesLet say that you have two typed queues
typedQueue1andtypedQueue2such :
var typedQueue1 : Queue = new Queue( String );
var typedQueue2 : Queue = new Queue( String );
typedQueue1.add( "foo1" );
typedQueue1.add( "foo2" );
typedQueue1.add( "foo3" );
typedQueue1.add( "foo4" );
typedQueue2.add( "foo3" );
typedQueue2.add( "foo4" );
typedQueue2.add( "foo5" );
typedQueue2.add( "foo6" );
And two untyped queuesuntypedQueue1anduntypedQueue2such :
var untypedQueue1 : Queue = new Queue();
var untypedQueue2 : Queue = new Queue();
untypedQueue1.add( 1 );
untypedQueue1.add( 2 );
untypedQueue1.add( 3 );
untypedQueue1.add( "foo1" );
untypedQueue2.add( 3 );
untypedQueue2.add( 4 );
untypedQueue2.add( 5 );
untypedQueue2.add( "foo1" );
The two operations below will work as expected,
realizing the union of the queues objects.
typedQueue1.addAll ( typedQueue2 );
untypedQueue1.addAll ( untypedQueue2 );
As an untyped queue can contain any types of objects at the
same time, the code below is always valid.
untypedQueue1.addAll( typedQueue2 );
But if you try to add an untyped collection to a typed one
the call will fail with an exception.
try
{
typedQueue2.addAll( untypedQueue2 );
}
catch( e : IllegalArgumentException )
{
trace( e );
}
clear
(optional operation). This queue will be empty
after this call returns (unless it throws an exception).
- {VISDOC_LINK_0}#
NullPointerException— if theCollectionmethod is
not supported by this collection.
contains
true if this queue contains at leastone occurence of the specified element. Moreformally,
returns
true if and only if this queue containsat least an element
e such that o === e. Object whose presence in this queueis to be tested.
-
trueif this queue contains the specified
element.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this queue
containsAll
true if this queue containsall of the elements of the specified collection. If the specified
collection is also a
Set, this method returns trueif it is a subliset of this queue.If the passed-in Collection is null the method throw aNullPointerException error.
If the passed-in Collection type is different than the current
one the function will throw an IllegalArgumentException.
However, if the type of this queue is null,
the passed-in Collection can have any type.
The rules which govern collaboration between typed and untyped Collectionare described in the isValidCollection descrition, all rules described
there are supported by the containsAll method.
Collection-
trueif this queue contains all of the elements of the
specified collection.
- {VISDOC_LINK_0}
NullPointerException— If the passed-in collection
isnull - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
getType
An untyped queue returns null, as the
wildcard type (*) is not a Classand Object class doesn't fit for primitive types.
-
Classtype of the queue's elements
isEmpty
true if this queue contains no elements. -
trueif this queue contains no elements.
isTyped
true if this queue perform a verificationof the type of elements.
-
trueif this queue perform a verification
of the type of elements.
isValidCollection
Collection is a validcollection for use with the
addAll, removeAll,retainAll and containsAll methods.When dealing with typed and untyped collection, the following rules apply :
- Two typed collection, which have the same type, can collaborate each other.
- Two untyped collection can collaborate each other.
- An untyped collection can add, remove, retain or contains any typed collection
of any type without throwing errors. - A typed collection will always fail when attempting to add, remove, retain
or contains an untyped collection.
If the passed-in Collection is null the method throw aNullPointerException error.
Collection to verify- boolean
trueif the collection is valid,
eitherfalse
- {VISDOC_LINK_0}
NullPointerException— If the passed-in collection
isnull - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
isValidType
Queue element's type.In the case that the queue is untyped the function
will always returns true.
In the case that the object's type prevents it to be added
as element for this queue the method will throw
a ClassCastException.
Object to verify-
trueif the object is elligible for this
queue object, eitherfalse.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this queue
iterator
The elements are returned according to the FIFO
(first-in-first-out) order.
- an iterator over the elements in this queue.
matchType
queue element's type.
-
trueif the object is elligible for this
queue object, eitherfalse.
peek
or returns
null if this queue is empty. - the head of this queue, or
nullif this queue is empty
poll
or returns
null if this queue is empty. - the head of this queue, or
nullif this queue is empty
remove
queue, if this queue contains one or more such elements.
Returns
true if this queue contained the specifiedelement (or equivalently, if this collection changed as a result
of the call).
In order to remove all occurences of an element you have to call
the remove method as long as the queue contains an
occurrence of the passed-in element. Typically, the construct to
remove all occurrences of an element should look like that :
while( queue.contains( element ) ) queue.remove( element );
If the current queue object is typed and if the passed-in object's
type prevents it to be added (and then removed) in this queue,
the function throws a ClassCastException.
object to be removed from this queue,if present.
-
trueif the queue contained the
specified element.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this queue
- Using the
Queue.remove()method of an untyped queue :
var queue : Queue = new Queue();
queue.add ( "foo" );
queue.add ( "foo" );
trace( queue.size() );
trace( queue.remove( "foo" ) );
trace( queue.size() );
trace( queue.remove( "foo" ) );
trace( queue.size() );
trace( queue.remove( "foo" ) );
Using theQueue.remove()method of a typed queue :
var queue : Queue = new Queue( String );
queue.add ( "foo" );
trace( queue.size() );
trace( queue.remove( "foo" ) );
try
{
queue.remove( 45 );
}
catch( e : ClassCastException )
{
trace ( e );
}
removeAll
in the specified collection (optional operation). At the end
of the call there's no occurences of any elements contained
in the passed-in collection.
The rules which govern collaboration between typed and untyped Collectionare described in the isValidCollection descrition, all rules described
there are supported by the removeAll method.
Collection that defines which elements will beremoved from this queue.
-
trueif this queue changed as a result
of the call.
- {VISDOC_LINK_0}
NullPointerException— if the specified collection is
null. - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
- Using the
Queue.removeAll()with untyped queues
var queue1 : Queue = new Queue();
var queue2 : Queue = new Queue();
queue1.add( 1 );
queue1.add( 2 );
queue1.add( 3 );
queue1.add( 4 );
queue1.add( "foo1" );
queue1.add( "foo2" );
queue1.add( "foo3" );
queue1.add( "foo4" );
queue2.add( 1 );
queue2.add( 3 );
queue2.add( "foo1" );
queue2.add( "foo3" );
trace ( queue1.removeAll ( queue2 ) ) ;
retainAll
in the specified collection (optional operation). In other words,
removes from this queue all of its elements that are not
contained in the specified collection.
The rules which govern collaboration between typed and untyped Collectionare described in the isValidCollection descrition, all rules described
there are supported by the retainAll method.
Collection that defines which elements thisqueue will retain.
-
trueif this collection changed as a result of the
call.
- {VISDOC_LINK_0}
NullPointerException— if the specified collection is
null. - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
- Using the
Queue.retainAll()with untyped queues
var queue1 : Queue = new Queue();
var queue2 : Queue = new Queue();
queue1.add( 1 );
queue1.add( 2 );
queue1.add( 3 );
queue1.add( 4 );
queue1.add( "foo1" );
queue1.add( "foo2" );
queue1.add( "foo3" );
queue1.add( "foo4" );
queue2.add( 1 );
queue2.add( 3 );
queue2.add( "foo1" );
queue2.add( "foo3" );
trace ( queue1.retainAll ( queue2 ) ) ;
size
-
Numberof elements in this queue (its cardinality).
toArray
Obeys the general contract of the
Collection.toArraymethod. -
Arraycontaining all of the elements
in this queue.
toString
String representation ofthis object.
The function return a string likecom.bourre.collection::Queue<String>for a typed collection. The string between the <
and > is the name of the type of the collection's
elements. If the collection is an untyped collection
the function will simply return the result of thePixlibStringifier.stringify call.
-
Stringrepresentation of
this object.