Class TypeHolder<T>

  • Type Parameters:
    T - entity type
    Direct Known Subclasses:
    TypeComparators, TypeMessages

    abstract class TypeHolder<T>
    extends java.lang.Object
    An abstract type holder which provides to pair a specific entities for types.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Comparator<java.lang.Class<?>> DEFAULT_CLASS_COMPARATOR  
      protected java.util.Map<java.lang.Class<?>,​T> typeHolder  
    • Constructor Summary

      Constructors 
      Constructor Description
      TypeHolder()  
      TypeHolder​(java.util.Comparator<java.lang.Class<?>> comparator)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all registered entities.
      java.util.stream.Stream<java.util.Map.Entry<java.lang.Class<?>,​T>> entityByTypes()
      Returns a sequence of all type-entity pairs which the current holder supplies.
      boolean equals​(java.lang.Object o)  
      private static <T> java.lang.String formatRegisteredEntity​(java.util.Map.Entry<java.lang.Class<?>,​T> entry)  
      T get​(java.lang.Class<?> clazz)
      This method returns the most relevant entity for the given class.
      private java.lang.Class<?> getRelevantClass​(java.lang.Class<?> cls)
      Returns the most relevant class for the given type from the giving collection of types.
      boolean hasEntity​(java.lang.Class<?> type)
      Checks, whether an entity is associated with the giving type.
      int hashCode()  
      boolean isEmpty()  
      void put​(java.lang.Class<?> clazz, T entity)
      Puts the entity for the given clazz.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DEFAULT_CLASS_COMPARATOR

        private static final java.util.Comparator<java.lang.Class<?>> DEFAULT_CLASS_COMPARATOR
      • typeHolder

        protected final java.util.Map<java.lang.Class<?>,​T> typeHolder
    • Constructor Detail

      • TypeHolder

        public TypeHolder()
      • TypeHolder

        public TypeHolder​(java.util.Comparator<java.lang.Class<?>> comparator)
    • Method Detail

      • get

        public T get​(java.lang.Class<?> clazz)
        This method returns the most relevant entity for the given class. The most relevant entity is the entity which is registered for the class that is closest in the inheritance chain of the given clazz. The order of checks is the following: 1. If there is a registered entity for clazz then this one is used 2. We check if there is a registered entity for a superclass of clazz 3. We check if there is a registered entity for an interface of clazz
        Parameters:
        clazz - the class for which to find a entity
        Returns:
        the most relevant entity, or null if on entity could be found
      • put

        public void put​(java.lang.Class<?> clazz,
                        T entity)
        Puts the entity for the given clazz.
        Parameters:
        clazz - the class for the comparator
        entity - the entity itself
      • hasEntity

        public boolean hasEntity​(java.lang.Class<?> type)
        Checks, whether an entity is associated with the giving type.
        Parameters:
        type - the type for which to check an entity
        Returns:
        is the giving type associated with any entity
      • isEmpty

        public boolean isEmpty()
        Returns:
        true is there are registered entities, false otherwise
      • clear

        public void clear()
        Removes all registered entities.
      • entityByTypes

        public java.util.stream.Stream<java.util.Map.Entry<java.lang.Class<?>,​T>> entityByTypes()
        Returns a sequence of all type-entity pairs which the current holder supplies.
        Returns:
        sequence of field-entity pairs
      • getRelevantClass

        private java.lang.Class<?> getRelevantClass​(java.lang.Class<?> cls)
        Returns the most relevant class for the given type from the giving collection of types.

        The order of checks is the following:

        1. If there is a registered message for clazz then this one is used
        2. We check if there is a registered message for a superclass of clazz
        3. We check if there is a registered message for an interface of clazz
        If there is no relevant type in the giving collection - null will be returned.
        Parameters:
        cls - type to find a relevant class.
        Returns:
        the most relevant class.
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • formatRegisteredEntity

        private static <T> java.lang.String formatRegisteredEntity​(java.util.Map.Entry<java.lang.Class<?>,​T> entry)