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

History | View | Annotate | Download (4.82 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.Date;
28
import java.util.Locale;
29
import org.apache.commons.lang3.StringUtils;
30
import org.gvsig.tools.dataTypes.CoercionException;
31
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
32

    
33
public class CoerceToInt implements CoercionWithLocale {
34

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

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

    
120
}