Class Utils

  • All Implemented Interfaces:

    
    public class Utils
    
                        

    Utility methods.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static <T> T getOrDefault(T value, T defaultValue) Returns the given value if it is not null, otherwise returns the given default value.
      static <T> List<T> getOrDefault(List<T> list, List<T> defaultList) Returns the given list if it is not null and not empty, otherwise returns the given default list.
      static <K, V> Map<K, V> getOrDefault(Map<K, V> map, Map<K, V> defaultMap) Returns the given map if it is not null and not empty, otherwise returns the given default map.
      static <T> T getOrDefault(T value, Supplier<T> defaultValueSupplier) Returns the given value if it is not null, otherwise returns the value returned by the given supplier.
      static boolean isNullOrBlank(String string) Is the given string null or blank?
      static boolean isNullOrEmpty(String string) Is the given string null or empty ("")?
      static boolean isNotNullOrBlank(String string) Is the given string not null and not blank?
      static boolean isNotNullOrEmpty(String string) Is the given string not null and not empty ("")?
      static boolean areNotNullOrBlank(Array<String> strings) Are all the given strings not null and not blank?
      static boolean isNullOrEmpty(Collection<out Object> collection) Is the collection null or empty?
      static boolean isNullOrEmpty(Iterable<out Object> iterable) Is the iterable object null or empty?
      static boolean isNullOrEmpty(Map<out Object, out Object> map) Is the map object null or empty?
      static boolean isCollectionEmpty(Collection<out Object> collection)
      static String repeat(String string, int times) Returns a string consisting of the given string repeated times times.
      static String randomUUID() Returns a random UUID.
      static String generateUUIDFrom(String input) Generates a UUID from a hash of the given input string.
      static String ensureTrailingForwardSlash(String url) Appends a trailing '/' if the provided URL does not end with '/'
      static String quoted(Object object) Returns the given object's toString() surrounded by quotes.
      static String firstChars(String string, int numberOfChars) Returns the first numberOfChars characters of the given string.
      static Array<byte> readBytes(String url) Reads the content as bytes from the given URL as a GET request for HTTP/HTTPS resources, and from files stored on the local filesystem.
      static <T> Set<T> copyIfNotNull(Set<T> set) Returns an (unmodifiable) copy of the provided set.
      static <T> List<T> copyIfNotNull(List<T> list) Returns an (unmodifiable) copy of the provided list.
      static <K, V> Map<K, V> copyIfNotNull(Map<K, V> map) Returns an (unmodifiable) copy of the provided map.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • getOrDefault

         static <T> T getOrDefault(T value, T defaultValue)

        Returns the given value if it is not null, otherwise returns the given default value.

        Parameters:
        value - The value to return if it is not null.
        defaultValue - The value to return if the value is null.
        Returns:

        the given value if it is not null, otherwise returns the given default value.

      • getOrDefault

         static <T> List<T> getOrDefault(List<T> list, List<T> defaultList)

        Returns the given list if it is not null and not empty, otherwise returns the given default list.

        Parameters:
        list - The list to return if it is not null and not empty.
        defaultList - The list to return if the list is null or empty.
        Returns:

        the given list if it is not null and not empty, otherwise returns the given default list.

      • getOrDefault

         static <K, V> Map<K, V> getOrDefault(Map<K, V> map, Map<K, V> defaultMap)

        Returns the given map if it is not null and not empty, otherwise returns the given default map.

        Parameters:
        map - The map to return if it is not null and not empty.
        defaultMap - The map to return if the map is null or empty.
        Returns:

        the given map if it is not null and not empty, otherwise returns the given default map.

      • getOrDefault

         static <T> T getOrDefault(T value, Supplier<T> defaultValueSupplier)

        Returns the given value if it is not null, otherwise returns the value returned by the given supplier.

        Parameters:
        value - The value to return if it is not null.
        defaultValueSupplier - The supplier to call if the value is null.
        Returns:

        the given value if it is not null, otherwise returns the value returned by the given supplier.

      • isNullOrBlank

         static boolean isNullOrBlank(String string)

        Is the given string null or blank?

        Parameters:
        string - The string to check.
        Returns:

        true if the string is null or blank.

      • isNullOrEmpty

         static boolean isNullOrEmpty(String string)

        Is the given string null or empty ("")?

        Parameters:
        string - The string to check.
        Returns:

        true if the string is null or empty.

      • isNotNullOrBlank

         static boolean isNotNullOrBlank(String string)

        Is the given string not null and not blank?

        Parameters:
        string - The string to check.
        Returns:

        true if there's something in the string.

      • isNotNullOrEmpty

         static boolean isNotNullOrEmpty(String string)

        Is the given string not null and not empty ("")?

        Parameters:
        string - The string to check.
        Returns:

        true if the given string is not null and not empty ("")?

      • areNotNullOrBlank

         static boolean areNotNullOrBlank(Array<String> strings)

        Are all the given strings not null and not blank?

        Parameters:
        strings - The strings to check.
        Returns:

        true if every string is non-null and non-empty.

      • isNullOrEmpty

         static boolean isNullOrEmpty(Collection<out Object> collection)

        Is the collection null or empty?

        Parameters:
        collection - The collection to check.
        Returns:

        true if the collection is null or isEmpty, otherwise false.

      • isNullOrEmpty

         static boolean isNullOrEmpty(Iterable<out Object> iterable)

        Is the iterable object null or empty?

        Parameters:
        iterable - The iterable object to check.
        Returns:

        true if the iterable object is null or there are no objects to iterate over, otherwise false.

      • isNullOrEmpty

         static boolean isNullOrEmpty(Map<out Object, out Object> map)

        Is the map object null or empty?

        Parameters:
        map - The iterable object to check.
        Returns:

        true if the map object is null or empty map, otherwise false.

      • isCollectionEmpty

        @Deprecated(forRemoval = true) static boolean isCollectionEmpty(Collection<out Object> collection)
        Parameters:
        collection - The collection to check.
        Returns:

        true if the collection is null or empty, false otherwise.

      • repeat

         static String repeat(String string, int times)

        Returns a string consisting of the given string repeated times times.

        Parameters:
        string - The string to repeat.
        times - The number of times to repeat the string.
        Returns:

        A string consisting of the given string repeated times times.

      • generateUUIDFrom

         static String generateUUIDFrom(String input)

        Generates a UUID from a hash of the given input string.

        Parameters:
        input - The input string.
        Returns:

        A UUID.

      • ensureTrailingForwardSlash

         static String ensureTrailingForwardSlash(String url)

        Appends a trailing '/' if the provided URL does not end with '/'

        Parameters:
        url - URL to check for trailing '/'
        Returns:

        Same URL if it already ends with '/' or a new URL with '/' appended

      • quoted

         static String quoted(Object object)

        Returns the given object's toString() surrounded by quotes.

        If the given object is null, the string "null" is returned.

        Parameters:
        object - The object to quote.
        Returns:

        The given object surrounded by quotes.

      • firstChars

         static String firstChars(String string, int numberOfChars)

        Returns the first numberOfChars characters of the given string. If the string is shorter than numberOfChars, the whole string is returned.

        Parameters:
        string - The string to get the first characters from.
        numberOfChars - The number of characters to return.
        Returns:

        The first numberOfChars characters of the given string.

      • readBytes

         static Array<byte> readBytes(String url)

        Reads the content as bytes from the given URL as a GET request for HTTP/HTTPS resources, and from files stored on the local filesystem.

        Parameters:
        url - The URL to read from.
        Returns:

        The content as bytes.

      • copyIfNotNull

         static <T> Set<T> copyIfNotNull(Set<T> set)

        Returns an (unmodifiable) copy of the provided set. Returns null if the provided set is null.

        Parameters:
        set - The set to copy.
        Returns:

        The copy of the provided set.

      • copyIfNotNull

         static <T> List<T> copyIfNotNull(List<T> list)

        Returns an (unmodifiable) copy of the provided list. Returns null if the provided list is null.

        Parameters:
        list - The list to copy.
        Returns:

        The copy of the provided list.

      • copyIfNotNull

         static <K, V> Map<K, V> copyIfNotNull(Map<K, V> map)

        Returns an (unmodifiable) copy of the provided map. Returns null if the provided map is null.

        Parameters:
        map - The map to copy.
        Returns:

        The copy of the provided map.