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