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
A collection designed for holding elements prior to processing.
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.

Example:
  • 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 typed Queuevar 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


Constructor
  • Queue (type:Class = null)
    • Create an empty Queue object.
Instance methods
  • 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

public function Queue (
type:Class = null)

Create an empty Queue object.

Instance methods

add

public function add (
o:Object) : Boolean

Adds the specified element to this queue.
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.

Parameters:
o:
element to be added to this queue.
Returns:
  • true if this queue have changed at
    the end of the call
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's
    type prevents it to be added into this queue
Example:
  • 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() );
Specified by:

addAll

public function addAll (
c:Collection) : Boolean

Adds all of the elements in the specified 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.

Parameters:
c:
Collection whose elements are to be added to this queue.
Returns:
  • true if this queue changed as a result of the call.
Throws:
  • {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.
Example:
  • How the Queue.addAll method works with types

    Let say that you have two typed queues typedQueue1and typedQueue2 such :


    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 queues untypedQueue1and untypedQueue2 such :


    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 );


    }
Specified by:

clear

public function clear (
) : void

Removes all of the elements from this queue
(optional operation). This queue will be empty
after this call returns (unless it throws an exception).
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the Collection method is
    not supported by this collection.
    #
Specified by:

contains

public function contains (
o:Object) : Boolean

Returns true if this queue contains at least
one occurence of the specified element. Moreformally,
returns true if and only if this queue contains
at least an element e such that o === e.
Parameters:
o:
Object whose presence in this queue
is to be tested.
Returns:
  • true if this queue contains the specified
    element.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this queue
Specified by:

containsAll

public function containsAll (
c:Collection) : Boolean

Returns true if this queue contains
all 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 a

NullPointerException 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.

Parameters:
c:
Collection
Returns:
  • true if this queue contains all of the elements of the
    specified collection.
Throws:
  • {VISDOC_LINK_0}NullPointerException — If the passed-in collection
    is null
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.
Specified by:

getType

public function getType (
) : Class

Return the class type of elements in this queue object.

An untyped queue returns null, as the
wildcard type (*) is not a Classand Object class doesn't fit for primitive types.

Returns:
  • Class type of the queue's elements
Specified by:

isEmpty

public function isEmpty (
) : Boolean

Returns true if this queue contains no elements.
Returns:
  • true if this queue contains no elements.
Specified by:

isTyped

public function isTyped (
) : Boolean

Returns true if this queue perform a verification
of the type of elements.
Returns:
  • true if this queue perform a verification
    of the type of elements.
Specified by:

isValidCollection

public function isValidCollection (
c:Collection) : Boolean

Verify that the passed-in Collection is a valid
collection 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 a

NullPointerException error.

Parameters:
c:
Collection to verify
Returns:
  • boolean true if the collection is valid,
    either false
Throws:
  • {VISDOC_LINK_0}NullPointerException — If the passed-in collection
    is null
  • {VISDOC_LINK_1}IllegalArgumentException — If the passed-in collection
    type is not the same that the current one.

isValidType

public function isValidType (
o:Object) : Boolean

Verify that the passed-in object type match the current

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.

Parameters:
o:
Object to verify
Returns:
  • true if the object is elligible for this
    queue object, either false.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this queue

iterator

public function iterator (

Returns an iterator over the elements in this queue.
The elements are returned according to the FIFO
(first-in-first-out) order.
Returns:
  • an iterator over the elements in this queue.
Specified by:

matchType

public function matchType (
o:*) : Boolean

Verify that the passed-in object type match the current
queue element's type.
Returns:
  • true if the object is elligible for this
    queue object, either false.

peek

public function peek (
) : Object

Retrieves, but does not remove, the head of this queue,
or returns null if this queue is empty.
Returns:
  • the head of this queue, or null if this queue is empty

poll

public function poll (
) : Object

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

remove

public function remove (
o:Object) : Boolean

Removes a single instance of the specified element from this
queue, if this queue contains one or more such elements.
Returns true if this queue contained the specified
element (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.

Parameters:
o:
object to be removed from this queue,
if present.
Returns:
  • true if the queue contained the
    specified element.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this queue
Example:
  • 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 the Queue.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 );
    }
Specified by:

removeAll

public function removeAll (
c:Collection) : Boolean

Removes from this queue all of its elements that are contained
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.

Parameters:
c:
Collection that defines which elements will be
removed from this queue.
Returns:
  • true if this queue changed as a result
    of the call.
Throws:
  • {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.
Example:
  • 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 ) ) ;


Specified by:

retainAll

public function retainAll (
c:Collection) : Boolean

Retains only the elements in this queue that are contained
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.

Parameters:
c:
Collection that defines which elements this
queue will retain.
Returns:
  • true if this collection changed as a result of the
    call.
Throws:
  • {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.
Example:
  • 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 ) ) ;


Specified by:

size

public function size (
) : uint

Returns the number of elements in this queue (its cardinality).
Returns:
  • Number of elements in this queue (its cardinality).
Specified by:

toArray

public function toArray (
) : Array

Returns an array containing all the elements in this queue.
Obeys the general contract of the Collection.toArraymethod.
Returns:
  • Array containing all of the elements
    in this queue.
Specified by:

toString

public function toString (
) : String

Returns the String representation of
this object.

The function return a string like

com.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 the

PixlibStringifier.stringify call.

Returns:
  • String representation of
    this object.