Class ObjectUtility
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> intcompareNullable(T a, T b, Comparator<? super T> comparator) Compares two nullable values using the provided comparatorstatic <T> TgetRandomItem(Collection<? extends T> collection) Get a random element out of a collection, with equal probability.static <T> TgetRandomItem(List<? extends T> list) Get a random element out of a list, with equal probability.static <T> Optional<T> getRandomItemSafe(List<T> list) static <T> TnonNull(T first, T second) The method is returns the same as a call to the following code:T result = (getFirst() != null) ? getFirst() : getSecond();with the major difference that getFirst() and getSecond() get evaluated exactly once.static <T> TnonNull(T first, T second, T... others)
-
Constructor Details
-
ObjectUtility
public ObjectUtility()
-
-
Method Details
-
nonNull
The method is returns the same as a call to the following code:T result = (getFirst() != null) ? getFirst() : getSecond();with the major difference that getFirst() and getSecond() get evaluated exactly once.This means that it doesn't matter if getFirst() is relatively expensive to evaluate or has side effects. It also means that getSecond() gets evaluated regardless if it is needed or not. Since Java guarantees the order of evaluation for arguments to be the same as the order in which they appear (JSR 15.7.4), this makes it more suitable for re-playable procedural generation and similar method calls with side effects.
- Returns:
- the first argument if it's not null, otherwise the second argument
-
nonNull
- Returns:
- the first non-null argument, else null if all are null
- See Also:
-
compareNullable
public static <T> int compareNullable(@Nullable T a, @Nullable T b, Comparator<? super T> comparator) Compares two nullable values using the provided comparator- Type Parameters:
T- the specified class- Parameters:
a- the first valueb- the second valuecomparator- the comparator to use to compare provided they are both not null- Returns:
- the comparison sort value
-
getRandomItem
Get a random element out of a collection, with equal probability.This is the same as calling the following code, only plays nicely with all collections (including ones like Set which don't implement RandomAccess) and deals gracefully with empty collections.
collection.get(Compute.randomInt(collection.size());- Parameters:
collection- the collect to get a random item from, which may be null- Returns:
- a random item from the collection. Returns
nullif the collection itself is null or empty. It can also returnnullif the collection containsnullitems.
-
getRandomItem
Get a random element out of a list, with equal probability.This is the same as calling the following code, only deals gracefully with empty lists.
list.get(Compute.randomInt(list.size());- Parameters:
list- the list to get a random item from, which may benull- Returns:
- a random item from the list. This will be
nullif the list itself is null or empty, and can benullif the list containsnullitems.
-
getRandomItemSafe
-