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

History | View | Annotate | Download (4.37 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.apache.commons.lang3.StringUtils;
29
import org.gvsig.tools.dataTypes.CoercionException;
30
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
31

    
32
public class CoerceToInt implements CoercionWithLocale {
33

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

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

    
111
}