Package overit.geocall.platform
Class ClassInstances<T>
java.lang.Object
overit.geocall.platform.ClassInstances<T>
- Type Parameters:
T- type of registered objects
The purpose of this class is to allow a set of objects to be recorded, and then to be able to
recover by a name, that uniquely identifies the instance, or by the java class to which it belongs.
Duplicate names are not allowed. However, it is possible to register instances of the same class, but with different names.
In this case, be careful when invoking the
get(Class) method because, since there are several instances
registered with the same class, it will not be possible to distinguish them by class, and therefore an exception will be raised.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ConcurrentHashMap<Class<?>, Optional<T>> protected final ConcurrentHashMap<String, T> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the class instance from the class object under which it was registered.Returns the class instance from the name under which it was registered.voidRegister a new instance by associating it with a certain name.protected voidprotected voidvalues()
-
Field Details
-
classes
-
names
-
-
Constructor Details
-
ClassInstances
public ClassInstances()
-
-
Method Details
-
register
Register a new instance by associating it with a certain name.- Parameters:
name- name that uniquely identifies the instanceinstance- instance of the object to be registered- Throws:
IllegalArgumentException- if there is already an instance registered under the same name
-
register
-
unregisterAll
protected void unregisterAll() -
get
Returns the class instance from the name under which it was registered. If there is no instance registered to that name, a null value is returned- Parameters:
name- the instance's name- Returns:
- the class instance or null if there's no instances registered with the passed name.
-
get
Returns the class instance from the class object under which it was registered. If there is no instance registered to that class, a null value is returned.- Parameters:
cls- the instance's class- Returns:
- the class instance or null if there's no instances registered with the passed class.
- Throws:
IllegalArgumentException- if several instances of the same class have been registered
-
values
- Returns:
- all registered instances
-