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 @ 2081
History | View | Annotate | Download (4.15 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 org.apache.commons.lang3.StringUtils; |
31 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
32 |
|
33 |
import org.gvsig.tools.dataTypes.CoercionException; |
34 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
35 |
import org.gvsig.tools.dataTypes.CoercionContext; |
36 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
37 |
import org.gvsig.tools.dataTypes.CoercionContextDecimal; |
38 |
|
39 |
@SuppressWarnings("UseSpecificCatch") |
40 |
public class CoerceToDecimal extends AbstractCoercion { |
41 |
|
42 |
@Override
|
43 |
public Object coerce(Object value) throws CoercionException { |
44 |
if (value == null || value instanceof BigDecimal) { |
45 |
return value;
|
46 |
} |
47 |
return coerce(value, DataTypeUtils.coerceContextDefaultDecimal());
|
48 |
} |
49 |
|
50 |
@Override
|
51 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
52 |
if (value == null || value instanceof BigDecimal) { |
53 |
return value;
|
54 |
} |
55 |
CoercionContextDecimal theContext; |
56 |
if (context instanceof CoercionContextDecimal) { |
57 |
theContext = (CoercionContextDecimal) context; |
58 |
} else {
|
59 |
if (context instanceof CoercionContextLocale) { |
60 |
theContext = DataTypeUtils.coerceContextDecimal(((CoercionContextLocale) context).locale()); |
61 |
} else {
|
62 |
theContext = DataTypeUtils.coerceContextDefaultDecimal(); |
63 |
} |
64 |
} |
65 |
BigDecimal num;
|
66 |
try {
|
67 |
if (value instanceof Number) { |
68 |
num = new BigDecimal(((Number) value).doubleValue(), theContext.getMathContext()); |
69 |
|
70 |
} else if (value instanceof Boolean) { |
71 |
num = new BigDecimal((boolean) value ? 1 : 0, theContext.getMathContext()); |
72 |
|
73 |
} else if (value instanceof Date) { |
74 |
num = new BigDecimal(((Date) value).getTime(), theContext.getMathContext()); |
75 |
|
76 |
} else {
|
77 |
String s = value.toString().trim().toLowerCase();
|
78 |
if (StringUtils.isBlank(s)) {
|
79 |
return null; |
80 |
} |
81 |
if (s.startsWith("0x")) { |
82 |
num = new BigDecimal(Long.parseLong(s.substring(2), 16), theContext.getMathContext()); |
83 |
|
84 |
} else {
|
85 |
if (s.startsWith("+")) { |
86 |
s = s.substring(1);
|
87 |
} |
88 |
ParsePosition p = new ParsePosition(0); |
89 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
90 |
nf.setParseBigDecimal(true);
|
91 |
num = (BigDecimal) nf.parse(s, p);
|
92 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
93 |
throw new CoercionException("Can't coerce '" + s + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
94 |
} |
95 |
} |
96 |
} |
97 |
if( theContext.hasScale() ) {
|
98 |
// Ojo, que este set no es un set. Contruye un nuevo BigDecimal, no
|
99 |
// modifica el existente.
|
100 |
num = num.setScale(theContext.scale(), theContext.roundMode()); |
101 |
} |
102 |
return num;
|
103 |
} catch (CoercionException e) {
|
104 |
throw e;
|
105 |
} catch (Exception e) { |
106 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "'.", e); |
107 |
} |
108 |
} |
109 |
|
110 |
} |