org.apache.empire.commons
Class ObjectUtils

java.lang.Object
  extended by org.apache.empire.commons.ObjectUtils

public final class ObjectUtils
extends Object

This class contains common functions for comparing and converting values of type Object.


Field Summary
static org.apache.empire.commons.ObjectUtils.NoValue NO_VALUE
          Constant that defines a object of type NoValue.
 
Method Summary
static
<T> T
coalesce(T preferred, T alternative)
          Checks whether a preferred value is valid and returns an alternative value if not.
static boolean compareEqual(Object o1, Object o2)
          Compares two objects for equality
static
<T> boolean
contains(T[] array, T item)
          returns wheter or not a array contains a certain item
static
<T> List<T>
convert(Class<T> t, Collection<? extends T> source)
          Generic conversion function that will convert a list to another list type.
static
<T> T
convert(Class<T> c, Object v)
          Generic conversion function that will convert a object to another value type.
static String formatDate(Date date, boolean withTime)
          Formats a given date object to a standard date string.
static boolean getBoolean(Object v)
          Converts an object value to a boolean.
static Date getDate(Object v)
          Converts an object value to a Date.
static Date getDate(Object v, Locale locale)
          Converts an object value to a Date.
static BigDecimal getDecimal(Object v)
          Converts an object value to a BigDecimal.
static BigDecimal getDecimal(Object v, BigDecimal defValue)
          Converts an object value to a BigDecimal.
static double getDouble(Object v)
          Converts an object value to a double.
static double getDouble(Object v, double defValue)
          Converts an object value to a double.
static int getInteger(Object v)
          Converts an object value to an integer.
static int getInteger(Object v, int defValue)
          Converts an object value to an integer.
static long getLong(Object v)
          Converts an object value to a long.
static long getLong(Object v, long defValue)
          Converts an object value to a long.
static boolean isAssignmentCompatible(Class<?> target, Class<?> source)
           
static boolean isEmpty(Object o)
          Checks whether an object has no value.
static BigDecimal toDecimal(Object v)
          converts an object to a decimal.
static double toDouble(Object v)
          converts an object to a double.
static int toInteger(Object v)
          converts an object to an integer.
static long toLong(Object v)
          converts an object to a long.
static String[] toStringArray(Object[] objArray, String defValue)
          Converts an Object array to a String array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_VALUE

public static final org.apache.empire.commons.ObjectUtils.NoValue NO_VALUE
Constant that defines a object of type NoValue. This may be used in cases where the value of null is a valid value.

Method Detail

isEmpty

public static boolean isEmpty(Object o)
Checks whether an object has no value. A Object is empty if and only if the Object is null or if its an empty string.

Parameters:
o - the object to check
Returns:
true if the Object is null or if its an empty string.

compareEqual

public static boolean compareEqual(Object o1,
                                   Object o2)
Compares two objects for equality

Parameters:
o1 - the first object
o2 - the second object
Returns:
true if both objects are equal or false otherwise

coalesce

public static <T> T coalesce(T preferred,
                             T alternative)
Checks whether a preferred value is valid and returns an alternative value if not.

Type Parameters:
T - the type of the values
Parameters:
preferred - the preferred return value
alternative - the alternative return value used if the preferred value is null
Returns:
the preferred value if it is not null or the alternative value otherwise

toInteger

public static int toInteger(Object v)
converts an object to an integer. If conversion is not possible, an error is thrown

Parameters:
v - the value to convert
Returns:
the integer value

getInteger

public static int getInteger(Object v,
                             int defValue)
Converts an object value to an integer.

If the object value supplied is null or if conversion is not possible then the default value is returned.

Parameters:
v - the obect to convert
defValue - the default value if o is null or conversion is not possible
Returns:
the Integer value of o or a default value

getInteger

public static int getInteger(Object v)
Converts an object value to an integer.

If the object value supplied is null or if conversion is not possible then 0 is returned.

Parameters:
v - the object value to convert
Returns:
the Integer value of o or 0

toLong

public static long toLong(Object v)
converts an object to a long. If conversion is not possible, an error is thrown

Parameters:
v - the value to convert
Returns:
the long value

