Revision 2080 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToBoolean.java
CoerceToBoolean.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.tools.dataTypes.impl.coercion; |
25 | 25 |
|
26 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
|
26 | 27 |
import org.gvsig.tools.dataTypes.CoercionException; |
27 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
|
|
28 |
import org.gvsig.tools.dataTypes.CoercionContext;
|
|
28 | 29 |
|
29 |
public class CoerceToBoolean implements Coercion {
|
|
30 |
public class CoerceToBoolean extends AbstractCoercion {
|
|
30 | 31 |
|
31 |
public Object coerce(Object value) throws CoercionException { |
|
32 |
if( value == null ) { |
|
33 |
return null; |
|
34 |
} |
|
32 |
@Override |
|
33 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
34 |
if (value == null || value instanceof Boolean) { |
|
35 |
return value; |
|
36 |
} |
|
35 | 37 |
|
36 |
try { |
|
37 |
if (!(value instanceof Boolean)) { |
|
38 |
if (value instanceof Number) { |
|
39 |
if (((Number) value).intValue() == 1) { |
|
40 |
value = Boolean.TRUE; |
|
41 |
} else { |
|
42 |
value = Boolean.FALSE; |
|
43 |
} |
|
44 |
} else { |
|
45 |
String s = value.toString(); |
|
46 |
if( s == null ) { |
|
47 |
return null; |
|
48 |
} |
|
49 |
s = s.trim().toLowerCase(); |
|
50 |
if( s.length()==0 ) { |
|
51 |
return null; |
|
52 |
} |
|
53 |
value = Boolean.valueOf(s); |
|
54 |
} |
|
55 |
} |
|
56 |
return value; |
|
57 |
} catch (Exception e) { |
|
58 |
throw new CoercionException(e); |
|
59 |
} |
|
38 |
try { |
|
39 |
if (value instanceof Number) { |
|
40 |
if (((Number) value).intValue() == 1) { |
|
41 |
value = Boolean.TRUE; |
|
42 |
} else { |
|
43 |
value = Boolean.FALSE; |
|
44 |
} |
|
45 |
} else { |
|
46 |
String s = value.toString(); |
|
47 |
if (s == null) { |
|
48 |
return null; |
|
49 |
} |
|
50 |
s = s.trim().toLowerCase(); |
|
51 |
if (s.length() == 0) { |
|
52 |
return null; |
|
53 |
} |
|
54 |
value = Boolean.valueOf(s); |
|
55 |
} |
|
56 |
return value; |
|
57 |
} catch (Exception e) { |
|
58 |
throw new CoercionException(e); |
|
59 |
} |
|
60 | 60 |
|
61 |
}
|
|
61 |
}
|
|
62 | 62 |
|
63 | 63 |
} |
Also available in: Unified diff