View Javadoc
1 /*** 2 * ObjectConverter.java 3 * 4 * $Author: mballesteros $ 5 * $Date: 2003/11/28 19:18:04 $ 6 * $Revision: 1.1 $ 7 */ 8 package net.sf.jec; 9 10 /*** ObjectsConverter instances provide methods for common object to object 11 * conversions and object to basic type conversions. It handles special 12 * conversions as currencies, dates, and percents. 13 * @author mballesteros 14 */ 15 public class ObjectConverter { 16 17 public static Object convert(Object value, Class targetClass) 18 throws ConversionException { 19 if (value == null) 20 return null; 21 if (value.getClass() == targetClass) 22 return value; 23 if (value instanceof String) { 24 if (targetClass == Integer.TYPE || targetClass == Integer.class) 25 return new Integer((String) value); 26 else if (targetClass == Long.TYPE || targetClass == Long.class) 27 return new Long((String) value); 28 else if ( 29 targetClass == Character.TYPE 30 || targetClass == Character.class) 31 return new Character(((String) value).charAt(0)); 32 else if (targetClass == Float.TYPE || targetClass == Float.class) 33 return new Float((String) value); 34 else if (targetClass == Double.TYPE || targetClass == Double.class) 35 return new Double((String) value); 36 else if ( 37 targetClass == Boolean.TYPE || targetClass == Boolean.class) 38 return new Boolean((String) value); 39 } else if (value instanceof Number) { 40 if (targetClass == Integer.TYPE || targetClass == Integer.class) 41 return new Integer(((Number) value).intValue()); 42 else if (targetClass == Long.TYPE || targetClass == Long.class) 43 return new Long(((Number) value).longValue()); 44 else if (targetClass == Float.TYPE || targetClass == Float.class) 45 return new Float(((Number) value).floatValue()); 46 else if (targetClass == Double.TYPE || targetClass == Double.class) 47 return new Double(((Number) value).doubleValue()); 48 } 49 throw new ConversionException("Cannot convert"); 50 } 51 52 /*** Converts a number into an int basic type 53 * @param obj The Number to convert 54 */ 55 public static int toInt(Number obj) throws ConversionException { 56 if (obj == null) 57 throw new ConversionException("Cannot convert a null into an int basic type. "); 58 return ((Number) obj).intValue(); 59 } 60 61 /*** Converts an Object into an int basic type 62 * @param obj The Object to convert 63 */ 64 public static int toInt(Object obj) throws ConversionException { 65 if (obj == null) 66 throw new ConversionException("Cannot convert a null into an int basic type. "); 67 try { 68 return Integer.parseInt(obj.toString()); 69 } catch (NumberFormatException ex) { 70 throw new ConversionException( 71 "Cannot convert this Object into an int basic type, check it: " 72 + obj.toString()); 73 } 74 } 75 76 /*** Converts a number into a double basic type 77 * @param obj The Number to convert 78 */ 79 public static double toDouble(Number obj) throws ConversionException { 80 if (obj == null) 81 throw new ConversionException("Cannot convert a null into an int basic type. "); 82 return ((Number) obj).doubleValue(); 83 } 84 85 /*** Converts an Object into a double basic type 86 * @param obj The Object to convert 87 */ 88 public static double toDouble(Object obj) throws ConversionException { 89 if (obj == null) 90 throw new ConversionException("Cannot convert a null into a double basic type. "); 91 try { 92 return Double.parseDouble(obj.toString()); 93 } catch (NumberFormatException ex) { 94 throw new ConversionException( 95 "Cannot convert this Object into a double basic type, check it: " 96 + obj.toString()); 97 } 98 } 99 100 /*** Normalizes a Number: its minimum values equal 'null' objects. If the 101 * given object is not a Number it will be left 102 * @param number The Number to normalize 103 * @return The normalized value 104 */ 105 public static Object normalize(Object number) { 106 if (number instanceof Integer 107 && ((Integer) number).intValue() == Integer.MIN_VALUE) 108 number = null; 109 else if ( 110 number instanceof Long 111 && ((Long) number).longValue() == Long.MIN_VALUE) 112 number = null; 113 else if ( 114 number instanceof Float 115 && ((Float) number).floatValue() == Float.MIN_VALUE) 116 number = null; 117 else if ( 118 number instanceof Double 119 && ((Double) number).doubleValue() == Double.MIN_VALUE) 120 number = null; 121 return number; 122 } 123 /*** 124 * Converts an object into a boolean basic type. 125 * <code>java.lang.Boolean</code> are converted into its values. Objects 126 * which toString() method equals (ignoring case) "true" or "false" are also 127 * succesfully converted. Other objects are rejected and a 128 * ConversionException is thrown. 129 * 130 * @param object The object to convert to <code>boolean</code> 131 * @return boolean The converted value, when possible. 132 */ 133 public static boolean toBoolean(Object obj) throws ConversionException { 134 if (obj instanceof Boolean) { 135 return ((Boolean) obj).booleanValue(); 136 } else if (obj.toString().equalsIgnoreCase("true")) { 137 return true; 138 } else if (obj.toString().equalsIgnoreCase("false")) { 139 return false; 140 } 141 throw new ConversionException( 142 "Cannot convert this Object into a boolean basic type, check it: " 143 + obj.toString()); 144 } 145 146 }

This page was automatically generated by Maven