Class KmgStaticUtilities
- java.lang.Object
-
- KmgStaticUtilities
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Stringformat(double value, int nDec)FormatsvaluewithnDecdecimals.static Stringformat(String text)Formats special characters intext.static Stringformat(String text, boolean addQuotes)Seeformat(String text).static StringgetClassName(Object o)Formats class name ofo.static ClassgetComponentType(Object o)Returns the class ofo.static StringgetHeadTrail(String delimString, int nHead, String separator, int nTrail, String text)Extracts the firstnHeadand the lastnTrailtokens delimited bydelimStringfromtext.static StringgetHeadTrail(String delimString, String separator, int mxChars, String text)AppliesgetHeadTrail(java.lang.String, int, java.lang.String, int, java.lang.String)until length of result is belowmxChars(if possible).static ClassgetPrimitive(Class cls)Returns the corresponding primitive type e.g.static intindexOfNotInString(String text, char chr)Finds the index of the firstchrintextoutside"..."or'.'.static intindexOfNotInString(String text, char chr, int fromIndex)Finds the index of the firstchrintextoutside"..."or'.'.static intindexOfNotInString(String text, String str)Finds the index of the firststrintextoutside"..."or'.'.static intindexOfNotInString(String text, String str, int fromIndex)Finds the index of the firststrintextoutside"..."or'.'.static voidmain(String[] args)Tests.static doubleparseDouble(String text)ReturnsDouble.parseDouble(java.lang.String), but (if necessary) returnsDouble.NaNinstead of throwing aNumberFormatException.static Stringrepeat(String text, int n)Returns a String which containstextrepeatedntimes.static StringreplaceNotInString(String text, String old, String replacement)Replaces all occurrences ofoldintextoutside"..."or'.'byreplacement.static booleansleep(int millis)CallsThread.sleep(long)and handles theInterruptedException.static Stringsqueeze(String text)Removes blanks fromtext.static Stringsqueeze(String text, char chRemove)Removes all occurrences ofchRemovefromtext.static Stringsqueeze(String text, String removeChars)Removes all occurrences of the characters contained inremoveCharsfromtext.static ObjecttoArray(Object arrayOrIterator)static ObjecttoArray(Object arrayOrIterator, int mode)static ArrayListtoArrayList(Object[] oa)Creates anArrayListfromoa.static ArrayListtoArrayList(Enumeration e)Creates anArrayListfrome.static ArrayListtoArrayList(Iterator iter)Creates anArrayListfromiter.static StringtoRawString(Object o)Formatsoto the string which would be returned by thetoString()method, if the same had not been overwriten for the class ofo.static StringtoString(Object o)Formats o as String.static StringtoString(Object o, boolean showHashCode)Formats o as String.static StringtoString(Object o, int verboseLevel)Formatsoas String.static String[]toStringArray(Object o)Formatsoas aStringarray.
-
-
-
Method Detail
-
toString
public static String toString(Object o)
Formats o as String.- Returns:
toString( o, true )
-
toString
public static String toString(Object o, boolean showHashCode)
Formats o as String.
Examples:oo.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)
Formatsoas 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)
Formatsoto the string which would be returned by thetoString()method, if the same had not been overwriten for the class ofo.- See Also:
Object.toString()
-
getClassName
public static String getClassName(Object o)
Formats class name ofo.
Examples:ojava.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 ifxis 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, ifclsis a java primitive wrapper e.g.Double.class, otherwisecls.
-
toArray
public static Object toArray(Object arrayOrIterator, int mode)
- Parameters:
arrayOrIterator- array or instance of a class that implements atoArray()method or ajava.util.Iteratormode- controlls what to do, if arrayOrIterator is not an array or anIteratorand atoArray()method is not available
-1 : throws Exception
0 : returnsnull
1 : returns array witharrayOrIteratoras the single element
2 : returnsarrayOrIterator- Returns:
arrayOrIterator, ifarrayOrIteratoris an array
elsearrayOrIterator.toArray(), if atoArray()method is available
else an array generated by iteration, ifarrayOrIteratorimplementsjava.util.Iterator
else see parametermode- See Also:
Collection.toArray(),Iterable.iterator(),Enumeration.asIterator()
-
toStringArray
public static String[] toStringArray(Object o)
Formatsoas aStringarray.- Parameters:
o- may be an array, aCollectionor an instance of a class implementing atoArraymethod
-
toArrayList
public static ArrayList toArrayList(Object[] oa)
Creates anArrayListfromoa.- Returns:
- new ArrayList<Object>( java.util.
Arrays.asList( oa ) ) - See Also:
java.util.Arrays.asList(...) for details
-
toArrayList
public static ArrayList toArrayList(Enumeration e)
Creates anArrayListfrome.
-
format
public static String format(String text)
Formats special characters intext. Replaces'\n', '\t', '\b', '\f', '\r'intextby"\n", "\t", "\b", "\f", "\r".
-
format
public static String format(String text, boolean addQuotes)
Seeformat(String text).- Parameters:
addQuotes- show"text"in quotes
-
format
public static String format(double value, int nDec)
FormatsvaluewithnDecdecimals.- Parameters:
nDec- maximum of number of decimals shown ifnDec > 0else fixed number of decimals
-
parseDouble
public static double parseDouble(String text)
ReturnsDouble.parseDouble(java.lang.String), but (if necessary) returnsDouble.NaNinstead of throwing aNumberFormatException.
-
repeat
public static String repeat(String text, int n)
Returns a String which containstextrepeatedntimes.
-
squeeze
public static String squeeze(String text, char chRemove)
Removes all occurrences ofchRemovefromtext.
-
squeeze
public static String squeeze(String text, String removeChars)
Removes all occurrences of the characters contained inremoveCharsfromtext.
-
indexOfNotInString
public static int indexOfNotInString(String text, char chr)
Finds the index of the firstchrintextoutside"..."or'.'.- See Also:
String.indexOf(int)
-
indexOfNotInString
public static int indexOfNotInString(String text, char chr, int fromIndex)
Finds the index of the firstchrintextoutside"..."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 firststrintextoutside"..."or'.'.- See Also:
String.indexOf(String)
-
indexOfNotInString
public static int indexOfNotInString(String text, String str, int fromIndex)
Finds the index of the firststrintextoutside"..."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 ofoldintextoutside"..."or'.'byreplacement.
-
getHeadTrail
public static String getHeadTrail(String delimString, int nHead, String separator, int nTrail, String text)
Extracts the firstnHeadand the lastnTrailtokens delimited bydelimStringfromtext. The removed tokens are replaced byseparator. 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
-
getHeadTrail
public static String getHeadTrail(String delimString, String separator, int mxChars, String text)
AppliesgetHeadTrail(java.lang.String, int, java.lang.String, int, java.lang.String)until length of result is belowmxChars(if possible).
-
sleep
public static boolean sleep(int millis)
CallsThread.sleep(long)and handles theInterruptedException. In JXN simply use:Thread.sleep( millis )- Returns:
true, ifsleepwas interrupted
-
main
public static void main(String[] args)
Tests.
-
-