org.eclipse.osgi.framework.util
Class KeyedHashSet

java.lang.Object
  extended by org.eclipse.osgi.framework.util.KeyedHashSet

public class KeyedHashSet
extends java.lang.Object

A set data structure which only accepts KeyedElement objects as elements of the set. Unlike typical set implementations this set requires each element to provide its own key. This helps reduce the overhead of storing the keys of each individual element

This class in not thread safe, clients must ensure synchronization when modifying an object of this type.

Since:
3.2

Field Summary
static int MINIMUM_SIZE
           
 
Constructor Summary
KeyedHashSet()
          Constructs an KeyedHashSet which allows elements to be replaced and with the minimum initial capacity.
KeyedHashSet(boolean replace)
          Constructs an KeyedHashSet with the minimum initial capacity.
KeyedHashSet(int capacity)
          Constructs an KeyedHashSet which allows elements to be replaced.
KeyedHashSet(int capacity, boolean replace)
          Constructs an KeyedHashSet
KeyedHashSet(KeyedHashSet original)
          Constructs a new KeyedHashSet and copies to specified KeyedHashSet's contents to the new KeyedHashSet.
 
Method Summary
 boolean add(KeyedElement element)
          Adds an element to this set.
 void addAll(KeyedElement[] toAdd)
          Adds the specified list of elements to this set.
 void clear()
          Clears all elements from this set
 boolean contains(KeyedElement element)
          Returns true if the specified element exists in this set.
 boolean containsKey(java.lang.Object key)
          Returns true if an element with the specified key exists in this set.
 int countCollisions()
          Returns the number of collisions this set currently has
 KeyedElement[] elements()
          Returns all elements that exist in this set
 java.lang.Object[] elements(java.lang.Object[] result)
          Copies all elements that exist in this set into the specified array.
protected  void expand()
          The array isn't large enough so double its size and rehash all its current values.
 KeyedElement get(KeyedElement otherElement)
          Returns the element which compares to the specified element, or null if not found.
 KeyedElement getByKey(java.lang.Object key)
          Returns the element with the specified key, or null if not found.
 boolean isEmpty()
          Returns true if this set is empty
 java.util.Iterator iterator()
          Returns an iterator of elements in this set
protected  void rehashTo(int anIndex)
          The element at the given index has been removed so move elements to keep the set properly hashed.
 boolean remove(KeyedElement toRemove)
          Removes the element which compares to the specified element
 void removeAll(KeyedElement[] toRemove)
          Removes all of the specified elements from this set
 boolean removeByKey(java.lang.Object key)
          Removes the element with the specified key
 int size()
          Returns the number of elements in this set
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MINIMUM_SIZE

public static final int MINIMUM_SIZE
See Also:
Constant Field Values
Constructor Detail

KeyedHashSet

public KeyedHashSet()
Constructs an KeyedHashSet which allows elements to be replaced and with the minimum initial capacity.


KeyedHashSet

public KeyedHashSet(boolean replace)
Constructs an KeyedHashSet with the minimum initial capacity.

Parameters:
replace - true if this set allows elements to be replaced

KeyedHashSet

public KeyedHashSet(int capacity)
Constructs an KeyedHashSet which allows elements to be replaced.

Parameters:
capacity - the initial capacity of this set

KeyedHashSet

public KeyedHashSet(int capacity,
                    boolean replace)
Constructs an KeyedHashSet

Parameters:
capacity - the initial capacity of this set
replace - true if this set allows elements to be replaced

KeyedHashSet

public KeyedHashSet(KeyedHashSet original)
Constructs a new KeyedHashSet and copies to specified KeyedHashSet's contents to the new KeyedHashSet.

Parameters:
original - the KeyedHashSet to copy
Method Detail

add

public boolean add(KeyedElement element)
Adds an element to this set. If an element with the same key already exists, replaces it depending on the replace flag.

Returns:
true if the element was added/stored, false otherwise

addAll

public void addAll(KeyedElement[] toAdd)
Adds the specified list of elements to this set. Some elements may not get added if the replace flag is set.

Parameters:
toAdd - the list of elements to add to this set.

contains

public boolean contains(KeyedElement element)
Returns true if the specified element exists in this set.

Parameters:
element - the requested element
Returns:
true if the specified element exists in this set; false otherwise.

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if an element with the specified key exists in this set.

Parameters:
key - the key of the requested element
Returns:
true if an element with the specified key exists in this set; false otherwise

elements

public KeyedElement[] elements()
Returns all elements that exist in this set

Returns:
all elements that exist in this set

elements

public java.lang.Object[] elements(java.lang.Object[] result)
Copies all elements that exist in this set into the specified array. No size checking is done. If the specified array is to small an ArrayIndexOutOfBoundsException will be thrown.

Parameters:
result - the array to copy the existing elements into.
Returns:
the specified array.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the specified array is to small.

expand

protected void expand()
The array isn't large enough so double its size and rehash all its current values.


getByKey

public KeyedElement getByKey(java.lang.Object key)
Returns the element with the specified key, or null if not found.

Parameters:
key - the requested element's key
Returns:
the element with the specified key, or null if not found.

get

public KeyedElement get(KeyedElement otherElement)
Returns the element which compares to the specified element, or null if not found.

Parameters:
otherElement - the requested element
Returns:
the element which compares to the specified element, or null if not found.
See Also:
KeyedElement.compare(KeyedElement)

isEmpty

public boolean isEmpty()
Returns true if this set is empty

Returns:
true if this set is empty

rehashTo

protected void rehashTo(int anIndex)
The element at the given index has been removed so move elements to keep the set properly hashed.

Parameters:
anIndex - the index that has been removed

removeByKey

public boolean removeByKey(java.lang.Object key)
Removes the element with the specified key

Parameters:
key - the requested element's key
Returns:
true if an element was removed

remove

public boolean remove(KeyedElement toRemove)
Removes the element which compares to the specified element

Parameters:
toRemove - the requested element to remove
Returns:
true if an element was removed

removeAll

public void removeAll(KeyedElement[] toRemove)
Removes all of the specified elements from this set

Parameters:
toRemove - the requested elements to remove

size

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

Returns:
the number of elements in this set

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

countCollisions

public int countCollisions()
Returns the number of collisions this set currently has

Returns:
the number of collisions this set currently has

iterator

public java.util.Iterator iterator()
Returns an iterator of elements in this set

Returns:
an iterator of elements in this set

clear

public void clear()
Clears all elements from this set