| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
A collection that contains no duplicate elements. More formally, sets
contain no pair of elements
e1 and
e2 such that
e1 === e2, and at most one null element. As implied by
its name, this interface models the mathematical
set abstraction.
The Set class places additional stipulations, beyond those
inherited from the Collection interface, on the contracts of the
constructor and on the contracts of the add method.
Declarations for other inherited methods are also included here for convenience.
(The specifications accompanying these declarations have been tailored to the
Set class, but they do not contain any additional stipulations.)
The additional stipulation on constructors is, not surprisingly,
that all constructors must create a set that contains no duplicate elements
(as defined above).
Some set implementations have restrictions on the elements that
they may contain. For example, some implementations prohibit null elements,
and some have restrictions on the types of their elements. Attempting to
add an ineligible element throws an unchecked exception, typically
NullPointerException or ClassCastException.
Attempting to query the presence of an ineligible element may throw an exception,
or it may simply return false; some implementations will exhibit the former
behavior and some will exhibit the latter. More generally, attempting an
operation on an ineligible element whose completion would not result in
the insertion of an ineligible element into the set may throw an
exception or it may succeed, at the option of the implementation.
Such exceptions are marked as "optional" in the specification for this
class.
Example
Using an untyped
Set
var set : Set = new Set();
set.add( "foo" );
set.add( 25 );
trace ( set.add( 25 ) ) // false, as the object already exist in this set
set.add( false );
trace( set.size() ); // 3
Using a typed
Set
var set : Set = new Set( String );
set.add( "foo" );
try
{
set.add( 25 ); // throw an error, as 25 is not a String
}
catch( e : Error ) {}
trace ( set.add( "foo" ); ) // false, as the object already exist in this set
set.add( "hello" );
trace( set.size() ); // 2
protected var _aSet:Array
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
protected var _oType:Class
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
public function Set(type:Class = null)
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Creates a new set object. If the type
argument is defined, the set is considered as typed, and then
the type of all elements inserted in this set is checked.
Parameters
| type:Class (default = null) — Class type for elements of this set
|
public function add(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Adds the specified element to this set if it is not already
present (optional operation). More formally, adds the specified
element, o, to this set if this set contains no
element e such that o === e. If this set
already contains the specified element, the call leaves this set
unchanged and returns false. In combination with the
restriction on constructors, this ensures that sets never contain
duplicate elements.
The stipulation above does not imply that sets must accept all
elements; sets may refuse to add any particular element, including
null, and throwing an exception, as described in the
specification for Collection.add. Individual set
implementations should clearly document any restrictions on the the
elements that they may contain.
Parameters
| o:Object — element to be added to this set.
|
Returns
| Boolean — true if this set did not already contain the specified
element.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this set
|
Example
How to use the
Set.add method
var set : Set = new Set( String );
set.add( "foo" );
set.add( "foo" ); // return false, as 'foo' already exist in this set
set.add( 25 ); // throw a ClassCastException, as 25 is not a string
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
Set
class use the native operator to perform comparison, thus two objects with
equals properties are considered as differents.
var set : Set = new Set( Object );
var o : Object = { x : 50, y : 100 };
set.add( o );
set.add( o ); // return false, as o' already exist in this set
set.add( { x : 50, y : 100 } ); // return true, as the argument is not
// the same object than o, even if all
// their properties are equals
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 set if they're not already present (optional operation).
If the specified collection is also a set, the addAll
operation 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.
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:Collection — Collection whose elements are to be added to this set.
|
Returns
| Boolean — true if this set changed as a result of the call.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this set
|
| |
| — IllegalArgumentException — If the passed-in collection
type is not the same that the current one.
|
See also
Example
How the
Set.addAll method works with types
Let say that you have two typed sets typedSet1
and typedSet1.
var typedSet1 : Set = new Set( String );
var typedSet2 : Set = new Set( String );
typedSet1.add( "foo1" );
typedSet1.add( "foo2" );
typedSet1.add( "foo3" );
typedSet1.add( "foo4" );
typedSet2.add( "foo3" );
typedSet2.add( "foo4" );
typedSet2.add( "foo5" );
typedSet2.add( "foo6" );
And two untyped sets
untypedSet1
and
untypedSet1.
var untypedSet1 : Set = new Set();
var untypedSet2 : Set = new Set();
untypedSet1.add( 1 );
untypedSet1.add( 2 );
untypedSet1.add( 3 );
untypedSet1.add( "foo1" );
untypedSet1.add( 3 );
untypedSet1.add( 4 );
untypedSet1.add( 5 );
untypedSet1.add( "foo1" );
The two operations below will work as expected,
realizing an union between
Sets objects.
typedSet1.addAll ( typedSet2 );
// will produce a set containing :
// 'foo1'
// 'foo2'
// 'foo3'
// 'foo4'
// 'foo5'
// 'foo6'
untypedSet1.addAll ( untypedSet2 );
// will produce a set containing :
// 'foo1'
// 1
// 2
// 3
// 4
// 5
As an untyped set can contain any types of objects at the
same time, the code below is always valid.
untypedSet1.addAll( typedSet2 );
// will produce a set containing :
// 'foo1'
// 'foo3'
// 'foo4'
// 'foo5'
// 'foo6'
// 1
// 2
// 3
// 4
// 5
But if you try to add an untyped collection to a typed one
the call will fail with an exception.
try
{
typedSet2.addAll( untypedSet2 );
}
catch( e : IllegalArgumentException )
{
trace( e );
// The passed-in collection with type 'null' has not the same type
// than com.bourre.collections::Set<String>
}
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 set 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
| Boolean — true if this set 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
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 set. Shifts the element currently at that
position (if any) and any subsequent elements to the
right (adds one to their indices).
Parameters
| index:uint — uint 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
public function clear():void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes all of the elements from this set
(optional operation). This set will be empty
after this call returns (unless it throws an exception).
public function contains(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this set contains the
specified element. More formally, returns true if
and only if this set contains an element e
such that o === e.
Parameters
| o:Object — Object whose presence in this set
is to be tested.
|
Returns
| Boolean — true if this set contains the specified
element.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this set
|
public function containsAll(c:Collection):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this set 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 subset of this set.
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 — Collection to be checked for containment in this set.
|
Returns
| Boolean — true if this set contains all of the elements of the
specified collection.
|
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
public function equals(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Compares the specified object with this set for equality.
Returns true if the specified object is also a Set,
the two sets have the same size, and every member of the specified
Set is contained in this set (or equivalently,
every member of this set is contained in the specified
set). This definition ensures that the equals method works
properly across different implementations of the set class.
Parameters
| o:Object — Object to be compared for equality with this
set.
|
Returns
| Boolean — true if the specified Object is equal to this
set.
|
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 set object.
If the passed-in index is not a valid index
for this set, the function throw an
IndexOutOfBoundsException exception.
Parameters
| index:uint — uint index of the entry to get.
|
Returns
| Object — Object stored at the specified index
|
Throws
| — IndexOutOfBoundsException — The passed-in
index is not a valid index for this set
|
public function getType():Class
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Return the class type of element in this set object.
An untyped set returns null, as the
wildcard type (/code>) is not a Class
and Object class doesn't fit for primitive types.
Returns
| Class — Class type of the set's elements
|
public function indexOf(o:Object):int
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns the index of the object in this set
Parameters
| o:Object — the object to find
|
Returns
| int — int index of the passed object in this
set, either if the object isn't contained
in this set the function return -1
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this stack
|
public function isEmpty():Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this set contains no elements.
Returns
| Boolean — true if this set contains no elements.
|
public function isTyped():Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this set perform a verification
of the type of elements.
Returns
| Boolean — true if this set perform a verification
of the type of elements.
|
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
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
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 Set. If not, an
IndexOutOfBoundsException exception is
thrown.
Parameters
| index:uint — uint index to verify
|
Throws
| — IndexOutOfBoundsException — The passed-in
index is not a valid index for this set
|
public function isValidIndexForAdd(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 an insertion in this Set.
If not, an IndexOutOfBoundsException exception
is thrown.
Parameters
| index:uint — uint index to verify
|
Throws
| — IndexOutOfBoundsException — The passed-in
index is not a valid index for this set
|
public function isValidObject(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Verify that the passed-in object is valid for this Set
(well-typed, not already present in the set).
In the case that the object's type prevents it to be added
as element for this set the method will throw
a ClassCastException.
Parameters
Returns
| Boolean — true if the object is valid
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this set
|
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
Set element's type.
In the case that the set 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 set the method will throw
a ClassCastException.
Parameters
| o:Object — Object to verify
|
Returns
| Boolean — true if the object is elligible for this
set object, either false.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this set
|
public function iterator():Iterator
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns an iterator over the elements in this set.
The elements are returned in no particular order (unless this
set is an instance of some class that provides
a guarantee).
Returns
| Iterator —
an iterator over the elements in this set.
|
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 Set.
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
|
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()).
|
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
set element's type.
Parameters
Returns
| Boolean — true if the object is elligible for this
set object, either false.
|
public function remove(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes the specified element from this set
if it is present (optional operation). More formally,
removes an element e such that
o === e, if the set contains
such an element. Returns true if the set
contained the specified element (or equivalently, if the
set changed as a result of the call).
(The set will not contain the specified element
once the call returns.)
Parameters
| o:Object — object to be removed from this Set,
if present.
|
Returns
| Boolean — true if the set contained the specified element.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this set
|
Example
Using the
Set.remove() method of an untyped set :
var set : Set = new Set();
set.add ( "foo" );
trace( set.size() ); // 1
trace( set.remove( "foo" ) ); // true, the passed-in value have been removed
trace( set.size() ); // 0
trace( set.remove( "foo" ) ); // false, the passed-in value is no longer stored in this set
Using the
Set.remove() method of a typed set :
var set : Set = new Set( String );
set.add ( "foo" );
trace( set.size() ); // 1
trace( set.remove( "foo" ) ); // true, the passed-in value have been removed
// the code below will produce an exception
try
{
set.remove( 45 ); // fail, as the passed-in value is not of type string
}
catch( e : ClassCastException )
{
trace ( e );
}
public function removeAll(c:Collection):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes from this set all of its elements that are contained
in the specified collection (optional operation). If the specified
Collection is also a Set, this operation
effectively modifies this set so that its value is the
asymmetric set difference of the two sets.
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:Collection — Collection that defines which elements will be
removed from this set.
|
Returns
| Boolean — true if this set 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
Set.removeAll() with untyped sets
var set1 : Set = new Set();
var set2 : Set = new Set();
set1.add( 1 );
set1.add( 2 );
set1.add( 3 );
set1.add( 4 );
set1.add( "foo1" );
set1.add( "foo2" );
set1.add( "foo3" );
set1.add( "foo4" );
set2.add( 1 );
set2.add( 3 );
set2.add( "foo1" );
set2.add( "foo3" );
trace ( set1.removeAll ( set2 ) ) ;// true
// set1 now contains :
// 2, 4, 'foo2', 'foo4'
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 set.
Shifts any subsequent elements to the right (subtracts one from
their indices).
Removes the specified element from this set
if it is present (optional operation). More formally,
removes an element e such that
o === e, if the set contains
such an element. Returns true if the set
contained the specified element (or equivalently, if the
set changed as a result of the call).
(The set will not contain the specified element
once the call returns.)
Parameters
| o:Object — object to be removed from this Set,
if present.
|
Returns
| Boolean — true if the set contained the specified element.
|
Throws
| — IndexOutOfBoundsException — The passed-in
index is not a valid index for this set
|
See also
Example
Using the
Set.removeAt method with an untyped set
var set : Set = new Set();
set.add( "foo1" );
set.add( "foo2" );
set.add( "foo3" );
trace ( set.removeAt( 2 ) ); // return true, 'foo3' have been removed
trace ( set.removeAt( 0 ) ); // return true, 'foo1' have been removed
// and 'foo2' is now at index 0
try
{
set.removeAt( 1 ); // fail, as set have only one entry at index 0
}
catch( e : IndexOutOfBoundsException )
{
trace( e );
}
public function retainAll(c:Collection):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Retains only the elements in this set that are contained
in the specified collection (optional operation). In other words,
removes from this set all of its elements that are not
contained in the specified collection. If the specified collection
is also a Set, this operation effectively modifies this
set so that its value is the intersection of the
two sets.
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:Collection — Collection that defines which elements this
set will retain.
|
Returns
| Boolean — true 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
Set.retainAll() with untyped sets
var set1 : Set = new Set();
var set2 : Set = new Set();
set1.add( 1 );
set1.add( 2 );
set1.add( 3 );
set1.add( 4 );
set1.add( "foo1" );
set1.add( "foo2" );
set1.add( "foo3" );
set1.add( "foo4" );
set2.add( 1 );
set2.add( 3 );
set2.add( "foo1" );
set2.add( "foo3" );
trace ( set1.retainAll ( set2 ) ) ;// true
// set1 now contains :
// 1, 3, 'foo1', 'foo3'
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 set
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 set, the function throw an
IndexOutOfBoundsException exception.
If the passed-in object's type prevents it to be added
in this set the function will throw a
ClassCastException.
Parameters
| index:uint — uint index at which insert the
passed-in Object.
|
| |
| o:Object — Object to insert in this set
|
Returns
| Object — Object 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 set
|
| |
| — ClassCastException — If the object's type
prevents it to be added into this set
|
public function size():uint
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns the number of elements in this set (its cardinality).
Returns
| uint — Number of elements in this set (its cardinality).
|
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()).
|
public function swap(index1:uint, index2:uint):void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Swap two objects stored in the passed-in index
If the passed-in index are not valid index
for this set, the function throw an
IndexOutOfBoundsException exception.
Parameters
| index1:uint — uint index of the first Object.
|
| |
| index2:uint — uint index of the second Object.
|
Throws
| — — The passed-in
index are not valid index for this set
|
public function toArray():Array
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns an array containing all the elements in this set.
Obeys the general contract of the Collection.toArray
method.
Returns
| Array — Array containing all of the elements in this set.
|
See also
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::Set<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 — String representation of
this object.
|
LowRA API documentation 2008- 2009
Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1
mer. févr. 25 2009, 9:22 AM GMT+01:00