Packagecom.bourre.collection
Classpublic class Stack
ImplementsList, TypedContainer

Player version: Flash Player 9.0
Language version: ActionScript 3.0

The Stack class represents a last-in-first-out (LIFO) stack of objects. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.

When a stack is first created, it contains no items.


Example
Using a Stack
  var stack : Stack = new Stack ( Number );
  stack.push( 20 );
  
  try
  {
   stack.push("20"); // fail, as "20" isn't a Number
  }
  catch( e : ClassCastException )
  {
   trace( e );
  }
  

See also

List


Protected Properties
 PropertyDefined by
  _aStack : Array
Stack
  _oType : Class
Stack
Public Methods
 MethodDefined by
  
Stack(type:Class = null, content:Array = null)
Create an empty Stack.
Stack
  
add(o:Object):Boolean
Appends the specified element to the end of this stack.
Stack
  
addAll(c:Collection):Boolean
Appends all of the elements in the specified Collection to the end of this stack, in the order that they are returned by the specified Collection's Iterator.
Stack
  
addAllAt(index:uint, c:Collection):Boolean
Inserts all of the elements in the specified Collection into this stack at the specified position.
Stack
  
addAt(index:uint, o:Object):void
Inserts the specified element at the specified position in this stack.
Stack
  
clear():void
Removes all of the elements from this Stack.
Stack
  
contains(o:Object):Boolean
Returns true if this stack contains at least one occurence of the specified element.
Stack
  
Returns true if this Stack contains all of the elements in the specified Collection.
Stack
  
get(index:uint):Object
Returns the Object stored at the passed-in index in this stack object.
Stack
  
getType():Class
Return the current type allowed in the Stack
Stack
  
indexOf(o:Object):int
Searches for the first occurence of the given argument.
Stack
  
isEmpty():Boolean
Returns true if this stack contains no elements.
Stack
  
isTyped():Boolean
Returns true if this stack perform a verification of the type of elements.
Stack
  
Verify that the passed-in Collection is a valid collection for use with the addAll, addAllAt, removeAll, retainAll and containsAll methods.
Stack
  
isValidIndex(index:uint):void
Verify that the passed-in uint index is a valid index for this Stack.
Stack
  
isValidType(o:Object):Boolean
Verify that the passed-in object type match the current Stack element's type.
Stack
  
Returns an iterator over the elements in this list in proper sequence.
Stack
  
lastIndexOf(o:Object):int
Returns the index of the last occurrence of the specified object in this Stack.
Stack
  
listIterator(index:uint = 0):ListIterator
Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
Stack
  
matchType(o:*):Boolean
Verify if the passed-in object can be inserted in the current Stack.
Stack
  
peek():Object
Looks at the object at the top of this stack without removing it from the stack.
Stack
  
pop():Object
Removes the object at the top of this stack and returns that object as the value of this function.
Stack
  
push(item:Object):Object
Pushes an item onto the top of this stack.
Stack
  
remove(o:Object):Boolean
Removes a single instance of the specified element from this stack, if this stack contains one or more such elements.
Stack
  
removeAll(c:Collection):Boolean
Removes from this stack all of its elements that are contained in the specified collection (optional operation).
Stack
  
removeAt(o:Object):Boolean
Removes the element at the specified position in this stack.
Stack
  
retainAll(c:Collection):Boolean
Retains only the elements in this stack that are contained in the specified collection (optional operation).
Stack
  
search(o:Object):int
Searches for the first occurence of the given argument.
Stack
  
set(index:uint, o:Object):Object
Insert the passed-in Object in this stack at the specified index.
Stack
  
size():uint
Returns the number of components in this Stack.
Stack
  
subList(fromIndex:uint, toIndex:uint):List
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
Stack
  
toArray():Array
Returns an array containing all the elements in this stack.
Stack
  
toString():String
Returns the String representation of this object.
Stack
Property detail
_aStackproperty
protected var _aStack: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
Stack()constructor
public function Stack(type:Class = null, content:Array = null)

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Create an empty Stack.

You can pass the type for stack elements as argument of the constructor. In that case the stack is considered as typed

Parameters
type:Class (default = null) — A Class instance used as type for elements
 
content:Array (default = null)

