org.javalite.common
Class Convert

java.lang.Object
  extended by org.javalite.common.Convert

public class Convert
extends Object

Convenience class for type conversions.

Author:
Igor Polevoy

Constructor Summary
Convert()
           
 
Method Summary
static BigDecimal toBigDecimal(Object value)
          Converts value to BigDecimal if c it can.
static Boolean toBoolean(Object value)
          Returns true if the value is any numeric type and has a value of 1, or if string type has a value of 'y', 't', 'true' or 'yes'.
static byte[] toBytes(Object value)
          Converts value to bytes array if it can.
static Double toDouble(Object value)
          Converts any value to Double.
static Float toFloat(Object value)
          Converts value to Float if c it can.
static Integer toInteger(Object value)
          Converts value to Integer if c it can.
static Long toLong(Object value)
          Converts value to Long if c it can.
static Date toSqlDate(Object value)
          Expects a java.sql.Date, java.sql.Timestamp, java.sql.Time, java.util.Date or any object whose toString method has this format: yyyy-mm-dd.
static String toString(Object value)
          Returns string representation of an object, including Clob.
static Timestamp toTimestamp(Object value)
          If the value is instance of java.sql.Timestamp, returns it, else tries to convert the value to Timestamp using Timestamp.valueOf(String).
static Date truncateToSqlDate(Object value)
          Expects a java.sql.Date, java.sql.Timestamp, java.sql.Time, java.util.Date or string with format: yyyy-mm-dd.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Convert

public Convert()
Method Detail

toString

public static String toString(Object value)
Returns string representation of an object, including Clob. For large CLOBs, be careful because this will load an entire CLOB in memory as java.lang.String.

Parameters:
value - value to convert.
Returns:
string representation of an object, including Clob.

toBoolean

public static Boolean toBoolean(Object value)
Returns true if the value is any numeric type and has a value of 1, or if string type has a value of 'y', 't', 'true' or 'yes'. Otherwise, return false.

Parameters:
value - value to convert
Returns:
true if the value is any numeric type and has a value of 1, or if string type has a value of 'y', 't', 'true' or 'yes'. Otherwise, return false.

toSqlDate

public static Date toSqlDate(Object value)
Expects a java.sql.Date, java.sql.Timestamp, java.sql.Time, java.util.Date or any object whose toString method has this format: yyyy-mm-dd.

Parameters:
value - argument that is possible to convert to java.sql.Date.
Returns:
java.sql.Date instance representing input value.

truncateToSqlDate

public static Date truncateToSqlDate(Object value)
Expects a java.sql.Date, java.sql.Timestamp, java.sql.Time, java.util.Date or string with format: yyyy-mm-dd. This method will also truncate hours, minutes, seconds and milliseconds to zeros, to conform with JDBC spec: http://download.oracle.com/javase/6/docs/api/java/sql/Date.html.

Parameters:
value - argument that is possible to convert to java.sql.Date: java.sql.Date, java.sql.Timestamp, java.sql.Time, java.util.Date or any object with toString() == yyyy-mm-dd.
Returns:
java.sql.Date instance representing input value.

toDouble

public static Double toDouble(Object value)
Converts any value to Double.

Parameters:
value - value to convert.
Returns:
converted double.

toTimestamp

public static Timestamp toTimestamp(Object value)
If the value is instance of java.sql.Timestamp, returns it, else tries to convert the value to Timestamp using Timestamp.valueOf(String). This method might trow IllegalArgumentException if fails at conversion.

Parameters:
value - value to convert.
Returns:
instance of Timestamp.
See Also:
Timestamp#valueOf(String)}

toFloat

public static Float toFloat(Object value)
Converts value to Float if c it can. If value is a Float, it is returned, if it is a Number, it is promoted to Float and then returned, in all other cases, it converts the value to String, then tries to parse Float from it.

Parameters:
value - value to be converted to Float.
Returns:
value converted to Float.

toLong

public static Long toLong(Object value)
Converts value to Long if c it can. If value is a Long, it is returned, if it is a Number, it is promoted to Long and then returned, in all other cases, it converts the value to String, then tries to parse Long from it.

Parameters:
value - value to be converted to Long.
Returns:
value converted to Long.

toInteger

public static Integer toInteger(Object value)
Converts value to Integer if c it can. If value is a Integer, it is returned, if it is a Number, it is promoted to Integer and then returned, in all other cases, it converts the value to String, then tries to parse Integer from it.

Parameters:
value - value to be converted to Integer.
Returns:
value converted to Integer.

toBigDecimal

public static BigDecimal toBigDecimal(Object value)
Converts value to BigDecimal if c it can. If value is a BigDecimal, it is returned, if it is a BigDecimal, it is promoted to BigDecimal and then returned, in all other cases, it converts the value to String, then tries to parse BigDecimal from it.

Parameters:
value - value to be converted to Integer.
Returns:
value converted to Integer.

toBytes

public static byte[] toBytes(Object value)
Converts value to bytes array if it can. If the value is byte array, it is simply returned, if it is a java.sql.Blob, then data is read from it as bytes. In all other cases the object is converted to String, then bytes are read from it.

Parameters:
value - value to be converted.
Returns:
value converted to byte array.


Copyright © 2013. All Rights Reserved.