Packagecom.bourre.collection
Classpublic class Queue
ImplementsCollection, TypedContainer

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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 Queue
var 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 Queue
var 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 
  



Protected Properties
 PropertyDefined by
  _aQueue : Array
Queue
  _oType : Class
Queue
Public Methods
 MethodDefined by
  
Queue(type:Class = null)
Create an empty Queue object.
Queue
  
add(o:Object):Boolean
Adds the specified element to this queue.
Queue
  
addAll(c:Collection):Boolean
Adds all of the elements in the specified Collection to this queue.
Queue
  
clear():void
Removes all of the elements from this queue (optional operation).
Queue
  
contains(o:Object):Boolean
Returns true if this queue contains at least one occurence of the specified element.
Queue
  
Returns true if this queue contains all of the elements of the specified collection.
Queue
  
getType():Class
Return the class type of elements in this queue object.
Queue
  
isEmpty():Boolean
Returns true if this queue contains no elements.
Queue
  
isTyped():Boolean
Returns true if this queue perform a verification of the type of elements.
Queue
  
Verify that the passed-in Collection is a valid collection for use with the addAll, removeAll, retainAll and containsAll methods.
Queue
  
isValidType(o:Object):Boolean
Verify that the passed-in object type match the current Queue element's type.
Queue
  
Returns an iterator over the elements in this queue.
Queue
  
matchType(o:*):Boolean
Verify that the passed-in object type match the current queue element's type.
Queue
  
peek():Object
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
Queue
  
poll():Object
Retrieves and removes the head of this queue, or returns null if this queue is empty.
Queue
  
remove(o:Object):Boolean
Removes a single instance of the specified element from this queue, if this queue contains one or more such elements.
Queue
  
removeAll(c:Collection):Boolean
Removes from this queue all of its elements that are contained in the specified collection (optional operation).
Queue
  
retainAll(c:Collection):Boolean
Retains only the elements in this queue that are contained in the specified collection (optional operation).
Queue
  
size():uint
Returns the number of elements in this queue (its cardinality).
Queue
  
toArray():Array
Returns an array containing all the elements in this queue.
Queue
  
toString():String
Returns the String representation of this object.
Queue
Property detail
_aQueueproperty
protected var _aQueue:Array

Player version: Flash Player 9.0
Language version: ActionScript 3.0

_oTypeproperty 
protected var _oType:Class

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Constructor detail
Queue()constructor
public function Queue(type:Class = null)

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Create an empty Queue object.

Parameters
type:Class (default = null)
Method detail
add()method
public function add(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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:Object — element to be added to this queue.

Returns
Booleantrue if this queue have changed at the end of the call

Throws
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" ) ); // true, because queue allow one or more entries
           // for an element
   
   trace( queue.size() ); // 3
   
Adding elements to a typed queue
   var queue : Queue = new Queue( String );
   
   queue.add( "foo" );
   
   trace( queue.add( "foo" ) ); // true, because queue allow one or more occurrences
           // for an element
           
   try
   {
    queue.add( 50 ); // fail, as 50 is not a string
   }
   catch( e : ClassCastException )
   {
    trace ( e );
   }
   
   trace( queue.size() ); // 2
   

addAll()method 
public function addAll(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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 Collection are described in the isValidCollection descrition, all rules described there are supported by the addAll method.

Parameters
c:CollectionCollection whose elements are to be added to this queue.

Returns
Booleantrue if this queue changed as a result of the call.

Throws
ClassCastException — If the object's type prevents it to be added into this queue
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.

See also


Example
How the Queue.addAll method works with types

Let say that you have two typed queues typedQueue1 and 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 untypedQueue1 and 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 );
   // will produce a queue containing : 
   // 'foo1'
   // 'foo2'
   // 'foo3'
   // 'foo4'
   // 'foo3'
   // 'foo4'
   // 'foo5'
   // 'foo6'
   
   untypedQueue1.addAll ( untypedQueue2 );
   // will produce a queue containing : 
   // 1
   // 2
   // 3
   // 'foo1'
   // 3
   // 4
   // 5
   // 'foo1'
   
As an untyped queue can contain any types of objects at the same time, the code below is always valid.
   untypedQueue1.addAll( typedQueue2 );
   // will produce a queue containing : 
   // 1
   // 2
   // 3
   // 'foo1'
   // 3
   // 4
   // 5
   // 'foo1'
   // 'foo3'
   // 'foo4'
   // 'foo5'
   // 'foo6'
   
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 ); 
    // The passed-in collection with type 'null' has not the same type 
    // than com.bourre.collections::Queue<String>
   }
   

