JADE v6.1

com.dautelle.util
Class FastSet

java.lang.Object
  extended bycom.dautelle.realtime.RealtimeObject
      extended bycom.dautelle.util.FastSet
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection, Realtime, java.io.Serializable, java.util.Set

public class FastSet
extends RealtimeObject
implements java.util.Set, java.lang.Cloneable

This class represents a Set backed by a FastMap instance.

Instances of this class can directly be allocated from the current thread stack using the newInstance(int) factory method (e.g. for throw-away set to avoid the creation cost).

FastSet has a predictable iteration order, which is the order in which the element were added to the set. The set iterator is also real-time compliant (allocated on the stack when running in a pool context).

Version:
6.0, Mar 24, 2004
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class com.dautelle.realtime.RealtimeObject
RealtimeObject.Factory
 
Constructor Summary
FastSet()
          Creates a FastSet with a default capacity.
FastSet(int capacity)
          Creates a FastSet with the specified capacity.
FastSet(java.util.Set set)
          Creates a FastSet, copy of the specified Set.
 
Method Summary
 boolean add(java.lang.Object o)
          Adds the specified element to this set if it is not already present.
 boolean addAll(java.util.Collection c)
          Adds all of the elements in the specified collection to this set if they're not already present.
 void clear()
          Removes all of the elements from this set.
 java.lang.Object clone()
          Returns a shallow copy of this FastSet.
 boolean contains(java.lang.Object o)
          Returns true if this set contains the specified element.
 boolean containsAll(java.util.Collection c)
          Returns true if this set contains all of the elements of the specified collection.
 boolean equals(java.lang.Object obj)
          Compares the specified object with this FastSet for equality.
 java.lang.Object export()
          Exports this object out of the current pool context.
 int hashCode()
          Returns the hash code value for this FastSet.
 boolean isEmpty()
          Returns true if this set contains no elements.
 java.util.Iterator iterator()
          Returns an iterator over the elements in this set (allocated from the "stack" when executing in a PoolContext).
static FastSet newInstance(int capacity)
          Returns a FastSet allocated from the current stack (or the heap if not in a pool context).
 boolean remove(java.lang.Object o)
          Removes the specified element from this set if it is present.
 boolean removeAll(java.util.Collection c)
          Removes from this set all of its elements that are contained in the specified collection.
 boolean retainAll(java.util.Collection c)
          Retains only the elements in this set that are contained in the specified collection.
 int size()
          Returns the number of elements in this set (its cardinality).
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this set.
 java.lang.Object[] toArray(java.lang.Object[] a)
          Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.
 java.lang.Object toHeap()
          Moves this object to the heap.
 java.lang.String toString()
          Returns a String representation of this set.
 
Methods inherited from class com.dautelle.realtime.RealtimeObject
isLocalObject, isPoolObject, recycle
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FastSet

public FastSet()
Creates a FastSet with a default capacity.


FastSet

public FastSet(java.util.Set set)
Creates a FastSet, copy of the specified Set. The newly created set has a capacity set to the specified set's size. The copy has the same order as the original, regardless of the original set's implementation.

Parameters:
set - the set whose elements are to be placed in this set.

FastSet

public FastSet(int capacity)
Creates a FastSet with the specified capacity. Unless the capacity is exceeded, operations on this set do not allocate memory. For optimum performance, the capacity should be of the same order of magnitude or larger than the expected set's size.

Parameters:
capacity - the initial capacity of the backing map.
Method Detail

newInstance

public static FastSet newInstance(int capacity)
Returns a FastSet allocated from the current stack (or the heap if not in a pool context).

Parameters:
capacity - the minimum capacity for the set to return.
Returns:
a new or recycled set instance.

size

public int size()
Returns the number of elements in this set (its cardinality).

Specified by:
size in interface java.util.Set
Returns:
the number of elements in this set (its cardinality).

isEmpty

public boolean isEmpty()
Returns true if this set contains no elements.

Specified by:
isEmpty in interface java.util.Set
Returns:
true if this set contains no elements.

contains

