org.ckkloverdos.util
Class Util

java.lang.Object
  extended by org.ckkloverdos.util.Util

public final class Util
extends java.lang.Object

General utility methods.

Author:
Christos KK Loverdos

Field Summary
static java.lang.Object[] EMPTY_ARRAY
          Empty object array
static java.util.Random RANDOM
          A utility random instance.
static java.text.SimpleDateFormat STD_DATE_FMT
          The default date format
 
Method Summary
static java.lang.Object[] arrayFromItem(java.lang.Object object)
          Returns either an empty array or a one-item array containing the given object.
static java.lang.Object[] arrayFromItem(java.lang.Object component, java.lang.Class componentType)
          Returns either an empty array or a one-item array containing the given component.
static boolean emptySafe(java.util.Collection collection)
          Checks if the provided collection is empty.
static boolean emptySafe(IL list)
          Checks if the provided list is empty.
static boolean emptySafe(java.lang.Object[] array)
          Checks if the provided array is empty.
static boolean emptySafe(java.lang.String s)
          Checks if the provided string is empty.
static boolean equalSafe(java.lang.Object a, java.lang.Object b)
          Checks if the two objects are equal by also taking into account that one of them (or even both) may be null.
static java.lang.String getenv(java.lang.String name)
          Returns the environment variable of the given name.
static java.lang.String getEnvOrProperty(java.lang.String name)
          Tries to obtain name either as an environment variable or as a system property.
static java.lang.String getFormattedDate()
          Formats the current date by using the default date format.
static java.lang.String getFormattedDate(java.text.SimpleDateFormat sdf)
          Formats the current date by using the given SimpleDateFormat.
static java.lang.String getLocalHost()
          Returns the localhost name.
static java.lang.String getProperty(java.lang.String name)
          A wrapper call around System.getProperty(String).
static java.lang.String getPropertyOrEnv(java.lang.String name)
          Tries to obtain name either as a system property or as an environment variable.
static java.util.Random getRandom()
          Creates a new Random instance, preferably of the SHA1PRNG algorithm.
static java.util.Random getRandom(java.lang.String algorithm)
          Creates a new Random instance, of the algorithm provided.
static java.lang.String getStackTrace(java.lang.Throwable throwable)
          Returns the string representation of the stacktrace provided by throwable.
static boolean isInteger(java.lang.String s)
          Returns true iff s can be parsed correctly by Integer.parseInt(s).
static boolean isLong(java.lang.String s)
          Returns true iff s can be parsed correctly by Long.parseLong(String).
static java.lang.ClassLoader[] safe(java.lang.ClassLoader[] array)
          Returns array "safe" value for array.
static java.io.File[] safe(java.io.File[] array)
          Returns array "safe" value for array.
static java.lang.Object[] safe(java.lang.Object[] array, java.lang.Class componentType)
          Returns a "safe" value for array of the given componentType.
static java.util.Set safe(java.util.Set set)
          Returns a "safe" value for set.
static java.lang.String safe(java.lang.String s)
          Returns a "safe" value for s.
static java.lang.String[] safe(java.lang.String[] array)
          Returns array "safe" value for array.
static double safe(java.lang.String s, double safe)
          Returns a "safe" value of the double represented by String s.
static int safe(java.lang.String s, int safe)
          Returns a "safe" value of the int represented by String s.
static long safe(java.lang.String s, long safe)
          Returns a "safe" value of the long represented by String s.
static java.net.URL toURL(java.lang.String s)
          Tries to convert s to a URL and ignores a possible MalformedURLException.
static boolean trimmedEmpty(java.lang.String s)
          Checks if the provided string is empty, after it is trimmed.
static boolean trimmedEmptySafe(java.lang.String s)
          Checks if the provided string is empty, after it is trimmed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ARRAY

public static final java.lang.Object[] EMPTY_ARRAY
Empty object array


RANDOM

public static final java.util.Random RANDOM
A utility random instance.


STD_DATE_FMT

public static final java.text.SimpleDateFormat STD_DATE_FMT
The default date format

Method Detail

getFormattedDate

