Revision 2650 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDecimal.java
CoerceToDecimal.java | ||
---|---|---|
28 | 28 |
import java.text.ParsePosition; |
29 | 29 |
import java.util.Date; |
30 | 30 |
import java.util.Locale; |
31 |
import org.apache.commons.lang3.BooleanUtils; |
|
31 | 32 |
import org.apache.commons.lang3.StringUtils; |
32 | 33 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
33 | 34 |
import org.gvsig.tools.dataTypes.Coercion; |
... | ... | |
42 | 43 |
@SuppressWarnings("UseSpecificCatch") |
43 | 44 |
public class CoerceToDecimal extends AbstractCoercion { |
44 | 45 |
|
45 |
@Override |
|
46 |
public Object coerce(Object value) throws CoercionException { |
|
47 |
if (value == null || value instanceof BigDecimal) { |
|
48 |
return value; |
|
46 |
@Override |
|
47 |
public Object coerce(Object value) throws CoercionException { |
|
48 |
if (value == null || value instanceof BigDecimal) { |
|
49 |
return value; |
|
50 |
} |
|
51 |
return coerce(value, DataTypeUtils.coerceContextDefaultDecimal()); |
|
49 | 52 |
} |
50 |
return coerce(value, DataTypeUtils.coerceContextDefaultDecimal()); |
|
51 |
} |
|
52 | 53 |
|
53 |
@Override |
|
54 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
55 |
if (value == null ) { |
|
56 |
return value; |
|
57 |
} |
|
58 |
CoercionContextDecimal theContext; |
|
59 |
if (context instanceof CoercionContextDecimal) { |
|
60 |
theContext = (CoercionContextDecimal) context; |
|
61 |
} else { |
|
62 |
if (context instanceof CoercionContextLocale) { |
|
63 |
theContext = DataTypeUtils.coerceContextDecimal(((CoercionContextLocale) context).locale()); |
|
64 |
} else { |
|
65 |
theContext = DataTypeUtils.coerceContextDefaultDecimal(); |
|
66 |
} |
|
67 |
} |
|
68 |
BigDecimal num; |
|
69 |
try { |
|
70 |
if (value instanceof BigDecimal) { |
|
71 |
num = (BigDecimal) value; |
|
72 |
|
|
73 |
} else if (value instanceof Number) { |
|
74 |
num = new BigDecimal(((Number) value).doubleValue(), theContext.getMathContext()); |
|
54 |
@Override |
|
55 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
56 |
if (value == null) { |
|
57 |
return value; |
|
58 |
} |
|
59 |
CoercionContextDecimal theContext; |
|
60 |
if (context instanceof CoercionContextDecimal) { |
|
61 |
theContext = (CoercionContextDecimal) context; |
|
62 |
} else { |
|
63 |
if (context instanceof CoercionContextLocale) { |
|
64 |
theContext = DataTypeUtils.coerceContextDecimal(((CoercionContextLocale) context).locale()); |
|
65 |
} else { |
|
66 |
theContext = DataTypeUtils.coerceContextDefaultDecimal(); |
|
67 |
} |
|
68 |
} |
|
69 |
BigDecimal num; |
|
70 |
try { |
|
71 |
if (value instanceof BigDecimal) { |
|
72 |
num = (BigDecimal) value; |
|
75 | 73 |
|
76 |
} else if (value instanceof Boolean) { |
|
77 |
num = new BigDecimal((boolean) value ? 1 : 0, theContext.getMathContext()); |
|
78 |
|
|
79 |
} else if (value instanceof Date) { |
|
80 |
num = new BigDecimal(((Date) value).getTime(), theContext.getMathContext()); |
|
74 |
} else if (value instanceof Number) { |
|
75 |
num = new BigDecimal(((Number) value).doubleValue(), theContext.getMathContext()); |
|
81 | 76 |
|
82 |
} else { |
|
83 |
String s = value.toString().trim().toLowerCase(); |
|
84 |
if (StringUtils.isBlank(s)) { |
|
85 |
return null; |
|
86 |
} |
|
87 |
if (s.startsWith("0x")) { |
|
88 |
num = new BigDecimal(Long.parseLong(s.substring(2), 16), theContext.getMathContext()); |
|
77 |
} else if (value instanceof Boolean) { |
|
78 |
num = new BigDecimal((boolean) value ? 1 : 0, theContext.getMathContext()); |
|
89 | 79 |
|
90 |
} else { |
|
91 |
if (s.startsWith("+")) { |
|
92 |
s = s.substring(1); |
|
93 |
} |
|
94 |
ParsePosition p = new ParsePosition(0); |
|
95 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
96 |
nf.setParseBigDecimal(true); |
|
97 |
num = (BigDecimal) nf.parse(s, p); |
|
98 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
99 |
throw new CoercionException("Can't coerce '" + s + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
100 |
} |
|
80 |
} else if (value instanceof Date) { |
|
81 |
num = new BigDecimal(((Date) value).getTime(), theContext.getMathContext()); |
|
82 |
|
|
83 |
} else { |
|
84 |
String s = value.toString().trim().toLowerCase(); |
|
85 |
if (StringUtils.isBlank(s)) { |
|
86 |
return null; |
|
87 |
} |
|
88 |
if (s.startsWith("0x")) { |
|
89 |
num = new BigDecimal(Long.parseLong(s.substring(2), 16), theContext.getMathContext()); |
|
90 |
|
|
91 |
} else { |
|
92 |
Boolean bool = BooleanUtils.toBooleanObject(s); |
|
93 |
if (bool != null) { |
|
94 |
num = ((boolean) bool ? BigDecimal.ONE : BigDecimal.ZERO); |
|
95 |
} else { |
|
96 |
if (s.startsWith("+")) { |
|
97 |
s = s.substring(1); |
|
98 |
} |
|
99 |
ParsePosition p = new ParsePosition(0); |
|
100 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
101 |
nf.setParseBigDecimal(true); |
|
102 |
num = (BigDecimal) nf.parse(s, p); |
|
103 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
104 |
throw new CoercionException("Can't coerce '" + s + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
} |
|
109 |
if (theContext.hasScale() && num.scale() != theContext.scale()) { |
|
110 |
// Ojo, que este set no es un set. Contruye un nuevo BigDecimal, no |
|
111 |
// modifica el existente. |
|
112 |
num = num.setScale(theContext.scale(), theContext.roundMode()); |
|
113 |
} |
|
114 |
if (theContext.hasPrecision() && num.precision() > theContext.precision()) { |
|
115 |
throw new ArithmeticException("Overflow"); |
|
116 |
} |
|
117 |
return num; |
|
118 |
} catch (CoercionException e) { |
|
119 |
throw e; |
|
120 |
} catch (Exception e) { |
|
121 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "'.", e); |
|
101 | 122 |
} |
102 |
} |
|
103 |
if( theContext.hasScale() && num.scale()!=theContext.scale()) { |
|
104 |
// Ojo, que este set no es un set. Contruye un nuevo BigDecimal, no |
|
105 |
// modifica el existente. |
|
106 |
num = num.setScale(theContext.scale(), theContext.roundMode()); |
|
107 |
} |
|
108 |
if( theContext.hasPrecision() && num.precision()>theContext.precision() ) { |
|
109 |
throw new ArithmeticException("Overflow"); |
|
110 |
} |
|
111 |
return num; |
|
112 |
} catch (CoercionException e) { |
|
113 |
throw e; |
|
114 |
} catch (Exception e) { |
|
115 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "'.", e); |
|
116 | 123 |
} |
117 |
} |
|
118 | 124 |
|
119 |
private static BigDecimal toBigDecimal(Object value, int precision, int scale) throws CoercionException { |
|
120 |
CoercionContextDecimalImpl context = new CoercionContextDecimalImpl(Locale.ENGLISH, precision, scale); |
|
121 |
Coercion c = new CoerceToDecimal(); |
|
122 |
BigDecimal x = (BigDecimal) c.coerce(value,context); |
|
123 |
return x; |
|
124 |
} |
|
125 |
|
|
126 |
public static void main(String[] args) throws CoercionException { |
|
127 |
BigDecimal n; |
|
125 |
private static BigDecimal toBigDecimal(Object value, int precision, int scale) throws CoercionException { |
|
126 |
CoercionContextDecimalImpl context = new CoercionContextDecimalImpl(Locale.ENGLISH, precision, scale); |
|
127 |
Coercion c = new CoerceToDecimal(); |
|
128 |
BigDecimal x = (BigDecimal) c.coerce(value, context); |
|
129 |
return x; |
|
130 |
} |
|
128 | 131 |
|
129 |
n = toBigDecimal(1,6,3);
|
|
130 |
System.out.println("BigDecimal(1,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
132 |
public static void main(String[] args) throws CoercionException {
|
|
133 |
BigDecimal n;
|
|
131 | 134 |
|
132 |
n = toBigDecimal(10,6,3);
|
|
133 |
System.out.println("BigDecimal(10,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
135 |
n = toBigDecimal(1, 6, 3);
|
|
136 |
System.out.println("BigDecimal(1,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")");
|
|
134 | 137 |
|
135 |
n = toBigDecimal(12.345,6,3);
|
|
136 |
System.out.println("BigDecimal(12.345,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
138 |
n = toBigDecimal(10, 6, 3);
|
|
139 |
System.out.println("BigDecimal(10,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")");
|
|
137 | 140 |
|
138 |
n = toBigDecimal(-12.345,6,3);
|
|
139 |
System.out.println("BigDecimal(-12.345,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
141 |
n = toBigDecimal(12.345, 6, 3);
|
|
142 |
System.out.println("BigDecimal(12.345,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")");
|
|
140 | 143 |
|
141 |
n = toBigDecimal(12.3457,6,3);
|
|
142 |
System.out.println("BigDecimal(12.3457,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
144 |
n = toBigDecimal(-12.345, 6, 3);
|
|
145 |
System.out.println("BigDecimal(-12.345,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")");
|
|
143 | 146 |
|
144 |
n = toBigDecimal(120.3457,6,3); |
|
145 |
System.out.println("BigDecimal(120.3457,6,3): "+n+"("+n.precision()+","+n.scale()+")"); |
|
146 |
} |
|
147 |
n = toBigDecimal(12.3457, 6, 3); |
|
148 |
System.out.println("BigDecimal(12.3457,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")"); |
|
149 |
|
|
150 |
n = toBigDecimal(120.3457, 6, 3); |
|
151 |
System.out.println("BigDecimal(120.3457,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")"); |
|
152 |
} |
|
147 | 153 |
} |
Also available in: Unified diff