Class Utils


  • public final class Utils
    extends Object
    Common utilities.
    • Method Detail

      • byteSizeFormat

        public static String byteSizeFormat​(long bytes)
      • mod

        public static int mod​(int hash,
                              int size)
      • isPowerOfTwo

        public static boolean isPowerOfTwo​(int n)
        Returns true if the specified value is a power of two.
        Parameters:
        n - Number to check.
        Returns:
        true if the specified value is a power of two.
      • toString

        public static <T> String toString​(Collection<T> collection,
                                          Function<? super T,​String> mapper)
        Converts the specified collection to a comma-separated string by applying the specified mapper to each element and wrapping the resulting string with {...}.
        Type Parameters:
        T - Collection type.
        Parameters:
        collection - Collection.
        mapper - Element mapper.
        Returns:
        String that has the following format: {element1, element2, ..., elementN}
      • nullOrTrim

        public static String nullOrTrim​(String str)
        Returns null if the specified string is null or is an empty string after trimming; returns a trimmed string otherwise.
        Parameters:
        str - String.
        Returns:
        null or trimmed string.
      • nullOrTrim

        public static String nullOrTrim​(String str,
                                        String defaultVal)
        Returns defaultVal if the specified string is null or is an empty string after trimming; returns a trimmed string otherwise.
        Parameters:
        str - String.
        defaultVal - Default value.
        Returns:
        null or trimmed string.
      • nullSafeImmutableCopy

        public static <T> List<T> nullSafeImmutableCopy​(List<T> source)
        Returns an immutable copy of the specified list by filtering out all null elements.
        Type Parameters:
        T - Element type.
        Parameters:
        source - Source list, can be null (in such case an empty list will be returned by this method).
        Returns:
        Immutable copy of the specified list.