1 11 12 package org.eclipse.core.databinding.conversion; 13 14 import java.math.BigInteger ; 15 16 import com.ibm.icu.text.NumberFormat; 17 18 24 public class NumberToStringConverter extends Converter { 25 private final NumberFormat numberFormat; 26 private final Class fromType; 27 private boolean fromTypeIsLong; 28 private boolean fromTypeIsDecimalType; 29 private boolean fromTypeIsBigInteger; 30 31 40 private NumberToStringConverter(NumberFormat numberFormat, Class fromType) { 41 super(fromType, String .class); 42 43 this.numberFormat = numberFormat; 44 this.fromType = fromType; 45 46 if (Integer .class.equals(fromType) || Integer.TYPE.equals(fromType) 47 || Long .class.equals(fromType) || Long.TYPE.equals(fromType)) { 48 fromTypeIsLong = true; 49 } else if (Float .class.equals(fromType) || Float.TYPE.equals(fromType) 50 || Double .class.equals(fromType) 51 || Double.TYPE.equals(fromType)) { 52 fromTypeIsDecimalType = true; 53 } else if (BigInteger .class.equals(fromType)) { 54 fromTypeIsBigInteger = true; 55 } 56 } 57 58 69 public Object convert(Object fromObject) { 70 if (fromObject == null && !fromType.isPrimitive()) { 72 return ""; } 74 75 Number number = (Number ) fromObject; 76 String result = null; 77 if (fromTypeIsLong) { 78 synchronized (numberFormat) { 79 result = numberFormat.format(number.longValue()); 80 } 81 } else if (fromTypeIsDecimalType) { 82 synchronized (numberFormat) { 83 result = numberFormat.format(number.doubleValue()); 84 } 85 } else if (fromTypeIsBigInteger) { 86 synchronized (numberFormat) { 87 result = numberFormat.format((BigInteger ) number); 88 } 89 } 90 91 return result; 92 } 93 94 99 public static NumberToStringConverter fromDouble(boolean primitive) { 100 return fromDouble(NumberFormat.getNumberInstance(), primitive); 101 } 102 103 108 public static NumberToStringConverter fromDouble(NumberFormat numberFormat, 109 boolean primitive) { 110 return new NumberToStringConverter(numberFormat, 111 (primitive) ? Double.TYPE : Double .class); 112 } 113 114 119 public static NumberToStringConverter fromLong(boolean primitive) { 120 return fromLong(NumberFormat.getIntegerInstance(), primitive); 121 } 122 123 128 public static NumberToStringConverter fromLong(NumberFormat numberFormat, 129 boolean primitive) { 130 return new NumberToStringConverter(numberFormat, 131 (primitive) ? Long.TYPE : Long .class); 132 } 133 134 139 public static NumberToStringConverter fromFloat(boolean primitive) { 140 return fromFloat(NumberFormat.getNumberInstance(), primitive); 141 } 142 143 148 public static NumberToStringConverter fromFloat(NumberFormat numberFormat, 149 boolean primitive) { 150 return new NumberToStringConverter(numberFormat, 151 (primitive) ? Float.TYPE : Float .class); 152 } 153 154 159 public static NumberToStringConverter fromInteger(boolean primitive) { 160 return fromInteger(NumberFormat.getIntegerInstance(), primitive); 161 } 162 163 168 public static NumberToStringConverter fromInteger( 169 NumberFormat numberFormat, boolean primitive) { 170 return new NumberToStringConverter(numberFormat, 171 (primitive) ? Integer.TYPE : Integer .class); 172 } 173 174 177 public static NumberToStringConverter fromBigInteger() { 178 return fromBigInteger(NumberFormat.getIntegerInstance()); 179 } 180 181 185 public static NumberToStringConverter fromBigInteger( 186 NumberFormat numberFormat) { 187 return new NumberToStringConverter(numberFormat, BigInteger .class); 188 } 189 } 190 | Popular Tags |