Package net.shibboleth.shared.collection

# Class ClassToInstanceMultiMap<B>

java.lang.Object
net.shibboleth.shared.collection.ClassToInstanceMultiMap<B>

Type Parameters:
  `B` - a bound for the types of values in the map

Direct Known Subclasses:
  `LockableClassToInstanceMultiMap`

@NotThreadSafe public class ClassToInstanceMultiMap<B> extends Object
An append-only multimap where each entry associates a raw type (i.e. class) to instances of that class. In addition the map may also associate any supertype (i.e. all superclasses and interfaces implemented by the class) with the class. Null values are not supported.

## Field Summary

Fields
Modifier and Type
Field
Description
`private final HashMap<Class<?>,List<B>>`
`backingMap`
Map which backs this map.
`private final boolean`
`indexSupertypes`
Whether supertypes should also be indexed.
`private final List<B>`
`values`
List of values that are indexed.

## Constructor Summary

Constructors
Constructor
Description
`ClassToInstanceMultiMap()`
Constructor.
`ClassToInstanceMultiMap(boolean isIndexingSupertypes)`
Constructor.

## Method Summary

Modifier and Type
Method
Description
`void`
`clear()`
Removes all mappings from this map.
`boolean`
`containsKey(Class<?> key)`
Returns true if the map contains a mapping for the given key.
`boolean`
`containsValue(B value)`
Returns true iff the map contains a mapping to the given value, false if value is null.
`boolean`
`equals(Object obj)`
`<T> List<T>`
`get(Class<T> type)`
Gets the instances mapped to the given type or an empty list.
`private Set<Class<?>>`
`getIndexTypes(B value)`
Get the effective set of all class types via which the specified value should be indexed.
`private void`
`getSuperTypes(Class<?> clazz, Set<Class<?>> accumulator)`
Gets all of the superclasses and interfaces implemented by the given class.
`int`
`hashCode()`
`boolean`
`isEmpty()`
Returns true if this map contains no entries, false otherwise.
`Set<Class<?>>`
`keys()`
Gets the set of keys contained in this map.
`void`
`put(B value)`
Adds a value to this map.
`void`
`putAll(Iterable<? extends B> newValues)`
Adds all the values to the map.
`void`
`putAll(ClassToInstanceMultiMap<? extends B> map)`
Adds all the values to the map.
`void`
`remove(B value)`
Remove the specified value from the map and from the value list of all indexes.
`void`
`remove(Class<?> type)`
Remove from the map all values which have the specified type.
`void`
`removeAll(Iterable<? extends B> removeValues)`
Remove the specified values from the map and from the value list of all indexes.
`void`
`removeAll(ClassToInstanceMultiMap<? extends B> map)`
Remove the values contained in the specified map from this map and from the value list of all indexes.
`Collection<? extends B>`
`values()`
The collection of values currently present in the map.

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### indexSupertypes

private final boolean indexSupertypes
Whether supertypes should also be indexed.

### backingMap

@Nonnull private final HashMap<Class<?>,List<B>> backingMap
Map which backs this map.

### values

@Nonnull private final List<B> values
List of values that are indexed.

## Constructor Details

### ClassToInstanceMultiMap

public ClassToInstanceMultiMap()
Constructor. Does not index supertypes.

### ClassToInstanceMultiMap

public ClassToInstanceMultiMap(boolean isIndexingSupertypes)
Constructor.

Parameters:
  `isIndexingSupertypes` - indicates whether supertypes of a value should be indexed

## Method Details

### clear

public void clear()
Removes all mappings from this map.

### containsKey

public boolean containsKey(@Nullable Class<?> key)
Returns true if the map contains a mapping for the given key.

Parameters:
  `key` - key to check for in the map
Returns:
  true if the map contains a mapping for the specified key

### containsValue

public boolean containsValue(@Nullable B value)
Returns true iff the map contains a mapping to the given value, false if value is null.

Parameters:
  `value` - value to check for in this map
Returns:
  true iff the map contains a mapping to the specified value

### get

