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 28 public class IntegerToStringConverter extends Converter { 29 private final boolean primitive; 30 private final NumberFormat numberFormat; 31 private final Class boxedType; 32 33 38 private IntegerToStringConverter(NumberFormat numberFormat, Class fromType, 39 Class boxedType) { 40 super(fromType, String .class); 41 this.primitive = fromType.isPrimitive(); 42 this.numberFormat = numberFormat; 43 this.boxedType = boxedType; 44 } 45 46 51 public Object convert(Object fromObject) { 52 if (fromObject == null && !primitive) { 54 return ""; } 56 57 if (!boxedType.isInstance(fromObject)) { 58 throw new IllegalArgumentException ( 59 "'fromObject' is not of type [" + boxedType + "]."); } 61 62 return numberFormat.format(((Number ) fromObject).longValue()); 63 } 64 65 69 public static IntegerToStringConverter fromShort(boolean primitive) { 70 return fromShort(NumberFormat.getIntegerInstance(), primitive); 71 } 72 73 78 public static IntegerToStringConverter fromShort(NumberFormat numberFormat, 79 boolean primitive) { 80 return new IntegerToStringConverter(numberFormat, 81 primitive ? Short.TYPE : Short .class, Short .class); 82 } 83 84 88 public static IntegerToStringConverter fromByte(boolean primitive) { 89 return fromByte(NumberFormat.getIntegerInstance(), primitive); 90 } 91 92 97 public static IntegerToStringConverter fromByte(NumberFormat numberFormat, 98 boolean primitive) { 99 return new IntegerToStringConverter(numberFormat, primitive ? Byte.TYPE 100 : Byte .class, Byte .class); 101 } 102 } 103 | Popular Tags |