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.
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).
the data from the passed-in array (optional operation).
Parameters:
a:
Array initializer for the collectionInstance methods
add
public function add (
o:Object) : Boolean
Adds the passed-in object into this collection.
Returns
as a result of the call. Returns
contains the specified element.
Returns
true if this collection changedas a result of the call. Returns
falseif this collection does not permit duplicates and alreadycontains the specified element.
Parameters:
o:
element to add to this collection
Returns:
-
trueif the collection changed
as a result of the call
Example:
- How to use the
WeakCollection.addmethod
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 anequalsmethod which is used instead of the==or===operators. Nevertheless, theWeakCollectionclass 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 === e—Objectis 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
Adds all of the elements in the specified collection to this collection.
If the specified collection is also a weak collection, 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.
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:
-
trueif this collection changed as a result of the
call
See also:
Throws:
- #
- {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 istrue. - {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.
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
specified element. More formally, returns
and only if this collection contains an element
true if this collection contains thespecified element. More formally, returns
true ifand only if this collection contains an element
esuch that o === e. Parameters:
is to be tested.
o:
Object whose presence in this setis to be tested.
Returns:
-
trueif this set contains the specified
element.
Specified by:
containsAll
Returns
all of the elements of the specified collection. If the specified
collection is also a
true if this collection containsall 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:
in this collection.
c:
Collection to be checked for containmentin this collection.
Returns:
-
trueif 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:
-
trueif this collection contains no elements
Specified by:
iterator
Returns an iterator over the elements in this collection.
The elements are returned in no particular order.
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
such an element. Returns
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.)
if it is present. More formally,
removes an element
e such thato === e, if the collection containssuch an element. Returns
true if the collectioncontained 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:
if present.
o:
object to be removed from this collection,if present.
Returns:
-
trueif 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}#
NullPointerException—nullis not supported
by this collection.
Specified by:
removeAll
Removes from this collection all of its elements that are contained
in the specified collection. If the specified
modifies this collection so that its value is the asymmetric
difference of the two collections.
in the specified collection. If the specified
Collectionis also a WeakCollection, this operation effectivelymodifies this collection so that its value is the asymmetric
difference of the two collections.
Parameters:
removed from this collection.
c:
Collection that defines which elements will beremoved from this collection.
Returns:
-
trueif 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
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
this operation effectively modifies this set so that its value is the
intersection of the two sets.
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:
collection will retain.
c:
Collection that defines which elements thiscollection will retain.
Returns:
-
trueif 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:
-
Numberof 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
Obeys the general contract of the
Collection.toArraymethod. Returns:
-
Arraycontaining all of the elements in this set.
See also:
Specified by:
toString
public function toString (
) : String
Returns the
this object.
String representation ofthis object.
Returns:
-
Stringrepresentation of
this object.