java.lang.Object
ru.vyarus.dropwizard.guice.module.context.info.ItemId<T>
Type Parameters:
T - class type

public final class ItemId<T> extends 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 Details

    • getType

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

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

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

      public static String identity(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(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(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> List<Class<T>> typesOnly(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(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object