com.acsinet_solutions.util
Class Conversion

java.lang.Object
  extended by com.acsinet_solutions.util.Conversion

public class Conversion
extends java.lang.Object

Useful conversion utility methods.

(c) 2004 ACSINET S.A. de C.V. Derechos Reservados

Author:
Santiago Arriaga

Method Summary
static
<T> java.util.List<T>
arrayAsList(T[] arr)
          Return the array as a List
static
<T> java.util.Set<T>
arrayAsSet(T[] arr)
          Return the array as a set
static java.lang.Integer[] asArray(int number)
          Return the given object as an Integer array
static java.lang.Object[] asArray(java.lang.Object obj)
          Return the given object as an object array
static java.math.BigDecimal asBigDecimal(java.lang.Object obj)
          Return the Object as a BigDecimal.
static java.lang.Boolean asBoolean(java.lang.Object obj)
          Return the Object as a Boolean.
static java.lang.Byte asByte(java.lang.Object obj)
          Return the Object as a Byte.
static java.util.Calendar asCalendar(java.lang.Object obj)
          Return the Object as a java.util.Calendar.
static java.lang.Class asClass(java.lang.Object obj)
          Return the Object as a Class instance.
static java.util.Collection asCollection(java.lang.Object obj)
          Return the Object as a List instance.
static java.util.Date asDate(java.lang.Object obj)
          Return the Object as a java.util.Date.
static java.lang.Double asDouble(java.lang.Object obj)
          Return the Object as a Double.
static java.io.File asFile(java.lang.Object obj)
          Return the Object as a java.io.File instance.
static java.lang.Float asFloat(java.lang.Object obj)
          Return the Object as a Float.
static java.lang.String asIdentifier(java.lang.String str)
          Transform to identifier - capitalize and pass word boundaries to capital letter, eliminating underscores.
static java.lang.Integer asInteger(java.lang.Object obj)
          Return the Object as an Integer.
static javax.mail.internet.InternetAddress asInternetAddress(java.lang.Object obj)
          Return the Object as an internet address.
static java.util.List asList(java.lang.Object obj)
          Return the Object as a List instance.
static java.lang.Long asLong(java.lang.Object obj)
          Return the Object as a Long.
static java.lang.String asNotNullString(java.lang.Object obj)
          Return the Object as a String.
static java.lang.Number asNumber(java.lang.Object obj)
          Return the Object as a Number.
static java.util.Set asSet(java.lang.Object obj)
          Return the Object as a Set instance.
static java.lang.Short asShort(java.lang.Object obj)
          Return the Object as a Short.
static java.sql.Date asSQLDate(java.lang.Object obj)
          Return the Object as a java.sql.Date.
static java.lang.String asString(java.lang.Object obj)
          Return the Object as a String.
static java.lang.String[] asStringArray(java.lang.Object obj)
          Return the object as a String[] The conversion is done by the following rules: If the object is null, a null is returned If the object is a String[], it is casted and returned If the object is an Object[], every element is casted to String and a String[] is returned If the object is a Collection, it is converted to an Object[] and the to a String[] Otherwise the object is converted to String and a single valued String[] is returned
static java.sql.Time asTime(java.lang.Object obj)
          Return the Object as a java.sql.Time.
static java.sql.Timestamp asTimestamp(java.lang.Object obj)
          Return the Object as a java.sql.Timestamp.
static java.lang.Object asType(java.lang.Object obj, java.lang.Class type)
           
static java.lang.Object asType(java.lang.Object obj, ConversionType type)
           
static java.lang.String asUnderscored(java.lang.String str)
          Transform to underscored - The opposite procedure than asIndentifier() method.
static java.lang.String capitalize(java.lang.String str)
          Simple capitalization utility
static java.util.List<java.lang.String> stringToList(java.lang.String str)
          Convert the given delimiter separated String to a List of Strings using the comma ',' as separator
static java.util.List<java.lang.String> stringToList(java.lang.String str, java.lang.String separator)
          Convert the given delimiter separated String to a List of Strings
