gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToFloat.java @ 2650
History | View | Annotate | Download (4.19 KB)
1 | 802 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 1405 | jjdelcerro | * 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 | 802 | cordinyana | *
|
11 | 1405 | jjdelcerro | * 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 | 802 | cordinyana | *
|
16 | 1405 | jjdelcerro | * 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 | 802 | cordinyana | *
|
20 | 1405 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 802 | cordinyana | */
|
23 | 112 | jjdelcerro | package org.gvsig.tools.dataTypes.impl.coercion; |
24 | |||
25 | 2080 | jjdelcerro | import java.text.DecimalFormat; |
26 | 989 | jjdelcerro | import java.text.NumberFormat; |
27 | import java.text.ParsePosition; |
||
28 | 1588 | jjdelcerro | import java.util.Date; |
29 | 2650 | omartinez | import org.apache.commons.lang3.BooleanUtils; |
30 | 2080 | jjdelcerro | import org.gvsig.tools.dataTypes.AbstractCoercion; |
31 | import org.gvsig.tools.dataTypes.CoercionContext; |
||
32 | import org.gvsig.tools.dataTypes.CoercionContextLocale; |
||
33 | 989 | jjdelcerro | |
34 | 112 | jjdelcerro | import org.gvsig.tools.dataTypes.CoercionException; |
35 | 2080 | jjdelcerro | import org.gvsig.tools.dataTypes.DataTypeUtils; |
36 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
37 | 112 | jjdelcerro | |
38 | 2080 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
39 | public class CoerceToFloat extends AbstractCoercion { |
||
40 | 112 | jjdelcerro | |
41 | 2650 | omartinez | @Override
|
42 | public Object coerce(Object value) throws CoercionException { |
||
43 | if (value == null || value instanceof Float) { |
||
44 | return value;
|
||
45 | } |
||
46 | return coerce(value, DataTypeUtils.coerceContextDefaultDecimal());
|
||
47 | 1405 | jjdelcerro | } |
48 | 112 | jjdelcerro | |
49 | 2650 | omartinez | @Override
|
50 | public Object coerce(Object value, CoercionContext context) throws CoercionException { |
||
51 | if (value == null || value instanceof Float) { |
||
52 | return value;
|
||
53 | 1405 | jjdelcerro | } |
54 | 2650 | omartinez | CoercionContextLocale theContext; |
55 | if (context instanceof CoercionContextLocale) { |
||
56 | theContext = (CoercionContextLocale) context; |
||
57 | 2080 | jjdelcerro | } else {
|
58 | 2650 | omartinez | theContext = DataTypeUtils.coerceContextDefaultLocale(); |
59 | 2080 | jjdelcerro | } |
60 | 2650 | omartinez | Float num;
|
61 | try {
|
||
62 | if (value instanceof Number) { |
||
63 | num = ((Number) value).floatValue();
|
||
64 | |||
65 | } else if (value instanceof Boolean) { |
||
66 | num = ((boolean) value ? 1f : 0f); |
||
67 | |||
68 | } else if (value instanceof Date) { |
||
69 | num = (float) (((Date) value).getTime()); |
||
70 | |||
71 | } else {
|
||
72 | String s = value.toString();
|
||
73 | if (s == null) { |
||
74 | return null; |
||
75 | } |
||
76 | s = s.trim().toLowerCase(); |
||
77 | if (s.length() == 0) { |
||
78 | return null; |
||
79 | } |
||
80 | if (s.startsWith("0x")) { |
||
81 | num = (float) Long.parseLong(s.substring(2), 16); |
||
82 | |||
83 | } else {
|
||
84 | Boolean bool = BooleanUtils.toBooleanObject(s);
|
||
85 | if (bool != null) { |
||
86 | num = ((boolean) bool ? 1f : 0f); |
||
87 | } else {
|
||
88 | if (s.startsWith("+")) { |
||
89 | s = s.substring(1);
|
||
90 | } |
||
91 | ParsePosition p = new ParsePosition(0); |
||
92 | DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
||
93 | Number n = nf.parse(s, p);
|
||
94 | if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
||
95 | throw new CoercionException("Can't coerce '" + s + "' to float with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
||
96 | } |
||
97 | num = n.floatValue(); |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | return num;
|
||
102 | } catch (CoercionException e) {
|
||
103 | throw e;
|
||
104 | } catch (Exception e) { |
||
105 | throw new CoercionException("Can't coerce '" + value.toString() + "' to double with locale " + theContext.locale().getLanguage() + "'.", e); |
||
106 | } |
||
107 | 1405 | jjdelcerro | } |
108 | 112 | jjdelcerro | } |