Class Util


  • public class Util
    extends Object
    General purpose utility functions
    Author:
    Sergey.Anchipolevsky Date: 18.10.2007
    • Constructor Detail

      • Util

        public Util()
    • Method Detail

      • max

        @Nullable
        public static <T extends Comparable> T max​(@Nullable
                                                   T o1,
                                                   @Nullable
                                                   T o2)
        Compares two comparables and return the greater one. If both are equal any of them can be returned. If one is null the other is returned. If both are nulls null is returned.
        Parameters:
        o1 - the first comparable
        o2 - the second comparable
        Returns:
        the greater one
      • compare

        public static int compare​(long f,
                                  long s)
        Compares two long values
        Parameters:
        f - the first value
        s - the second value
        Returns:
        -1 when f < s; 0 when f == s; 1 when f > s
      • doUnderContextClassLoader

        public static <T,​E extends Throwable> T doUnderContextClassLoader​(@NotNull
                                                                                ClassLoader cl,
                                                                                FuncThrow<T,​E> action)
                                                                         throws E extends Throwable
        Performs an action under context class loader.
        Type Parameters:
        E - exception that action could throw
        Parameters:
        cl - classloader to set
        action - action to run
        Returns:
        the value returned from action
        Throws:
        E - exception if action throws an exception.
        E extends Throwable
      • map

        public static <T> Map<T,​T> map​(T... args)
        Converts input arguments into a map: elements with even indexes are keys, elements with odd indexes are values. If the number of arguments is odd the last argument is considered as a key with a null value.
        Type Parameters:
        T - any type
        Parameters:
        args - arguments in the following format - key1, value1, key2, value2...
        Returns:
        the map
      • unbox

        public static int unbox​(@Nullable
                                Integer nullable,
                                int nullValue)
        Unbox nullable
        Parameters:
        nullable - Nullable to unbox
        nullValue - Value in case nullable is null
        Returns:
        unboxed value if nullable is not null, nullValue otherwise
      • ofNullable

        public static <T> T ofNullable​(@Nullable
                                       T value,
                                       @NotNull
                                       T defaultValue)
        Nullable with default
        Parameters:
        value - actual value
        defaultValue - value for null
        Returns:
        value if not null or default
      • ofNullable

        public static <T> T ofNullable​(@NotNull
                                       Supplier<T> value,
                                       @NotNull
                                       T defaultValue)
        Nullable of supplier with default
        Parameters:
        value - value supplier
        defaultValue - value for null
        Returns:
        value if supplier returns not null or default
      • ofNullable

        public static <T> T ofNullable​(@Nullable
                                       T value,
                                       @NotNull
                                       Supplier<T> defaultValue)
        Nullable with default
        Parameters:
        value - actual value
        defaultValue - supplier of value for null
        Returns:
        value if not null or default
      • unwrapCause

        @Nullable
        public static <T extends Throwable> T unwrapCause​(@Nullable
                                                          Throwable t,
                                                          @NotNull
                                                          Class<T> causeClass)
      • unwrapCause

        @Nullable
        public static Throwable unwrapCause​(@Nullable
                                            Throwable t,
                                            @NotNull
                                            String causeClassName)