Class Utils

java.lang.Object
net.luis.utils.util.Utils

public final class Utils extends Object
Utility class for various helper methods.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final UUID
    Empty UUID constant.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor to prevent instantiation.
    This is a static helper class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> void
    executeIfNotNull(T value, @NotNull Consumer<T> action)
    Executes the given action if the given value is not null.
    static <T> T
    getRandom(@NotNull Random rng, @NotNull List<T> values)
    Returns a random value from the given list using the given random number generator.
    static <T> T
    getRandom(@NotNull Random rng, T @NotNull ... values)
    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
    isEmpty(@Nullable UUID uuid)
    Checks if the given UUID is empty.
    An empty UUID is a UUID with all zeros.
    static <T, K, V> @NotNull Map<K,V>
    listToMap(@Nullable List<T> list, @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.
    static <T> T
    make(T object, @NotNull Consumer<T> action)
    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>
    mapKey(@Nullable Map<K,V> map, @Nullable Function<K,T> function)
    Maps the keys of the given map using the given mapper function.
    static <T, U> @NotNull List<U>
    mapList(@Nullable List<T> list, @Nullable Function<T,U> function)
    Maps the given list to a new list using the given mapper function.
    static <T, U, V> @NotNull List<V>
    mapList(@Nullable List<T> list, @Nullable Function<T,U> first, @Nullable Function<U,V> second)
    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>
    mapValue(@Nullable Map<K,V> map, @Nullable Function<V,T> function)
    Maps the values of the given map using the given mapper function.
    static @NotNull Random
    Returns a new Random instance using the current system time as seed.
    static <E extends Throwable>
    void
    throwSneaky(@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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • EMPTY_UUID

      public static final UUID EMPTY_UUID
      Empty UUID constant.
  • Constructor Details

    • Utils

      private Utils()
      Private constructor to prevent instantiation.
      This is a static helper class.
  • Method Details

    • isEmpty

      public static boolean isEmpty(@Nullable @Nullable UUID uuid)
      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

      @NotNull public static <T> T make(@NotNull T object, @NotNull @NotNull Consumer<T> action)
      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 to
      action - 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, T> @NotNull List<T> mapToList(@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 key
      V - The type of the map value
      T - The type of the list
      Parameters:
      map - The map to convert to a list
      function - The function to use for mapping
      Returns:
      The list containing the mapped entries
    • listToMap

      @NotNull public static <T, K, V> @NotNull Map<K,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 list
      K - The type of the map key
      V - The type of the map value
      Parameters:
      list - The list to convert
      function - 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 list
      U - The type of the resulting list
      Parameters:
      list - The list to map
      function - The function to use for mapping
      Returns:
      The list after mapping or an empty list
    • mapList

      @NotNull public static <T, U, V> @NotNull List<V> mapList(@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 list
      U - The type of the list after the first mapper
      V - The type of the resulting list
      Parameters:
      list - The list to map
      first - The first function to use for mapping
      second - The second function to use for mapping
      Returns:
      The list after mapping or an empty list
    • mapKey

      @NotNull public static <K, T, V> @NotNull Map<T,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 key
      T - The type of the key in the resulting map
      V - The type of the map value
      Parameters:
      map - The map to map the keys
      function - The function to use for mapping
      Returns:
      A new map with the mapped keys
    • mapValue

      @NotNull public static <K, V, T> @NotNull Map<K,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 key
      V - The type of the map value
      T - The type of the value in the resulting map
      Parameters:
      map - The map to map the values
      function - The function to use for mapping
      Returns:
      A new map with the mapped values
    • hasDuplicates

      public static boolean hasDuplicates(Object @Nullable [] array)
      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

      public static boolean hasDuplicates(@Nullable @Nullable List<?> list)
      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

      public static boolean hasDuplicates(@Nullable @Nullable Object object, Object @Nullable [] array)
      Checks if the given object is contained multiple times in the given array.
      Parameters:
      object - The object to check
      array - 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 check
      list - 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 wrap
      nullFallback - 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 wrap
      nullFallback - 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

      public static <T> void executeIfNotNull(@Nullable T value, @NotNull @NotNull Consumer<T> action)
      Executes the given action if the given value is not null.
      Type Parameters:
      T - The type of the value
      Parameters:
      value - The value to check
      action - The action to execute
      Throws:
      NullPointerException - If the action is null
    • systemRandom

      @NotNull public static @NotNull Random systemRandom()
      Returns a new Random 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 use
      values - 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 null
      IllegalArgumentException - 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 use
      values - 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 use
      values - 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 use
      values - 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

      public static <E extends Throwable> void throwSneaky(@NotNull @NotNull Throwable e) throws E
      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 exception
      NullPointerException - If the exception is null