Revision 2080 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToByte.java

View differences:

CoerceToByte.java
23 23
 */
24 24
package org.gvsig.tools.dataTypes.impl.coercion;
25 25

  
26
import org.gvsig.tools.dataTypes.AbstractCoercion;
27
import org.gvsig.tools.dataTypes.CoercionContext;
26 28
import org.gvsig.tools.dataTypes.CoercionException;
27
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
29
import org.gvsig.tools.dataTypes.DataTypesManager;
28 30

  
29
public class CoerceToByte implements Coercion {
31
public class CoerceToByte extends AbstractCoercion {
30 32

  
31
    @Override
32
	public Object coerce(Object value) throws CoercionException {
33
    	if( value == null ) {
34
    		return null;
35
    	}
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();
36 43

  
37
		try {
38
			if( ! (value instanceof Byte) ) {
39
				if( value instanceof Number ) {
40
                    value = ((Number) value).byteValue();
41
                } else if( value instanceof Boolean ) {
42
                    return (boolean)value ? 1:0;
43
				} else {
44
					String s = value.toString();
45
                                        if( s == null ) {
46
                                            return null;
47
                                        }
48
                                        s = s.trim().toLowerCase();
49
                                        if( s.length()==0 ) {
50
                                            return null;
51
                                        }
52
					if( s.startsWith("0x")) {
53
						value = Byte.valueOf(s.substring(2), 16);
54
					} else {
55
					    value = Byte.valueOf(value.toString());
56
					}
57
				}
58
			}
59
			return value;
60
		} catch (Exception e) {
61
			throw new CoercionException(e);
62
		}
44
        } else if (value instanceof Boolean) {
45
          num = (byte) ((boolean) value ? 1 : 0);
63 46

  
64
	}
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
          }
61
        }
62
      return num;
63
    } catch (Exception e) {
64
      throw new CoercionException(e);
65
    }
65 66

  
67
  }
68

  
66 69
}

Also available in: Unified diff