1 3 package jodd.typeconverter; 4 5 8 public class DoubleArrayConverter implements TypeConverter { 9 10 public static double[] valueOf(Object value) { 11 12 if (value == null) { 13 return null; 14 } 15 16 Class type = value.getClass(); 17 if (type.isArray() == false) { 18 if (value instanceof Number ) { 19 return new double[] {((Number ) value).doubleValue()}; 20 } 21 try { 22 return new double[] {Double.parseDouble(value.toString())}; 23 } catch (NumberFormatException nfex) { 24 throw new TypeConversionException(nfex); 25 } 26 27 } 28 29 if (type == double[].class) { 30 return (double[]) value; 31 } 32 if (type == float[].class) { 33 float[] values = (float[]) value; 34 double[] results = new double[values.length]; 35 for (int i = 0; i < values.length; i++) { 36 results[i] = values[i]; 37 } 38 return results; 39 } 40 if (type == int[].class) { 41 int[] values = (int[]) value; 42 double[] results = new double[values.length]; 43 for (int i = 0; i < values.length; i++) { 44 results[i] = values[i]; 45 } 46 return results; 47 } 48 if (type == long[].class) { 49 long[] values = (long[]) value; 50 double[] results = new double[values.length]; 51 for (int i = 0; i < values.length; i++) { 52 results[i] = values[i]; 53 } 54 return results; 55 } 56 if (type == byte[].class) { 57 byte[] values = (byte[]) value; 58 double[] results = new double[values.length]; 59 for (int i = 0; i < values.length; i++) { 60 results[i] = values[i]; 61 } 62 return results; 63 } 64 if (type == boolean[].class) { 65 boolean[] values = (boolean[]) value; 66 double[] results = new double[values.length]; 67 for (int i = 0; i < values.length; i++) { 68 results[i] = (values[i] == true ? 1 : 0); 69 } 70 return results; 71 } 72 if (type == short[].class) { 73 short[] values = (short[]) value; 74 double[] results = new double[values.length]; 75 for (int i = 0; i < values.length; i++) { 76 results[i] = values[i]; 77 } 78 return results; 79 } 80 81 82 Object [] values = (Object []) value; 83 double[] results = new double[values.length]; 84 try { 85 for (int i = 0; i < values.length; i++) { 86 if (values[i] != null) { 87 if (values[i] instanceof Number ) { 88 results[i] = ((Number ) values[i]).doubleValue(); 89 } else { 90 results[i] = Double.parseDouble(values[i].toString()); 91 } 92 } 93 } 94 } catch (NumberFormatException nfex) { 95 throw new TypeConversionException(nfex); 96 } 97 return results; 98 } 99 100 public Object convert(Object value) { 101 return valueOf(value); 102 } 103 } 104 | Popular Tags |