-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Utils()
Private constructor to prevent instantiation.
This is a static helper class. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> void
executeIfNotNull
(T value, @NotNull Consumer<T> action) Executes the given action if the given value is not null.static <T> T
Returns a random value from the given list using the given random number generator.static <T> T
Returns a random value from the given array using the given random number generator.static <T> @NotNull Optional
<T> getRandomSafe
(@NotNull Random rng, @Nullable List<T> values) Returns a random value from the given list using the given random number generator.
If the list is empty or null, an empty optional is returned.static <T> @NotNull Optional
<T> getRandomSafe
(@NotNull Random rng, T @Nullable ... values) Returns a random value from the given array using the given random number generator.
If the array is empty or null, an empty optional is returned.static boolean
hasDuplicates
(@Nullable Object object, Object @Nullable [] array) Checks if the given object is contained multiple times in the given array.static boolean
hasDuplicates
(@Nullable Object object, @Nullable List<?> list) Checks if the given object is contained multiple times in the given list.static boolean
hasDuplicates
(Object @Nullable [] array) Checks if the given array contains duplicate values.
If the array is null or empty, false is returned.static boolean
hasDuplicates
(@Nullable List<?> list) Checks if the given list contains duplicate values.
If the list is null or empty, false is returned.static boolean
Checks if the given UUID is empty.
An empty UUID is a UUID with all zeros.static <T,
K, V> @NotNull Map <K, V> Maps the given list to a map using the given mapper function.
The mapper takes the list value and returns the resulting map entry.static <T> T
Applies the given action to the given object and returns the object.
This is useful for creating objects and applying actions to them in one line.static <K,
T, V> @NotNull Map <T, V> Maps the keys of the given map using the given mapper function.static <T,
U> @NotNull List <U> Maps the given list to a new list using the given mapper function.static <T,
U, V> @NotNull List <V> Maps the given list to a new list using the given two mapper functions.static <K,
V, T> @NotNull List <T> mapToList
(@Nullable Map<K, V> map, @Nullable BiFunction<K, V, T> function) Maps the given map to a list using the given mapper function.
The mapper takes the key and value of the map and returns the resulting list value.static <K,
V, T> @NotNull Map <K, T> Maps the values of the given map using the given mapper function.static @NotNull Random
Returns a newRandom
instance using the current system time as seed.static <E extends Throwable>
voidthrowSneaky
(@NotNull Throwable e) Throws the given exception as a sneaky exception.static <T> T
warpNullTo
(T value, @NotNull Supplier<T> nullFallback) Wraps the given value to the value returned by the given fallback supplier if the value is null.
If the value is not null, the value is returned.static <T> T
warpNullTo
(T value, T nullFallback) Wraps the given value to the given fallback value if the value is null.
If the value is not null, the value is returned.
-
Field Details
-
EMPTY_UUID
Empty UUID constant.
-
-
Constructor Details
-
Utils
private Utils()Private constructor to prevent instantiation.
This is a static helper class.
-
-
Method Details
-
isEmpty
Checks if the given UUID is empty.
An empty UUID is a UUID with all zeros.- Parameters:
uuid
- The UUID to check- Returns:
- True, if the UUID is empty, otherwise false
-
make
Applies the given action to the given object and returns the object.
This is useful for creating objects and applying actions to them in one line.- Type Parameters:
T
- The type of the object- Parameters:
object
- The object to apply the action toaction
- The action to apply to the object- Returns:
- The object after the action has been applied
- Throws:
NullPointerException
- If the object or action is null
-
mapToList
@NotNull public static <K,V, @NotNull List<T> mapToListT> (@Nullable @Nullable Map<K, V> map, @Nullable @Nullable BiFunction<K, V, T> function) Maps the given map to a list using the given mapper function.
The mapper takes the key and value of the map and returns the resulting list value.
If the map or the function is null, an empty list is returned.
In the case the map is empty, the resulting list is also empty.
- Type Parameters:
K
- The type of the map keyV
- The type of the map valueT
- The type of the list- Parameters:
map
- The map to convert to a listfunction
- The function to use for mapping- Returns:
- The list containing the mapped entries
-
listToMap
@NotNull public static <T,K, @NotNull Map<K,V> V> listToMap(@Nullable @Nullable List<T> list, @Nullable @Nullable Function<T, Map.Entry<K, V>> function) Maps the given list to a map using the given mapper function.
The mapper takes the list value and returns the resulting map entry.
If the list or the function is null, an empty map is returned.
In the case the list is empty, the resulting map is also empty.
- Type Parameters:
T
- The type of the listK
- The type of the map keyV
- The type of the map value- Parameters:
list
- The list to convertfunction
- The function to use for mapping- Returns:
- The map containing the mapped elements
-
mapList
@NotNull public static <T,U> @NotNull List<U> mapList(@Nullable @Nullable List<T> list, @Nullable @Nullable Function<T, U> function) Maps the given list to a new list using the given mapper function.
If the list or the function is null, an empty list is returned.
In the case the list is empty, the resulting list is also empty.
Shorthand for:
list.stream().map(function).collect(Collectors.toList());
- Type Parameters:
T
- The type of the listU
- The type of the resulting list- Parameters:
list
- The list to mapfunction
- The function to use for mapping- Returns:
- The list after mapping or an empty list
-
mapList
@NotNull public static <T,U, @NotNull List<V> mapListV> (@Nullable @Nullable List<T> list, @Nullable @Nullable Function<T, U> first, @Nullable @Nullable Function<U, V> second) Maps the given list to a new list using the given two mapper functions.
If the list or the functions are null, an empty list is returned.
In the case the list is empty, the resulting list is also empty.
Shorthand for:
list.stream().map(first).map(second).collect(Collectors.toList());
- Type Parameters:
T
- The type of the listU
- The type of the list after the first mapperV
- The type of the resulting list- Parameters:
list
- The list to mapfirst
- The first function to use for mappingsecond
- The second function to use for mapping- Returns:
- The list after mapping or an empty list
-
mapKey
@NotNull public static <K,T, @NotNull Map<T,V> V> mapKey(@Nullable @Nullable Map<K, V> map, @Nullable @Nullable Function<K, T> function) Maps the keys of the given map using the given mapper function.
If the map or the function is null, an empty map is returned.
In the case the map is empty, the resulting map is also empty.
If the function returns null for a key, the key is not added to the resulting map.
- Type Parameters:
K
- The type of the map keyT
- The type of the key in the resulting mapV
- The type of the map value- Parameters:
map
- The map to map the keysfunction
- The function to use for mapping- Returns:
- A new map with the mapped keys
-
mapValue
@NotNull public static <K,V, @NotNull Map<K,T> T> mapValue(@Nullable @Nullable Map<K, V> map, @Nullable @Nullable Function<V, T> function) Maps the values of the given map using the given mapper function.
If the map or the function is null, an empty map is returned.
In the case the map is empty, the resulting map is also empty.
The function must be able to handle null values.
- Type Parameters:
K
- The type of the map keyV
- The type of the map valueT
- The type of the value in the resulting map- Parameters:
map
- The map to map the valuesfunction
- The function to use for mapping- Returns:
- A new map with the mapped values
-
hasDuplicates
Checks if the given array contains duplicate values.
If the array is null or empty, false is returned.- Parameters:
array
- The array to check- Returns:
- True, if the array contains duplicate values, otherwise false
-
hasDuplicates
Checks if the given list contains duplicate values.
If the list is null or empty, false is returned.- Parameters:
list
- The list to check- Returns:
- True, if the list contains duplicate values, otherwise false
-
hasDuplicates
Checks if the given object is contained multiple times in the given array.- Parameters:
object
- The object to checkarray
- The array to check in- Returns:
- True, if the object is contained multiple times in the array, otherwise false
-
hasDuplicates
public static boolean hasDuplicates(@Nullable @Nullable Object object, @Nullable @Nullable List<?> list) Checks if the given object is contained multiple times in the given list.- Parameters:
object
- The object to checklist
- The list to check in- Returns:
- True, if the object is contained multiple times in the list, otherwise false
-
warpNullTo
@NotNull public static <T> T warpNullTo(@Nullable T value, @NotNull T nullFallback) Wraps the given value to the given fallback value if the value is null.
If the value is not null, the value is returned.- Type Parameters:
T
- The type of the value- Parameters:
value
- The value to wrapnullFallback
- The fallback value to use- Returns:
- The value or the fallback value if the value is null
- Throws:
NullPointerException
- If the fallback value is null
-
warpNullTo
@NotNull public static <T> T warpNullTo(@Nullable T value, @NotNull @NotNull Supplier<T> nullFallback) Wraps the given value to the value returned by the given fallback supplier if the value is null.
If the value is not null, the value is returned.- Type Parameters:
T
- The type of the value- Parameters:
value
- The value to wrapnullFallback
- The fallback supplier to use- Returns:
- The value or the value returned by the fallback supplier if the value is null
- Throws:
NullPointerException
- If the fallback supplier is null
-
executeIfNotNull
Executes the given action if the given value is not null.- Type Parameters:
T
- The type of the value- Parameters:
value
- The value to checkaction
- The action to execute- Throws:
NullPointerException
- If the action is null
-
systemRandom
Returns a newRandom
instance using the current system time as seed.- Returns:
- A new random number generator
-
getRandom
@SafeVarargs @NotNull public static <T> T getRandom(@NotNull @NotNull Random rng, T @NotNull ... values) Returns a random value from the given array using the given random number generator.- Type Parameters:
T
- The type of the array- Parameters:
rng
- The random number generator to usevalues
- The array to get the random value from- Returns:
- A random value from the array
- Throws:
NullPointerException
- If the random number generator or array is nullIllegalArgumentException
- If the array is empty
-
getRandomSafe
@SafeVarargs @NotNull public static <T> @NotNull Optional<T> getRandomSafe(@NotNull @NotNull Random rng, T @Nullable ... values) Returns a random value from the given array using the given random number generator.
If the array is empty or null, an empty optional is returned.- Type Parameters:
T
- The type of the array- Parameters:
rng
- The random number generator to usevalues
- The array to get the random value from- Returns:
- A random value from the array
- Throws:
NullPointerException
- If the random number generator is null- See Also:
-
getRandom
@NotNull public static <T> T getRandom(@NotNull @NotNull Random rng, @NotNull @NotNull List<T> values) Returns a random value from the given list using the given random number generator.- Type Parameters:
T
- The type of the list- Parameters:
rng
- The random number generator to usevalues
- The list to get the random value from- Returns:
- A random value from the list
- Throws:
NullPointerException
- If the random number generator or list is null
-
getRandomSafe
@NotNull public static <T> @NotNull Optional<T> getRandomSafe(@NotNull @NotNull Random rng, @Nullable @Nullable List<T> values) Returns a random value from the given list using the given random number generator.
If the list is empty or null, an empty optional is returned.- Type Parameters:
T
- The type of the list- Parameters:
rng
- The random number generator to usevalues
- The list to get the random value from- Returns:
- A random value from the list
- Throws:
NullPointerException
- If the random number generator is null- See Also:
-
throwSneaky
Throws the given exception as a sneaky exception.- Type Parameters:
E
- The type of the exception- Parameters:
e
- The exception to throw- Throws:
E
- The given exception as a sneaky exceptionNullPointerException
- If the exception is null
-