WeakCollection

Kind of class:public class
Package:com.bourre.collection
Inherits from:none
Implements:
Author:Francis Bourre
Classpath:com.bourre.collection.WeakCollection
File last modified:Monday, 24 November 2008, 11:36:49
A weak collection stores element using weak references instead
of the classical hard references. That mean that the integrity
of the collection is never guarantee. An element which is in the
weak collection could be removed as soon as the Garbage Collector
has defined that this element is no longer used in the program.

Weak reference internally use a Dictionnary object
instead of an Array. Elements are stored as keys in
the dictionnary, resulting that there can be only one occurrence
of an element in the collection.

In current state of the art the weak collection allow any types
for its elements.

See also:

Summary


Constructor
  • WeakCollection (a:Array = null)
    • Creates a new weak collection, which initially contains
Instance methods
  • add (o:Object) : Boolean
    • Adds the passed-in object into this collection.
  • addAll (c:Collection) : Boolean
    • Adds all of the elements in the specified collection to this collection.
  • clear : void
    • Removes all of the elements from this collection.
  • contains (o:Object) : Boolean
    • Returns true if this collection contains the
  • containsAll (c:Collection) : Boolean
    • Returns true if this collection contains
  • isEmpty : Boolean
    • Returns true if this collection contains no elements.
  • iterator : Iterator
    • Returns an iterator over the elements in this collection.
  • remove (o:Object) : Boolean
    • Removes the specified element from this collection
  • removeAll (c:Collection) : Boolean
    • Removes from this collection all of its elements that are contained
  • retainAll (c:Collection) : Boolean
    • Retains only the elements in this collection that are contained
  • size : uint
    • Returns the number of elements in this collection (its cardinality).
  • toArray : Array
    • Returns an array containing all the elements in this set.
  • toString : String
    • Returns the String representation of

Constructor

WeakCollection

public function WeakCollection (
a:Array = null)

Creates a new weak collection, which initially contains
the data from the passed-in array (optional operation).
Parameters:
a:
Array initializer for the collection

Instance methods

add

public function add (
o:Object) : Boolean

Adds the passed-in object into this collection.
Returns true if this collection changed
as a result of the call. Returns falseif this collection does not permit duplicates and already
contains the specified element.
Parameters:
o:
element to add to this collection
Returns:
  • true if the collection changed
    as a result of the call
Example:
  • How to use the WeakCollection.add method

    var col : WeakCollection = new WeakCollection();

    col.add( "foo" );

    col.add( "foo" );


    In comparison with Java, where object which have all of their properties
    equals are considered as equals, AS3 doesn't allow that, except if objects
    provides an equals method which is used instead of the

    == or === operators. Nevertheless, the

    WeakCollection class use the native operator to perform
    comparison, thus two objects with equals properties are considered
    as differents.


    var col : WeakCollection = new WeakCollection();
    var o : Object = { x : 50, y : 100 };

    col.add( o );
    col.add( o );

    col.add( { x : 50, y : 100 } );


Throws:
  • {VISDOC_LINK_0}o === eObject is not supported by
    this collection.
    #
  • {VISDOC_LINK_1}true — class of the specified element prevents it
    from being added to this collection.
    #
  • {VISDOC_LINK_2}true — if the specified element is null and this
    collection does not support null elements.
    #
  • {VISDOC_LINK_3}WeakCollection — some aspect of this element prevents
    it from being added to this collection.
    #
Specified by:

addAll

public function addAll (
c:Collection) : Boolean

Adds all of the elements in the specified collection to this collection.
If the specified collection is also a weak collection, the addAlloperation effectively modifies this set so that its value is the
union of the two sets. The behavior of this operation is
unspecified if the specified collection is modified while the
operation is in progress.
Parameters:
c:
elements to be inserted into this collection.
Returns:
  • true if this collection changed as a result of the
    call
See also:
Throws:
  • CollectionWeakCollection.remove() — if this collection does not
    support the Collection method.
    #
  • {VISDOC_LINK_1}WeakCollection — if the class of an element of the specified
    collection prevents it from being added to this collection.
    #
  • {VISDOC_LINK_2}Collection — if the specified collection contains one
    or more null elements and this collection does not support null
    elements, or if the specified collection is true.
    #
  • {VISDOC_LINK_3}NullPointerException — some aspect of an element of the
    specified collection prevents it from being added to this collection.
    #
Specified by:

clear

public function clear (
) : void

Removes all of the elements from this collection.
This collection will be empty after this method returns unless it
throws an exception.
Throws:
  • {VISDOC_LINK_0}{VISDOC_CODE_BLOCK_53} — if the {VISDOC_CODE_BLOCK_54} method is
    not supported by this collection.
    #
