1 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 StringToByteConverter extends NumberFormatConverter { 23 private String outOfRangeMessage; 24 private NumberFormat numberFormat; 25 private boolean primitive; 26 27 31 private StringToByteConverter(NumberFormat numberFormat, Class toType) { 32 super(String .class, toType, numberFormat); 33 primitive = toType.isPrimitive(); 34 this.numberFormat = numberFormat; 35 } 36 37 42 public static StringToByteConverter toByte(NumberFormat numberFormat, 43 boolean primitive) { 44 return new StringToByteConverter(numberFormat, (primitive) ? Byte.TYPE : Byte .class); 45 } 46 47 51 public static StringToByteConverter toByte(boolean primitive) { 52 return toByte(NumberFormat.getIntegerInstance(), primitive); 53 } 54 55 58 public Object convert(Object fromObject) { 59 ParseResult result = StringToNumberParser.parse(fromObject, 60 numberFormat, primitive); 61 62 if (result.getPosition() != null) { 63 throw new IllegalArgumentException (StringToNumberParser 67 .createParseErrorMessage((String ) fromObject, result 68 .getPosition())); 69 } else if (result.getNumber() == null) { 70 return null; 73 } 74 75 if (StringToNumberParser.inByteRange(result.getNumber())) { 76 return new Byte (result.getNumber().byteValue()); 77 } 78 79 synchronized (this) { 80 if (outOfRangeMessage == null) { 81 outOfRangeMessage = StringToNumberParser 82 .createOutOfRangeMessage(new Byte (Byte.MIN_VALUE), new Byte (Byte.MAX_VALUE), numberFormat); 83 } 84 85 throw new IllegalArgumentException (outOfRangeMessage); 86 } 87 } 88 } 89 | Popular Tags |