clear()method 
public function clear():void

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Removes all of the elements from this queue (optional operation). This queue will be empty after this call returns (unless it throws an exception).

contains()method 
public function contains(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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:ObjectObject whose presence in this queue is to be tested.

Returns
Booleantrue if this queue contains the specified element.

Throws
ClassCastException — If the object's type prevents it to be added into this queue
containsAll()method 
public function containsAll(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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 true if 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 Collection are described in the isValidCollection descrition, all rules described there are supported by the containsAll method.

Parameters
c:CollectionCollection

Returns
Booleantrue if this queue contains all of the elements of the specified collection.

Throws
NullPointerException — If the passed-in collection is null
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.
getType()method 
public function getType():Class

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Return the class type of elements in this queue object.

An untyped queue returns null, as the wildcard type (/code>) is not a Class and Object class doesn't fit for primitive types.

Returns
ClassClass type of the queue's elements
isEmpty()method 
public function isEmpty():Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns true if this queue contains no elements.

Returns
Booleantrue if this queue contains no elements.
isTyped()method 
public function isTyped():Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns true if this queue perform a verification of the type of elements.

Returns
Booleantrue if this queue perform a verification of the type of elements.
isValidCollection()method 
public function isValidCollection(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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:CollectionCollection to verify

Returns
Boolean — boolean true if the collection is valid, either false

Throws
NullPointerException — If the passed-in collection is null
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.

See also

isValidType()method 
public function isValidType(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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:ObjectObject to verify

Returns
Booleantrue if the object is elligible for this queue object, either false.

Throws
ClassCastException — If the object's type prevents it to be added into this queue
iterator()method 
public function iterator():Iterator

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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

Returns
Iterator — an iterator over the elements in this queue.
matchType()method 
public function matchType(o:*):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Verify that the passed-in object type match the current queue element's type.

Parameters
o:*

Returns
Booleantrue if the object is elligible for this queue object, either false.
peek()method 
public function peek():Object

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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

Returns
Object — the head of this queue, or null if this queue is empty
poll()method 
public function poll():Object

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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

Returns
Object — the head of this queue, or null if this queue is empty
remove()method 
public function remove(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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:Objectobject to be removed from this queue, if present.

Returns
Booleantrue if the queue contained the specified element.

Throws
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() ); // 1
   trace( queue.remove( "foo" ) ); // true, the first occurence of 'foo' have
              // been removed from the queue
   
   trace( queue.size() ); // 1
   trace( queue.remove( "foo" ) ); // true, the passed-in value has always
              // an occurence contained in the queue
   
   trace( queue.size() ); // 0
   trace( queue.remove( "foo" ) ); // false, as there is no more occurence
              // in the queue
   
Using the Queue.remove() method of a typed queue :
   var queue : Queue = new Queue( String );
   queue.add ( "foo" );
   
   trace( queue.size() ); // 1
   trace( queue.remove( "foo" ) ); // true, the passed-in value have been removed
   
   // the code below will produce an exception
   try
   {
    queue.remove( 45 ); // fail, as the passed-in value is not of type string
   }
   catch( e : ClassCastException )
   {
    trace ( e );  
   }
   

removeAll()method 
public function removeAll(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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 Collection are described in the isValidCollection descrition, all rules described there are supported by the removeAll method.

Parameters
c:CollectionCollection that defines which elements will be removed from this queue.

Returns
Booleantrue if this queue changed as a result of the call.

Throws
NullPointerException — if the specified collection is null.
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.

See also


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 ) ) ;// true
   // queue1 now contains :
   // 2, 4, 'foo2', 'foo4' 
   

retainAll()method 
public function retainAll(c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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 Collection are described in the isValidCollection descrition, all rules described there are supported by the retainAll method.

Parameters
c:CollectionCollection that defines which elements this queue will retain.

Returns
Booleantrue if this collection changed as a result of the call.

Throws
NullPointerException — if the specified collection is null.
 
IllegalArgumentException — If the passed-in collection type is not the same that the current one.

See also


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 ) ) ;// true
   // queue1 now contains :
   // 1, 3, 'foo1', 'foo3' 
   

size()method 
public function size():uint

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns the number of elements in this queue (its cardinality).

Returns
uintNumber of elements in this queue (its cardinality).
toArray()method 
public function toArray():Array

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns an array containing all the elements in this queue. Obeys the general contract of the Collection.toArray method.

Returns
ArrayArray containing all of the elements in this queue.

See also

toString()method 
public function toString():String

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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
StringString representation of this object.