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

View differences:

CoerceToDouble.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.Coercion;
31 32

  
......
37 38
@SuppressWarnings("UseSpecificCatch")
38 39
public class CoerceToDouble extends AbstractCoercion {
39 40

  
40
  @Override
41
  public Object coerce(Object value) throws CoercionException {
42
    if (value == null || value instanceof Double) {
43
      return value;
41
    @Override
42
    public Object coerce(Object value) throws CoercionException {
43
        if (value == null || value instanceof Double) {
44
            return value;
45
        }
46
        return coerce(value, DataTypeUtils.coerceContextDefaultDecimal());
44 47
    }
45
    return coerce(value, DataTypeUtils.coerceContextDefaultDecimal());
46
  }
47 48

  
48
  @Override
49
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
50
    if (value == null || value instanceof Double) {
51
      return value;
52
    }
53
    CoercionContextLocale theContext;
54
    if (context instanceof CoercionContextLocale) {
55
      theContext = (CoercionContextLocale) context;
56
    } else {
57
      theContext = DataTypeUtils.coerceContextDefaultLocale();
58
    }
59
    Double num;
60
    try {
61
      if (value instanceof Number) {
62
        num = ((Number) value).doubleValue();
63
        
64
      } else if (value instanceof Boolean) {
65
        num = ((boolean) value ? 1d : 0d);
66
        
67
      } else if (value instanceof Date) {
68
        num = (double) (((Date) value).getTime());
69
        
70
      } else {
71
        String s = value.toString();
72
        if (s == null) {
73
          return null;
49
    @Override
50
    public Object coerce(Object value, CoercionContext context) throws CoercionException {
51
        if (value == null || value instanceof Double) {
52
            return value;
74 53
        }
75
        s = s.trim().toLowerCase();
76
        if (s.length() == 0) {
77
          return null;
78
        }
79
        if (s.startsWith("0x")) {
80
          num = (double) Long.parseLong(s.substring(2), 16);
81
          
54
        CoercionContextLocale theContext;
55
        if (context instanceof CoercionContextLocale) {
56
            theContext = (CoercionContextLocale) context;
82 57
        } else {
83
          if (s.startsWith("+")) {
84
            s = s.substring(1);
85
          }
86
          ParsePosition p = new ParsePosition(0);
87
          DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale());
88
          Number n = nf.parse(s, p);
89
          if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
90
            throw new CoercionException("Can't coerce '" + s + "' to double with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ").");
91
          }
92
          num = n.doubleValue();
58
            theContext = DataTypeUtils.coerceContextDefaultLocale();
93 59
        }
94
      }
95
      return num;
96
    } catch (CoercionException e) {
97
      throw e;
98
    } catch (Exception e) {
99
      throw new CoercionException("Can't coerce '" + value.toString() + "' to double with locale " + theContext.locale().getLanguage() + "'.", e);
60
        Double num;
61
        try {
62
            if (value instanceof Number) {
63
                num = ((Number) value).doubleValue();
64

  
65
            } else if (value instanceof Boolean) {
66
                num = ((boolean) value ? 1d : 0d);
67

  
68
            } else if (value instanceof Date) {
69
                num = (double) (((Date) value).getTime());
70

  
71
            } else {
72
                String s = value.toString();
73
                if (s == null) {
74
                    return null;
75
                }
76
                s = s.trim().toLowerCase();
77
                if (s.length() == 0) {
78
                    return null;
79
                }
80
                if (s.startsWith("0x")) {
81
                    num = (double) Long.parseLong(s.substring(2), 16);
82

  
83
                } else {
84
                    Boolean bool = BooleanUtils.toBooleanObject(s);
85
                    if (bool != null) {
86
                        num = ((boolean) bool ? 1d : 0d);
87
                    } else {
88
                        if (s.startsWith("+")) {
89
                            s = s.substring(1);
90
                        }
91
                        ParsePosition p = new ParsePosition(0);
92
                        DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale());
93
                        Number n = nf.parse(s, p);
94
                        if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
95
                            throw new CoercionException("Can't coerce '" + s + "' to double with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ").");
96
                        }
97
                        num = n.doubleValue();
98
                    }
99
                }
100
            }
101
            return num;
102
        } catch (CoercionException e) {
103
            throw e;
104
        } catch (Exception e) {
105
            throw new CoercionException("Can't coerce '" + value.toString() + "' to double with locale " + theContext.locale().getLanguage() + "'.", e);
106
        }
100 107
    }
101
  }
102 108

  
103
  public static void main(String[] args) throws CoercionException {
104
    Coercion toDouble = new CoerceToDouble();
105
    
106
    Double x = (double) toDouble.coerce("10.5");
107
    System.out.println(x);
108
    x = (double) toDouble.coerce("123456789.5");
109
    System.out.println(x);
110
    x = (double) toDouble.coerce("123,456,789.5");
111
    System.out.println(x);
112
  }
109
    public static void main(String[] args) throws CoercionException {
110
        Coercion toDouble = new CoerceToDouble();
111

  
112
        Double x = (double) toDouble.coerce("10.5");
113
        System.out.println(x);
114
        x = (double) toDouble.coerce("123456789.5");
115
        System.out.println(x);
116
        x = (double) toDouble.coerce("123,456,789.5");
117
        System.out.println(x);
118
    }
113 119
}

Also available in: Unified diff