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