ehsbe CommonsTM
Version 1.1.0rc3

com.ehsbe.commons.util
Class Misc

java.lang.Object
  extended by com.ehsbe.commons.util.Misc

public class Misc
extends Object

Misc utilities.

Version:
$Revision: 484 $
Author:
Jens Elkner

Constructor Summary
Misc()
           
 
Method Summary
static String breakByLine(String s, Locale locale)
          Break a string into LF delimited lines using a line breaker.
static String breakBySentence(String s, Locale locale)
          Break a string into LF delimited lines by sentence.
static String breakByWord(String s, int maxCharsPerLine, Locale locale)
          Break a string into LF delimited lines by words, whereby the line is not longer than the given length per line.
static String breakByWord(String s, int maxCharsPerLine, Locale locale, char[] exclude)
          Break a string into LF delimited lines by words, whereby the line is not longer than the given length per line.
static int compareFpNumbers(Number n1, Number n2)
          Compare two floating point numbers wrt. their binary representation.
static int compareNumbers(Number n1, Number n2)
          Compare two numbers wrt. their long value.
static int compareStrings(String s1, String s2)
          Compares two Strings.
static boolean differentDates(Date oldValue, Date newValue)
          checks, whether two Dates are "equal".
static boolean differentStrings(Object oldValue, Object newValue)
          Checks, whether two Objects are "equal" wrt. to their String representation.
static boolean differentStrings(String oldValue, String newValue)
          checks, whether two Strings are "equal".
static boolean differentStrings2(String oldValue, String newValue)
          checks, whether two Strings are "equal".
static byte[] getBytes(InputStream is)
          Map the given iput stream into a byte array.
static Stack<Throwable> getCauseStack(Throwable t)
          Get the real cause for an exception.
static Stack<Throwable> getCauseStack(Throwable t, boolean needMsg)
          Get the real cause for an exception.
static TreeMap<String,String> getCountry2iso(Locale l)
          Get a country to ISO Code 2 mapping.
static String[] getCurrencyCodes()
          Get a list of available 3 letter ISO currency codes.
static String getExtension(File f)
          Get the extension of a file.
static ImageIcon getIcon(Object ref, String imagePath)
          Get the icon using the given image path relative to package name of the given object.
static ImageIcon getIcon(String resPath, String imagePath)
          Get the icon wrt. to the given resource and image path.
static TreeMap<String,Locale> getIso3locales()
          Get a map of available 3 letter ISO language codes and their associated locale.
static StringBuilder getLicenseInfo(VersionInfo info, boolean html, Locale locale)
          Get an overview wrt. to the given info.
static OutputStream getOutputStream(File path, boolean append, boolean gzip, boolean dirCreate, boolean dirDelete)
          Boilerplate: Get a writable output stream for the given file.
static String getResourcePath(Object ref)
          Construct the resource path for the given object, i.e. it package name + "/res/".
static Throwable getRootCause(Throwable t)
          Get the root cause for a throwable.
static Throwable getRootCause(Throwable t, boolean needMsg)
          Get the root cause for a throwable.
static String getTooltip(String key, ResourceBundle i18n, int maxCharsPerLine, Locale locale)
          Lookup the value for the given key in the given Resourcebundle, replace all linefeeds with <br> and make sure, that no line is longer than the given number of characters.
static String[] localize(Throwable t, Locale locale)
          Try to localize the given throwable wrt. the given locale.
static String md5base64(byte[] msg)
          Hash the given String as MD5 and encode the result as Base64.
static String md5base64(char[] msg)
          Hash the given String as MD5 and encode the result as Base64.
static String md5base64(String msg)
          Hash the given String as MD5 and encode the result as Base64.
static String normalize(String s)
          Normalize a String.