static java.util.Set<java.lang.String> stringToSet(java.lang.String str)
          Convert the given delimiter separated String to a Set of Strings using the comma ',' as separator
static java.util.Set<java.lang.String> stringToSet(java.lang.String str, java.lang.String separator)
          Convert the given delimiter separated String to a Set of Strings
static boolean toBoolean(java.lang.Object obj)
          Return the Object instance as an boolean primitive type.
static boolean toBoolean(java.lang.Object obj, boolean defaultValue)
          Return the Object instance as an boolean primitive type.
static byte toByte(java.lang.Object obj)
          Return the Object instance as a byte primitive type.
static double toDouble(java.lang.Object obj)
          Return the Object instance as a double primitive type.
static float toFloat(java.lang.Object obj)
          Return the Object instance as a float primitive type.
static int toInt(java.lang.Object obj)
          Return the Object instance as an int primitive type.
static long toLong(java.lang.Object obj)
          Return the Object instance as an long primitive type.
static short toShort(java.lang.Object obj)
          Return the Object instance as a short primitive type.
static java.lang.String uncapitalize(java.lang.String str)
          Simple uncapitalization utility
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toBoolean

public static boolean toBoolean(java.lang.Object obj)
Return the Object instance as an boolean primitive type. null values are returned as false. Check asBoolean() algorithm for value interpretation

Parameters:
obj - Object to be converted to a boolean.
Returns:
int value representing the object; false is returned for null values

toBoolean

public static boolean toBoolean(java.lang.Object obj,
                                boolean defaultValue)
Return the Object instance as an boolean primitive type. null values are returned as the default value. Check asBoolean() algorithm for value interpretation

Parameters:
obj - Object to be converted to a boolean.
Returns:
defaultValue Value to be returned on null objects

toLong

public static long toLong(java.lang.Object obj)
Return the Object instance as an long primitive type. null values are returned as 0. See asLong() method for conversion algorithm.

Parameters:
obj - Object to be converted to an long.
Returns:
long value representing the object; 0 is returned for null values
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to an long

toInt

public static int toInt(java.lang.Object obj)
Return the Object instance as an int primitive type. null values are returned as 0. See asInt() method for conversion algorithm.

Parameters:
obj - Object to be converted to an integer.
Returns:
int value representing the object; 0 is returned for null values
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to an int

toShort

public static short toShort(java.lang.Object obj)
Return the Object instance as a short primitive type. null values are returned as 0. See asShort() method for conversion algorithm.

Parameters:
obj - Object to be converted to an short.
Returns:
short value representing the object; 0 is returned for null values
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to an short

toByte

public static byte toByte(java.lang.Object obj)
Return the Object instance as a byte primitive type. null values are returned as 0. See asByte() method for conversion algorithm.

Parameters:
obj - Object to be converted to an byte.
Returns:
byte value representing the object; 0 is returned for null values
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to an byte

toDouble

public static double toDouble(java.lang.Object obj)
Return the Object instance as a double primitive type. null values are returned as 0. See asDouble() method for conversion algorithm.

Parameters:
obj - Object to be converted to a double.
Returns:
double value representing the object; 0 is returned for null values
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a double

toFloat

public static float toFloat(java.lang.Object obj)
Return the Object instance as a float primitive type. null values are returned as 0. See asFloat() method for conversion algorithm.

Parameters:
obj - Object to be converted to a float.
Returns:
float value representing the object; 0 is returned for null values
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a float

asStringArray

public static java.lang.String[] asStringArray(java.lang.Object obj)
Return the object as a String[] The conversion is done by the following rules:

Parameters:
obj - object to be converted to a String[]
Returns:
if possible the string[] object
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a String[]

asArray

public static java.lang.Object[] asArray(java.lang.Object obj)
Return the given object as an object array

Parameters:
obj - The not-null Object instance
Returns:
A maybe-null Object[]

asArray

