Class ClassInstances<T>

java.lang.Object
overit.geocall.platform.ClassInstances<T>
Type Parameters:
T - type of registered objects

public class ClassInstances<T> extends Object
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 Details

  • Constructor Details

    • ClassInstances

      public ClassInstances()
  • Method Details

    • register

      public void register(String name, T instance)
      Register a new instance by associating it with a certain name.
      Parameters:
      name - name that uniquely identifies the instance
      instance - instance of the object to be registered
      Throws:
      IllegalArgumentException - if there is already an instance registered under the same name
    • register

      protected void register(String name, T instance, Function<T,Class<?>> classExtractor)
    • unregisterAll

      protected void unregisterAll()
    • get

      public T get(String name)
      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

      public T get(Class<?> cls)
      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

      public Collection<T> values()
      Returns:
      all registered instances