static String normalizeNull(String s)
          Normalize a String to null, if it is an empty String (i.e.
static String toCssHex(Color c)
          Convert the given color to a CSS2 style hex color string (i.e. #001122).
static boolean writableDir(File path, boolean create, boolean delete)
          Check, whether the given directory is writable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Misc

public Misc()
Method Detail

getIcon

public static final ImageIcon getIcon(Object ref,
                                      String imagePath)
Get the icon using the given image path relative to package name of the given object.

Parameters:
ref - object to use for obtaining the appropriate resource path
imagePath - relative path of the image wrt. to refs resource path
Returns:
null if not found, the icon otherwise.
See Also:
getIcon(String, String)

getIcon

public static final ImageIcon getIcon(String resPath,
                                      String imagePath)
Get the icon wrt. to the given resource and image path.

Parameters:
resPath - resource base directory. Ignored if null. If not null, it has to end with a /.
imagePath - relative path of the image wrt. the given resource base directory.
Returns:
null if not found, the icon otherwise.

getResourcePath

public static final String getResourcePath(Object ref)
Construct the resource path for the given object, i.e. it package name + "/res/".

Parameters:
ref - a class or object, which should be used as reference
Returns:
/res/ if ref is null, the resource path otherwise.

getCauseStack

public static final Stack<Throwable> getCauseStack(Throwable t)
Get the real cause for an exception.

Parameters:
t - throwable top analyze
Returns:
the cause stack of the throwable with the root cause on top. Is empty, if the argument is null.

getCauseStack

public static final Stack<Throwable> getCauseStack(Throwable t,
                                                   boolean needMsg)
Get the real cause for an exception.

Parameters:
t - throwable top analyze
needMsg - if true, the all cause from the stack gets removed, 'til a cause has a non-null message or the stack size of 1 has been reached.
Returns:
the cause stack of the throwable with the root cause on top. Is empty, if the argument is null.

getRootCause

public static final Throwable getRootCause(Throwable t)
Get the root cause for a throwable.

Parameters:
t - the throwable caught
Returns:
the the root cause of the throwable or the throwble itself, it doesn't have a cause set.
See Also:
getCauseStack(Throwable, boolean)

getRootCause

public static final Throwable getRootCause(Throwable t,
                                           boolean needMsg)
Get the root cause for a throwable.

Parameters:
t - the throwable caught
needMsg - if true, the deepest cause with a non-null message gets returned.
Returns:
the the root cause of the throwable or the throwble itself, it doesn't have a cause set.
See Also:
getCauseStack(Throwable, boolean)

localize

public static final String[] localize(Throwable t,
                                      Locale locale)
Try to localize the given throwable wrt. the given locale.

Parameters:
t - throwable to localize
locale - locale to use. if null, the default locale will be used.
Returns:
an array[2], which might have null values if the given throwable is null. [0] contains the localized error message if available (otherwise the original localized message), [1] a possible explanation

breakBySentence

public static final String breakBySentence(String s,
                                           Locale locale)
Break a string into LF delimited lines by sentence.

Parameters:
s - string to break
locale - locale to use. If null, default locale is used
Returns:
a newly allocated String

breakByLine

public static final String breakByLine(String s,
                                       Locale locale)
Break a string into LF delimited lines using a line breaker.

Parameters:
s - string to break
locale - locale to use. If null, default locale is used
Returns:
a newly allocated String

breakByWord

public static final String breakByWord(String s,
                                       int maxCharsPerLine,
                                       Locale locale)
Break a string into LF delimited lines by words, whereby the line is not longer than the given length per line.

Parameters:
s - string to break
maxCharsPerLine - max. number of characters in a line.
locale - locale to use. If null, default locale is used
Returns:
a newly allocate String

breakByWord

public static final String breakByWord(String s,
                                       int maxCharsPerLine,
                                       Locale locale,
                                       char[] exclude)
Break a string into LF delimited lines by words, whereby the line is not longer than the given length per line.

Parameters:
s - string to break
maxCharsPerLine - max. number of characters in a line.
locale - locale to use. If null, default locale is used
exclude - an optional set of characters, which should be considered not being a word boundery. Ignored if null.
Returns:
a newly allocate String

getTooltip

public static final String getTooltip(String key,
                                      ResourceBundle i18n,
                                      int maxCharsPerLine,
                                      Locale locale)
Lookup the value for the given key in the given Resourcebundle, replace all linefeeds with <br> and make sure, that no line is longer than the given number of characters.

Parameters:
key - key to use for looking up the text to format
i18n - resource bundle to use
maxCharsPerLine - max number of characters per line
locale - locale to use for linebreaking
Returns:
a html string, if linebreaks are required, plain text otherwise.

differentStrings

public static final boolean differentStrings(String oldValue,
                                             String newValue)
checks, whether two Strings are "equal". In the contrary to the String#equals method, null Strings are handled correct without throwing NullPointerExceptions.

Parameters:
oldValue - the old Value
newValue - the new Value
Returns:
false if both Strings are null or equal, true otherwise.

differentStrings2

public static final boolean differentStrings2(String oldValue,
                                              String newValue)
checks, whether two Strings are "equal". In the contrary to the String#equals method, null Strings are handled as empty Strings.

Parameters:
oldValue - the old Value
newValue - the new Value
Returns:
false if both Strings are null or equal, true otherwise.

compareStrings

public static final int compareStrings(String s1,
                                       String s2)
Compares two Strings. In the contrary to the String#compare method, null Strings are handled correct without throwing NullPointerExceptions.

Parameters:
s1 - first string
s2 - second string
Returns:
the value 0 if s2 is equal to to s1; a value less than 0 if s1 is lexicographically less than s2; and a value greater than 0 if s1 is lexicographically greater than s2.

differentStrings

public static final boolean differentStrings(Object oldValue,
                                             Object newValue)
Checks, whether two Objects are "equal" wrt. to their String representation.

Parameters:
oldValue - the old Value
newValue - the new Value
Returns:
false if both Strings are null or equal.
See Also:
differentStrings(String, String)

differentDates

public static final boolean differentDates(Date oldValue,
                                           Date newValue)
checks, whether two Dates are "equal". In the contrary to the Date#equals method, null Dates are handled correct without throwing NullPointerExceptions.

Parameters:
oldValue - the old Value
newValue - the new Value
Returns:
false if both Dates are null or equal, true otherwise.

compareNumbers

public static final int compareNumbers(Number n1,
                                       Number n2)
Compare two numbers wrt. their long value. Per definition, a null Number is always less than a non-null Number. Should not be used for floating point numbers, since rounding to none-floating counter parts will lead to wrong results.

Parameters:
n1 - number to compare
n2 - number to compare
Returns:
0 if both numbers are null or represent the same long value. Otherwise if number n1 is less than n2 a negative value, otherwise a positive value.

compareFpNumbers

public static int compareFpNumbers(Number n1,
                                   Number n2)
Compare two floating point numbers wrt. their binary representation. Per definition, a null Number is always less than a non-null Number. Should not be used for none-floating point numbers.

Parameters:
n1 - number to compare
n2 - number to compare
Returns:
0 if both numbers are null or have the same binary representation. Otherwise if number n1 is less than n2 a negative value, otherwise a positive value.

normalize

public static final String normalize(String s)
Normalize a String.

Parameters:
s - string to normalize
Returns:
an empty String of length 0, if the given s is null, the trimmed String otherwise.

normalizeNull

public static final String normalizeNull(String s)
Normalize a String to null, if it is an empty String (i.e. consists of whitespaces only). Otherwise just trim the given String.

Parameters:
s - string to normalize
Returns:
null if s is an empty string, the trimmed string otherwise.

getCountry2iso

public static final TreeMap<String,String> getCountry2iso(Locale l)
Get a country to ISO Code 2 mapping.

NOTE: It may take some time to construct this mapping ...

Parameters:
l - locale to use for the country string. If null, the default locale gets used.
Returns:
a (possible empty) map.

getIso3locales

public static final TreeMap<String,Locale> getIso3locales()
Get a map of available 3 letter ISO language codes and their associated locale.

NOTE: It may take some time to construct this mapping ...

Returns:
always a non-null map.

getCurrencyCodes

public static final String[] getCurrencyCodes()
Get a list of available 3 letter ISO currency codes.

NOTE: It may take some time to construct this mapping ...

Returns:
always a non-null list.

getExtension

public static String getExtension(File f)
Get the extension of a file.

Parameters:
f - file, where to extract the extension
Returns:
null if the file has no extension, the extension otherwise.

getBytes

public static byte[] getBytes(InputStream is)
Map the given iput stream into a byte array.

Parameters:
is - input stream to read
Returns:
an empty array, if an error occurs, the input stream as byte array otherwsie.

writableDir

public static final boolean writableDir(File path,
                                        boolean create,
                                        boolean delete)
Check, whether the given directory is writable.

Parameters:
path - directory to check
create - if true, create the directory incl. missing parents if it does not already exist.
delete - if true and the given path exists but is not a directory, deletes the path and creates the given directory
Returns:
true, if the given path is a writable directory.

getOutputStream

public static final OutputStream getOutputStream(File path,
                                                 boolean append,
                                                 boolean gzip,
                                                 boolean dirCreate,
                                                 boolean dirDelete)
Boilerplate: Get a writable output stream for the given file.

Parameters:
path - write destination
append - if true append to the file, if it exists.
gzip - if true create a GZIP output stream
dirCreate - if true, create missing parent directories
dirDelete - if true delete and recreat the parent path, if it is not a directory
Returns:
null if no output stream could be created, the stream otherwise.

toCssHex

public static final String toCssHex(Color c)
Convert the given color to a CSS2 style hex color string (i.e. #001122).

Parameters:
c - color to convert
Returns:
null if the given color is null, the converted rgb color string otherwise.

md5base64

public static String md5base64(String msg)
Hash the given String as MD5 and encode the result as Base64.

Parameters:
msg - message to hash and encode. If null, an empty String will be used instead.
Returns:
null on error, the encoded String otherwise.

md5base64

public static String md5base64(char[] msg)
Hash the given String as MD5 and encode the result as Base64.

Characters are converted using the UTF-8 charset.

Parameters:
msg - message to hash and encode. If null, an empty String will be used instead.
Returns:
null on error, the encoded String otherwise.

md5base64

public static String md5base64(byte[] msg)
Hash the given String as MD5 and encode the result as Base64.

Parameters:
msg - message to hash and encode. If null, an empty String will be used instead.
Returns:
null on error, the encoded String otherwise.

getLicenseInfo

public static StringBuilder getLicenseInfo(VersionInfo info,
                                           boolean html,
                                           Locale locale)
Get an overview wrt. to the given info.

Parameters:
info - info, which provides the details to format.
html - if true format as html, as plain text otherwise.
locale - preferred locale (language) to use.
Returns:
always the license overview, which might be empty if no infos are available.

ehsbe CommonsTM
Version 1.1.0rc3

Copyright ©2005-2009 EHSBE, Walther-Rathenau-Str. 58, 39104 Magdeburg, Sachsen-Anhalt, Germany. All Rights Reserved.