public static java.lang.Integer[] asArray(int number)
Return the given object as an Integer array

Parameters:
obj - The not-null Object instance
Returns:
A maybe-null Object[]

asString

public static java.lang.String asString(java.lang.Object obj)
Return the Object as a String. The conversion is done by the following rules:

Parameters:
Obj - Object to be converted to string.
Returns:
The object as a string; or a null value if the object is null.

asNotNullString

public static java.lang.String asNotNullString(java.lang.Object obj)
Return the Object as a String. The conversion is done by the following rules:

Parameters:
obj - Object to be converted to string.
Returns:
The object as a string; or a null value if the object is null.

asBoolean

public static java.lang.Boolean asBoolean(java.lang.Object obj)
Return the Object as a Boolean. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.lang.Boolean, the object is casted and returned.
  3. If the object can be converted to a integer: if the number is different than 0, a TRUE Boolean is returned; otherwise, a FALSE Boolean is returned.
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the string constructor of Boolean is used.

Parameters:
obj - Object to be converted to Boolean.

asNumber

public static java.lang.Number asNumber(java.lang.Object obj)
Return the Object as a Number. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.lang.Number, the object is casted and returned.
  3. If the object is an empty string, a null value is returned.
  4. If the object is a non-empty string, the string constructor of Integer or Long is intended to be used if not '.' is present, the string constructor of Double is intended to be used if '.' is present.

Parameters:
obj - Object to be converted to Number.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Number

asLong

public static java.lang.Long asLong(java.lang.Object obj)
Return the Object as a Long. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.lang.Long, the object is casted and returned.
  3. If the object is an instance of java.lang.Number, the longValue() method is used to obtain the Long value.
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the string constructor of Long is intended to be used.

Parameters:
obj - Object to be converted to Long.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Long

asInteger

public static java.lang.Integer asInteger(java.lang.Object obj)
Return the Object as an Integer. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.lang.Integer, the object is casted and returned.
  3. If the object is an instance of java.lang.Number, the intValue() method is used to obtain the Integer value.
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the string constructor of Integer is intended to be used.

Parameters:
obj - Object to be converted to Integer.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to an Integer

asShort

public static java.lang.Short asShort(java.lang.Object obj)
Return the Object as a Short. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.lang.Short, the object is casted and returned.
  3. If the object is an instance of java.lang.Number, the shortValue() method is used to obtain the Short value.
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the string constructor of Short is intended to be used.

Parameters:
obj - Object to be converted to Short.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Short

asByte

public static java.lang.Byte asByte(java.lang.Object obj)
Return the Object as a Byte. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.lang.Byte, the object is casted and returned.
  3. If the object is an instance of java.lang.Number, the byteValue() method is used to obtain the Byte value.
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the string constructor of Byte is intended to be used.

Parameters:
obj - Object to be converted to Byte.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Byte

asDouble

public static java.lang.Double asDouble(java.lang.Object obj)
Return the Object as a Double. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.lang.Double, the object is casted and returned.
  3. If the object is an instance of java.lang.Number, the doubleValue() method is used to obtain the Double value.
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the string constructor of Double is intended to be used.

Parameters:
obj - Object to be converted to Double.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Double

asFloat

public static java.lang.Float asFloat(java.lang.Object obj)
Return the Object as a Float. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.lang.Float, the object is casted and returned.
  3. If the object is an instance of java.lang.Number, the floatValue() method is used to obtain the Float value.
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the string constructor of Float is intended to be used.

Parameters:
obj - Object to be converted to Float.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Float

asBigDecimal

public static java.math.BigDecimal asBigDecimal(java.lang.Object obj)
Return the Object as a BigDecimal. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.math.BigDecimal, the object is casted and returned.
  3. If the object is an instance of java.math.BigInteger, the BigInteger constructor is used
  4. If the object is an instance of java.lang.Number, the object is converted to double, and the double constructor is used.
  5. If the object is an empty string, a null value is returned.
  6. If the object is a non-empty string, the string constructor of BigDecimal is used.

