com.gentics.api.lib.etc
Class ObjectTransformer

java.lang.Object
  extended by com.gentics.api.lib.etc.ObjectTransformer

public final class ObjectTransformer
extends Object

Helper class to transform objects into specific types (classes).


Nested Class Summary
static class ObjectTransformer.InputStreamReaderRunnable
          Helper class that extends Runnable to read data from an input stream in a separate thread
 
Field Summary
protected static org.apache.log4j.Logger logger
          the logger
 
Method Summary
static int compareObjects(Comparable first, Comparable second, boolean nullfirst)
          Compare objects, care also for null objects
static byte[] decodeBinary(String encoded)
          Decode the encoded binary data (hexcode, leading zeros, all uppercase letters)
static String encodeBinary(byte[] binaryData)
          Encode the given binary data into hexcode (leading zeros, all uppercase letters)
static byte[] getBinary(Object o, byte[] defaultValue)
          Get the object as binary value (byte array)
static boolean getBoolean(Object o, boolean defaultValue)
          Get the boolean value of the given object.
true, 1, yes and on will be interpreted as TRUE.
false, 0, no and off will be interpreted as FALSE.
Everything else cannot be interpreted as boolean and will return the defaultValue.
static Boolean getBoolean(Object o, Boolean defaultValue)
          Get the Boolean value of the given object.
true, 1, yes and on will be interpreted as TRUE.
false, 0, no and off will be interpreted as FALSE.
Everything else cannot be interpreted as boolean and will return the defaultValue.
static Collection getCollection(Object o, Collection defaultValue)
          Get the given object as a collection.
static Date getDate(Object o, Date defaultValue)
          Get the Date representation of the given object
static double getDouble(Object o, double defaultValue)
          Get a double value of the given object
static Double getDouble(Object o, Double defaultValue)
          Get the Double representation of the object
static int getInt(Object o, int defaultValue)
          Get an integer representation of the given object
static Integer getInteger(Object o, Integer defaultValue)
          Get the Integer representation of the given object
static long getLong(Object o, long defaultValue)
          Get a long representation of the given object
static Long getLong(Object o, Long defaultValue)
          Get a Long representation of the given object
static long getLongTimestamp(Object object, long defaultValue)
          Get the timestamp in milliseconds of the object (if it is a Date) or the defaultValue for non-Date objects
static Number getNumber(Object object, Number defaultValue)
          Get the number represented by the given object
static String getString(Object o, String defaultValue)
          Transform the given object into a string (its string representation)
static String[] getStringArray(Object o, String[] defaultValue)
          Transform the given object into a string array
static int getTimestamp(Object object, int defaultValue)
          Get the timestamp value of the object (if it is a Date) or the defaultValue for non-Date objects
static String getToken(Object o, String defaultValue)
          Transform the given object into a token.
static boolean isEmpty(Object value)
          Check whether the given value somehow is empty.
static String readInputStreamIntoString(InputStream inputStream)
          Read data coming from the given input stream, using the default charset.
static String readInputStreamIntoString(InputStream inputStream, String charset)
          Read data coming from the given input stream, using the given charset.
static Object transformObject(Object baseObject, Class targetClass)
          Tries to transforms a given object to the specified class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static org.apache.log4j.Logger logger
the logger

Method Detail

getCollection

public static Collection getCollection(Object o,
                                       Collection defaultValue)
Get the given object as a collection.

Parameters:
o - object to get as collection
defaultValue - default value
Returns:
o as collection or default value when o is null

getInt

public static int getInt(Object o,
                         int defaultValue)
Get an integer representation of the given object

Parameters:
o - object to transform
defaultValue - default value (when the object does not have an integer representation)
Returns:
integer representation of the object or defaultValue

getInteger

public static Integer getInteger(Object o,
                                 Integer defaultValue)
Get the Integer representation of the given object

Parameters:
o - object to transform
defaultValue - default value (when the object does not have an Integer representation)
Returns:
Integer or defaultValue

getLong

public static long getLong(Object o,
                           long defaultValue)
Get a long representation of the given object

Parameters:
o - object to transform
defaultValue - default value (when the object does not have a long representation)
Returns:
long value of the object or defaultValue

getLong

public static Long getLong(Object o,
                           Long defaultValue)
Get a Long representation of the given object

Parameters:
o - object to transform
defaultValue - default value (when the object does not have a Long representation)
Returns:
Long value of the object or defaultValue

getDouble

public static double getDouble(Object o,
                               double defaultValue)
Get a double value of the given object

Parameters:
o - object to transform
defaultValue - default value (when the object does not have a double representation)
Returns:
double value of the object or defaultValue

getDouble

public static Double getDouble(Object o,
                               Double defaultValue)
Get the Double representation of the object

Parameters:
o - object to transform
defaultValue - default value
Returns:
Double value of the object or defaultValue

getBoolean

public static boolean getBoolean(Object o,
                                 boolean defaultValue)
