Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToDouble.java @ 2172

History | View | Annotate | Download (3.91 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 2 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.tools.dataTypes.impl.coercion;
24

    
25
import java.text.DecimalFormat;
26
import java.text.NumberFormat;
27
import java.text.ParsePosition;
28
import java.util.Date;
29
import org.gvsig.tools.dataTypes.AbstractCoercion;
30
import org.gvsig.tools.dataTypes.Coercion;
31

    
32
import org.gvsig.tools.dataTypes.CoercionException;
33
import org.gvsig.tools.dataTypes.DataTypeUtils;
34
import org.gvsig.tools.dataTypes.CoercionContext;
35
import org.gvsig.tools.dataTypes.CoercionContextLocale;
36

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

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

    
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;
74
        }
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
          
82
        } 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();
93
        }
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);
100
    }
101
  }
102

    
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
  }
113
}