Revision 2080 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDouble.java
CoerceToDouble.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package org.gvsig.tools.dataTypes.impl.coercion; |
24 | 24 |
|
25 |
import java.text.DecimalFormat; |
|
25 | 26 |
import java.text.NumberFormat; |
26 | 27 |
import java.text.ParsePosition; |
27 | 28 |
import java.util.Date; |
28 |
import java.util.Locale; |
|
29 |
import org.apache.commons.lang3.ArrayUtils; |
|
30 |
import org.apache.commons.lang3.StringUtils; |
|
29 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
|
31 | 30 |
|
32 | 31 |
import org.gvsig.tools.dataTypes.CoercionException; |
33 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
|
32 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
|
33 |
import org.gvsig.tools.dataTypes.CoercionContext; |
|
34 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
|
34 | 35 |
|
35 |
public class CoerceToDouble implements CoercionWithLocale { |
|
36 |
@SuppressWarnings("UseSpecificCatch") |
|
37 |
public class CoerceToDouble extends AbstractCoercion { |
|
36 | 38 |
|
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 |
} |
|
39 |
@Override |
|
40 |
public Object coerce(Object value) throws CoercionException { |
|
41 |
if (value == null || value instanceof Double) { |
|
42 |
return value; |
|
69 | 43 |
} |
44 |
return coerce(value, DataTypeUtils.coerceContextDefaultLocale()); |
|
45 |
} |
|
70 | 46 |
|
71 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
|
72 |
if (value == null) { |
|
73 |
return null; |
|
47 |
@Override |
|
48 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
49 |
if (value == null || value instanceof Double) { |
|
50 |
return value; |
|
51 |
} |
|
52 |
CoercionContextLocale theContext; |
|
53 |
if (context instanceof CoercionContextLocale) { |
|
54 |
theContext = (CoercionContextLocale) context; |
|
55 |
} else { |
|
56 |
theContext = DataTypeUtils.coerceContextDefaultLocale(); |
|
57 |
} |
|
58 |
Double num; |
|
59 |
try { |
|
60 |
if (value instanceof Number) { |
|
61 |
num = ((Number) value).doubleValue(); |
|
62 |
|
|
63 |
} else if (value instanceof Boolean) { |
|
64 |
num = ((boolean) value ? 1d : 0d); |
|
65 |
|
|
66 |
} else if (value instanceof Date) { |
|
67 |
num = (double) (((Date) value).getTime()); |
|
68 |
|
|
69 |
} else { |
|
70 |
String s = value.toString(); |
|
71 |
if (s == null) { |
|
72 |
return null; |
|
74 | 73 |
} |
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); |
|
74 |
s = s.trim().toLowerCase(); |
|
75 |
if (s.length() == 0) { |
|
76 |
return null; |
|
112 | 77 |
} |
78 |
if (s.startsWith("0x")) { |
|
79 |
num = (double) Long.parseLong(s.substring(2), 16); |
|
80 |
|
|
81 |
} else { |
|
82 |
if (s.startsWith("+")) { |
|
83 |
s = s.substring(1); |
|
84 |
} |
|
85 |
ParsePosition p = new ParsePosition(0); |
|
86 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
87 |
Number n = nf.parse(s, p); |
|
88 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
89 |
throw new CoercionException("Can't coerce '" + s + "' to double with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
90 |
} |
|
91 |
num = n.doubleValue(); |
|
92 |
} |
|
93 |
} |
|
94 |
return num; |
|
95 |
} catch (CoercionException e) { |
|
96 |
throw e; |
|
97 |
} catch (Exception e) { |
|
98 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to double with locale " + theContext.locale().getLanguage() + "'.", e); |
|
113 | 99 |
} |
100 |
} |
|
114 | 101 |
|
115 | 102 |
} |
Also available in: Unified diff