Parameters:
obj - Object to be converted to Float.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Float

asCalendar

public static java.util.Calendar asCalendar(java.lang.Object obj)
Return the Object as a java.util.Calendar. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.util.Calendar, the object is casted and returned
  3. asDate() method is used otherwise

Parameters:
obj - Object to be converted to Calendar.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Date

asDate

public static java.util.Date asDate(java.lang.Object obj)
Return the Object as a java.util.Date. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.sql.Date or java.sqlDatetime a new Date object is constructed from this (to prevent errors due change of implementation in this subclasses).
  3. If the object is an instance of java.util.Date, the object is casted and returned.
  4. If the object is an instance of java.util.Calendar, the associated date is returned
  5. If the object can be converted to a long type, a new java.util.Date is created from that representation
  6. asSqlDate() method is used to see if the string can be used to obtain an sql date, and with that date a java.util.Date is created.

Parameters:
obj - Object to be converted to Date.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Date

asSQLDate

public static java.sql.Date asSQLDate(java.lang.Object obj)
Return the Object as a java.sql.Date. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.util.Date, the object is casted and returned.
  3. If the object is an instance of java.util.Date a new java.sql.Date object is constructed from there
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the java.sql.Date.valueOf() method is intended to be used.
  6. If the object can be converted to a long type, a new java.sql.Date is created from that representation

Parameters:
obj - Object to be converted to Date.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Date

asTime

public static java.sql.Time asTime(java.lang.Object obj)
Return the Object as a java.sql.Time. The following logic is internally used for conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.util.Time, the object is casted and returned.
  3. If the object is an instance of java.util.Date a new java.sql.Time object is constructed from there
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the java.sql.Time.valueOf() method is intended to be used.
  6. If the object can be converted to a long type, a new java.sql.Time is created from that representation

Parameters:
Obj - Object to be converted to Time.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Time

asTimestamp

public static java.sql.Timestamp asTimestamp(java.lang.Object obj)
Return the Object as a java.sql.Timestamp. The following logic is internally used for the conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.util.Timestamp, the object is casted and returned.
  3. If the object is an instance of java.util.Date a new java.sql.Timestamp object is constructed from there
  4. If the object is an empty string, a null value is returned.
  5. If the object is a non-empty string, the java.sql.Timestamp.valueOf() method is intended to be used.
  6. If the object can be converted to a long type, a new java.sql.Timestamp is created from that representation

Parameters:
obj - Object to be converted to Timestamp.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Timestamp

asFile

public static java.io.File asFile(java.lang.Object obj)
Return the Object as a java.io.File instance. The following logic is internally used for the conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.io.File, the object is casted and returned.
  3. If the object is an empty string, a null value is returned.
  4. If the object is a non-empty string, the default host parser from PathFormatRegistry is used.

Parameters:
obj - Object to be converted to java.io.File
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Path instance

asSet

public static java.util.Set asSet(java.lang.Object obj)
Return the Object as a Set instance. The following logic is internally used for the conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.util.Set, the object is casted and returned.
  3. If the object is an instance of java.util.Collection, the set is constructed from that correction.
  4. If the object is a Object[] a set is created with it
  5. A string set is created using the toStringArray method.

Parameters:
obj - Object to be converted to Set.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a Set instance

asList

public static java.util.List asList(java.lang.Object obj)
Return the Object as a List instance. The following logic is internally used for the conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.util.List, the object is casted and returned.
  3. If the object is an instance of java.util.Collection, an ArrayList is constructed from that correction.
  4. If the object is a Object[] a list is created with it
  5. A string list is created using the toStringArray method.

Parameters:
obj - Object to be converted to List.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a List instance

asCollection

public static java.util.Collection asCollection(java.lang.Object obj)
Return the Object as a List instance. The following logic is internally used for the conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.util.Collection, the object is casted and returned.
  3. If the object is an instance of java.util.Map, the values() method is used and returned
  4. Otherwise the asList() method is used