Throws
ClassCastException — if the class of the specified elements prevents it from being added to this list.
Method detail
add()method
public function add(o:Object):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Appends the specified element to the end of this stack.

Parameters
o:Object — element to be appended to this Stack

Returns
Booleantrue if this stack has changed as result of the call (as per the general contract of Collection.add).

Throws
ClassCastException — if the class of the specified element prevents it from being added to this list.

See also


Example
Adding elements to an untyped stack
   var stack : Stack = new Stack();
   
   stack.add( 50 );
   stack.add( "foo" );
   
   trace( stack.add( "foo" ) ); // true, because stack allow one or more entries
           // for an element
   
   trace( stack.size() ); // 3
   
Adding elements to a typed stack
   var stack : Stack = new Stack( String );
   
   stack.add( "foo" );
   
   trace( stack.add( "foo" ) ); // true, because stack allow one or more occurrences
           // for an element
           
   try
   {
    stack.add( 50 ); // fail, as 50 is not a string
   }
   catch( e : ClassCastException )
   {
    trace ( e );
   }
   
   trace( stack.size() ); // 2
   

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

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Appends all of the elements in the specified Collection to the end of this stack, in the order that they are returned by the specified Collection's Iterator. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this stack, and this stack is nonempty.)

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:Collection — elements to be inserted into this stack.

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

Throws
ClassCastException — if the class of an element of the specified collection prevents it from being added to this collection.
 
NullPointerException — if the passed in collection is null.

See also

addAllAt()method 
public function addAllAt(index:uint, c:Collection):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Inserts all of the elements in the specified Collection into this stack at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the stack in the order that they are returned by the specified Collection's iterator.

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
index:uint — index uint index at which to insert first element from the specified collection.
 
c:Collection — c elements to be inserted into this stack.

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

Throws
IndexOutOfBoundsException — index is out of range (index < 0 || index > size()).
 
ClassCastException — if the class of an element of the specified collection prevents it from being added to this collection.
 
NullPointerException — if the passed in collection is null.

See also

addAt()method 
public function addAt(index:uint, o:Object):void

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Inserts the specified element at the specified position in this stack. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters
index:uintuint index at which the specified element is to be inserted.
 
o:Object — element to be inserted.

Throws
IndexOutOfBoundsException — index is out of range (index < 0 || index > size()).
 
ClassCastException — if the class of the specified element prevents it from being added to this list.

See also

clear()method 
public function clear():void

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Removes all of the elements from this Stack. The Stack 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 stack contains at least one occurence of the specified element. Moreformally, returns true if and only if this stack contains at least an element e such that o === e.

Parameters
o:ObjectObject whose presence in this stack is to be tested.

Returns
Booleantrue if this stack contains the specified element.

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

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns true if this Stack contains all of the elements in the specified Collection.

Parameters
c:Collection — a collection whose elements will be tested for containment in this Stack

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

Throws
NullPointerException — if the passed in collection is null.
get()method 
public function get(index:uint):Object

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns the Object stored at the passed-in index in this stack object.

If the passed-in index is not a valid index for this stack, the function throw an IndexOutOfBoundsException exception.

Parameters
index:uintuint index of the entry to get.

Returns
ObjectObject stored at the specified index

Throws
IndexOutOfBoundsException — The passed-in index is not a valid index for this stack
getType()method 
public function getType():Class

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Return the current type allowed in the Stack

Returns
ClassClass used to type checking.
indexOf()method 
public function indexOf(o:Object):int

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Searches for the first occurence of the given argument.

Parameters
o:Object — an object

Returns
int — the index of the first occurrence of the object argument in this stack, that is, the smallest value k such that (elem === elementData[k]) && (k >= index) is true; returns -1 if the object is not found.

Throws
ClassCastException — If the object's type prevents it to be added into this stack
isEmpty()method 
public function isEmpty():Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns true if this stack contains no elements.

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

Player version: Flash Player 9.0
Language version: ActionScript 3.0

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

Returns
Booleantrue if this stack 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, addAllAt, removeAll, retainAll and containsAll methods.

When dealing with typed and untyped collection, the following rules apply :

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

isValidIndex()method 
public function isValidIndex(index:uint):void

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Verify that the passed-in uint index is a valid index for this Stack. If not, an IndexOutOfBoundsException exception is thrown.

Parameters
index:uintuint index to verify

