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 / CoerceToFloat.java @ 1405

History | View | Annotate | Download (3.49 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.NumberFormat;
26
import java.text.ParsePosition;
27
import java.util.Locale;
28

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

    
32
public class CoerceToFloat implements CoercionWithLocale {
33

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

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

    
95
}