Revision 2650 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToByte.java
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 |
} |
Also available in: Unified diff