1 11 12 package org.eclipse.core.internal.databinding.conversion; 13 14 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult; 15 import org.eclipse.core.internal.databinding.validation.NumberFormatConverter; 16 17 import com.ibm.icu.text.NumberFormat; 18 19 22 public class StringToShortConverter extends NumberFormatConverter { 23 private final NumberFormat numberFormat; 24 private final boolean primitive; 25 26 private String outOfRangeMessage; 27 28 31 private StringToShortConverter(NumberFormat numberFormat, Class toType) { 32 super(String .class, toType, numberFormat); 33 this.numberFormat = numberFormat; 34 primitive = toType.isPrimitive(); 35 } 36 37 42 public Object convert(Object fromObject) { 43 ParseResult result = StringToNumberParser.parse(fromObject, 44 numberFormat, primitive); 45 46 if (result.getPosition() != null) { 47 throw new IllegalArgumentException (StringToNumberParser 51 .createParseErrorMessage((String ) fromObject, result 52 .getPosition())); 53 } else if (result.getNumber() == null) { 54 return null; 57 } 58 59 if (StringToNumberParser.inShortRange(result.getNumber())) { 60 return new Short (result.getNumber().shortValue()); 61 } 62 63 synchronized (this) { 64 if (outOfRangeMessage == null) { 65 outOfRangeMessage = StringToNumberParser 66 .createOutOfRangeMessage(new Short (Short.MIN_VALUE), new Short (Short.MAX_VALUE), numberFormat); 67 } 68 69 throw new IllegalArgumentException (outOfRangeMessage); 70 } 71 } 72 73 78 public static StringToShortConverter toShort(boolean primitive) { 79 return toShort(NumberFormat.getIntegerInstance(), primitive); 80 } 81 82 87 public static StringToShortConverter toShort(NumberFormat numberFormat, 88 boolean primitive) { 89 return new StringToShortConverter(numberFormat, 90 (primitive) ? Short.TYPE : Short .class); 91 } 92 } 93 | Popular Tags |