Stack

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.Stack
File last modified:Monday, 24 November 2008, 11:36:49
The Stack class represents a last-in-first-out (LIFO) stack
of objects. The usual push and popoperations are provided, as well as a method to peekat 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.

See also:
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 );
    }

Summary


Constructor
  • Stack (type:Class = null, content:Array = null)
    • Create an empty Stack.
Instance methods
  • add (o:Object) : Boolean
    • Appends the specified element to the end of this stack.
  • addAt (index:uint, o:Object) : void
    • Inserts the specified element at the specified position
  • addAll (c:Collection) : Boolean
    • Appends all of the elements in the specified Collection
  • addAllAt (index:uint, c:Collection) : Boolean
    • Inserts all of the elements in the specified Collection into
  • remove (o:Object) : Boolean
    • Removes a single instance of the specified element from this
  • removeAt (index:uint) : Boolean
    • Removes the element at the specified position in this stack.
  • removeAll (c:Collection) : Boolean
    • Removes from this stack all of its elements that are contained
  • retainAll (c:Collection) : Boolean
    • Retains only the elements in this stack that are contained
  • contains (o:Object) : Boolean
    • Returns true if this stack contains at least
  • containsAll (c:Collection) : Boolean
    • Returns true if this Stack contains all of the elements
  • search (o:Object) : int
    • Searches for the first occurence of the given argument.
  • indexOf (o:Object) : int
    • Searches for the first occurence of the given argument.
  • lastIndexOf (o:Object) : int
    • Returns the index of the last occurrence of the specified
  • clear : void
    • Removes all of the elements from this Stack.
  • iterator : Iterator
    • Returns an iterator over the elements in this list
  • listIterator (index:uint = 0) : ListIterator
    • Returns a list iterator of the elements in this list
  • size : uint
    • Returns the number of components in this Stack.
  • get (index:uint) : Object
    • Returns the Object stored at the passed-in
  • set (index:uint, o:Object) : Object
    • Insert the passed-in Object in this stack
  • peek : Object
    • Looks at the object at the top of this stack
  • pop : Object
    • Removes the object at the top of this stack and returns
  • push (item:Object) : Object
    • Pushes an item onto the top of this stack.
  • subList (fromIndex:uint, toIndex:uint) : List
    • Returns a view of the portion of this List between fromIndex,
  • isValidIndex (index:uint) : void
    • Verify that the passed-in uint index is a
  • isEmpty : Boolean
    • Returns true if this stack contains no elements.
  • 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
  • matchType (o:*) : Boolean
    • Verify if the passed-in object can be inserted in the
  • isTyped : Boolean
    • Returns true if this stack perform a verification
  • getType : Class
    • Return the current type allowed in the Stack
  • toArray : Array
    • Returns an array containing all the elements in this stack.
  • toString : String
    • Returns the String representation of

Constructor

Stack

public function Stack (
type:Class = null, content:Array = null)

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:
A Class instance used as type for elements
Throws:
  • {VISDOC_LINK_0}ClassCastException — if the class of the specified
    elements prevents it from being added to this list.

Instance methods

add

public function add (
o:Object) : Boolean

Appends the specified element to the end of this stack.
Parameters:
o:
element to be appended to this Stack
Returns:
  • true if this stack has changed as result
    of the call (as per the general contract of Collection.add).
See also:
Throws:
  • {VISDOC_LINK_0}ClassCastException — if the class of the specified
    element prevents it from being added to this list.
Example:
  • Adding elements to an untyped stack

    var stack : Stack = new Stack();

    stack.add( 50 );
    stack.add( "foo" );

    trace( stack.add( "foo" ) );


    trace( stack.size() );


    Adding elements to a typed stack

    var stack : Stack = new Stack( String );

    stack.add( "foo" );

    trace( stack.add( "foo" ) );


    try
    {
    stack.add( 50 );
    }
    catch( e : ClassCastException )
    {
    trace ( e );
    }

    trace( stack.size() );
Specified by:

addAll

public function 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.
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 isValidCollectiondescrition, all rules described there are supported by the

containsAll method.

Parameters:
c:
elements to be inserted into this stack.
Returns:
  • true if this stack changed as a result of the call.
Throws:
  • {VISDOC_LINK_0}ClassCastException — if the class of an element of the specified
    collection prevents it from being added to this collection.
  • {VISDOC_LINK_1}NullPointerException — if the passed in collection is null.
Specified by:

addAllAt

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

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 isValidCollectiondescrition, all rules described there are supported by the

containsAll method.

Parameters:
index:
uint index at which to insert
first element from the specified collection.
c :
elements to be inserted into this stack.
Returns:
  • true if this stack changed as a result of the call.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — index is out of range
    (index < 0 || index > size()).
  • {VISDOC_LINK_1}ClassCastException — if the class of an element of
    the specified collection prevents it from being added to this collection.
  • {VISDOC_LINK_2}NullPointerException — if the passed in collection is null.
Specified by:

addAt

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

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:
uint index at which the specified
element is to be inserted.
o :
element to be inserted.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — index is out of range
    (index < 0 || index > size()).
  • {VISDOC_LINK_1}ClassCastException — if the class of the specified
    element prevents it from being added to this list.
See also:
Specified by:

clear

public function clear (
) : void

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

contains

public function contains (
o:Object) : Boolean

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:
Object whose presence in this stack
is to be tested.
Returns:
  • true if this stack contains the specified
    element.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this stack
Specified by:

containsAll

public function containsAll (
c:Collection) : Boolean

