Revision 2650
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/test/java/org/gvsig/tools/datatypes/impl/coercion/CoerceToIntTest.java | ||
---|---|---|
37 | 37 |
public void testCoerce() throws Exception { |
38 | 38 |
CoerceToInt coerce = new CoerceToInt(); |
39 | 39 |
|
40 |
Integer integer12 = new Integer(12);
|
|
40 |
Integer integer12 = 12;
|
|
41 | 41 |
|
42 |
assertCoercion(coerce, new Integer(12), integer12);
|
|
43 |
assertCoercion(coerce, new Long(12l), integer12);
|
|
44 |
assertCoercion(coerce, new Float(12.0f), integer12);
|
|
45 |
assertCoercion(coerce, new Double(12.0d), integer12);
|
|
42 |
assertCoercion(coerce, (int) 12, integer12);
|
|
43 |
assertCoercion(coerce, 12l, integer12);
|
|
44 |
assertCoercion(coerce, 12.0f, integer12);
|
|
45 |
assertCoercion(coerce, 12.0d, integer12);
|
|
46 | 46 |
assertCoercion(coerce, "12", integer12); |
47 | 47 |
assertCoercion(coerce, "0x0C", integer12); |
48 |
assertCoercion(coerce, "t", 1); |
|
49 |
assertCoercion(coerce, "f", 0); |
|
50 |
assertCoercion(coerce, "true", 1); |
|
48 | 51 |
} |
49 | 52 |
|
50 | 53 |
private void assertCoercion(CoerceToInt coerce, Object value, |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/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 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDecimal.java | ||
---|---|---|
28 | 28 |
import java.text.ParsePosition; |
29 | 29 |
import java.util.Date; |
30 | 30 |
import java.util.Locale; |
31 |
import org.apache.commons.lang3.BooleanUtils; |
|
31 | 32 |
import org.apache.commons.lang3.StringUtils; |
32 | 33 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
33 | 34 |
import org.gvsig.tools.dataTypes.Coercion; |
... | ... | |
42 | 43 |
@SuppressWarnings("UseSpecificCatch") |
43 | 44 |
public class CoerceToDecimal extends AbstractCoercion { |
44 | 45 |
|
45 |
@Override |
|
46 |
public Object coerce(Object value) throws CoercionException { |
|
47 |
if (value == null || value instanceof BigDecimal) { |
|
48 |
return value; |
|
46 |
@Override |
|
47 |
public Object coerce(Object value) throws CoercionException { |
|
48 |
if (value == null || value instanceof BigDecimal) { |
|
49 |
return value; |
|
50 |
} |
|
51 |
return coerce(value, DataTypeUtils.coerceContextDefaultDecimal()); |
|
49 | 52 |
} |
50 |
return coerce(value, DataTypeUtils.coerceContextDefaultDecimal()); |
|
51 |
} |
|
52 | 53 |
|
53 |
@Override |
|
54 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
55 |
if (value == null ) { |
|
56 |
return value; |
|
57 |
} |
|
58 |
CoercionContextDecimal theContext; |
|
59 |
if (context instanceof CoercionContextDecimal) { |
|
60 |
theContext = (CoercionContextDecimal) context; |
|
61 |
} else { |
|
62 |
if (context instanceof CoercionContextLocale) { |
|
63 |
theContext = DataTypeUtils.coerceContextDecimal(((CoercionContextLocale) context).locale()); |
|
64 |
} else { |
|
65 |
theContext = DataTypeUtils.coerceContextDefaultDecimal(); |
|
66 |
} |
|
67 |
} |
|
68 |
BigDecimal num; |
|
69 |
try { |
|
70 |
if (value instanceof BigDecimal) { |
|
71 |
num = (BigDecimal) value; |
|
72 |
|
|
73 |
} else if (value instanceof Number) { |
|
74 |
num = new BigDecimal(((Number) value).doubleValue(), theContext.getMathContext()); |
|
54 |
@Override |
|
55 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
56 |
if (value == null) { |
|
57 |
return value; |
|
58 |
} |
|
59 |
CoercionContextDecimal theContext; |
|
60 |
if (context instanceof CoercionContextDecimal) { |
|
61 |
theContext = (CoercionContextDecimal) context; |
|
62 |
} else { |
|
63 |
if (context instanceof CoercionContextLocale) { |
|
64 |
theContext = DataTypeUtils.coerceContextDecimal(((CoercionContextLocale) context).locale()); |
|
65 |
} else { |
|
66 |
theContext = DataTypeUtils.coerceContextDefaultDecimal(); |
|
67 |
} |
|
68 |
} |
|
69 |
BigDecimal num; |
|
70 |
try { |
|
71 |
if (value instanceof BigDecimal) { |
|
72 |
num = (BigDecimal) value; |
|
75 | 73 |
|
76 |
} else if (value instanceof Boolean) { |
|
77 |
num = new BigDecimal((boolean) value ? 1 : 0, theContext.getMathContext()); |
|
78 |
|
|
79 |
} else if (value instanceof Date) { |
|
80 |
num = new BigDecimal(((Date) value).getTime(), theContext.getMathContext()); |
|
74 |
} else if (value instanceof Number) { |
|
75 |
num = new BigDecimal(((Number) value).doubleValue(), theContext.getMathContext()); |
|
81 | 76 |
|
82 |
} else { |
|
83 |
String s = value.toString().trim().toLowerCase(); |
|
84 |
if (StringUtils.isBlank(s)) { |
|
85 |
return null; |
|
86 |
} |
|
87 |
if (s.startsWith("0x")) { |
|
88 |
num = new BigDecimal(Long.parseLong(s.substring(2), 16), theContext.getMathContext()); |
|
77 |
} else if (value instanceof Boolean) { |
|
78 |
num = new BigDecimal((boolean) value ? 1 : 0, theContext.getMathContext()); |
|
89 | 79 |
|
90 |
} else { |
|
91 |
if (s.startsWith("+")) { |
|
92 |
s = s.substring(1); |
|
93 |
} |
|
94 |
ParsePosition p = new ParsePosition(0); |
|
95 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
96 |
nf.setParseBigDecimal(true); |
|
97 |
num = (BigDecimal) nf.parse(s, p); |
|
98 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
99 |
throw new CoercionException("Can't coerce '" + s + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
100 |
} |
|
80 |
} else if (value instanceof Date) { |
|
81 |
num = new BigDecimal(((Date) value).getTime(), theContext.getMathContext()); |
|
82 |
|
|
83 |
} else { |
|
84 |
String s = value.toString().trim().toLowerCase(); |
|
85 |
if (StringUtils.isBlank(s)) { |
|
86 |
return null; |
|
87 |
} |
|
88 |
if (s.startsWith("0x")) { |
|
89 |
num = new BigDecimal(Long.parseLong(s.substring(2), 16), theContext.getMathContext()); |
|
90 |
|
|
91 |
} else { |
|
92 |
Boolean bool = BooleanUtils.toBooleanObject(s); |
|
93 |
if (bool != null) { |
|
94 |
num = ((boolean) bool ? BigDecimal.ONE : BigDecimal.ZERO); |
|
95 |
} else { |
|
96 |
if (s.startsWith("+")) { |
|
97 |
s = s.substring(1); |
|
98 |
} |
|
99 |
ParsePosition p = new ParsePosition(0); |
|
100 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
101 |
nf.setParseBigDecimal(true); |
|
102 |
num = (BigDecimal) nf.parse(s, p); |
|
103 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
104 |
throw new CoercionException("Can't coerce '" + s + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
} |
|
109 |
if (theContext.hasScale() && num.scale() != theContext.scale()) { |
|
110 |
// Ojo, que este set no es un set. Contruye un nuevo BigDecimal, no |
|
111 |
// modifica el existente. |
|
112 |
num = num.setScale(theContext.scale(), theContext.roundMode()); |
|
113 |
} |
|
114 |
if (theContext.hasPrecision() && num.precision() > theContext.precision()) { |
|
115 |
throw new ArithmeticException("Overflow"); |
|
116 |
} |
|
117 |
return num; |
|
118 |
} catch (CoercionException e) { |
|
119 |
throw e; |
|
120 |
} catch (Exception e) { |
|
121 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "'.", e); |
|
101 | 122 |
} |
102 |
} |
|
103 |
if( theContext.hasScale() && num.scale()!=theContext.scale()) { |
|
104 |
// Ojo, que este set no es un set. Contruye un nuevo BigDecimal, no |
|
105 |
// modifica el existente. |
|
106 |
num = num.setScale(theContext.scale(), theContext.roundMode()); |
|
107 |
} |
|
108 |
if( theContext.hasPrecision() && num.precision()>theContext.precision() ) { |
|
109 |
throw new ArithmeticException("Overflow"); |
|
110 |
} |
|
111 |
return num; |
|
112 |
} catch (CoercionException e) { |
|
113 |
throw e; |
|
114 |
} catch (Exception e) { |
|
115 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "'.", e); |
|
116 | 123 |
} |
117 |
} |
|
118 | 124 |
|
119 |
private static BigDecimal toBigDecimal(Object value, int precision, int scale) throws CoercionException { |
|
120 |
CoercionContextDecimalImpl context = new CoercionContextDecimalImpl(Locale.ENGLISH, precision, scale); |
|
121 |
Coercion c = new CoerceToDecimal(); |
|
122 |
BigDecimal x = (BigDecimal) c.coerce(value,context); |
|
123 |
return x; |
|
124 |
} |
|
125 |
|
|
126 |
public static void main(String[] args) throws CoercionException { |
|
127 |
BigDecimal n; |
|
125 |
private static BigDecimal toBigDecimal(Object value, int precision, int scale) throws CoercionException { |
|
126 |
CoercionContextDecimalImpl context = new CoercionContextDecimalImpl(Locale.ENGLISH, precision, scale); |
|
127 |
Coercion c = new CoerceToDecimal(); |
|
128 |
BigDecimal x = (BigDecimal) c.coerce(value, context); |
|
129 |
return x; |
|
130 |
} |
|
128 | 131 |
|
129 |
n = toBigDecimal(1,6,3);
|
|
130 |
System.out.println("BigDecimal(1,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
132 |
public static void main(String[] args) throws CoercionException {
|
|
133 |
BigDecimal n;
|
|
131 | 134 |
|
132 |
n = toBigDecimal(10,6,3);
|
|
133 |
System.out.println("BigDecimal(10,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
135 |
n = toBigDecimal(1, 6, 3);
|
|
136 |
System.out.println("BigDecimal(1,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")");
|
|
134 | 137 |
|
135 |
n = toBigDecimal(12.345,6,3);
|
|
136 |
System.out.println("BigDecimal(12.345,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
138 |
n = toBigDecimal(10, 6, 3);
|
|
139 |
System.out.println("BigDecimal(10,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")");
|
|
137 | 140 |
|
138 |
n = toBigDecimal(-12.345,6,3);
|
|
139 |
System.out.println("BigDecimal(-12.345,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
141 |
n = toBigDecimal(12.345, 6, 3);
|
|
142 |
System.out.println("BigDecimal(12.345,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")");
|
|
140 | 143 |
|
141 |
n = toBigDecimal(12.3457,6,3);
|
|
142 |
System.out.println("BigDecimal(12.3457,6,3): "+n+"("+n.precision()+","+n.scale()+")");
|
|
144 |
n = toBigDecimal(-12.345, 6, 3);
|
|
145 |
System.out.println("BigDecimal(-12.345,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")");
|
|
143 | 146 |
|
144 |
n = toBigDecimal(120.3457,6,3); |
|
145 |
System.out.println("BigDecimal(120.3457,6,3): "+n+"("+n.precision()+","+n.scale()+")"); |
|
146 |
} |
|
147 |
n = toBigDecimal(12.3457, 6, 3); |
|
148 |
System.out.println("BigDecimal(12.3457,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")"); |
|
149 |
|
|
150 |
n = toBigDecimal(120.3457, 6, 3); |
|
151 |
System.out.println("BigDecimal(120.3457,6,3): " + n + "(" + n.precision() + "," + n.scale() + ")"); |
|
152 |
} |
|
147 | 153 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToLong.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 CoerceToLong extends AbstractCoercion { |
37 | 38 |
|
38 |
@Override |
|
39 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
40 |
if (value == null || value instanceof Long) { |
|
41 |
return value; |
|
42 |
} |
|
43 |
CoercionContextLocale theContext; |
|
44 |
if (context instanceof CoercionContextLocale) { |
|
45 |
theContext = (CoercionContextLocale) context; |
|
46 |
} else { |
|
47 |
theContext = DataTypeUtils.coerceContextDefaultLocale(); |
|
48 |
} |
|
49 |
Number num; |
|
50 |
try { |
|
51 |
if (value instanceof Number) { |
|
52 |
num = (Number) value; |
|
39 |
@Override
|
|
40 |
public Object coerce(Object value, CoercionContext context) throws CoercionException {
|
|
41 |
if (value == null || value instanceof Long) {
|
|
42 |
return value;
|
|
43 |
}
|
|
44 |
CoercionContextLocale theContext;
|
|
45 |
if (context instanceof CoercionContextLocale) {
|
|
46 |
theContext = (CoercionContextLocale) context;
|
|
47 |
} else {
|
|
48 |
theContext = DataTypeUtils.coerceContextDefaultLocale();
|
|
49 |
}
|
|
50 |
Number num;
|
|
51 |
try {
|
|
52 |
if (value instanceof Number) {
|
|
53 |
num = (Number) value;
|
|
53 | 54 |
|
54 |
} else if (value instanceof Boolean) { |
|
55 |
num = ((boolean) value ? 1l : 0l); |
|
55 |
} else if (value instanceof Boolean) {
|
|
56 |
num = ((boolean) value ? 1l : 0l);
|
|
56 | 57 |
|
57 |
} else if (value instanceof Date) { |
|
58 |
num = ((Date) value).getTime(); |
|
58 |
} else if (value instanceof Date) {
|
|
59 |
num = ((Date) value).getTime();
|
|
59 | 60 |
|
60 |
} else { |
|
61 |
String s = value.toString(); |
|
62 |
if (s == null) { |
|
63 |
return null; |
|
64 |
} |
|
65 |
s = s.trim().toLowerCase(); |
|
66 |
if (s.length() == 0) { |
|
67 |
return null; |
|
68 |
} |
|
69 |
if (s.startsWith("0x")) { |
|
70 |
num = Long.valueOf(s.substring(2), 16); |
|
61 |
} else {
|
|
62 |
String s = value.toString();
|
|
63 |
if (s == null) {
|
|
64 |
return null;
|
|
65 |
}
|
|
66 |
s = s.trim().toLowerCase();
|
|
67 |
if (s.length() == 0) {
|
|
68 |
return null;
|
|
69 |
}
|
|
70 |
if (s.startsWith("0x")) {
|
|
71 |
num = Long.valueOf(s.substring(2), 16);
|
|
71 | 72 |
|
72 |
} else { |
|
73 |
if (s.startsWith("+")) { |
|
74 |
s = s.substring(1); |
|
75 |
} |
|
76 |
ParsePosition p = new ParsePosition(0); |
|
77 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
78 |
num = nf.parse(s, p); |
|
79 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
80 |
throw new CoercionException("Can't coerce '" + s + "' to long with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
81 |
} |
|
73 |
} else { |
|
74 |
Boolean bool = BooleanUtils.toBooleanObject(s); |
|
75 |
if (bool != null) { |
|
76 |
num = ((boolean) bool ? 1l : 0l); |
|
77 |
} else { |
|
78 |
if (s.startsWith("+")) { |
|
79 |
s = s.substring(1); |
|
80 |
} |
|
81 |
ParsePosition p = new ParsePosition(0); |
|
82 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
83 |
num = nf.parse(s, p); |
|
84 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
85 |
throw new CoercionException("Can't coerce '" + s + "' to long with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
86 |
} |
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
return num.longValue(); |
|
91 |
} catch (Exception e) { |
|
92 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to long.", e); |
|
82 | 93 |
} |
83 |
} |
|
84 |
return num.longValue(); |
|
85 |
} catch (Exception e) { |
|
86 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to long.", e); |
|
87 | 94 |
} |
88 |
} |
|
89 | 95 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToByte.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.tools.dataTypes.impl.coercion; |
25 | 25 |
|
26 |
import org.apache.commons.lang3.BooleanUtils; |
|
26 | 27 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
27 | 28 |
import org.gvsig.tools.dataTypes.CoercionContext; |
28 | 29 |
import org.gvsig.tools.dataTypes.CoercionException; |
... | ... | |
30 | 31 |
|
31 | 32 |
public class CoerceToByte extends AbstractCoercion { |
32 | 33 |
|
33 |
@Override |
|
34 |
@SuppressWarnings("UseSpecificCatch") |
|
35 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
36 |
if (value == null || value instanceof Byte) { |
|
37 |
return value; |
|
38 |
} |
|
39 |
Byte num; |
|
40 |
try { |
|
41 |
if (value instanceof Number) { |
|
42 |
num = ((Number) value).byteValue(); |
|
34 |
@Override
|
|
35 |
@SuppressWarnings("UseSpecificCatch")
|
|
36 |
public Object coerce(Object value, CoercionContext context) throws CoercionException {
|
|
37 |
if (value == null || value instanceof Byte) {
|
|
38 |
return value;
|
|
39 |
}
|
|
40 |
Byte num;
|
|
41 |
try {
|
|
42 |
if (value instanceof Number) {
|
|
43 |
num = ((Number) value).byteValue();
|
|
43 | 44 |
|
44 |
} else if (value instanceof Boolean) { |
|
45 |
num = (byte) ((boolean) value ? 1 : 0); |
|
45 |
} else if (value instanceof Boolean) {
|
|
46 |
num = (byte) ((boolean) value ? 1 : 0);
|
|
46 | 47 |
|
47 |
} else { |
|
48 |
String s = value.toString(); |
|
49 |
if (s == null) { |
|
50 |
return null; |
|
51 |
} |
|
52 |
s = s.trim().toLowerCase(); |
|
53 |
if (s.length() == 0) { |
|
54 |
return null; |
|
55 |
} |
|
56 |
if (s.startsWith("0x")) { |
|
57 |
num = Byte.valueOf(s.substring(2), 16); |
|
58 |
} else { |
|
59 |
num = Byte.valueOf(value.toString()); |
|
60 |
} |
|
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 |
num = Byte.valueOf(s.substring(2), 16); |
|
59 |
} else { |
|
60 |
Boolean bool = BooleanUtils.toBooleanObject(s); |
|
61 |
if (bool != null) { |
|
62 |
num = ((boolean) bool ? (byte) 1 :(byte) 0); |
|
63 |
} else { |
|
64 |
num = Byte.valueOf(value.toString()); |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
return num; |
|
69 |
} catch (Exception e) { |
|
70 |
throw new CoercionException(e); |
|
61 | 71 |
} |
62 |
return num; |
|
63 |
} catch (Exception e) { |
|
64 |
throw new CoercionException(e); |
|
72 |
|
|
65 | 73 |
} |
66 | 74 |
|
67 |
} |
|
68 |
|
|
69 | 75 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/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 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDouble.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.Coercion; |
31 | 32 |
|
... | ... | |
37 | 38 |
@SuppressWarnings("UseSpecificCatch") |
38 | 39 |
public class CoerceToDouble extends AbstractCoercion { |
39 | 40 |
|
40 |
@Override |
|
41 |
public Object coerce(Object value) throws CoercionException { |
|
42 |
if (value == null || value instanceof Double) { |
|
43 |
return value; |
|
41 |
@Override |
|
42 |
public Object coerce(Object value) throws CoercionException { |
|
43 |
if (value == null || value instanceof Double) { |
|
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 Double) { |
|
51 |
return value; |
|
52 |
} |
|
53 |
CoercionContextLocale theContext; |
|
54 |
if (context instanceof CoercionContextLocale) { |
|
55 |
theContext = (CoercionContextLocale) context; |
|
56 |
} else { |
|
57 |
theContext = DataTypeUtils.coerceContextDefaultLocale(); |
|
58 |
} |
|
59 |
Double num; |
|
60 |
try { |
|
61 |
if (value instanceof Number) { |
|
62 |
num = ((Number) value).doubleValue(); |
|
63 |
|
|
64 |
} else if (value instanceof Boolean) { |
|
65 |
num = ((boolean) value ? 1d : 0d); |
|
66 |
|
|
67 |
} else if (value instanceof Date) { |
|
68 |
num = (double) (((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 Double) { |
|
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 = (double) 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 double with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
91 |
} |
|
92 |
num = n.doubleValue(); |
|
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 |
Double num; |
|
61 |
try { |
|
62 |
if (value instanceof Number) { |
|
63 |
num = ((Number) value).doubleValue(); |
|
64 |
|
|
65 |
} else if (value instanceof Boolean) { |
|
66 |
num = ((boolean) value ? 1d : 0d); |
|
67 |
|
|
68 |
} else if (value instanceof Date) { |
|
69 |
num = (double) (((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 = (double) Long.parseLong(s.substring(2), 16); |
|
82 |
|
|
83 |
} else { |
|
84 |
Boolean bool = BooleanUtils.toBooleanObject(s); |
|
85 |
if (bool != null) { |
|
86 |
num = ((boolean) bool ? 1d : 0d); |
|
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 double with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
96 |
} |
|
97 |
num = n.doubleValue(); |
|
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 |
|
103 |
public static void main(String[] args) throws CoercionException { |
|
104 |
Coercion toDouble = new CoerceToDouble(); |
|
105 |
|
|
106 |
Double x = (double) toDouble.coerce("10.5"); |
|
107 |
System.out.println(x); |
|
108 |
x = (double) toDouble.coerce("123456789.5"); |
|
109 |
System.out.println(x); |
|
110 |
x = (double) toDouble.coerce("123,456,789.5"); |
|
111 |
System.out.println(x); |
|
112 |
} |
|
109 |
public static void main(String[] args) throws CoercionException {
|
|
110 |
Coercion toDouble = new CoerceToDouble();
|
|
111 |
|
|
112 |
Double x = (double) toDouble.coerce("10.5");
|
|
113 |
System.out.println(x);
|
|
114 |
x = (double) toDouble.coerce("123456789.5");
|
|
115 |
System.out.println(x);
|
|
116 |
x = (double) toDouble.coerce("123,456,789.5");
|
|
117 |
System.out.println(x);
|
|
118 |
}
|
|
113 | 119 |
} |
Also available in: Unified diff