Revision 2080 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
22 22
 */
23 23
package org.gvsig.tools.dataTypes.impl.coercion;
24 24

  
25
import java.text.NumberFormat;
26
import java.text.ParsePosition;
27 25
import java.util.Date;
28
import java.util.Locale;
26
import org.gvsig.tools.dataTypes.AbstractCoercion;
29 27
import org.gvsig.tools.dataTypes.CoercionException;
30
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
28
import org.gvsig.tools.dataTypes.CoercionContext;
31 29

  
32
public class CoerceToLong implements CoercionWithLocale {
30
@SuppressWarnings("UseSpecificCatch")
31
public class CoerceToLong extends AbstractCoercion {
33 32

  
34
    @Override
35
    public Object coerce(Object value) throws CoercionException {
36
        if (value == null) {
37
            return null;
38
        }
39
        try {
40
            if (!(value instanceof Long)) {
41
                if (value instanceof Number) {
42
                    value = ((Number) value).longValue();
43
                } else if( value instanceof Boolean ) {
44
                    return (long)((boolean)value ? 1:0);
45
                } else if (value instanceof Date) {
46
                    value = (long)(((Date)value).getTime());
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
                        value = Long.valueOf(s.substring(2), 16);
58
                    } else {
59
                        try {
60
                            value = Long.valueOf(s);
61
                        } catch (NumberFormatException ex) {
62
                            value = Float.valueOf(s);
63
                            value = ((Number) value).longValue();
64
                        }
65
                    }
66
                }
67
            }
68
            return value;
69
        } catch (Exception e) {
70
            throw new CoercionException(e);
71
        }
33
  @Override
34
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
35
    if (value == null || value instanceof Long) {
36
      return value;
72 37
    }
38
    Long num;
39
    try {
40
      if (value instanceof Number) {
41
        num = ((Number) value).longValue();
73 42

  
74
    @Override
75
    public Object coerce(Object value, Locale locale) throws CoercionException {
76
        if (value == null) {
77
            return null;
43
      } else if (value instanceof Boolean) {
44
        num = ((boolean) value ? 1l : 0l);
45

  
46
      } else if (value instanceof Date) {
47
        num = ((Date) value).getTime();
48

  
49
      } else {
50
        String s = value.toString();
51
        if (s == null) {
52
          return null;
78 53
        }
79
        try {
80
            if (!(value instanceof Long)) {
81
                if (value instanceof Number) {
82
                    value = ((Number) value).longValue();
83
                } else if( value instanceof Boolean ) {
84
                    return (long)((boolean)value ? 1:0);
85
                } else if (value instanceof Date) {
86
                    value = (long)(((Date)value).getTime());
87
                } else {
88
                    String s = value.toString();
89
                    if (s == null) {
90
                        return null;
91
                    }
92
                    s = s.trim().toLowerCase();
93
                    if (s.length() == 0) {
94
                        return null;
95
                    }
96
                    if (s.startsWith("0x")) {
97
                        value = Long.valueOf(s.substring(2), 16);
98
                    } else {
99
                        try {
100
                            value = Long.valueOf(s);
101
                        } catch (NumberFormatException ex) {
102
                            ParsePosition p = new ParsePosition(0);
103
                            NumberFormat nf = NumberFormat.getInstance(locale);
104
                            Number num = nf.parse(s, p);
105
                            if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
106
                                throw new CoercionException("can't coerce to double with locale " + locale.getLanguage());
107
                            }
108
                            value = ((Number) num).longValue();
109
                        }
110
                    }
111
                }
112
            }
113
            return value;
114
        } catch (Exception e) {
115
            throw new CoercionException(e);
54
        s = s.trim().toLowerCase();
55
        if (s.length() == 0) {
56
          return null;
116 57
        }
117
    }
58
        if (s.startsWith("0x")) {
59
          num = Long.valueOf(s.substring(2), 16);
118 60

  
61
        } else {
62
          if (s.startsWith("+")) {
63
            s = s.substring(1);
64
          }
65
          num = Long.valueOf(s);
66
        }
67
      }
68
      return num;
69
    } catch (Exception e) {
70
      throw new CoercionException("Can't coerce '" + value.toString() + "' to long.", e);
71
    }
72
  }
119 73
}

Also available in: Unified diff