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