1 16 17 18 package org.apache.commons.beanutils.converters; 19 20 21 import java.util.List ; 22 import org.apache.commons.beanutils.ConversionException; 23 import org.apache.commons.beanutils.Converter; 24 25 26 36 37 public final class DoubleArrayConverter extends AbstractArrayConverter { 38 39 40 42 43 47 public DoubleArrayConverter() { 48 49 this.defaultValue = null; 50 this.useDefault = false; 51 52 } 53 54 55 61 public DoubleArrayConverter(Object defaultValue) { 62 63 this.defaultValue = defaultValue; 64 this.useDefault = true; 65 66 } 67 68 69 71 72 75 private static double model[] = new double[0]; 76 77 78 80 81 91 public Object convert(Class type, Object value) { 92 93 if (value == null) { 95 if (useDefault) { 96 return (defaultValue); 97 } else { 98 throw new ConversionException("No value specified"); 99 } 100 } 101 102 if (model.getClass() == value.getClass()) { 104 return (value); 105 } 106 107 if (strings.getClass() == value.getClass()) { 109 try { 110 String values[] = (String []) value; 111 double results[] = new double[values.length]; 112 for (int i = 0; i < values.length; i++) { 113 results[i] = Double.parseDouble(values[i]); 114 } 115 return (results); 116 } catch (Exception e) { 117 if (useDefault) { 118 return (defaultValue); 119 } else { 120 throw new ConversionException(value.toString(), e); 121 } 122 } 123 } 124 125 try { 128 List list = parseElements(value.toString()); 129 double results[] = new double[list.size()]; 130 for (int i = 0; i < results.length; i++) { 131 results[i] = Double.parseDouble((String ) list.get(i)); 132 } 133 return (results); 134 } catch (Exception e) { 135 if (useDefault) { 136 return (defaultValue); 137 } else { 138 throw new ConversionException(value.toString(), e); 139 } 140 } 141 142 } 143 144 145 } 146 | Popular Tags |