Class 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 Details

    • ClassToInstanceStore

      public ClassToInstanceStore()
  • Method Details

    • 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
      instances - instances to add to the new store
      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
      clazz - Class under which we have created the mapping (String and CharSequence are two distinct mappings!)
      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
      clazz - Class for which to create a mapping (String and CharSequence create two distinct mappings!)
      instance - Instance of the interface/class
      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
      clazz - Class of object to remove
      instance that was removed (if there was one)
    • putAll

      public void putAll(ClassToInstanceStore<? extends T> toCopy)
      Put all mappings from another instance.
      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.
      clazz - class to check
      true if there is a mapping for the class
    • isEmpty

      public boolean isEmpty()
      Check if this store is empty.
      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 use putInstance(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
      instance - instance to map to its class (obtained through Object.getClass()
      existing value for the class of this instance
    • values

      public Collection<T> values()
      Get all values of this store.
      collection of values
    • keys

      public Collection<Class<? extends T>> keys()
      Get all keys of this store.
      collection of classes used for mapping to instances
    • toString

      public String toString()
      Returns the toString on underlying map.
      toString in class Object
      String representation of the mapping