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