Class IdentifiableUtils

java.lang.Object
ubic.gemma.persistence.util.IdentifiableUtils

public class IdentifiableUtils extends Object
Utilities for Identifiable.
Author:
poirigui
  • Constructor Details

    • IdentifiableUtils

      public IdentifiableUtils()
  • Method Details

    • getIds

      public static <T extends Identifiable> List<Long> getIds(Collection<T> entities)
      Convert a collection of identifiable to their IDs.
      Parameters:
      entities - entities
      Returns:
      returns a collection of IDs. Avoids using reflection by requiring that the given entities all implement the Identifiable interface.
    • getIdMap

      public static <T extends Identifiable> Map<Long,T> getIdMap(Collection<T> entities)
      Given a set of entities, create a map of their ids to the entities.

      Note: If more than one entity share the same ID, there is no guarantee on which will be kept in the final mapping. If the collection is ordered, the first encountered entity will be kept.

      Type Parameters:
      T - the type
      Parameters:
      entities - where id is called "id"
      Returns:
      the created map
    • toIdentifiableSet

      public static <T extends Identifiable> Collector<T,?,Set<T>> toIdentifiableSet()
      Collect results into an identifiable set.

      This uses Identifiable.getId() for comparing elements, making the collection safe for holding proxies unlike a HashSet that relies on Object.hashCode().

      See Also:
    • toIdentifiableMap

      public static <T, K extends Identifiable, U> Collector<T,?,Map<K,U>> toIdentifiableMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
      Collect results into an identifiable map.

      This uses Identifiable.getId() for comparing elements, making the collection safe for holding proxies unlike a HashMap that relies on Object.hashCode().

      See Also:
    • toString

      public static <T extends Identifiable> String toString(T identifiable, Class<T> clazz)
      Converts an identifiable to string, avoiding its initialization of it is a proxy.
    • hash

      public static int hash(Identifiable... identifiables)
      Hash identifiables in a proxy-safe way using their IDs.

      Hashing an entity that does ont have an assigned ID is not allowed as its hash code would change once persisted.

    • equals

      public static <T extends Identifiable> boolean equals(@Nullable T a, @Nullable T b)
      Compare two identifiables of the same type without risking initializing them.
      Returns:
      true if they have the same ID or are equal according to Objects.equals(Object, Object).