gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToDouble.java @ 989
History | View | Annotate | Download (2.73 KB)
1 | 802 | cordinyana | /**
|
---|---|---|---|
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
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 112 | jjdelcerro | package org.gvsig.tools.dataTypes.impl.coercion; |
25 | |||
26 | 989 | jjdelcerro | import java.text.NumberFormat; |
27 | import java.text.ParsePosition; |
||
28 | import java.util.Locale; |
||
29 | |||
30 | 112 | jjdelcerro | import org.gvsig.tools.dataTypes.CoercionException; |
31 | 989 | jjdelcerro | import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
32 | 112 | jjdelcerro | |
33 | 989 | jjdelcerro | public class CoerceToDouble implements CoercionWithLocale { |
34 | 112 | jjdelcerro | |
35 | public Object coerce(Object value) throws CoercionException { |
||
36 | 962 | jjdelcerro | if( value == null ) { |
37 | return null; |
||
38 | } |
||
39 | 112 | jjdelcerro | try {
|
40 | if (!(value instanceof Double)) { |
||
41 | if (value instanceof Number) { |
||
42 | value = new Double(((Number) value).doubleValue()); |
||
43 | } else {
|
||
44 | 144 | jjdelcerro | String s = value.toString().trim().toLowerCase();
|
45 | if( s.startsWith("0x")) { |
||
46 | value = new Double(Long.parseLong(s.substring(2), 16)); |
||
47 | } else {
|
||
48 | value = Double.valueOf(s);
|
||
49 | } |
||
50 | 112 | jjdelcerro | } |
51 | } |
||
52 | return value;
|
||
53 | } catch (Exception e) { |
||
54 | throw new CoercionException(e); |
||
55 | } |
||
56 | 989 | jjdelcerro | } |
57 | 112 | jjdelcerro | |
58 | 989 | jjdelcerro | public Object coerce(Object value, Locale locale) throws CoercionException { |
59 | if( value == null ) { |
||
60 | return null; |
||
61 | } |
||
62 | try {
|
||
63 | if (!(value instanceof Double)) { |
||
64 | if (value instanceof Number) { |
||
65 | value = new Double(((Number) value).doubleValue()); |
||
66 | } else {
|
||
67 | String s = value.toString().trim().toLowerCase();
|
||
68 | if( s.startsWith("0x")) { |
||
69 | value = new Double(Long.parseLong(s.substring(2), 16)); |
||
70 | } else {
|
||
71 | ParsePosition p = new ParsePosition(0); |
||
72 | NumberFormat nf = NumberFormat.getInstance(locale); |
||
73 | Number num = nf.parse(s,p);
|
||
74 | if( p.getErrorIndex()>0 || p.getIndex()<s.length() ) { |
||
75 | throw new CoercionException("can't coerce to double with locale "+locale.getLanguage()); |
||
76 | } |
||
77 | value = new Double(num.doubleValue()); |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | return value;
|
||
82 | } catch (CoercionException e) {
|
||
83 | throw e;
|
||
84 | } catch (Exception e) { |
||
85 | throw new CoercionException(e); |
||
86 | } |
||
87 | 112 | jjdelcerro | } |
88 | |||
89 | } |