/** * Converts value to BigDecimal if 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. * * @param value value to be converted to Integer. * @return value converted to Integer. */ public static BigDecimal toBigDecimal(Object value) { if (value == null) { return null; } else if (value instanceof BigDecimal) { return (BigDecimal) value; } else { try { return new BigDecimal(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to BigDecimal", e); } } }
/** * Converts any value to <code>Double</code>. * @param value value to convert. * * @return converted double. */ public static Double toDouble(Object value) { if (value == null) { return null; } else if (value instanceof Double) { return (Double) value; } else if (value instanceof Number) { return ((Number) value).doubleValue(); } else { try { return Double.valueOf(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Double", e); } } }
/** * Converts value to Integer if 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. * * @param value value to be converted to Integer. * @return value converted to Integer. */ public static Integer toInteger(Object value) { if (value == null) { return null; } else if (value instanceof Integer) { return (Integer) value; } else if (value instanceof Number) { return ((Number) value).intValue(); } else { try { return Integer.valueOf(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Integer", e); } } }
/** * Converts value to Short if it can. If value is a Short, it is returned, if it is a Number, it is * promoted to Short and then returned, in all other cases, it converts the value to String, * then tries to parse Short from it. * * @param value value to be converted to Integer. * @return value converted to Integer. */ public static Short toShort(Object value) { if (value == null) { return null; } else if (value instanceof Short) { return (Short) value; } else if (value instanceof Number) { return ((Number) value).shortValue(); } else { try { return Short.valueOf(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Short", e); } } } }
/** * Converts value to <code>Long</code> if it can. If value is a <code>Long</code>, it is returned, if it is a <code>Number</code>, it is * promoted to <code>Long</code> and then returned, if it is a <code>Date</code>, returns its getTime() value, in all other cases, it converts the value to String, * then tries to parse Long from it. * * @param value value to be converted to Long. * @return value converted to Long. */ public static Long toLong(Object value) { if (value == null) { return null; } else if (value instanceof Long) { return (Long) value; } else if (value instanceof Number) { return ((Number) value).longValue(); } else if (value instanceof java.util.Date) { return ((java.util.Date) value).getTime(); } else { try { return Long.valueOf(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Long", e); } } }
/** * Expects a <code>java.sql.Date</code>, <code>java.sql.Timestamp</code>, <code>java.sql.Time</code>, <code>java.util.Date</code>, * <code>Long</code> or any object whose toString method has this format: <code>yyyy-mm-dd</code>. * * @param value argument that is possible to convert to <code>java.sql.Date</code>. * @return <code>java.sql.Date</code> instance representing input value. */ public static java.sql.Date toSqlDate(Object value){ if (value == null) { return null; } else if (value instanceof java.sql.Date) { return (java.sql.Date) value; } else if (value instanceof java.util.Date) { return new java.sql.Date(((java.util.Date) value).getTime()); } else if (value instanceof Number) { return new java.sql.Date(((Number) value).longValue()); } else { try { return java.sql.Date.valueOf(value.toString().trim()); } catch (IllegalArgumentException e) { throw new ConversionException("failed to convert: '" + value + "' to java.sql.Date", e); } } }
/** * Converts <code>java.sql.Blob</code> to bytes array. * * @param blob Blob to be converted * @return blob converted to byte array */ public static byte[] toBytes(Blob blob) { InputStream is = null; try { is = blob.getBinaryStream(); return bytes(is); } catch (Exception e) { throw new ConversionException(e); } finally { closeQuietly(is); } }
private static String clobToString(Clob clob) { Reader r = null; StringWriter sw = null; try { r = clob.getCharacterStream(); sw = new StringWriter(); copyStream(r, sw); return sw.toString(); } catch (Exception e) { throw new ConversionException(e); } finally { closeQuietly(sw); closeQuietly(r); } }
/** * Expects a <code>java.sql.Date</code>, <code>java.sql.Timestamp</code>, <code>java.sql.Time</code>, <code>java.util.Date</code>, * <code>Long</code> or string with format "yyyy-MM-dd". This method will truncate hours, minutes, seconds and * milliseconds to zeros, to conform with JDBC spec: * <a href="http://download.oracle.com/javase/6/docs/api/java/sql/Date.html">http://download.oracle.com/javase/6/docs/api/java/sql/Date.html</a>. * * <p>This method is tread-safe. * * @param value argument that is possible to convert to <code>java.sql.Date</code>: <code>java.sql.Date</code>, * <code>java.sql.Timestamp</code>, <code>java.sql.Time</code>, <code>java.util.Date</code>, <code>Long</code> or any object with toString() == <code>yyyy-mm-dd</code>. * @return <code>java.sql.Date</code> instance representing input value. */ public static java.sql.Date truncateToSqlDate(Object value){ if (value == null) { return null; } else if (value instanceof java.sql.Date) { return (java.sql.Date) value; } else if (value instanceof java.util.Date) { return truncateToSqlDate(((java.util.Date) value).getTime()); } else if (value instanceof Number) { return truncateToSqlDate(((Number) value).longValue()); } else { try { return java.sql.Date.valueOf(value.toString().trim()); } catch (IllegalArgumentException e) { throw new ConversionException("failed to convert: '" + value + "' to java.sql.Date", e); } } }
/** * Converts value to BigDecimal if 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. * * @param value value to be converted to Integer. * @return value converted to Integer. */ public static BigDecimal toBigDecimal(Object value) { if (value == null) { return null; } else if (value instanceof BigDecimal) { return (BigDecimal) value; } else { try { return new BigDecimal(value.toString()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to BigDecimal", e); } } }
/** * Converts value to BigDecimal if 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. * * @param value value to be converted to Integer. * @return value converted to Integer. */ public static BigDecimal toBigDecimal(Object value) { if (value == null) { return null; } else if (value instanceof BigDecimal) { return (BigDecimal) value; } else { try { return new BigDecimal(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to BigDecimal", e); } } }
/** * Converts any value to <code>Double</code>. * @param value value to convert. * * @return converted double. */ public static Double toDouble(Object value) { if (value == null) { return null; } else if (value instanceof Double) { return (Double) value; } else if (value instanceof Number) { return ((Number) value).doubleValue(); } else { try { return Double.valueOf(value.toString()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Double", e); } } }
/** * Converts value to Short if it can. If value is a Short, it is returned, if it is a Number, it is * promoted to Short and then returned, in all other cases, it converts the value to String, * then tries to parse Short from it. * * @param value value to be converted to Integer. * @return value converted to Integer. */ public static Short toShort(Object value) { if (value == null) { return null; } else if (value instanceof Short) { return (Short) value; } else if (value instanceof Number) { return ((Number) value).shortValue(); } else { try { return Short.valueOf(value.toString()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Short", e); } } } }
/** * Converts value to Integer if 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. * * @param value value to be converted to Integer. * @return value converted to Integer. */ public static Integer toInteger(Object value) { if (value == null) { return null; } else if (value instanceof Integer) { return (Integer) value; } else if (value instanceof Number) { return ((Number) value).intValue(); } else { try { return Integer.valueOf(value.toString()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Integer", e); } } }
/** * Converts any value to <code>Double</code>. * @param value value to convert. * * @return converted double. */ public static Double toDouble(Object value) { if (value == null) { return null; } else if (value instanceof Double) { return (Double) value; } else if (value instanceof Number) { return ((Number) value).doubleValue(); } else { try { return Double.valueOf(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Double", e); } } }
/** * Converts value to Integer if 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. * * @param value value to be converted to Integer. * @return value converted to Integer. */ public static Integer toInteger(Object value) { if (value == null) { return null; } else if (value instanceof Integer) { return (Integer) value; } else if (value instanceof Number) { return ((Number) value).intValue(); } else { try { return Integer.valueOf(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Integer", e); } } }
/** * Converts value to Short if it can. If value is a Short, it is returned, if it is a Number, it is * promoted to Short and then returned, in all other cases, it converts the value to String, * then tries to parse Short from it. * * @param value value to be converted to Integer. * @return value converted to Integer. */ public static Short toShort(Object value) { if (value == null) { return null; } else if (value instanceof Short) { return (Short) value; } else if (value instanceof Number) { return ((Number) value).shortValue(); } else { try { return Short.valueOf(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Short", e); } } } }
/** * Converts value to <code>Long</code> if it can. If value is a <code>Long</code>, it is returned, if it is a <code>Number</code>, it is * promoted to <code>Long</code> and then returned, if it is a <code>Date</code>, returns its getTime() value, in all other cases, it converts the value to String, * then tries to parse Long from it. * * @param value value to be converted to Long. * @return value converted to Long. */ public static Long toLong(Object value) { if (value == null) { return null; } else if (value instanceof Long) { return (Long) value; } else if (value instanceof Number) { return ((Number) value).longValue(); } else if (value instanceof java.util.Date) { return ((java.util.Date) value).getTime(); } else { try { return Long.valueOf(value.toString()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Long", e); } } }
/** * Converts value to <code>Long</code> if it can. If value is a <code>Long</code>, it is returned, if it is a <code>Number</code>, it is * promoted to <code>Long</code> and then returned, if it is a <code>Date</code>, returns its getTime() value, in all other cases, it converts the value to String, * then tries to parse Long from it. * * @param value value to be converted to Long. * @return value converted to Long. */ public static Long toLong(Object value) { if (value == null) { return null; } else if (value instanceof Long) { return (Long) value; } else if (value instanceof Number) { return ((Number) value).longValue(); } else if (value instanceof java.util.Date) { return ((java.util.Date) value).getTime(); } else { try { return Long.valueOf(value.toString().trim()); } catch (NumberFormatException e) { throw new ConversionException("failed to convert: '" + value + "' to Long", e); } } }
/** * Expects a <code>java.sql.Date</code>, <code>java.sql.Timestamp</code>, <code>java.sql.Time</code>, <code>java.util.Date</code>, * <code>Long</code> or any object whose toString method has this format: <code>yyyy-mm-dd</code>. * * @param value argument that is possible to convert to <code>java.sql.Date</code>. * @return <code>java.sql.Date</code> instance representing input value. */ public static java.sql.Date toSqlDate(Object value){ if (value == null) { return null; } else if (value instanceof java.sql.Date) { return (java.sql.Date) value; } else if (value instanceof java.util.Date) { return new java.sql.Date(((java.util.Date) value).getTime()); } else if (value instanceof Number) { return new java.sql.Date(((Number) value).longValue()); } else { try { return java.sql.Date.valueOf(value.toString()); } catch (IllegalArgumentException e) { throw new ConversionException("failed to convert: '" + value + "' to java.sql.Date", e); } } }