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 |
/**
|
---|---|
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.text.DecimalFormat; |
26 |
import java.text.NumberFormat; |
27 |
import java.text.ParsePosition; |
28 |
import java.util.Date; |
29 |
import org.apache.commons.lang3.BooleanUtils; |
30 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
31 |
import org.gvsig.tools.dataTypes.CoercionContext; |
32 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
33 |
|
34 |
import org.gvsig.tools.dataTypes.CoercionException; |
35 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
36 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
37 |
|
38 |
@SuppressWarnings("UseSpecificCatch") |
39 |
public class CoerceToFloat extends AbstractCoercion { |
40 |
|
41 |
@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 |
} |
48 |
|
49 |
@Override
|
50 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
51 |
if (value == null || value instanceof Float) { |
52 |
return value;
|
53 |
} |
54 |
CoercionContextLocale theContext; |
55 |
if (context instanceof CoercionContextLocale) { |
56 |
theContext = (CoercionContextLocale) context; |
57 |
} else {
|
58 |
theContext = DataTypeUtils.coerceContextDefaultLocale(); |
59 |
} |
60 |
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 |
} |
108 |
} |