getLong

public static long getLong(Object v,
                           long defValue)
Converts an object value to a long.

If the object value supplied is null or if conversion is not possible then the default value is returned.

Parameters:
v - the obect to convert
defValue - the default value if o is null or conversion is not possible
Returns:
the Integer value of o or a default value

getLong

public static long getLong(Object v)
Converts an object value to a long.

If the object value supplied is null or if conversion is not possible then 0 is returned.

Parameters:
v - the object value to convert
Returns:
the Long value of o or 0

toDouble

public static double toDouble(Object v)
converts an object to a double. If conversion is not possible, an error is thrown

Parameters:
v - the value to convert
Returns:
the double value

getDouble

public static double getDouble(Object v,
                               double defValue)
Converts an object value to a double.

If the object value supplied is null or if conversion is not possible then defValue is returned.

Parameters:
v - the object value to convert
defValue - the default value
Returns:
the Long value of o or defValue

getDouble

public static double getDouble(Object v)
Converts an object value to a double.

If the object value supplied is null or if conversion is not possible then 0.0 is returned.

Parameters:
v - the object value to convert
Returns:
the Long value of o or 0

toDecimal

public static BigDecimal toDecimal(Object v)
converts an object to a decimal. If conversion is not possible, an error is thrown

Parameters:
v - the value to convert
Returns:
the decimal value

getDecimal

public static BigDecimal getDecimal(Object v,
                                    BigDecimal defValue)
Converts an object value to a BigDecimal.

If the object value supplied is null or if conversion is not possible then defValue is returned.

Parameters:
v - the object value to convert
defValue - the default value
Returns:
the BigDecimal value of v or defValue

getDecimal

public static BigDecimal getDecimal(Object v)
Converts an object value to a BigDecimal.

If the object value supplied is null or if conversion is not possible then 0.0 is returned.

Parameters:
v - the object value to convert
Returns:
the Long value of o or 0

getBoolean

public static boolean getBoolean(Object v)
Converts an object value to a boolean.

If the object value supplied is null or if conversion is not possible then false is returned.

Parameters:
v - the object to convert
Returns:
the boolean value of o (true or false)

getDate

public static Date getDate(Object v,
                           Locale locale)
Converts an object value to a Date.

If the object value supplied is null or if conversion is not possible then null is returned.

Parameters:
v - the object to convert
locale - the locale used for conversion
Returns:
the Date value of o or null

getDate

public static Date getDate(Object v)
Converts an object value to a Date.

Parameters:
v - the object to convert
Returns:
the Date value of o or null

formatDate

public static String formatDate(Date date,
                                boolean withTime)
Formats a given date object to a standard date string. The date string is locale independent and has the follwowing format: "yyyy-MM-dd hh:mm:ss"

Parameters:
date - the date to be formated
withTime - indicates whether the date string should include the time or not
Returns:
the date string

convert

public static <T> T convert(Class<T> c,
                            Object v)
                 throws ClassCastException
Generic conversion function that will convert a object to another value type.

Type Parameters:
T - the type to convert to
Parameters:
c - the class type to convert to
v - the object to convert
Returns:
the Date value of o or null
Throws:
ClassCastException - if the object is not null and is not assignable to the type T.

isAssignmentCompatible

public static boolean isAssignmentCompatible(Class<?> target,
                                             Class<?> source)

convert

public static <T> List<T> convert(Class<T> t,
                                  Collection<? extends T> source)
Generic conversion function that will convert a list to another list type.

Type Parameters:
T - the type of elements
Parameters:
t - the type class
source - the source collection
Returns:
the new list type

toStringArray

public static String[] toStringArray(Object[] objArray,
                                     String defValue)
Converts an Object array to a String array.

Parameters:
objArray - the object array to convert
defValue - default value which will be set for all null objects
Returns:
the String array

contains

public static <T> boolean contains(T[] array,
                                   T item)
returns wheter or not a array contains a certain item

Type Parameters:
T - the ype of the objects in the array
Parameters:
array - the array to search
item - the item to search for
Returns:
true if the array contains the item or false otherwise


Copyright © 2008–2014 Apache Software Foundation. All rights reserved.