Class KmgStaticUtilities
- java.lang.Object
-
- KmgStaticUtilities
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
format(double value, int nDec)
Formatsvalue
withnDec
decimals.static String
format(String text)
Formats special characters intext
.static String
format(String text, boolean addQuotes)
Seeformat(String text)
.static String
getClassName(Object o)
Formats class name ofo
.static Class
getComponentType(Object o)
Returns the class ofo
.static String
getHeadTrail(String delimString, int nHead, String separator, int nTrail, String text)
Extracts the firstnHead
and the lastnTrail
tokens delimited bydelimString
fromtext
.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).static Class
getPrimitive(Class cls)
Returns the corresponding primitive type e.g.static int
indexOfNotInString(String text, char chr)
Finds the index of the firstchr
intext
outside"..."
or'.'
.static int
indexOfNotInString(String text, char chr, int fromIndex)
Finds the index of the firstchr
intext
outside"..."
or'.'
.static int
indexOfNotInString(String text, String str)
Finds the index of the firststr
intext
outside"..."
or'.'
.static int
indexOfNotInString(String text, String str, int fromIndex)
Finds the index of the firststr
intext
outside"..."
or'.'
.static void
main(String[] args)
Tests.static double
parseDouble(String text)
ReturnsDouble.parseDouble(java.lang.String)
, but (if necessary) returnsDouble.NaN
instead of throwing aNumberFormatException
.static String
repeat(String text, int n)
Returns a String which containstext
repeatedn
times.static String
replaceNotInString(String text, String old, String replacement)
Replaces all occurrences ofold
intext
outside"..."
or'.'
byreplacement
.static boolean
sleep(int millis)
CallsThread.sleep(long)
and handles theInterruptedException
.static String
squeeze(String text)
Removes blanks fromtext
.static String
squeeze(String text, char chRemove)
Removes all occurrences ofchRemove
fromtext
.static String
squeeze(String text, String removeChars)
Removes all occurrences of the characters contained inremoveChars
fromtext
.static Object
toArray(Object ao)
CallstoArray( ao, 0 )
.static Object
toArray(Object ao, int mode)
Check, ifao
is an array or try to callao.toArray()
.static ArrayList
toArrayList(Enumeration e)
Creates anArrayList
frome
.static ArrayList
toArrayList(Iterator iter)
Creates anArrayList
fromiter
.static String
toRawString(Object o)
Formatso
to the string which would be returned by thetoString()
method, if the same had not been overwriten for the class ofo
.static String
toString(Object o)
Formats o as String.static String
toString(Object o, boolean showHashCode)
Formats o as String.static String
toString(Object o, int verboseLevel)
Formatso
as String.static String[]
toStringArray(Collection c)
Formatsc
as aString
array.
-
-
-
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: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)
Formatso
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)
Formatso
to 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: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 ifx
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
, ifcls
is a java primitive wrapper e.g.Double.class
, otherwisecls
.
-
toArray
public static Object toArray(Object ao, int mode)
Check, ifao
is an array or try to callao.toArray()
.- Parameters:
ao
- array or instance of a class that implements atoArray()
methodmode
- controlls what to do, if ao is not an array and atoArray()
method is not available for ao
-1 : throws Exception
0 : returnsnull
1 : returns array withao
as the single element
2 : returnsao
- Returns:
ao
, ifao
is an array else
ao.toArray()
, iftoArray()
method is available else
see parametermode
-
toArrayList
public static ArrayList toArrayList(Enumeration e)
Creates anArrayList
frome
.
-
toStringArray
public static String[] toStringArray(Collection c)
Formatsc
as aString
array.
-
format
public static String format(String text)
Formats special characters intext
. Replaces'\n', '\t', '\b', '\f', '\r'
intext
by"\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)
Formatsvalue
withnDec
decimals.- Parameters:
nDec
- maximum of number of decimals shown ifnDec > 0
else fixed number of decimals
-
parseDouble
public static double parseDouble(String text)
ReturnsDouble.parseDouble(java.lang.String)
, but (if necessary) returnsDouble.NaN
instead of throwing aNumberFormatException
.
-
repeat
public static String repeat(String text, int n)
Returns a String which containstext
repeatedn
times.
-
squeeze
public static String squeeze(String text, char chRemove)
Removes all occurrences ofchRemove
fromtext
.
-
squeeze
public static String squeeze(String text, String removeChars)
Removes all occurrences of the characters contained inremoveChars
fromtext
.
-
indexOfNotInString
public static int indexOfNotInString(String text, char chr)
Finds the index of the firstchr
intext
outside"..."
or'.'
.- See Also:
String.indexOf(int)
-
indexOfNotInString
public static int indexOfNotInString(String text, char chr, int fromIndex)
Finds the index of the firstchr
intext
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 firststr
intext
outside"..."
or'.'
.- See Also:
String.indexOf(String)
-
indexOfNotInString
public static int indexOfNotInString(String text, String str, int fromIndex)
Finds the index of the firststr
intext
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 ofold
intext
outside"..."
or'.'
byreplacement
.
-
getHeadTrail
public static String getHeadTrail(String delimString, int nHead, String separator, int nTrail, String text)
Extracts the firstnHead
and the lastnTrail
tokens delimited bydelimString
fromtext
. 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
, ifsleep
was interrupted
-
main
public static void main(String[] args)
Tests.
-
-