- java.lang.Object
-
- io.helidon.security.ClassToInstanceStore<T>
-
- Type Parameters:
T- Type all classes in this instance must extend
public final class ClassToInstanceStore<T> extends Object
Map of classes to their instances. This class is mutable and not thread safe.
-
-
Constructor Summary
Constructors Constructor Description ClassToInstanceStore()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontainsKey(Class<? extends T> clazz)Check if this store contains a mapping for a class.static <T> ClassToInstanceStore<T>create(T... instances)Create a new instance based on explicit instances.<U extends T>
Optional<U>getInstance(Class<U> clazz)Get an instance from the store.booleanisEmpty()Check if this store is empty.Collection<Class<? extends T>>keys()Get all keys of this store.voidputAll(ClassToInstanceStore<? extends T> toCopy)Put all mappings from another instance.<U extends T>
Optional<U>putInstance(Class<? extends U> clazz, U instance)Put an explicit class to instance mapping.<U extends T>
Optional<U>putInstance(U instance)Put an instance with implicit class mapping into this store.<U extends T>
Optional<U>removeInstance(Class<U> clazz)Remove an instance from the store and return current value.StringtoString()Returns the toString on underlying map.Collection<T>values()Get all values of this store.
-
-
-
Method Detail
-
create
@SafeVarargs public static <T> ClassToInstanceStore<T> create(T... instances)
Create a new instance based on explicit instances. This method creates a MUTABLE instance (contrary to such methods on java collections).- Type Parameters:
T- type of the store- Parameters:
instances- instances to add to the new store- Returns:
- new store with instances inserted as when calling
putInstance(Object)for each of them
-
getInstance
public <U extends T> Optional<U> getInstance(Class<U> clazz)
Get an instance from the store.- Type Parameters:
U- Type we are expecting- Parameters:
clazz- Class under which we have created the mapping (String and CharSequence are two distinct mappings!)- Returns:
- Instance of the class or null if no mapping exists
-
putInstance
public <U extends T> Optional<U> putInstance(Class<? extends U> clazz, U instance)
Put an explicit class to instance mapping.- Type Parameters:
U- Type we are expecting- Parameters:
clazz- Class for which to create a mapping (String and CharSequence create two distinct mappings!)instance- Instance of the interface/class- Returns:
- Instance of the class if a mapping previously existed or null for no existing mapping
-
removeInstance
public <U extends T> Optional<U> removeInstance(Class<U> clazz)
Remove an instance from the store and return current value.- Type Parameters:
U- Type of the object- Parameters:
clazz- Class of object to remove- Returns:
- instance that was removed (if there was one)
-
putAll
public void putAll(ClassToInstanceStore<? extends T> toCopy)
Put all mappings from another instance.- Parameters:
toCopy- store to copy into this store
-
containsKey
public boolean containsKey(Class<? extends T> clazz)
Check if this store contains a mapping for a class.- Parameters:
clazz- class to check- Returns:
- true if there is a mapping for the class
-
isEmpty
public boolean isEmpty()
Check if this store is empty.- Returns:
- true if there are no mappings in this store
-
putInstance
public <U extends T> Optional<U> putInstance(U instance)
Put an instance with implicit class mapping into this store. Please useputInstance(Class, Object)when feasible as it provides more explicit mapping. This is a helper method for instances obtained by other means or where we are sure of the class of the instance.- Type Parameters:
U- Type of this instance- Parameters:
instance- instance to map to its class (obtained throughObject.getClass()- Returns:
- existing value for the class of this instance
-
values
public Collection<T> values()
Get all values of this store.- Returns:
- collection of values
-
keys
public Collection<Class<? extends T>> keys()
Get all keys of this store.- Returns:
- collection of classes used for mapping to instances
-
-