@Nonnull @Unmodifiable @Live public <T> List<T> get(@Nullable Class<T> type)
Gets the instances mapped to the given type or an empty list.

Type Parameters:
  `T` - type identifier
Parameters:
  `type` - map key
Returns:
  instances mapped to the given type or an empty list

### isEmpty

public boolean isEmpty()
Returns true if this map contains no entries, false otherwise.

Returns:
  true if this map contains no entries, false otherwise

### keys

@Nonnull @Unmodifiable @Live public Set<Class<?>> keys()
Gets the set of keys contained in this map. The set is backed by the map so changes made to the map are reflected in the set. However the set does not allow direct modification, any changes need to be done through this map.

Returns:
  set of keys contained in this map

### put

public void put(@Nonnull B value)
Adds a value to this map. If `indexSupertypes` is false only the values class type is used as a key to the value. If `indexSupertypes` is true, then the class types, all its supertypes, and all implemented interfaces are used as keys to the value. Duplicate values, as determined by the values `Object.hashCode()` and `Object.equals(Object)` methods, are not stored. Only one instance of the value is ever stored in the map.

Parameters:
  `value` - value to be stored in the map

### putAll

public void putAll(@Nullable Iterable<? extends B> newValues)
Adds all the values to the map.

Parameters:
  `newValues` - values to be added
See Also:
  - `put(Object)`

### putAll

public void putAll(@Nullable ClassToInstanceMultiMap<? extends B> map)
Adds all the values to the map. This operations operates only on the given map's value collection. Therefore, regardless of the given map's policy on indexing by value supertypes, this map will index values based on its policy.

Parameters:
  `map` - map containing values to be added
See Also:
  - `put(Object)`

### remove

public void remove(@Nullable B value)
Remove the specified value from the map and from the value list of all indexes.

If the value list for a type index becomes empty due to the value removal, the entire type index will be removed and `containsKey(Class)` for that type will then return `false`.

Parameters:
  `value` - the value to remove

### removeAll

public void removeAll(@Nullable Iterable<? extends B> removeValues)
Remove the specified values from the map and from the value list of all indexes.

If the value list for a type index becomes empty due to a value removal, the entire type index will be removed and `containsKey(Class)` for that type will then return `false`.

Parameters:
  `removeValues` - the values to remove

### removeAll

public void removeAll(@Nullable ClassToInstanceMultiMap<? extends B> map)
Remove the values contained in the specified map from this map and from the value list of all indexes.

If the value list for a type index becomes empty due to a value removal, the entire type index will be removed and `containsKey(Class)` for that type will then return `false`.

Parameters:
  `map` - the map containing the values to remove

### remove

public void remove(@Nullable Class<?> type)
Remove from the map all values which have the specified type.

Note that when a value was indexed by multiple superclass and/or interface types, it will be removed from all those type indexes, not just the specified one.

If the value list for a type index becomes empty due to a value removal, the entire type index will be removed and `containsKey(Class)` for that type will then return `false`.

Parameters:
  `type` - the type of values to remove

### values

@Nonnull @Unmodifiable @Live public Collection<? extends B> values()
The collection of values currently present in the map. This collection is backed by the map so changes to the map will be reflected in the collection. However the collection does not allow direct modification so any changes must be done through this map.

Returns:
  collection of values currently present in the map

### getIndexTypes

@Nonnull private Set<Class<?>> getIndexTypes(@Nonnull B value)
Get the effective set of all class types via which the specified value should be indexed.

Parameters:
  `value` - the value to index
Returns:
  the set of classes by which to index the value

### getSuperTypes

private void getSuperTypes(@Nonnull Class<?> clazz, @Nonnull @Live Set<Class<?>> accumulator)
Gets all of the superclasses and interfaces implemented by the given class.

Parameters:
  `clazz` - class for which supertypes will be determined
  `accumulator` - collection to which supertypes are added as they are determined

### hashCode

public int hashCode()

Overrides:
  `hashCode` in class `Object`

### equals

public boolean equals(Object obj)

Overrides:
  `equals` in class `Object`