Class Util


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

      Constructors 
      Constructor Description
      Util()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int compare​(long f, long s)
      Compares two long values
      static <T,​E extends java.lang.Throwable>
      T
      doUnderContextClassLoader​(java.lang.ClassLoader cl, FuncThrow<T,​E> action)
      Performs an action under context class loader.
      static <T> java.util.Map<T,​T> map​(T... args)
      Converts input arguments into a map: elements with even indexes are keys, elements with odd indexes are values.
      static <T extends java.lang.Comparable>
      T
      max​(T o1, T o2)
      Compares two comparables and return the greater one.
      static <T> T ofNullable​(java.util.function.Supplier<T> value, T defaultValue)
      Nullable of supplier with default
      static <T> T ofNullable​(T value, java.util.function.Supplier<T> defaultValue)
      Nullable with default
      static <T> T ofNullable​(T value, T defaultValue)
      Nullable with default
      static int unbox​(java.lang.Integer nullable, int nullValue)
      Unbox nullable
      static <T extends java.lang.Throwable>
      T
      unwrapCause​(java.lang.Throwable t, java.lang.Class<T> causeClass)  
      static java.lang.Throwable unwrapCause​(java.lang.Throwable t, java.lang.String causeClassName)  
      • Methods inherited from class java.lang.Object

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

      • Util

        public Util()
    • Method Detail

      • max

        @Nullable
        public static <T extends java.lang.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 java.lang.Throwable> T doUnderContextClassLoader​(@NotNull
                                                                                          java.lang.ClassLoader cl,
                                                                                          FuncThrow<T,​E> action)
                                                                                   throws E extends java.lang.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 java.lang.Throwable
      • map

        public static <T> java.util.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
                                java.lang.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
                                       java.util.function.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
                                       java.util.function.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 java.lang.Throwable> T unwrapCause​(@Nullable
                                                                    java.lang.Throwable t,
                                                                    @NotNull
                                                                    java.lang.Class<T> causeClass)
      • unwrapCause

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