Class ItemId<T>

  • Type Parameters:
    T - class type

    public final class ItemId<T>
    extends java.lang.Object
    As multiple instances of the same type may be used during configuration (e.g. bundle or module) then it's not enough to declare just class in order to identify type.

    Identity string is generated using object hash code (the same way as used in default Object.toString(). It is assumed that no one would override bundle hash code (who would be?) and toString so using the same hash as in default toString would be ideal for instance identification (at least instance could be easily associated in debugger).

    Type has special equals behaviour: id, created from class only is equal to any instance identity (containing instance identifier). This simplifies searches by class only. In order to make this work, hash code for itemId is the same for all ids of the same type.

    Reminder:

    • Identity object could always be prepared from instance: ItemId.from(instance)
    • Class item could be used to detect instanes: e.g. listOfIds.contains(ItemId.from(MyModule.class)) will be true if instance id is contained in collection.
    Since:
    05.07.2019
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)  
      static <T> ItemId<T> from​(java.lang.Class<?> type)
      Construct type key for class only.
      static <T> ItemId<T> from​(java.lang.Object instance)
      Construct type key for instance of class.
      java.lang.String getIdentity()  
      java.lang.Class<T> getType()  
      int hashCode()  
      static java.lang.String identity​(java.lang.Object instance)
      Creates object instance identity string.
      java.lang.String toString()  
      static <T> java.util.List<java.lang.Class<T>> typesOnly​(java.util.Collection<ItemId<T>> items)
      Utility method to project collections of item ids into classes.
      • Methods inherited from class java.lang.Object

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

      • getType

        public java.lang.Class<T> getType()
        Returns:
        type
      • getIdentity

        public java.lang.String getIdentity()
        Returns:
        object instance hash or null
      • toString

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

        public static java.lang.String identity​(java.lang.Object instance)
        Creates object instance identity string. Returned code is the same as number part after "@" in default Object.toString().
        Parameters:
        instance - object instance
        Returns:
        identity string (hash)
      • from

        public static <T> ItemId<T> from​(java.lang.Object instance)
        Construct type key for instance of class. When called with class instance it's equivalent to from(Class) call.
        Type Parameters:
        T - class type
        Parameters:
        instance - instance of object representing type (bundle, modules) or class
        Returns:
        type key
      • from

        public static <T> ItemId<T> from​(java.lang.Class<?> type)
        Construct type key for class only. Useful when only one instance of type class is used.
        Type Parameters:
        T - class type
        Parameters:
        type - type class
        Returns:
        type key
      • typesOnly

        public static <T> java.util.List<java.lang.Class<T>> typesOnly​(java.util.Collection<ItemId<T>> items)
        Utility method to project collections of item ids into classes. If list contains multiple instance ids of the same type then resulted list will contain only one type (and so resulted list will be shorter).
        Type Parameters:
        T - target items class type (when possible to declare)
        Parameters:
        items - item ids
        Returns:
        list of classes from provided ids
      • 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