|
ehsbe CommonsTM Version 1.1.0rc3 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.ehsbe.commons.util.Misc
public class Misc
Misc utilities.
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 |
---|
public Misc()
Method Detail |
---|
public static final ImageIcon getIcon(Object ref, String imagePath)
ref
- object to use for obtaining the appropriate resource pathimagePath
- relative path of the image wrt. to refs resource
path
null
if not found, the icon otherwise.getIcon(String, String)
public static final ImageIcon getIcon(String resPath, String imagePath)
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.
null
if not found, the icon otherwise.public static final String getResourcePath(Object ref)
+ "/res/"
.
ref
- a class or object, which should be used as reference
/res/
if ref is null
,
the resource path otherwise.public static final Stack<Throwable> getCauseStack(Throwable t)
t
- throwable top analyze
null
.public static final Stack<Throwable> getCauseStack(Throwable t, boolean needMsg)
t
- throwable top analyzeneedMsg
- 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.
null
.public static final Throwable getRootCause(Throwable t)
t
- the throwable caught
getCauseStack(Throwable, boolean)
public static final Throwable getRootCause(Throwable t, boolean needMsg)
t
- the throwable caughtneedMsg
- if true
, the deepest cause with a non-null
message gets returned.
getCauseStack(Throwable, boolean)
public static final String[] localize(Throwable t, Locale locale)
t
- throwable to localizelocale
- locale to use. if null
, the default locale
will be used.
null
values if
the given throwable is null
.
[0] contains the localized error message if available (otherwise
the original localized message),
[1] a possible explanationpublic static final String breakBySentence(String s, Locale locale)
s
- string to breaklocale
- locale to use. If null
, default locale is used
public static final String breakByLine(String s, Locale locale)
s
- string to breaklocale
- locale to use. If null
, default locale is used
public static final String breakByWord(String s, int maxCharsPerLine, Locale locale)
s
- string to breakmaxCharsPerLine
- max. number of characters in a line.locale
- locale to use. If null
, default locale is used
public static final String breakByWord(String s, int maxCharsPerLine, Locale locale, char[] exclude)
s
- string to breakmaxCharsPerLine
- max. number of characters in a line.locale
- locale to use. If null
, default locale is usedexclude
- an optional set of characters, which should be considered
not being a word boundery. Ignored if null
.
public static final String getTooltip(String key, ResourceBundle i18n, int maxCharsPerLine, Locale locale)
key
- key to use for looking up the text to formati18n
- resource bundle to usemaxCharsPerLine
- max number of characters per linelocale
- locale to use for linebreaking
public static final boolean differentStrings(String oldValue, String newValue)
null
Strings are handled correct
without throwing NullPointerExceptions.
oldValue
- the old ValuenewValue
- the new Value
false
if both Strings are null
or
equal, true
otherwise.public static final boolean differentStrings2(String oldValue, String newValue)
null
Strings are handled as empty
Strings.
oldValue
- the old ValuenewValue
- the new Value
false
if both Strings are null
or
equal, true
otherwise.public static final int compareStrings(String s1, String s2)
null
Strings are handled correct without throwing
NullPointerExceptions.
s1
- first strings2
- second string
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.public static final boolean differentStrings(Object oldValue, Object newValue)
oldValue
- the old ValuenewValue
- the new Value
false
if both Strings are null
or
equal.differentStrings(String, String)
public static final boolean differentDates(Date oldValue, Date newValue)
null
Dates are handled correct without throwing
NullPointerExceptions.
oldValue
- the old ValuenewValue
- the new Value
false
if both Dates are null
or
equal, true
otherwise.public static final int compareNumbers(Number n1, Number n2)
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.
n1
- number to comparen2
- number to compare
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.public static int compareFpNumbers(Number n1, Number n2)
null
Number is always less than a
non-null
Number. Should not be used for none-floating point
numbers.
n1
- number to comparen2
- number to compare
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.public static final String normalize(String s)
s
- string to normalize
null
, the trimmed String otherwise.public static final String normalizeNull(String s)
null
, if it is an empty String (i.e.
consists of whitespaces only). Otherwise just trim the given String.
s
- string to normalize
null
if s is an empty string, the trimmed
string otherwise.public static final TreeMap<String,String> getCountry2iso(Locale l)
NOTE: It may take some time to construct this mapping ...
l
- locale to use for the country string. If null
,
the default locale gets used.
public static final TreeMap<String,Locale> getIso3locales()
NOTE: It may take some time to construct this mapping ...
null
map.public static final String[] getCurrencyCodes()
NOTE: It may take some time to construct this mapping ...
null
list.public static String getExtension(File f)
f
- file, where to extract the extension
null
if the file has no extension, the extension
otherwise.public static byte[] getBytes(InputStream is)
is
- input stream to read
public static final boolean writableDir(File path, boolean create, boolean delete)
path
- directory to checkcreate
- 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
true
, if the given path is a writable directory.public static final OutputStream getOutputStream(File path, boolean append, boolean gzip, boolean dirCreate, boolean dirDelete)
path
- write destinationappend
- if true
append to the file, if it exists.gzip
- if true
create a GZIP output streamdirCreate
- if true
, create missing parent directoriesdirDelete
- if true
delete and recreat the parent path,
if it is not a directory
null
if no output stream could be created, the
stream otherwise.public static final String toCssHex(Color c)
c
- color to convert
null
if the given color is null
,
the converted rgb color string otherwise.public static String md5base64(String msg)
msg
- message to hash and encode. If null
, an empty
String will be used instead.
null
on error, the encoded String otherwise.public static String md5base64(char[] msg)
Characters are converted using the UTF-8 charset.
msg
- message to hash and encode. If null
, an empty
String will be used instead.
null
on error, the encoded String otherwise.public static String md5base64(byte[] msg)
msg
- message to hash and encode. If null
, an empty
String will be used instead.
null
on error, the encoded String otherwise.public static StringBuilder getLicenseInfo(VersionInfo info, boolean html, Locale locale)
info
- info, which provides the details to format.html
- if true
format as html, as plain text otherwise.locale
- preferred locale (language) to use.
|
ehsbe CommonsTM Version 1.1.0rc3 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |