Set
| Kind of class: | public class |
|---|---|
| Package: | com.bourre.collection |
| Inherits from: | none |
| Implements: | |
| Author: | Olympe Dignat, Cédric Néhémie |
| Classpath: | com.bourre.collection.Set |
| File last modified: | Monday, 24 November 2008, 11:36:50 |
contain no pair of elements
e1 and e2 such thate1 === e2, and at most one null element. As implied byits 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 theSet 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, typicallyNullPointerException 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.
- 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 typedSet
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
Summary
- Set (type:Class = null)
- Creates a new set object.
- add (o:Object) : Boolean
- Adds the specified element to this set if it is not already
- addAt (index:uint, o:Object) : void
- Inserts the specified element at the specified position
- addAll (c:Collection) : Boolean
- Adds 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 the specified element from this set
- removeAt (index:uint) : Boolean
- Removes the element at the specified position in this set.
- removeAll (c:Collection) : Boolean
- Removes from this set all of its elements that are contained
- retainAll (c:Collection) : Boolean
- Retains only the elements in this set that are contained
- contains (o:Object) : Boolean
- Returns true if this set contains the
- containsAll (c:Collection) : Boolean
- Returns true if this set contains
- equals (o:Object) : Boolean
- Compares the specified object with this set for equality.
- indexOf (o:Object) : int
- Returns the index of the object in this set
- lastIndexOf (o:Object) : int
- Returns the index of the last occurrence of the specified
- clear : void
- Removes all of the elements from this set
- iterator : Iterator
- Returns an iterator over the elements in this set.
- listIterator (index:uint = 0) : ListIterator
- Returns a list iterator of the elements in this list
- subList (fromIndex:uint, toIndex:uint) : List
- Returns a view of the portion of this List between fromIndex,
- size : uint
- Returns the number of elements in this set (its cardinality).
- 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 set
- isValidIndex (index:uint) : void
- Verify that the passed-in uint index is a
- isValidIndexForAdd (index:uint) : void
- Verify that the passed-in uint index is a
- isValidCollection (c:Collection) : Boolean
- Verify that the passed-in Collection is a valid
- isValidObject (o:Object) : Boolean
- Verify that the passed-in object is valid for this Set(well-typed, not already present in the set).
- isValidType (o:Object) : Boolean
- Verify that the passed-in object type match the current
- isEmpty : Boolean
- Returns true if this set contains no elements.
- matchType (o:*) : Boolean
- Verify that the passed-in object type match the current
- isTyped : Boolean
- Returns true if this set perform a verification
- getType : Class
- Return the class type of element in this set object.
- toArray : Array
- Returns an array containing all the elements in this set.
- swap (index1:uint, index2:uint) : void
- Swap two objects stored in the passed-in index
- toString : String
- Returns the String representation of
Constructor
Set
typeargument is defined, the set is considered as typed, and thenthe type of all elements inserted in this set is checked.
Class type for elements of this setInstance methods
add
present (optional operation). More formally, adds the specified
element,
o, to this set if this set contains noelement
e such that o === e. If this setalready contains the specified element, the call leaves this set
unchanged and returns
false. In combination with therestriction 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, includingnull, 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.
-
trueif this set did not already contain the specified
element.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this set
- How to use the
Set.addmethod
var set : Set = new Set( String );
set.add( "foo" );
set.add( "foo" );
set.add( 25 );
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, theSetclass 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 );
set.add( { x : 50, y : 100 } );
addAll
Collection to this set if they're not already present (optional operation).
If the specified collection is also a set, 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.
The rules which govern collaboration between typed and untyped Collectionare described in the isValidCollection descrition, all rules described
there are supported by the addAll method.
Collection whose elements are to be added to this set.-
trueif this set changed as a result of the call.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this set - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
- How the
Set.addAllmethod works with typesLet say that you have two typed sets
typedSet1andtypedSet1.
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 setsuntypedSet1anduntypedSet1.
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 betweenSets objects.
typedSet1.addAll ( typedSet2 );
untypedSet1.addAll ( untypedSet2 );
As an untyped set can contain any types of objects at the
same time, the code below is always valid.
untypedSet1.addAll( typedSet2 );
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 );
}
addAllAt
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 untypedCollection are described in the isValidCollectiondescrition, all rules described there are supported by thecontainsAll method.
uint index at which to insertfirst element from the specified collection.
-
trueif this set changed as a result of the call.
- {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.
addAt
in this set. Shifts the element currently at that
position (if any) and any subsequent elements to the
right (adds one to their indices).
uint index at which the specifiedelement is to be inserted.
- {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.
clear
(optional operation). This set will be empty
after this call returns (unless it throws an exception).
- {VISDOC_LINK_0}#
o === e— if thetruemethod is
not supported by this collection.
contains
true if this set contains thespecified element. More formally, returns
true ifand only if this set contains an element
esuch that o === e. Object whose presence in this setis to be tested.
-
trueif this set contains the specified
element.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this set
containsAll
true if this set containsall of the elements of the specified collection. If the specified
collection is also a
Set, this method returns trueif it is a subset of this set.The rules which govern collaboration between typed and untyped Collectionare described in the isValidCollection descrition, all rules described
there are supported by the containsAll method.
Collection to be checked for containment in this set.-
trueif this set contains all of the elements of the
specified collection.
- {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.
equals
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.
Object to be compared for equality with thisset.
-
trueif the specified Object is equal to this
set.
get
Object stored at the passed-inindex in this set object.If the passed-in index is not a valid index
for this set, the function throw an IndexOutOfBoundsException exception.
uint index of the entry to get.-
Objectstored at the specifiedindex
- {VISDOC_LINK_0}
IndexOutOfBoundsException— The passed-in
index is not a valid index for this set
getType
An untyped set returns null, as the
wildcard type (*) is not a Classand Object class doesn't fit for primitive types.
-
Classtype of the set's elements
indexOf
-
intindex of the passed object in this
set, either if the object isn't contained
in this set the function return-1
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this stack
isEmpty
true if this set contains no elements. -
trueif this set contains no elements.
isTyped
true if this set perform a verificationof the type of elements.
-
trueif this set perform a verification
of the type of elements.
isValidCollection
Collection is a validcollection 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 aNullPointerException error.
Collection to verify- boolean
trueif the collection is valid,
eitherfalse
- {VISDOC_LINK_0}
NullPointerException— If the passed-in collection
isnull - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
isValidIndex
uint index is avalid index for this Set. If not, an
IndexOutOfBoundsException exception isthrown.
uint<code> index to verify— The passed-in
@throws <code>IndexOutOfBoundsException
index is not a valid index for this set
isValidIndexForAdd
uint index is avalid index for an insertion in this Set.
If not, an
IndexOutOfBoundsException exception is thrown.
uint<code> index to verify— The passed-in
@throws <code>IndexOutOfBoundsException
index is not a valid index for this set
isValidObject
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.
-
trueif the object is valid
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this set
isValidType
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.
Object to verify-
trueif the object is elligible for this
set object, eitherfalse.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this set
iterator
The elements are returned in no particular order (unless this
set is an instance of some class that provides
a guarantee).
- an iterator over the elements in this set.
lastIndexOf
object in this
Set. - the index of the first occurrence of the object argument
in this stack, that is, the largest valueksuch that(elem === elementData[k])is true;
returns-1if the object is not found.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this stack
listIterator
(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'sget(int), set(int, Object), add(int, Object) and remove(int)methods.
uint index of the first elementto be returned from the list iterator (by
a call to the next method).
- a list iterator of the elements in this list (in proper sequence),
starting at the specified position in the list.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— index is out of range
(index < 0 || index > size()).
matchType
set element's type.
-
trueif the object is elligible for this
set object, eitherfalse.
remove
if it is present (optional operation). More formally,
removes an element
e such thato === e, if the set containssuch an element. Returns
true if the setcontained 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.)
object to be removed from this Set,if present.
-
trueif the set contained the specified element.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this set
- Using the
Set.remove()method of an untyped set :
var set : Set = new Set();
set.add ( "foo" );
trace( set.size() );
trace( set.remove( "foo" ) );
trace( set.size() );
trace( set.remove( "foo" ) );
Using theSet.remove()method of a typed set :
var set : Set = new Set( String );
set.add ( "foo" );
trace( set.size() );
trace( set.remove( "foo" ) );
try
{
set.remove( 45 );
}
catch( e : ClassCastException )
{
trace ( e );
}
removeAll
in the specified collection (optional operation). If the specified
Collection is also a Set, this operationeffectively 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 Collectionare described in the isValidCollection descrition, all rules described
there are supported by the removeAll method.
Collection that defines which elements will beremoved from this set.
-
trueif this set changed as a result
of the call.
- {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.
- 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 ) ) ;
removeAt
Shifts any subsequent elements to the right (subtracts one from
their indices).
@copy com.bourre.collection.Set#remove()
uint index at which to remove an elementfrom the specified
Collection.-
trueif the object have been removed, false otherwise.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— The passed-in
index is not a valid index for this set
- Using the
Set.removeAtmethod with an untyped set
var set : Set = new Set();
set.add( "foo1" );
set.add( "foo2" );
set.add( "foo3" );
trace ( set.removeAt( 2 ) );
trace ( set.removeAt( 0 ) );
try
{
set.removeAt( 1 );
}
catch( e : IndexOutOfBoundsException )
{
trace( e );
}
retainAll
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 thisset so that its value is the intersection of the
two sets.
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.
Collection that defines which elements thisset will retain.
-
trueif this collection changed as a result of the
call.
- {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.
- 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 ) ) ;
set
Object in this setat the specified
index. The method returnsthe 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.
uint index at which insert thepassed-in
Object.Object to insert in this set-
Objectpreviously stored at the specified
indexor null if the insertion haven't been
done.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— The passed-in
index is not a valid index for this set - {VISDOC_LINK_1}
ClassCastException— If the object's type
prevents it to be added into this set
size
-
Numberof elements in this set (its cardinality).
subList
inclusive, and toIndex, exclusive. (If fromIndex and ToIndex
are equal, the returned List is empty.)
- a view of the specified range within this List.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— fromIndex or toIndex are
out of range (index < 0 || index > size()).
swap
If the passed-in index are not valid index
for this set, the function throw an IndexOutOfBoundsException exception.
uint index of the first Object.uint index of the second Object.- {VISDOC_LINK_0}
IndexOutOfBoundsException— The passed-in
index are not valid index for this set
toArray
Obeys the general contract of the
Collection.toArraymethod. -
Arraycontaining all of the elements in this set.
toString
String representation ofthis object.
The function return a string likecom.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 thePixlibStringifier.stringify call.
-
Stringrepresentation of
this object.