org.opensaml.xml.util
Class ClassIndexedSet<T>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<T>
          extended by org.opensaml.xml.util.ClassIndexedSet<T>
Type Parameters:
T - the type of object stored by this class
All Implemented Interfaces:
Iterable<T>, Collection<T>, Set<T>
Direct Known Subclasses:
CredentialContextSet, CriteriaSet

public class ClassIndexedSet<T>
extends AbstractSet<T>
implements Set<T>

Set implementation which provides indexed access to set members via their class, and which allows only one instance of a given class to be present in the set. Null members are not allowed.


Nested Class Summary
protected  class ClassIndexedSet.ClassIndexedSetIterator
          Iterator for set implementation ClassIndexedSet.
 
Field Summary
private  HashMap<Class<? extends T>,T> index
          Storage for index of class -> member.
private  HashSet<T> set
          Storage for set members.
 
Constructor Summary
ClassIndexedSet()
          Constructor.
 
Method Summary
 boolean add(T o)
          
 boolean add(T o, boolean replace)
          Add member to set, optionally replacing any existing instance of the same class.
 void clear()
          
 boolean contains(Class<? extends T> clazz)
          Check whether set contains an instance of the specified class.
<X extends T>
X
get(Class<X> clazz)
          Get the set element specified by the class parameter.
protected  Class<? extends T> getIndexClass(Object o)
          Get the index class of the specified object.
 Iterator<T> iterator()
          
 boolean remove(Object o)
          
private  void removeFromIndex(T o)
          Remove the specified object from the index.
 int size()
          
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, contains, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
 

Field Detail

set

private HashSet<T> set
Storage for set members.


index

private HashMap<Class<? extends T>,T> index
Storage for index of class -> member.

Constructor Detail

ClassIndexedSet

public ClassIndexedSet()
Constructor.

Method Detail

add

public boolean add(T o)

Specified by:
add in interface Collection<T>
Specified by:
add in interface Set<T>
Overrides:
add in class AbstractCollection<T>

add

public boolean add(T o,
                   boolean replace)
            throws NullPointerException,
                   IllegalArgumentException
Add member to set, optionally replacing any existing instance of the same class.

Parameters:
o - the object to add
replace - flag indicating whether to replace an existing class type
Returns:
true if object was added
Throws:
IllegalArgumentException - if set already contained an instance of the object's class and replacement was specified as false
NullPointerException - if the object to add was null

clear

public void clear()

Specified by:
clear in interface Collection<T>
Specified by:
clear in interface Set<T>
Overrides:
clear in class AbstractCollection<T>

remove

public boolean remove(Object o)

Specified by:
remove in interface Collection<T>
Specified by:
remove in interface Set<T>
Overrides:
remove in class AbstractCollection<T>

iterator

public Iterator<T> iterator()

Specified by:
iterator in interface Iterable<T>
Specified by:
iterator in interface Collection<T>
Specified by:
iterator in interface Set<T>
Specified by:
iterator in class AbstractCollection<T>

size

public int size()

Specified by:
size in interface Collection<T>
Specified by:
size in interface Set<T>
Specified by:
size in class AbstractCollection<T>

contains

public boolean contains(Class<? extends T> clazz)
Check whether set contains an instance of the specified class.

Parameters:
clazz - the class to check
Returns:
true if set contains an instance of the specified class, false otherwise

get

public <X extends T> X get(Class<X> clazz)
Get the set element specified by the class parameter.

Type Parameters:
X - generic parameter which eliminates need for casting by the caller
Parameters:
clazz - the class to whose instance is to be retrieved
Returns:
the element whose class is of the type specified, or null

getIndexClass

protected Class<? extends T> getIndexClass(Object o)
Get the index class of the specified object. Subclasses may override to use a class index other than the main runtime class of the object.

Parameters:
o - the object whose class index to determine
Returns:
the class index value associated with the object instance

removeFromIndex

private void removeFromIndex(T o)
Remove the specified object from the index.

Parameters:
o - the object to remove


Copyright © 1999-2012. All Rights Reserved.