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

    • getRequiredId

      @NonNull public static Long getRequiredId(Identifiable identifiable)
      Obtain the identifier of the object, throwing a NullPointerException if it is null.
      Parameters:
      identifiable - an entity to get the ID from
      Throws:
      NullPointerException - if the identifier is null
    • getIds

      public static <T extends @MayBeUninitialized Identifiable> List<Long> getIds(Collection<T> entities)
      Convert a collection of identifiable to their IDs.

      This function cannot be used on non-persistent entities (i.e. those with null 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 @MayBeUninitialized 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.

      This function cannot be used on non-persistent entities (i.e. those with null IDs).

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

      public static <T extends @MayBeUninitialized 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().

      This collector cannot be used with non-persistent entities (i.e. those with null IDs).

      See Also:
    • toIdentifiableMap

      public static <T, K extends @MayBeUninitialized 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().

      This collector cannot be used with non-persistent entities (i.e. those with null IDs).

      See Also:
    • toString

      public static <T extends @MayBeUninitialized 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(@MayBeUninitialized 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 @MayBeUninitialized T a, @Nullable @MayBeUninitialized 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).