public boolean contains(java.lang.Object o)
Returns true if this set contains the specified element.

Specified by:
contains in interface java.util.Set
Parameters:
o - element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.

iterator

public java.util.Iterator iterator()
Returns an iterator over the elements in this set (allocated from the "stack" when executing in a PoolContext). The elements are returned in is the order in which they were inserted into the map.

Specified by:
iterator in interface java.util.Set
Returns:
an iterator over the elements in this set.

add

public boolean add(java.lang.Object o)
Adds the specified element to this set if it is not already present.

Specified by:
add in interface java.util.Set
Parameters:
o - element to be added to this set.
Returns:
true if this set did not already contain the specified element.

remove

public boolean remove(java.lang.Object o)
Removes the specified element from this set if it is present.

Specified by:
remove in interface java.util.Set
Parameters:
o - object to be removed from this set, if present.
Returns:
true if the set contained the specified element.

clear

public void clear()
Removes all of the elements from this set.

Specified by:
clear in interface java.util.Set

clone

public java.lang.Object clone()
Returns a shallow copy of this FastSet. The keys and the values themselves are not cloned but moved to the heap as well.

Overrides:
clone in class RealtimeObject
Returns:
a shallow copy of this set.

equals

public boolean equals(java.lang.Object obj)
Compares the specified object with this FastSet for equality. Returns true if the given object is also a set and the two set represent the collection (regardless of collection iteration order).

Specified by:
equals in interface java.util.Set
Parameters:
obj - the object to be compared for equality with this set.
Returns:
true if the specified object is equal to this set; false otherwise.

hashCode

public int hashCode()
Returns the hash code value for this FastSet.

Specified by:
hashCode in interface java.util.Set
Returns:
the hash code value for this set.

addAll

public boolean addAll(java.util.Collection c)
Adds all of the elements in the specified collection to this set if they're not already present.

Specified by:
addAll in interface java.util.Set
Parameters:
c - collection whose elements are to be added to this set.
Returns:
true if this set changed as a result of the call.

containsAll

public boolean containsAll(java.util.Collection c)
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.

Specified by:
containsAll in interface java.util.Set
Parameters:
c - collection to be checked for containment in this set.
Returns:
true if this set contains all of the elements of the specified collection.

removeAll

public boolean removeAll(java.util.Collection c)
Removes from this set all of its elements that are 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 asymmetric set difference of the two sets.

Specified by:
removeAll in interface java.util.Set
Parameters:
c - collection that defines which elements will be removed from this set.
Returns:
true if this set changed as a result of the call.

retainAll

public boolean retainAll(java.util.Collection c)
Retains only the elements in this set that are contained in the specified collection. 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.

Specified by:
retainAll in interface java.util.Set
Parameters:
c - collection that defines which elements this set will retain.
Returns:
true if this collection changed as a result of the call.

toArray

public java.lang.Object[] toArray()
Returns an array containing all of the elements in this set.

Specified by:
toArray in interface java.util.Set
Returns:
an array containing all of the elements in this set.

toArray

public java.lang.Object[] toArray(java.lang.Object[] a)
Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

Specified by:
toArray in interface java.util.Set
Parameters:
a - the array into which the elements of this set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of this set.

toString

public java.lang.String toString()
Returns a String representation of this set.

Returns:
the backing map key set representation.

export

public java.lang.Object export()
Description copied from interface: Realtime
Exports this object out of the current pool context. Realtime members are exported as well (recursion). This method affects only objects belonging to the current pool context. To avoid pool depletion, a "free" object from the outer pool is moved to replace the object being exported.

Specified by:
export in interface Realtime
Overrides:
export in class RealtimeObject

toHeap

public java.lang.Object toHeap()
Description copied from interface: Realtime
Moves this object to the heap. Realtime members are moved to the heap as well (recursion). This method affects only objects belonging to a pool context (current or not).

Specified by:
toHeap in interface Realtime
Overrides:
toHeap in class RealtimeObject

JADE v6.1

Copyright © 2004 Jean-Marie Dautelle.