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 @ 989

History | View | Annotate | Download (2.73 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.dataTypes.impl.coercion;
25

    
26
import java.text.NumberFormat;
27
import java.text.ParsePosition;
28
import java.util.Locale;
29

    
30
import org.gvsig.tools.dataTypes.CoercionException;
31
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
32

    
33
public class CoerceToDouble implements CoercionWithLocale {
34

    
35
        public Object coerce(Object value) throws CoercionException {
36
            if( value == null ) {
37
                    return null;
38
            }
39
                try {
40
                        if (!(value instanceof Double)) {
41
                                if (value instanceof Number) {
42
                                        value = new Double(((Number) value).doubleValue());
43
                                } else {
44
                                        String s = value.toString().trim().toLowerCase();
45
                                        if( s.startsWith("0x")) {
46
                                                value = new Double(Long.parseLong(s.substring(2), 16));
47
                                        } else {
48
                                            value = Double.valueOf(s);
49
                                        }
50
                                }
51
                        }
52
                        return value;
53
                } catch (Exception e) {
54
                        throw new CoercionException(e);
55
                }
56
        }
57

    
58
        public Object coerce(Object value, Locale locale) throws CoercionException {
59
            if( value == null ) {
60
                    return null;
61
            }
62
                try {
63
                        if (!(value instanceof Double)) {
64
                                if (value instanceof Number) {
65
                                        value = new Double(((Number) value).doubleValue());
66
                                } else {
67
                                        String s = value.toString().trim().toLowerCase();
68
                                        if( s.startsWith("0x")) {
69
                                                value = new Double(Long.parseLong(s.substring(2), 16));
70
                                        } else {
71
                                                ParsePosition p = new ParsePosition(0);
72
                                                NumberFormat nf = NumberFormat.getInstance(locale);
73
                                                Number num = nf.parse(s,p);
74
                                                if( p.getErrorIndex()>0 || p.getIndex()<s.length() ) {
75
                                                        throw new CoercionException("can't coerce to double with locale "+locale.getLanguage());
76
                                                }
77
                                                value = new Double(num.doubleValue());
78
                                        }
79
                                }
80
                        }
81
                        return value;
82
                } catch (CoercionException e) {
83
                        throw e;
84
                } catch (Exception e) {
85
                        throw new CoercionException(e);
86
                }
87
        }
88

    
89
}