Returns true if this Stack contains all of the elements
in the specified Collection.
Parameters:
c:
a collection whose elements will be tested for
containment in this Stack
Returns:
  • true if this Stack contains all of the elements
    in the specified collection.
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the passed in collection is null.
Specified by:

get

public function get (
index:uint) : Object

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:
uint index of the entry to get.
Returns:
  • Object stored at the specified index
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — The passed-in
    index is not a valid index for this stack
Specified by:

getType

public function getType (
) : Class

Return the current type allowed in the Stack
Returns:
  • Class used to type checking.
Specified by:

indexOf

public function indexOf (
o:Object) : int

Searches for the first occurence of the given argument.
Parameters:
o:
an object
Returns:
  • 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:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this stack
Specified by:

isEmpty

public function isEmpty (
) : Boolean

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

isTyped

public function isTyped (
) : Boolean

Returns true if this stack perform a verification
of the type of elements.
Returns:
  • true if this stack 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, addAllAt,

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.

isValidIndex

public function isValidIndex (
index:uint) : void

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

IndexOutOfBoundsException exception is
thrown.
Parameters:
index:
uint<code> index to verify

@throws <code>IndexOutOfBoundsException
— The passed-in
index is not a valid index for this stack

isValidType

public function isValidType (
o:Object) : Boolean

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:
Object to verify
Returns:
  • true if the object is elligible for this
    stack object, either false.
Throws:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this stack

iterator

public function iterator (

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:
  • an iterator over the elements in this list in proper sequence.
Specified by:

lastIndexOf

public function lastIndexOf (
o:Object) : int

Returns the index of the last occurrence of the specified
object in this Stack.
Parameters:
o:
the desired component.
Returns:
  • 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:
  • {VISDOC_LINK_0}ClassCastException — If the object's type
    prevents it to be added into this stack
Specified by:

listIterator

public function 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. 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 index of the first element
to be returned from the list iterator (by
a call to the next method).
Returns:
  • a list iterator of the elements in this list (in proper sequence),
    starting at the specified position in the list.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — index is out of range
    (index < 0 || index > size()).
Specified by:

matchType

public function matchType (
o:*) : Boolean

Verify if the passed-in object can be inserted in the
current Stack.
Parameters:
o:
Object to verify
Returns:
  • true if the object can be inserted in
    the Stack, either false.

peek

public function peek (
) : Object

Looks at the object at the top of this stack
without removing it from the stack.
Returns:
  • the object at the top of this stack
    (the last item of the object).

pop

public function pop (
) : Object

Removes the object at the top of this stack and returns
that object as the value of this function.
Returns:
  • The object at the top of this stack
    (the last item of the Stack object).

push

public function push (
item:Object) : Object

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

add(item)
Parameters:
item:
the item to be pushed onto this stack..
Returns:
  • the item argument.
See also:

remove

public function remove (
o:Object) : Boolean

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:
object to be removed from this stack,
if present.
Returns:
  • true if the stack contained the
    specified element.
Throws:
  • {VISDOC_LINK_0}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() );
    trace( stack.remove( "foo" ) );


    trace( stack.size() );
    trace( stack.remove( "foo" ) );


    trace( stack.size() );
    trace( stack.remove( "foo" ) );



    Using the Stack.remove() method with a typed stack :


    var stack : Stack = new Stack( String );
    stack.add ( "foo" );

    trace( stack.size() );
    trace( stack.remove( "foo" ) );


    try
    {
    stack.remove( 45 );
    }
    catch( e : ClassCastException )
    {
    trace ( e );
    }
Specified by:

removeAll

public function removeAll (
c:Collection) : Boolean

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 isValidCollectiondescrition, all rules described there are supported by the

removeAll method.

Parameters:
c:
Collection that defines which elements will be
removed from this stack.
Returns:
  • true if this stack 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 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 ) ) ;


Specified by:

removeAt

public function removeAt (
index:uint) : Boolean

Removes the element at the specified position in this stack.
Shifts any subsequent elements to the right (subtracts one from
their indices).
@copy com.bourre.collection.Stack#remove()
Parameters:
index:
uint index at which to remove an element
from the specified Collection.
Returns:
  • true if the object have been removed, false otherwise.
Throws:
  • {VISDOC_LINK_0}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 ) );
    trace ( stack.removeAt( 0 ) );


    try
    {
    stack.removeAt( 1 );
    }
    catch( e : IndexOutOfBoundsException )
    {
    trace( e );
    }
Specified by:

retainAll

public function retainAll (
c:Collection) : Boolean

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

Parameters:
c:
Collection that defines which elements this
stack 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 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 ) ) ;


Specified by:

set

public function set (
index:uint, o:Object) : Object

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:
uint index at which insert the
passed-in Object.
o :
Object to insert in this stack
Returns:
  • Object previously stored at the specified
    index or null if the insertion haven't been
    done.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — The passed-in
    index is not a valid index for this stack
  • {VISDOC_LINK_1}ClassCastException — If the object's type
    prevents it to be added into this stack
Specified by:

size

public function size (
) : uint

Returns the number of components in this Stack.
Returns:
  • the number of components in this Stack.
Specified by:

subList

public function subList (
fromIndex:uint, toIndex:uint) : List

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:
low endpoint (inclusive) of the subList.
toIndex :
high endpoint (exclusive) of the subList.
Returns:
  • a view of the specified range within this List.
Throws:
  • {VISDOC_LINK_0}IndexOutOfBoundsException — fromIndex or toIndex are
    out of range (index < 0 || index > size()).
Specified by:

toArray

public function toArray (
) : Array

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

toString

public function toString (
) : String

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:
  • String representation of
    this object.