Specified by:

contains

public function contains (
o:Object) : Boolean

Returns true if this collection contains the
specified element. More formally, returns true if
and only if this collection contains an element esuch that o === e.
Parameters:
o:
Object whose presence in this set
is to be tested.
Returns:
  • true if this set contains the specified
    element.
Throws:
  • {VISDOC_LINK_0}=== — if the type of the specified element
    is incompatible with this collection (optional).
    #
  • {VISDOC_LINK_1}WeakCollection — if the specified element is null and this
    collection does not support null elements (optional).
    #
Specified by:

containsAll

public function containsAll (
c:Collection) : Boolean

Returns true if this collection contains
all of the elements of the specified collection. If the specified
collection is also a WeakCollection, this method returns

true if it is a subset of this collection.
Parameters:
c:
Collection to be checked for containment
in this collection.
Returns:
  • true if this collection contains all of the
    elements of the specified collection.
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the specified collection is
    null.
See also:
Specified by:

isEmpty

public function isEmpty (
) : Boolean

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

iterator

public function iterator (

Returns an iterator over the elements in this collection.
The elements are returned in no particular order.
Returns:
  • an iterator over the elements in this set.
Specified by:

remove

public function remove (
o:Object) : Boolean

Removes the specified element from this collection
if it is present. More formally,
removes an element e such that

o === e, if the collection contains
such an element. Returns true if the collection
contained the specified element (or equivalently, if the
collection changed as a result of the call).
(The collection will not contain the specified element
once the call returns.)
Parameters:
o:
object to be removed from this collection,
if present.
Returns:
  • true if the collection contained the specified element.
Example:
  • Using the WeakCollection.remove() method :

    var set : Set = new Set();
    set.add ( "foo" );

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

    trace( set.size() );
    trace( set.remove( "foo" ) );
Throws:
  • {VISDOC_LINK_0}Collection — if the type of the specified element
    is incompatible with this collection (optional).
    #
  • {VISDOC_LINK_1}true — if the specified element is null and this
    collection does not support null elements (optional).
    #
  • {VISDOC_LINK_2}NullPointerExceptionnull is not supported
    by this collection.
    #
Specified by:

removeAll

public function removeAll (
c:Collection) : Boolean

Removes from this collection all of its elements that are contained
in the specified collection. If the specified Collectionis also a WeakCollection, this operation effectively
modifies this collection so that its value is the asymmetric
difference
of the two collections.
Parameters:
c:
Collection that defines which elements will be
removed from this collection.
Returns:
  • true if this collection changed as a result
    of the call.
Throws:
  • {VISDOC_LINK_0}NullPointerException — if the specified collection is
    null.
See also:
Example:
  • Using the WeakCollection.removeAll() method

    var col1 : WeakCollectio = new WeakCollectio();
    var col2 : WeakCollectio = new WeakCollectio();

    col1.add( 1 );
    col1.add( 2 );
    col1.add( 3 );
    col1.add( 4 );
    col1.add( "foo1" );
    col1.add( "foo2" );
    col1.add( "foo3" );
    col1.add( "foo4" );

    col2.add( 1 );
    col2.add( 3 );
    col2.add( "foo1" );
    col2.add( "foo3" );

    trace ( col1.removeAll ( col2 ) ) ;


Specified by:

retainAll

public function retainAll (
c:Collection) : Boolean

Retains only the elements in this collection that are contained
in the specified collection. In other words, removes from this
collection all of its elements that are not contained in the specified
collection. If the specified collection is also a WeakCollection,
this operation effectively modifies this set so that its value is the
intersection of the two sets.
Parameters:
c:
Collection that defines which elements this
collection 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.
See also:
Example:
  • Using the WeakCollection.retainAll() method

    var col1 : WeakCollection = new WeakCollection();
    var col2 : WeakCollection = new WeakCollection();

    col1.add( 1 );
    col1.add( 2 );
    col1.add( 3 );
    col1.add( 4 );
    col1.add( "foo1" );
    col1.add( "foo2" );
    col1.add( "foo3" );
    col1.add( "foo4" );

    col2.add( 1 );
    col2.add( 3 );
    col2.add( "foo1" );
    col2.add( "foo3" );

    trace ( col1.retainAll ( col2 ) ) ;


Specified by:

size

public function size (
) : uint

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

toArray

public function toArray (
) : Array

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

toString

public function toString (
) : String

Returns the String representation of
this object.
Returns:
  • String representation of
    this object.