Class IdentifiableUtils
Identifiable.- Author:
- poirigui
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Identifiable>
booleanequals(@MayBeUninitialized T a, @MayBeUninitialized T b) Compare two identifiables of the same type without risking initializing them.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.static <T extends @MayBeUninitialized Identifiable>
List<Long> getIds(Collection<T> entities) Convert a collection of identifiable to their IDs.static LonggetRequiredId(Identifiable identifiable) Obtain the identifier of the object, throwing aNullPointerExceptionif it is null.static inthash(@MayBeUninitialized Identifiable... identifiables) Hash identifiables in a proxy-safe way using their IDs.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.static <T extends @MayBeUninitialized Identifiable>
Collector<T, ?, Set<T>> Collect results into an identifiable set.static <T extends @MayBeUninitialized Identifiable>
StringConverts an identifiable to string, avoiding its initialization of it is a proxy.
-
Constructor Details
-
IdentifiableUtils
public IdentifiableUtils()
-
-
Method Details
-
getRequiredId
Obtain the identifier of the object, throwing aNullPointerExceptionif 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,?, toIdentifiableSet()Set<T>> Collect results into an identifiable set.This uses
Identifiable.getId()for comparing elements, making the collection safe for holding proxies unlike aHashSetthat relies onObject.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, Collector<T,U> ?, toIdentifiableMapMap<K, U>> (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 aHashMapthat relies onObject.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
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).
-