1 11 12 package org.eclipse.core.databinding.conversion; 13 14 import java.math.BigDecimal ; 15 import java.math.BigInteger ; 16 17 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser; 18 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult; 19 import org.eclipse.core.internal.databinding.validation.NumberFormatConverter; 20 21 import com.ibm.icu.text.NumberFormat; 22 23 29 public class StringToNumberConverter extends NumberFormatConverter { 30 private Class toType; 31 34 private NumberFormat numberFormat; 35 36 40 private final Number min; 41 45 private final Number max; 46 47 50 private final Class boxedType; 51 52 private static final Integer MIN_INTEGER = new Integer (Integer.MIN_VALUE); 53 private static final Integer MAX_INTEGER = new Integer (Integer.MAX_VALUE); 54 55 private static final Double MIN_DOUBLE = new Double (-Double.MAX_VALUE); 56 private static final Double MAX_DOUBLE = new Double (Double.MAX_VALUE); 57 58 private static final Long MIN_LONG = new Long (Long.MIN_VALUE); 59 private static final Long MAX_LONG = new Long (Long.MIN_VALUE); 60 61 private static final Float MIN_FLOAT = new Float (-Float.MAX_VALUE); 62 private static final Float MAX_FLOAT = new Float (Float.MAX_VALUE); 63 64 77 private StringToNumberConverter(NumberFormat numberFormat, Class toType, 78 Number min, Number max, Class boxedType) { 79 super(String .class, toType, numberFormat); 80 81 this.toType = toType; 82 this.numberFormat = numberFormat; 83 this.min = min; 84 this.max = max; 85 this.boxedType = boxedType; 86 } 87 88 100 public Object convert(Object fromObject) { 101 ParseResult result = StringToNumberParser.parse(fromObject, 102 numberFormat, toType.isPrimitive()); 103 104 if (result.getPosition() != null) { 105 throw new IllegalArgumentException (StringToNumberParser 109 .createParseErrorMessage((String ) fromObject, result 110 .getPosition())); 111 } else if (result.getNumber() == null) { 112 return null; 115 } 116 117 122 if (Integer .class.equals(boxedType)) { 123 if (StringToNumberParser.inIntegerRange(result.getNumber())) { 124 return new Integer (result.getNumber().intValue()); 125 } 126 } else if (Double .class.equals(boxedType)) { 127 if (StringToNumberParser.inDoubleRange(result.getNumber())) { 128 return new Double (result.getNumber().doubleValue()); 129 } 130 } else if (Long .class.equals(boxedType)) { 131 if (StringToNumberParser.inLongRange(result.getNumber())) { 132 return new Long (result.getNumber().longValue()); 133 } 134 } else if (Float .class.equals(boxedType)) { 135 if (StringToNumberParser.inFloatRange(result.getNumber())) { 136 return new Float (result.getNumber().floatValue()); 137 } 138 } else if (BigInteger .class.equals(boxedType)) { 139 return new BigDecimal (result.getNumber().doubleValue()) 140 .toBigInteger(); 141 } 142 143 if (min != null && max != null) { 144 throw new IllegalArgumentException (StringToNumberParser 145 .createOutOfRangeMessage(min, max, numberFormat)); 146 } 147 148 152 throw new IllegalArgumentException ( 153 "Could not convert [" + fromObject + "] to type [" + toType + "]"); } 155 156 161 public static StringToNumberConverter toInteger(boolean primitive) { 162 return toInteger(NumberFormat.getIntegerInstance(), primitive); 163 } 164 165 170 public static StringToNumberConverter toInteger(NumberFormat numberFormat, 171 boolean primitive) { 172 return new StringToNumberConverter(numberFormat, 173 (primitive) ? Integer.TYPE : Integer .class, MIN_INTEGER, 174 MAX_INTEGER, Integer .class); 175 } 176 177 182 public static StringToNumberConverter toDouble(boolean primitive) { 183 return toDouble(NumberFormat.getNumberInstance(), primitive); 184 } 185 186 191 public static StringToNumberConverter toDouble(NumberFormat numberFormat, 192 boolean primitive) { 193 return new StringToNumberConverter(numberFormat, 194 (primitive) ? Double.TYPE : Double .class, MIN_DOUBLE, 195 MAX_DOUBLE, Double .class); 196 } 197 198 203 public static StringToNumberConverter toLong(boolean primitive) { 204 return toLong(NumberFormat.getIntegerInstance(), primitive); 205 } 206 207 212 public static StringToNumberConverter toLong(NumberFormat numberFormat, 213 boolean primitive) { 214 return new StringToNumberConverter(numberFormat, 215 (primitive) ? Long.TYPE : Long .class, MIN_LONG, MAX_LONG, 216 Long .class); 217 } 218 219 224 public static StringToNumberConverter toFloat(boolean primitive) { 225 return toFloat(NumberFormat.getNumberInstance(), primitive); 226 } 227 228 233 public static StringToNumberConverter toFloat(NumberFormat numberFormat, 234 boolean primitive) { 235 return new StringToNumberConverter(numberFormat, 236 (primitive) ? Float.TYPE : Float .class, MIN_FLOAT, MAX_FLOAT, 237 Float .class); 238 } 239 240 243 public static StringToNumberConverter toBigInteger() { 244 return toBigInteger(NumberFormat.getIntegerInstance()); 245 } 246 247 251 public static StringToNumberConverter toBigInteger(NumberFormat numberFormat) { 252 return new StringToNumberConverter(numberFormat, BigInteger .class, 253 null, null, BigInteger .class); 254 } 255 } 256 | Popular Tags |