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