Throws
IndexOutOfBoundsException — The passed-in index is not a valid index for this stack
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 Stack element's type.

In the case that the stack 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 stack the method will throw a ClassCastException.

Parameters
o:ObjectObject to verify

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

Throws
ClassCastException — If the object's type prevents it to be added into this stack
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 list in proper sequence. The elements are returned according to the LIFO order of the stack.

This implementation returns a straightforward implementation of the iterator interface.

Returns
Iterator — an iterator over the elements in this list in proper sequence.
lastIndexOf()method 
public function lastIndexOf(o:Object):int

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns the index of the last occurrence of the specified object in this Stack.

Parameters
o:Object — o the desired component.

Returns
int — the index of the first occurrence of the object argument in this stack, that is, the largest value k such that (elem === elementData[k]) is true; returns -1 if the object is not found.

Throws
ClassCastException — If the object's type prevents it to be added into this stack
listIterator()method 
public function listIterator(index:uint = 0):ListIterator

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to the next method. An initial call to the previous method would return the element with the specified index minus one.

This implementation returns a straightforward implementation of the ListIterator interface that extends the implementation of the Iterator interface returned by the iterator() method. The ListIterator implementation relies on the backing list's get(int), set(int, Object), add(int, Object) and remove(int) methods.

Parameters
index:uint (default = 0)uint index of the first element to be returned from the list iterator (by a call to the next method).

Returns
ListIterator — a list iterator of the elements in this list (in proper sequence), starting at the specified position in the list.

Throws
IndexOutOfBoundsException — index is out of range (index < 0 || index > size()).
matchType()method 
public function matchType(o:*):Boolean

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Verify if the passed-in object can be inserted in the current Stack.

Parameters
o:* — Object to verify

Returns
Booleantrue if the object can be inserted in the Stack, either false.
peek()method 
public function peek():Object

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Looks at the object at the top of this stack without removing it from the stack.

Returns
Object — the object at the top of this stack (the last item of the object).
pop()method 
public function pop():Object

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Removes the object at the top of this stack and returns that object as the value of this function.

Returns
Object — The object at the top of this stack (the last item of the Stack object).
push()method 
public function push(item:Object):Object

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Pushes an item onto the top of this stack. This has exactly the same effect as:

add(item)

Parameters
item:Object — the item to be pushed onto this stack..

Returns
Object — the item argument.

See also

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 stack, if this stack contains one or more such elements. Returns true if this stack 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 stack contains an occurrence of the passed-in element. Typically, the construct to remove all occurrences of an element should look like that :

      while( stack.contains( element ) ) stack.remove( element );
      

If the current stack object is typed and if the passed-in object's type prevents it to be added (and then removed) in this stack, the function throws a ClassCastException.

Parameters
o:Objectobject to be removed from this stack, if present.

Returns
Booleantrue if the stack contained the specified element.

Throws
ClassCastException — If the object's type prevents it to be added into this stack

Example
Using the Stack.remove() method with an untyped stack :
   var stack : Stack = new Stack();
   stack.add ( "foo" );
   stack.add ( "foo" );
   
   trace( stack.size() ); // 1
   trace( stack.remove( "foo" ) ); // true, the first occurence of 'foo' have
              // been removed from the stack
   
   trace( stack.size() ); // 1
   trace( stack.remove( "foo" ) ); // true, the passed-in value has always
              // an occurence contained in the stack
   
   trace( stack.size() ); // 0
   trace( stack.remove( "foo" ) ); // false, as there is no more occurence
              // in the stack
   