public static java.lang.String getFormattedDate(java.text.SimpleDateFormat sdf)
Formats the current date by using the given SimpleDateFormat.

Parameters:
sdf -
Returns:
the formatted date.

getFormattedDate

public static java.lang.String getFormattedDate()
Formats the current date by using the default date format.

Returns:
the formatted date.

getRandom

public static java.util.Random getRandom()
Creates a new Random instance, preferably of the SHA1PRNG algorithm. If this is not possible, then just a new Random() is returned.

Returns:
the created Random.

getRandom

public static java.util.Random getRandom(java.lang.String algorithm)
Creates a new Random instance, of the algorithm provided. If this is not possible, then just a new Random() is returned.

Parameters:
algorithm - the desired algorithm.
Returns:
the created Random.

trimmedEmpty

public static boolean trimmedEmpty(java.lang.String s)
Checks if the provided string is empty, after it is trimmed. In case the string may be null, then you should consider using trimmedEmptySafe(String).

Parameters:
s - the string to be checked.
Returns:
true iff the following condition is true: 0 == s.trim().length()

getLocalHost

public static java.lang.String getLocalHost()
Returns the localhost name. If this cannot be determined, then localhost is returned.

Returns:
the name of localhost.

getStackTrace

public static java.lang.String getStackTrace(java.lang.Throwable throwable)
Returns the string representation of the stacktrace provided by throwable. In effect, the following algorithm is applied:
   StringWriter w = new StringWriter();
   PrintWriter pw = new PrintWriter(w);
   throwable.printStackTrace(pw);
   return w.toString();
 

Parameters:
throwable -
Returns:
the stacktrace in string form.

toURL

public static java.net.URL toURL(java.lang.String s)
Tries to convert s to a URL and ignores a possible MalformedURLException.

Parameters:
s -
Returns:
the string as a URL

getPropertyOrEnv

public static java.lang.String getPropertyOrEnv(java.lang.String name)
Tries to obtain name either as a system property or as an environment variable. First, it tries to obtain name by using System.getProperty(name). If this returns null and we are under Java 1.5+, then it uses System.getenv(name).

Parameters:
name -
Returns:
The system property or environment variable with the given name.

getEnvOrProperty

public static java.lang.String getEnvOrProperty(java.lang.String name)
Tries to obtain name either as an environment variable or as a system property. First, if we are under Java version < 1.5, it just returns System.getProperty(name). Otherwise, if we are under Java version >= 1.5, it first tries to obtain name by using System.getenv(name). If this returns null it returns System.getProperty(name).

Parameters:
name -
Returns:
The environment variable or system property with the given name.

isInteger

public static boolean isInteger(java.lang.String s)
Returns true iff s can be parsed correctly by Integer.parseInt(s).

Parameters:
s -
Returns:
true iff s contains an integer.

isLong

public static boolean isLong(java.lang.String s)
Returns true iff s can be parsed correctly by Long.parseLong(String).

Parameters:
s -
Returns:
true iff s contains a long.

getenv

public static java.lang.String getenv(java.lang.String name)
Returns the environment variable of the given name. Since System.getenv(String) is deprecated for java 1.4, if we are in a 1.4 VM then System.getProperty(String) is called, otherwise System.getenv(String) is called.

Parameters:
name -
Returns:
the environment variable.

getProperty

public static java.lang.String getProperty(java.lang.String name)
A wrapper call around System.getProperty(String).

Parameters:
name -
Returns:
the system property name.

equalSafe

public static boolean equalSafe(java.lang.Object a,
                                java.lang.Object b)
Checks if the two objects are equal by also taking into account that one of them (or even both) may be null. This is useful in situations where we are not certain of the contents of the two objects and we need to avoid NullPointerExceptions.

Parameters:
a -
b -
Returns:
true iff a and b are equal.

emptySafe

public static boolean emptySafe(java.lang.String s)
Checks if the provided string is empty. A null string is also considered empty.

Parameters:
s - the string to be checked.
Returns:
true iff the following condition is true: s == null || 0 == s.length()

trimmedEmptySafe

