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

History | View | Annotate | Download (4.04 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
import org.gvsig.tools.dataTypes.CoercionException;
29
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
30

    
31
public class CoerceToLong implements CoercionWithLocale {
32

    
33
    @Override
34
    public Object coerce(Object value) throws CoercionException {
35
        if (value == null) {
36
            return null;
37
        }
38
        try {
39
            if (!(value instanceof Long)) {
40
                if (value instanceof Number) {
41
                    value = ((Number) value).longValue();
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 = Long.valueOf(s.substring(2), 16);
53
                    } else {
54
                        try {
55
                            value = Long.valueOf(s);
56
                        } catch (NumberFormatException ex) {
57
                            value = Float.valueOf(s);
58
                            value = ((Number) value).longValue();
59
                        }
60
                    }
61
                }
62
            }
63
            return value;
64
        } catch (Exception e) {
65
            throw new CoercionException(e);
66
        }
67
    }
68

    
69
    @Override
70
    public Object coerce(Object value, Locale locale) throws CoercionException {
71
        if (value == null) {
72
            return null;
73
        }
74
        try {
75
            if (!(value instanceof Long)) {
76
                if (value instanceof Number) {
77
                    value = ((Number) value).longValue();
78
                } else {
79
                    String s = value.toString();
80
                    if (s == null) {
81
                        return null;
82
                    }
83
                    s = s.trim().toLowerCase();
84
                    if (s.length() == 0) {
85
                        return null;
86
                    }
87
                    if (s.startsWith("0x")) {
88
                        value = Long.valueOf(s.substring(2), 16);
89
                    } else {
90
                        try {
91
                            value = Long.valueOf(s);
92
                        } catch (NumberFormatException ex) {
93
                            ParsePosition p = new ParsePosition(0);
94
                            NumberFormat nf = NumberFormat.getInstance(locale);
95
                            Number num = nf.parse(s, p);
96
                            if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
97
                                throw new CoercionException("can't coerce to double with locale " + locale.getLanguage());
98
                            }
99
                            value = ((Number) num).longValue();
100
                        }
101
                    }
102
                }
103
            }
104
            return value;
105
        } catch (Exception e) {
106
            throw new CoercionException(e);
107
        }
108
    }
109

    
110
}