1 11 12 package org.eclipse.core.internal.databinding.conversion; 13 14 import org.eclipse.core.databinding.conversion.Converter; 15 16 import com.ibm.icu.text.NumberFormat; 17 18 26 public abstract class NumberToNumberConverter extends Converter { 27 private NumberFormat numberFormat; 28 29 private boolean primitive; 30 31 private String outOfRangeMessage; 32 33 protected NumberToNumberConverter(NumberFormat numberFormat, 34 Class fromType, Class toType) { 35 super(fromType, toType); 36 this.numberFormat = numberFormat; 37 this.primitive = toType.isPrimitive(); 38 } 39 40 45 public final Object convert(Object fromObject) { 46 if (fromObject == null) { 47 if (primitive) { 48 throw new IllegalArgumentException ( 49 "Parameter 'fromObject' cannot be null."); } 51 52 return null; 53 } 54 55 if (!(fromObject instanceof Number )) { 56 throw new IllegalArgumentException ( 57 "Parameter 'fromObject' must be of type Number."); } 59 60 Number number = (Number ) fromObject; 61 Number result = doConvert(number); 62 63 if (result != null) { 64 return result; 65 } 66 67 synchronized (this) { 68 if (outOfRangeMessage == null) { 69 outOfRangeMessage = StringToNumberParser 70 .createOutOfRangeMessage(new Short (Short.MIN_VALUE), 71 new Short (Short.MAX_VALUE), numberFormat); 72 } 73 74 throw new IllegalArgumentException (outOfRangeMessage); 75 } 76 } 77 78 85 protected abstract Number doConvert(Number number); 86 87 93 public NumberFormat getNumberFormat() { 94 return numberFormat; 95 } 96 } 97 | Popular Tags |