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 @ 1588
History | View | Annotate | Download (4.61 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.NumberFormat; |
26 |
import java.text.ParsePosition; |
27 |
import java.util.Date; |
28 |
import java.util.Locale; |
29 |
import org.apache.commons.lang3.ArrayUtils; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
|
32 |
import org.gvsig.tools.dataTypes.CoercionException; |
33 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
34 |
|
35 |
public class CoerceToDouble implements CoercionWithLocale { |
36 |
|
37 |
public Object coerce(Object value) throws CoercionException { |
38 |
if (value == null) { |
39 |
return null; |
40 |
} |
41 |
try {
|
42 |
if (!(value instanceof Double)) { |
43 |
if (value instanceof Number) { |
44 |
value = ((Number) value).doubleValue();
|
45 |
} else if( value instanceof Boolean ) { |
46 |
return (double)((boolean)value ? 1:0); |
47 |
} else if (value instanceof Date) { |
48 |
value = (double)(((Date)value).getTime()); |
49 |
} else {
|
50 |
String s = value.toString();
|
51 |
if (s == null) { |
52 |
return null; |
53 |
} |
54 |
s = s.trim().toLowerCase(); |
55 |
if (s.length() == 0) { |
56 |
return null; |
57 |
} |
58 |
if (s.startsWith("0x")) { |
59 |
value = new Double(Long.parseLong(s.substring(2), 16)); |
60 |
} else {
|
61 |
value = Double.valueOf(s);
|
62 |
} |
63 |
} |
64 |
} |
65 |
return value;
|
66 |
} catch (Exception e) { |
67 |
throw new CoercionException(e); |
68 |
} |
69 |
} |
70 |
|
71 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
72 |
if (value == null) { |
73 |
return null; |
74 |
} |
75 |
try {
|
76 |
if (!(value instanceof Double)) { |
77 |
if (value instanceof Number) { |
78 |
value = ((Number) value).doubleValue();
|
79 |
} else if( value instanceof Boolean ) { |
80 |
return (double)((boolean)value ? 1:0); |
81 |
} else if (value instanceof Date) { |
82 |
value = (double)(((Date)value).getTime()); |
83 |
} else {
|
84 |
String s = value.toString().trim().toLowerCase();
|
85 |
if( StringUtils.isBlank(s) ) {
|
86 |
return null; |
87 |
} |
88 |
if (s.startsWith("0x")) { |
89 |
value = new Double(Long.parseLong(s.substring(2), 16)); |
90 |
} else {
|
91 |
if( locale == null ) { |
92 |
locale = Locale.getDefault();
|
93 |
} |
94 |
if( s.startsWith("+") ) { |
95 |
s = s.substring(1);
|
96 |
} |
97 |
ParsePosition p = new ParsePosition(0); |
98 |
NumberFormat nf = NumberFormat.getInstance(locale); |
99 |
Number num = nf.parse(s, p);
|
100 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
101 |
throw new CoercionException("Can't coerce '"+s+"' to double with locale " + (locale==null?"null":locale.getLanguage()) + "(error index "+p.getErrorIndex()+", index "+p.getIndex()+")."); |
102 |
} |
103 |
value = new Double(num.doubleValue()); |
104 |
} |
105 |
} |
106 |
} |
107 |
return value;
|
108 |
} catch (CoercionException e) {
|
109 |
throw e;
|
110 |
} catch (Exception e) { |
111 |
throw new CoercionException("Can't coerce '"+(value==null?"null":value.toString())+"' to double with locale " + (locale==null?"null":locale.getLanguage())+"'.",e); |
112 |
} |
113 |
} |
114 |
|
115 |
} |