public static boolean trimmedEmptySafe(java.lang.String s)
Checks if the provided string is empty, after it is trimmed. A null string is also considered empty.

Parameters:
s - the string to be checked.
Returns:
true iff the following condition is true: s == null || 0 == s.trim().length()

safe

public static java.lang.String safe(java.lang.String s)
Returns a "safe" value for s. If s is null, then the empty string "" is returned.

Parameters:
s -
Returns:
s if it is not null, otherwise "".

safe

public static java.lang.Object[] safe(java.lang.Object[] array,
                                      java.lang.Class componentType)
Returns a "safe" value for array of the given componentType. If array is null, then an empty Object[] array is returned.

Parameters:
array -
componentType -
Returns:
array if it is not null, otherwise an empty Object[] array.

safe

public static java.util.Set safe(java.util.Set set)
Returns a "safe" value for set. If set is null, then an empty Set is returned.

Parameters:
set -
Returns:
set if it is not null, otherwise an empty Set.

arrayFromItem

public static java.lang.Object[] arrayFromItem(java.lang.Object object)
Returns either an empty array or a one-item array containing the given object. If object is null, then an empty array is returned. Otherwise, an array of type object.getJavaClass() and length 1 is created and filled with object.

Parameters:
object -
Returns:
the computed array.

arrayFromItem

public static java.lang.Object[] arrayFromItem(java.lang.Object component,
                                               java.lang.Class componentType)
Returns either an empty array or a one-item array containing the given component. The array component type is determined by componentType. If component is null, then an empty array is returned.

After succesfull return, the client can safely cast the return value to the proper array type.

Parameters:
component -
componentType -
Returns:
the computed array
Throws:
java.lang.NullPointerException - if componentType is null.

emptySafe

public static boolean emptySafe(java.lang.Object[] array)
Checks if the provided array is empty. A null value for array is also considered as an empty array.

Parameters:
array - the array to be checked.
Returns:
true iff the following condition is true: null == array ? true : 0 == array.length

safe

public static java.lang.String[] safe(java.lang.String[] array)
Returns array "safe" value for array. If array is null, then an empty String[] array is returned.

Parameters:
array -
Returns:
array if it is not null, otherwise an empty String[] array.

safe

public static java.io.File[] safe(java.io.File[] array)
Returns array "safe" value for array. If array is null, then an empty File[] array is returned.

Parameters:
array -
Returns:
array if it is not null, otherwise an empty File[] array.

safe

public static java.lang.ClassLoader[] safe(java.lang.ClassLoader[] array)
Returns array "safe" value for array. If array is null, then an empty ClassLoader[] array is returned.

Parameters:
array -
Returns:
array if it is not null, otherwise an empty ClassLoader[] array.

emptySafe

public static boolean emptySafe(java.util.Collection collection)
Checks if the provided collection is empty. A null value for collection is also considered as an empty collection.

Parameters:
collection - the collection to be checked.
Returns:
true iff it is safe to consider collection as empty.

emptySafe

public static boolean emptySafe(IL list)
Checks if the provided list is empty. A null value for list is also considered as an empty list.

Parameters:
list -
Returns:
true iff it is safe to consider list as empty.

safe

public static int safe(java.lang.String s,
                       int safe)
Returns a "safe" value of the int represented by String s. If s cannot be parsed correctly as an int, then the value safe is returned.

Parameters:
s - the string representation of the int.
safe - the safe value for the int.
Returns:
a safe value for the int provided in String s.

safe

public static long safe(java.lang.String s,
                        long safe)
Returns a "safe" value of the long represented by String s. If s cannot be parsed correctly as an long, then the value safe is returned.

Parameters:
s - the string representation of the long.
safe - the safe value for the long.
Returns:
a safe value for the long provided in String s.

safe

public static double safe(java.lang.String s,
                          double safe)
Returns a "safe" value of the double represented by String s. If s cannot be parsed correctly as an double, then the value safe is returned.

Parameters:
s - the string representation of the double.
safe - the safe value for the double.
Returns:
a safe value for the double provided in String s.


Copyright © 1999-2007 Christos KK Loverdos. All Rights Reserved.