Using the Stack.remove() method with a typed stack :
   var stack : Stack = new Stack( String );
   stack.add ( "foo" );
   
   trace( stack.size() ); // 1
   trace( stack.remove( "foo" ) ); // true, the passed-in value have been removed
   
   // the code below will produce an exception
   try
   {
    stack.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 stack 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 stack.

Returns
Booleantrue if this stack 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 Stack.removeAll() with untyped stacks
   var stack1 : Stack = new Stack();
   var stack2 : Stack = new Stack();
   
   stack1.add( 1 );
   stack1.add( 2 );
   stack1.add( 3 );
   stack1.add( 4 );
   stack1.add( "foo1" );
   stack1.add( "foo2" );
   stack1.add( "foo3" );
   stack1.add( "foo4" );
   
   stack2.add( 1 );
   stack2.add( 3 );
   stack2.add( "foo1" );
   stack2.add( "foo3" );
   
   trace ( stack1.removeAll ( stack2 ) ) ;// true
   // stack1 now contains :
   // 2, 4, 'foo2', 'foo4' 
   

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

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Removes the element at the specified position in this stack. Shifts any subsequent elements to the right (subtracts one from their indices).

Removes a single instance of the specified element from this stack, if this stack contains one or more such elements. Returns true if this stack 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 stack contains an occurrence of the passed-in element. Typically, the construct to remove all occurrences of an element should look like that :

      while( stack.contains( element ) ) stack.remove( element );
      

If the current stack object is typed and if the passed-in object's type prevents it to be added (and then removed) in this stack, the function throws a ClassCastException.

Parameters
o:Objectobject to be removed from this stack, if present.

Returns
Booleantrue if the stack contained the specified element.

Throws
IndexOutOfBoundsException — The passed-in index is not a valid index for this stack

See also


Example
Using the Stack.removeAt method with an untyped stack
   var stack : Stack = new Stack();
   stack.add( "foo1" );
   stack.add( "foo2" );
   stack.add( "foo3" );
   
   trace ( stack.removeAt( 2 ) ); // return true, 'foo3' have been removed
   trace ( stack.removeAt( 0 ) ); // return true, 'foo1' have been removed  
           // and 'foo2' is now at index 0
   
   try
   {
    stack.removeAt( 1 ); // fail, as stack have only one entry at index 0
   }
   catch( e : IndexOutOfBoundsException )
   {
    trace( e );
   }
   

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 stack that are contained in the specified collection (optional operation). In other words, removes from this stack 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 stack 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 Stack.retainAll() with untyped stacks
   var stack1 : Stack = new Stack();
   var stack2 : Stack = new Stack();
   
   stack1.add( 1 );
   stack1.add( 2 );
   stack1.add( 3 );
   stack1.add( 4 );
   stack1.add( "foo1" );
   stack1.add( "foo2" );
   stack1.add( "foo3" );
   stack1.add( "foo4" );
   
   stack2.add( 1 );
   stack2.add( 3 );
   stack2.add( "foo1" );
   stack2.add( "foo3" );
   
   trace ( stack1.retainAll ( stack2 ) ) ;// true
   // stack1 now contains :
   // 1, 3, 'foo1', 'foo3' 
   

search()method 
public function search(o:Object):int

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Searches for the first occurence of the given argument.

Parameters
o:Object — an object to search in the stack

Returns
int — the index of the first occurrence of the object argument in this stack, that is, the smallest value k such that (elem === elementData[k]) && (k >= index) is true; returns -1 if the object is not found.

Throws
ClassCastException — If the object's type prevents it to be added into this stack
set()method 
public function set(index:uint, o:Object):Object

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Insert the passed-in Object in this stack at the specified index. The method returns the object previously stored at this index.

If the passed-in index is not a valid index for this stack, the function throw an IndexOutOfBoundsException exception.

If the passed-in object's type prevents it to be added in this stack the function will throw a ClassCastException.

Parameters
index:uintuint index at which insert the passed-in Object.
 
o:ObjectObject to insert in this stack

Returns
ObjectObject previously stored at the specified index or null if the insertion haven't been done.

Throws
IndexOutOfBoundsException — The passed-in index is not a valid index for this stack
 
ClassCastException — If the object's type prevents it to be added into this stack
size()method 
public function size():uint

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns the number of components in this Stack.

Returns
uint — the number of components in this Stack.
subList()method 
public function subList(fromIndex:uint, toIndex:uint):List

Player version: Flash Player 9.0
Language version: ActionScript 3.0

Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and ToIndex are equal, the returned List is empty.)

Parameters
fromIndex:uint — fromIndex low endpoint (inclusive) of the subList.
 
toIndex:uint — high endpoint (exclusive) of the subList.

Returns
List — a view of the specified range within this List.

Throws
IndexOutOfBoundsException — fromIndex or toIndex are out of range (index < 0 || index > size()).
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 stack. Obeys the general contract of the Collection.toArray method.

Returns
ArrayArray containing all of the elements in this stack.

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::Stack<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.