Revision 2650 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToLong.java

View differences:

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
}

Also available in: Unified diff