1 32 package net.sf.retrotranslator.runtime.java.text; 33 34 import java.math.BigDecimal ; 35 import java.text.*; 36 import net.sf.retrotranslator.runtime.impl.*; 37 38 41 public class _DecimalFormat { 42 43 private static final WeakIdentityTable<DecimalFormat, _DecimalFormat> formats = 44 new WeakIdentityTable<DecimalFormat, _DecimalFormat>() { 45 protected _DecimalFormat initialValue() { 46 return new _DecimalFormat(); 47 } 48 }; 49 50 private boolean parseBigDecimal; 51 52 protected _DecimalFormat() { 53 } 54 55 @Advanced 56 public static boolean isParseBigDecimal(DecimalFormat format) { 57 _DecimalFormat value = formats.lookup(format); 58 return value != null && value.parseBigDecimal; 59 } 60 61 @Advanced 62 public static void setParseBigDecimal(DecimalFormat format, boolean newValue) { 63 formats.obtain(format).parseBigDecimal = newValue; 64 } 65 66 @Advanced 67 public static Number parse(DecimalFormat format, String source) throws ParseException { 68 return correctNumber(format, format.parse(source)); 69 } 70 71 @Advanced 72 public static Number parse(DecimalFormat format, String source, ParsePosition parsePosition) { 73 return correctNumber(format, format.parse(source, parsePosition)); 74 } 75 76 @Advanced 77 public static Object parseObject(DecimalFormat format, String source) throws ParseException { 78 return correctObject(format, format.parseObject(source)); 79 } 80 81 @Advanced 82 public static Object parseObject(DecimalFormat format, String source, ParsePosition pos) { 83 return correctObject(format, format.parseObject(source, pos)); 84 } 85 86 private static Number correctNumber(DecimalFormat format, Number number) { 87 if (number == null || !isParseBigDecimal(format)) { 88 return number; 89 } 90 if (number instanceof Double ) { 91 Double result = (Double ) number; 92 if (result.isInfinite() || result.isNaN()) { 93 return result; 94 } 95 } 96 return new BigDecimal (number.toString()); 97 } 98 99 private static Object correctObject(DecimalFormat format, Object object) { 100 return object instanceof Number ? correctNumber(format, (Number ) object) : object; 101 } 102 103 protected static Number fixNumber(Format format, Number number) { 104 return format instanceof DecimalFormat ? correctNumber((DecimalFormat) format, number) : number; 105 } 106 107 protected static Object fixObject(Format format, Object object) { 108 return object instanceof Number ? fixNumber(format, (Number ) object) : object; 109 } 110 111 } 112 | Popular Tags |