gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToDecimal.java @ 2650
History | View | Annotate | Download (6.52 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 2 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.tools.dataTypes.impl.coercion; |
24 |
|
25 |
import java.math.BigDecimal; |
26 |
import java.text.DecimalFormat; |
27 |
import java.text.NumberFormat; |
28 |
import java.text.ParsePosition; |
29 |
import java.util.Date; |
30 |
import java.util.Locale; |
31 |
import org.apache.commons.lang3.BooleanUtils; |
32 |
import org.apache.commons.lang3.StringUtils; |
33 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
34 |
import org.gvsig.tools.dataTypes.Coercion; |
35 |
|
36 |
import org.gvsig.tools.dataTypes.CoercionException; |
37 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
38 |
import org.gvsig.tools.dataTypes.CoercionContext; |
39 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
40 |
import org.gvsig.tools.dataTypes.CoercionContextDecimal; |
41 |
import org.gvsig.tools.dataTypes.CoercionContextDecimalImpl; |
42 |
|
43 |
@SuppressWarnings("UseSpecificCatch") |
44 |
public class CoerceToDecimal extends AbstractCoercion { |
45 |
|
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());
|
52 |
} |
53 |
|
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;
|
73 |
|
74 |
} else if (value instanceof Number) { |
75 |
num = new BigDecimal(((Number) value).doubleValue(), theContext.getMathContext()); |
76 |
|
77 |
} else if (value instanceof Boolean) { |
78 |
num = new BigDecimal((boolean) value ? 1 : 0, theContext.getMathContext()); |
79 |
|
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); |
122 |
} |
123 |
} |
124 |
|
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 |
} |
131 |
|
132 |
public static void main(String[] args) throws CoercionException { |
133 |
BigDecimal n;
|
134 |
|
135 |
n = toBigDecimal(1, 6, 3); |
136 |
System.out.println("BigDecimal(1,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")"); |
137 |
|
138 |
n = toBigDecimal(10, 6, 3); |
139 |
System.out.println("BigDecimal(10,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")"); |
140 |
|
141 |
n = toBigDecimal(12.345, 6, 3); |
142 |
System.out.println("BigDecimal(12.345,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")"); |
143 |
|
144 |
n = toBigDecimal(-12.345, 6, 3); |
145 |
System.out.println("BigDecimal(-12.345,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 |
} |
153 |
} |