Revision 2092 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToInt.java

View differences:

CoerceToInt.java
22 22
 */
23 23
package org.gvsig.tools.dataTypes.impl.coercion;
24 24

  
25
import java.text.DecimalFormat;
26
import java.text.NumberFormat;
27
import java.text.ParsePosition;
25 28
import java.util.Date;
26 29
import org.gvsig.tools.dataTypes.AbstractCoercion;
27 30
import org.gvsig.tools.dataTypes.CoercionException;
28 31
import org.gvsig.tools.dataTypes.CoercionContext;
32
import org.gvsig.tools.dataTypes.CoercionContextLocale;
33
import org.gvsig.tools.dataTypes.DataTypeUtils;
29 34

  
30 35
@SuppressWarnings("UseSpecificCatch")
31 36
public class CoerceToInt extends AbstractCoercion {
......
43 48
    if (value == null || value instanceof Integer) {
44 49
      return value;
45 50
    }
46
    Integer num;
51
    CoercionContextLocale theContext;
52
    if (context instanceof CoercionContextLocale) {
53
      theContext = (CoercionContextLocale) context;
54
    } else {
55
      theContext = DataTypeUtils.coerceContextDefaultLocale();
56
    }
57
    Number num;
47 58
    try {
48 59
      if (value instanceof Number) {
49
        num = ((Number) value).intValue();
60
        num = (Number) value;
50 61

  
51 62
      } else if (value instanceof Boolean) {
52 63
        num = ((boolean) value ? 1 : 0);
53 64

  
54 65
      } else if (value instanceof Date) {
55
        num = (int) ((Date) value).getTime();
66
        num = ((Date) value).getTime();
56 67

  
57 68
      } else {
58 69
        String s = value.toString();
......
70 81
          if (s.startsWith("+")) {
71 82
            s = s.substring(1);
72 83
          }
73
          num = Integer.valueOf(s);
84
          ParsePosition p = new ParsePosition(0);
85
          DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale());
86
          num = nf.parse(s, p);
87
          if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
88
            throw new CoercionException("Can't coerce '" + s + "' to int with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ").");
89
          }
74 90
        }
75 91
      }
76
      return num;
92
      return num.intValue();
77 93
    } catch (Exception e) {
78 94
      throw new CoercionException("Can't coerce '" + value.toString() + "' to integer.", e);
79 95
    }

Also available in: Unified diff