Parameters:
obj - Object to be converted to Collection.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a List instance

asClass

public static java.lang.Class asClass(java.lang.Object obj)
Return the Object as a Class instance. The following logic is internally used for the conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of java.lang.Class, the object is casted and returned.
  3. If the object is an empty string, a null value is returned.
  4. If the object is a non-empty string, Class.forName() is used

Parameters:
Obj - Object to be converted to Class.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a List instance

asInternetAddress

public static javax.mail.internet.InternetAddress asInternetAddress(java.lang.Object obj)
                                                             throws javax.mail.internet.AddressException
Return the Object as an internet address. The following logic is internally used for the conversion:
  1. If the object is null, a null value is returned.
  2. If the object is an instance of InternetAddress, the object is casted and returned.
  3. If the object is an empty string, a null value is returned.
  4. Otherwise, the object is converted to a String and that String is used to create a new InternetAddress

Parameters:
Obj - Object to be converted to Class.
Throws:
java.lang.IllegalArgumentException - Thrown if the object could not be converted to a List instance
javax.mail.internet.AddressException

asType

public static java.lang.Object asType(java.lang.Object obj,
                                      ConversionType type)
Parameters:
type - Any ConversionType valid type.

asType

public static java.lang.Object asType(java.lang.Object obj,
                                      java.lang.Class type)
Parameters:
type - A supported conversion instance.

capitalize

public static final java.lang.String capitalize(java.lang.String str)
Simple capitalization utility

Parameters:
The - not-null not-empty string to capitalize
Returns:
The capitalized String

uncapitalize

public static final java.lang.String uncapitalize(java.lang.String str)
Simple uncapitalization utility

Parameters:
The - not-null not-empty string to uncapitalize
Returns:
The uncapitalized String

asIdentifier

public static final java.lang.String asIdentifier(java.lang.String str)
Transform to identifier - capitalize and pass word boundaries to capital letter, eliminating underscores.

Parameters:
A - not-null not-empty string

asUnderscored

public static final java.lang.String asUnderscored(java.lang.String str)
Transform to underscored - The opposite procedure than asIndentifier() method. "TaskMethod" will be passed to "task_method"; "HTMLEditor" will be passed to "htmleditor"; "HTMLLogKit" will be "htmllog_kit"; "TaskDTO" will be "task_dto".

Parameters:
A - not-null not-empty string
See Also:
asIdentifier(String)

stringToList

public static final java.util.List<java.lang.String> stringToList(java.lang.String str,
                                                                  java.lang.String separator)
Convert the given delimiter separated String to a List of Strings

Parameters:
str - A maybe-null String instace
Returns:
A not-null List instance of not-null String instances; null just if the input string is null

stringToList

public static final java.util.List<java.lang.String> stringToList(java.lang.String str)
Convert the given delimiter separated String to a List of Strings using the comma ',' as separator

Parameters:
str - A maybe-null String instace
Returns:
A not-null List instance of not-null String instances; null just if the input string is null

stringToSet

public static final java.util.Set<java.lang.String> stringToSet(java.lang.String str,
                                                                java.lang.String separator)
Convert the given delimiter separated String to a Set of Strings

Parameters:
str - A maybe-null String instace
Returns:
A not-null Set instance of not-null String instances; null just if the input string is null

stringToSet

public static final java.util.Set<java.lang.String> stringToSet(java.lang.String str)
Convert the given delimiter separated String to a Set of Strings using the comma ',' as separator

Parameters:
str - A maybe-null String instace
Returns:
A not-null Set instance of not-null String instances; null just if the input string is null

arrayAsSet

public static final <T> java.util.Set<T> arrayAsSet(T[] arr)
Return the array as a set

Parameters:
a - not null array
Returns:
a not null Set instance

arrayAsList

public static final <T> java.util.List<T> arrayAsList(T[] arr)
Return the array as a List

Parameters:
a - not null array
Returns:
a not null list instance