Class Utility


  • public final class Utility
    extends Object
    Handy class full of static functions.
    • Constructor Detail

      • Utility

        public Utility()
    • Method Detail

      • checkJVMVersion

        public static void checkJVMVersion()
      • isEmpty

        public static boolean isEmpty​(String string)
        Returns true if the given string is null or is empty.
        Parameters:
        string - The string to be checked on emptiness.
        Returns:
        True if the given string is null or is empty.
      • isEmpty

        public static boolean isEmpty​(Object[] array)
        Returns true if the given array is null or is empty.
        Parameters:
        array - The array to be checked on emptiness.
        Returns:
        true if the given array is null or is empty.
      • isEmpty

        public static boolean isEmpty​(Collection<?> collection)
        Returns true if the given collection is null or is empty.
        Parameters:
        collection - The collection to be checked on emptiness.
        Returns:
        true if the given collection is null or is empty.
      • isEmpty

        public static boolean isEmpty​(Object value)
        Returns true if the given value is null or is empty. Types of String, Collection, Map, Optional and Array are recognized. If none is recognized, then examine the emptiness of the toString() representation instead.
        Parameters:
        value - The value to be checked on emptiness.
        Returns:
        true if the given value is null or is empty.
      • isAllEmpty

        public static boolean isAllEmpty​(Object... values)
        Returns true if all values are empty, false if at least one value is not empty.
        Parameters:
        values - the values to be checked on emptiness
        Returns:
        True if all values are empty, false otherwise
      • isAnyEmpty

        public static boolean isAnyEmpty​(Object... values)
        Returns true if at least one value is empty.
        Parameters:
        values - the values to be checked on emptiness
        Returns:
        true if any value is empty and false if no values are empty
      • isAllNull

        public static boolean isAllNull​(Object... values)
      • isAnyNull

        public static boolean isAnyNull​(Object... values)
      • isOneOf

        @SafeVarargs
        public static <T> boolean isOneOf​(T object,
                                          T... objects)
        Returns true if the given object equals one of the given objects.
        Type Parameters:
        T - The generic object type.
        Parameters:
        object - The object to be checked if it equals one of the given objects.
        objects - The argument list of objects to be tested for equality.
        Returns:
        true if the given object equals one of the given objects.
      • coalesce

        @SafeVarargs
        public static <T> T coalesce​(T... objects)
        Returns the first non-null object of the argument list, or null if there is no such element.
        Type Parameters:
        T - The generic object type.
        Parameters:
        objects - The argument list of objects to be tested for non-null.
        Returns:
        The first non-null object of the argument list, or null if there is no such element.
      • getLocalHost

        public static String getLocalHost()
        Return the hostname of the local machine.
      • getLocalAddress

        public static String getLocalAddress()
        Return the hostname of the local machine.
      • lookupObject

        public static Remote lookupObject​(String publishedName,
                                          Class anInterface)
                                   throws NamingException
        This is a convenience method to lookup a remote object by name within the naming context.
        Throws:
        NamingException - if the object with that name could not be found.
      • toCharArray

        public static char[] toCharArray​(CharBuffer cb)
        Returns a character array for the valid characters in a CharBuffer.
        Parameters:
        cb -
        Returns:
      • toByteArray

        public static byte[] toByteArray​(ByteBuffer bb)
        Returns a byte array for the valid bytes in a ByteBuffer.
        Parameters:
        bb -
        Returns:
      • bytesToInt

        public static int bytesToInt​(byte[] array,
                                     int offset)
        Unmarshal a byte array to an integer. Assume the bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+3] is the least-significant-byte.
        Parameters:
        array - The array of bytes.
        offset - The offset from which to start unmarshalling.
      • intToBytes

        public static void intToBytes​(int value,
                                      byte[] array,
                                      int offset)
        Marshal an integer to a byte array. The bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+3] is the least-significant-byte.
        Parameters:
        array - The array of bytes.
        offset - The offset from which to start marshalling.
      • bytesToLong

        public static long bytesToLong​(byte[] array,
                                       int offset)
        Unmarshal a byte array to an long. Assume the bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+7] is the least-significant-byte.
        Parameters:
        array - The array of bytes.
        offset - The offset from which to start unmarshalling.
      • longToBytes

        public static void longToBytes​(long value,
                                       byte[] array,
                                       int offset)
        Marshal an long to a byte array. The bytes are in BIGENDIAN order. i.e. array[offset] is the most-significant-byte and array[offset+7] is the least-significant-byte.
        Parameters:
        array - The array of bytes.
        offset - The offset from which to start marshalling.
      • intToShort

        public static short intToShort​(int value)
      • shortToInt

        public static int shortToInt​(short value)
      • getClassLoader

        public static ClassLoader getClassLoader()
        Get the current thread's context class loader which is set to the CommonClassLoader by ApplicationServer
        Returns:
        the thread's context classloader if it exists; else the system class loader.
      • setContextClassLoader

        public static ClassLoader setContextClassLoader​(ClassLoader newClassLoader)
        Utility routine for setting the context class loader. Returns previous class loader.
      • setEnvironment

        public static void setEnvironment()
      • getEnvOrProp

        public static String getEnvOrProp​(String name)
        Return the value for a given name from the System Properties or the Environmental Variables. The former overrides the latter.
        Parameters:
        name - - the name of the System Property or Environmental Variable
        Returns:
        the value of the variable or null if it was not found
      • convertByteArrayToCharArray

        public static char[] convertByteArrayToCharArray​(byte[] byteArray,
                                                         String charset)
                                                  throws CharacterCodingException
        Convert the byte array to char array with respect to given charset.
        Parameters:
        byteArray -
        charset - null or "" means default charset
        Throws:
        CharacterCodingException
      • convertCharArrayToByteArray

        public static byte[] convertCharArrayToByteArray​(char[] charArray,
                                                         String strCharset)
                                                  throws CharacterCodingException
        Convert the char array to byte array with respect to given charset.
        Parameters:
        charArray -
        strCharset - null or "" means default charset
        Throws:
        CharacterCodingException