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 Detail

      • ClassToInstanceStore

        public ClassToInstanceStore()
    • 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 TOptional<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 TOptional<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 TOptional<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 TOptional<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
        Parameters:
        instance - instance to map to its class (obtained through Object.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
      • toString

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