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

History | View | Annotate | Download (3.9 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.apache.commons.lang3.BooleanUtils;
30
import org.gvsig.tools.dataTypes.AbstractCoercion;
31
import org.gvsig.tools.dataTypes.CoercionException;
32
import org.gvsig.tools.dataTypes.CoercionContext;
33
import org.gvsig.tools.dataTypes.CoercionContextLocale;
34
import org.gvsig.tools.dataTypes.DataTypeUtils;
35

    
36
@SuppressWarnings("UseSpecificCatch")
37
public class CoerceToInt extends AbstractCoercion {
38

    
39
    @Override
40
    public Object coerce(Object value) throws CoercionException {
41
        if (value == null || value instanceof Integer) {
42
            return value;
43
        }
44
        return coerce(value, null);
45
    }
46

    
47
    @Override
48
    public Object coerce(Object value, CoercionContext context) throws CoercionException {
49
        if (value == null || value instanceof Integer) {
50
            return value;
51
        }
52
        CoercionContextLocale theContext;
53
        if (context instanceof CoercionContextLocale) {
54
            theContext = (CoercionContextLocale) context;
55
        } else {
56
            theContext = DataTypeUtils.coerceContextDefaultLocale();
57
        }
58
        Number num;
59
        try {
60
            if (value instanceof Number) {
61
                num = (Number) value;
62

    
63
            } else if (value instanceof Boolean) {
64
                num = ((boolean) value ? 1 : 0);
65

    
66
            } else if (value instanceof Date) {
67
                num = ((Date) value).getTime();
68

    
69
            } else {
70
                String s = value.toString();
71
                if (s == null) {
72
                    return null;
73
                }
74
                s = s.trim().toLowerCase();
75
                if (s.length() == 0) {
76
                    return null;
77
                } else if (s.startsWith("0x")) {
78
                    num = Integer.valueOf(s.substring(2), 16);
79

    
80
                } else {
81
                    Boolean bool = BooleanUtils.toBooleanObject(s);
82
                    if (bool != null) {
83
                        num = ((boolean) bool ? 1 : 0);
84
                    } else {
85
                        if (s.startsWith("+")) {
86
                            s = s.substring(1);
87
                        }
88
                        ParsePosition p = new ParsePosition(0);
89
                        DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale());
90
                        num = nf.parse(s, p);
91
                        if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) {
92
                            throw new CoercionException("Can't coerce '" + s + "' to int with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ").");
93
                        }
94
                    }
95
                }
96
            }
97
            return num.intValue();
98
        } catch (Exception e) {
99
            throw new CoercionException("Can't coerce '" + value.toString() + "' to integer.", e);
100
        }
101
    }
102

    
103
}