Class KmgStaticUtilities


  • public class KmgStaticUtilities
    extends Object
    Collection of (static) methods adding to package java.lang.
    • Method Detail

      • toString

        public static String toString​(Object o,
                                      boolean showHashCode)
        Formats o as String.
        Examples:
        o o.toString() toString( o, true ) toString( o, false )
        null -/- null(Object) null(Object)
        new Byte(7) 7 7 (java.lang.Byte@139a55~7) 7 (java.lang.Byte)
        "abc" abc abc (java.lang.String@1d2632d~17862) abc (java.lang.String)
        new Object() java.lang.Object@1beb8f3 java.lang.Object@1beb8f3 object (java.lang.Object)
        {"abc", "xyz"} [Ljava.lang.String;@1db9742 [Ljava.lang.String;@1db9742 (java.lang.String[2]) array (java.lang.String[2])
        { {11,12}, {21,22} } [[I@139a55 [[I@139a55 (int[2][]) array (int[2][])
        Parameters:
        showHashCode - if true, the result contains the hash code
        See Also:
        Object.toString(), Object.hashCode(), System.identityHashCode(java.lang.Object)
      • toString

        public static String toString​(Object o,
                                      int verboseLevel)
        Formats o as String.
        Parameters:
        verboseLevel - control verboseness of output:
        > 0 show hash code
        < 0 do not show hash code
        2 add class name
      • toRawString

        public static String toRawString​(Object o)
        Formats o to the string which would be returned by the toString() method, if the same had not been overwriten for the class of o.
        See Also:
        Object.toString()
      • getClassName

        public static String getClassName​(Object o)
        Formats class name of o.
        Examples:
        o java.lang.Class:
        o.getClass().getName()
        KmgStaticUtilities:
        getClassName(o)
        KmgStaticUtilities:
        getClassName( o.getClass() )
        null -/- null(Object) -/-
        "abc" java.lang.String java.lang.String java.lang.Class:java.lang.String
        {"abc", "xyz"} [Ljava.lang.String; java.lang.String[2] java.lang.Class:java.lang.String[]
        String.class java.lang.Class java.lang.Class:java.lang.String java.lang.Class:java.lang.Class
        { @Byte(2), @Byte(3) } [Ljava.lang.Byte; java.lang.Byte[2] java.lang.Class:java.lang.Byte[]
        { 2., 3., 5., 7. } [D double[4] java.lang.Class:double[]
        { { 11, 12 }, { 21, 22 } } [[I int[2][] java.lang.Class:int[][]

        Note: JxnObject.getClass(x).getName() also works if x is a java primitive.
        See Also:
        Object.getClass(), Class.getName(), Class.getSimpleName()
      • getPrimitive

        public static Class getPrimitive​(Class cls)
        Returns the corresponding primitive type e.g. Double.TYPE, if cls is a java primitive wrapper e.g. Double.class, otherwise cls.
      • toArray

        public static Object toArray​(Object ao)
        Calls toArray( ao, 0 ).
      • toArray

        public static Object toArray​(Object ao,
                                     int mode)
        Check, if ao is an array or try to call ao.toArray().
        Parameters:
        ao - array or instance of a class that implements a toArray() method
        mode - controlls what to do, if a toArray() method is not available
            0 : returns null
            1 : returns array with oa as the single element
           -1 : throws Exception
        Returns:
        oa, if oa is an array else
        oa.toArray(), if toArray() method is available else
        see mode parameter
      • toArrayList

        public static ArrayList toArrayList​(Iterator iter)
        Creates an ArrayList from iter.
      • toArrayList

        public static ArrayList toArrayList​(Enumeration e)
        Creates an ArrayList from e .
      • toStringArray

        public static String[] toStringArray​(Collection c)
        Formats c as a String array.
      • format

        public static String format​(String text)
        Formats special characters in text. Replaces '\n', '\t', '\b', '\f', '\r' in text by "\n", "\t", "\b", "\f", "\r".
      • squeeze

        public static String squeeze​(String text)
        Removes blanks from text.
      • squeeze

        public static String squeeze​(String text,
                                     char chRemove)
        Removes all occurrences of chRemove from text.
      • squeeze

        public static String squeeze​(String text,
                                     String removeChars)
        Removes all occurrences of the characters contained in removeChars from text.
      • indexOfNotInString

        public static int indexOfNotInString​(String text,
                                             char chr)
        Finds the index of the first chr in text outside "..." or '.'.
        See Also:
        String.indexOf(int)
      • indexOfNotInString

        public static int indexOfNotInString​(String text,
                                             char chr,
                                             int fromIndex)
        Finds the index of the first chr in text outside "..." or '.'.
        Parameters:
        fromIndex - the index to start the search from
        See Also:
        String.indexOf(int, int)
      • indexOfNotInString

        public static int indexOfNotInString​(String text,
                                             String str)
        Finds the index of the first str in text outside "..." or '.'.
        See Also:
        String.indexOf(String)
      • indexOfNotInString

        public static int indexOfNotInString​(String text,
                                             String str,
                                             int fromIndex)
        Finds the index of the first str in text outside "..." or '.'.
        Parameters:
        fromIndex - the index to start the search from
        See Also:
        String.indexOf(String, int)
      • replaceNotInString

        public static String replaceNotInString​(String text,
                                                String old,
                                                String replacement)
        Replaces all occurrences of old in text outside "..." or '.' by replacement.
      • getHeadTrail

        public static String getHeadTrail​(String delimString,
                                          int nHead,
                                          String separator,
                                          int nTrail,
                                          String text)
        Extracts the first nHead and the last nTrail tokens delimited by delimString from text. The removed tokens are replaced by separator. The delimiters are included in the result.
        Example: getHeadTrail( "/", 3, "...", 2, "C:/Users/Default/AppData/Roaming/Microsoft/Windows/SendTo" )
           returns: C:/Users/Default/.../Windows/SendTo
      • sleep

        public static boolean sleep​(int millis)
        Calls Thread.sleep(long) and handles the InterruptedException. In JXN simply use: Thread.sleep( millis )
        Returns:
        true, if sleep was interrupted
      • main

        public static void main​(String[] args)
        Tests.