com.im.commons 5.9

com.im.commons.util
Class DataConvertorUtils

java.lang.Object
  extended by com.im.commons.util.DataConvertorUtils

public final class DataConvertorUtils
extends Object


Nested Class Summary
static class DataConvertorUtils.DateOptions
          An options bean for date conversion functions.
 
Method Summary
static Object convert(Object input, Class targetType)
          Convert value to value with targetType.
static Boolean convertBoolean(Object o)
           
static Date convertDate(Object o, DataConvertorUtils.DateOptions opts)
           
static Double convertDouble(Object o)
           
static Float convertFloat(Object o)
           
static Integer convertInteger(Object o, DataConvertorOptions.Int options)
           
static byte[] decodeHex(String hex)
          Converts a hexadecimal number to a byte array.
static String encodeHex(byte[] raw)
          Converts an array of bytes to their hexadecimal representation.
static Object normalizeValue(Object value)
          Cut space in the beginning and end of the string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

normalizeValue

public static Object normalizeValue(Object value)
Cut space in the beginning and end of the string.

Returns the original value if it is not a String instance

Returns null if value is null

Returns null if value after cutting spaces is String("")

Parameters:
value - To be normalized
Returns:
The normalized value

convertBoolean

public static Boolean convertBoolean(Object o)

convertInteger

public static Integer convertInteger(Object o,
                                     DataConvertorOptions.Int options)

convertFloat

public static Float convertFloat(Object o)

convertDouble

public static Double convertDouble(Object o)

convertDate

public static Date convertDate(Object o,
                               DataConvertorUtils.DateOptions opts)

convert

public static Object convert(Object input,
                             Class targetType)
Convert value to value with targetType.

Parameters:
input - input value
targetType - target Type (Integer, Float, Date, Boolean, String)
Returns:
converted value.

encodeHex

public static String encodeHex(byte[] raw)
Converts an array of bytes to their hexadecimal representation. Each byte from the array is converted to two characters [0-9A-F] that represent the byte's value as a hexadecimal number.

Example: encodeHex(new byte [] { 10, 20, 30 }).equals("0A141E");

Parameters:
raw - An array of bytes to convert.
Returns:
A string of hexadecimal representations of all bytes in the array.

decodeHex

public static byte[] decodeHex(String hex)
Converts a hexadecimal number to a byte array. The string has to contain pairs of hexadecimal digits (ie. [0-9A-F]). Each pair is then converted to the byte value that it represents and added as a new item to a byte array.

Example: decodeHex("ABCDEF").equals(new byte [] { 171, 205, 239 });

Parameters:
hex - A string of hexadecimal digit pairs to convert. It must contain an even number of characters.
Returns:
An array of bytes represented by hexadecimal numbers in the hex string.
Throws:
IllegalArgumentException - If hex string contains odd number of characters or if the characters are not hexadecimal digits.

com.im.commons 5.9