Get the boolean value of the given object.
true, 1, yes and on will be interpreted as TRUE.
false, 0, no and off will be interpreted as FALSE.
Everything else cannot be interpreted as boolean and will return the defaultValue. All comparisons are made case-insensitive.

Parameters:
o - object to transform
defaultValue - default value (when the object cannot be interpreted as boolean)
Returns:
boolean value of the object or defaultValue

getBoolean

public static Boolean getBoolean(Object o,
                                 Boolean defaultValue)
Get the Boolean value of the given object.
true, 1, yes and on will be interpreted as TRUE.
false, 0, no and off will be interpreted as FALSE.
Everything else cannot be interpreted as boolean and will return the defaultValue. All comparisons are made case-insensitive.

Parameters:
o - object to transform
defaultValue - default value (when the object cannot be interpreted as boolean)
Returns:
Boolean value of the object or defaultValue

getBinary

public static byte[] getBinary(Object o,
                               byte[] defaultValue)
Get the object as binary value (byte array)

Parameters:
o - object to transform
defaultValue - default value
Returns:
byte array or default value

getString

public static String getString(Object o,
                               String defaultValue)
Transform the given object into a string (its string representation)

Parameters:
o - object to transform
defaultValue - default value (when the object is null)
Returns:
string representation of the object or the defaultvalue

getStringArray

public static String[] getStringArray(Object o,
                                      String[] defaultValue)
Transform the given object into a string array

Parameters:
o - object to transform
defaultValue - default value, when the object cannot be interpreted as string array
Returns:
string array representation of the object or the defaultvalue

getDate

public static Date getDate(Object o,
                           Date defaultValue)
Get the Date representation of the given object

Parameters:
o - object to transform
defaultValue - default value
Returns:
object as Date or default value

getToken

public static String getToken(Object o,
                              String defaultValue)
Transform the given object into a token. A token does not contain carriage return (#xD), line feed (#xA) or tab (#x9), does not have leading or trailing whitespace and no more than one space (#x20) in a sequence.

Parameters:
o - object to transform
defaultValue - default value (when the object is null)
Returns:
string representation of the object as token or the defaultvalue

isEmpty

public static boolean isEmpty(Object value)
Check whether the given value somehow is empty. The value is empty if at least one of the following is true:

Parameters:
value - value to check for emptyness
Returns:
true when the value is empty, false if not

encodeBinary

public static String encodeBinary(byte[] binaryData)
Encode the given binary data into hexcode (leading zeros, all uppercase letters)

Parameters:
binaryData - binary data to encode
Returns:
encoded binary data

decodeBinary

public static byte[] decodeBinary(String encoded)
Decode the encoded binary data (hexcode, leading zeros, all uppercase letters)

Parameters:
encoded - encoded binary data
Returns:
decoded data
Throws:
IllegalArgumentException - if the encoded data cannot be decoded

getTimestamp

public static int getTimestamp(Object object,
                               int defaultValue)
Get the timestamp value of the object (if it is a Date) or the defaultValue for non-Date objects

Parameters:
object - Date object
defaultValue - default value
Returns:
timestamp of the object

getLongTimestamp

public static long getLongTimestamp(Object object,
                                    long defaultValue)
Get the timestamp in milliseconds of the object (if it is a Date) or the defaultValue for non-Date objects

Parameters:
object - Date object
defaultValue - default value
Returns:
timestamp of the object or the default value

getNumber

public static Number getNumber(Object object,
                               Number defaultValue)
Get the number represented by the given object

Parameters:
object - object to transform into a number
defaultValue - defaultvalue when object is null, or does not represent a number
Returns:
number representation of the object or defaultValue

transformObject

public static Object transformObject(Object baseObject,
                                     Class targetClass)
Tries to transforms a given object to the specified class. (Currently only supports converting from Strings to Integers)

Parameters:
baseObject - The base object which to transform
targetClass - The target class to which the baseObect shall be transformed.
Returns:
returns the transformed object, or null if unsuccessful.

readInputStreamIntoString

public static String readInputStreamIntoString(InputStream inputStream)
                                        throws IOException
Read data coming from the given input stream, using the default charset. The input stream is not closed in this function.

Parameters:
inputStream - input stream containing data
Returns:
string
Throws:
IOException

readInputStreamIntoString

public static String readInputStreamIntoString(InputStream inputStream,
                                               String charset)
                                        throws IOException
Read data coming from the given input stream, using the given charset. The input stream is not closed in this function.

Parameters:
inputStream - input stream containing data
charset - charset to be used
Returns:
string
Throws:
IOException

compareObjects

public static int compareObjects(Comparable first,
                                 Comparable second,
                                 boolean nullfirst)
Compare objects, care also for null objects

Parameters:
first - first object
second - second object
nullfirst - true when nulls shall be smaller than nonnulls, false if nulls are bigger
Returns:
-1 when the first object is smaller, 0 when they are equal (or both null), 1 when the second object is smaller


Copyright © 2013 Gentics Software GmbH. All Rights Reserved.