Revision 1405 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToInt.java

View differences:

CoerceToInt.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
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 10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
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 15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
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.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.tools.dataTypes.impl.coercion;
25 24

  
25
import java.text.NumberFormat;
26
import java.text.ParsePosition;
27
import java.util.Locale;
28
import org.apache.commons.lang3.StringUtils;
26 29
import org.gvsig.tools.dataTypes.CoercionException;
27
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
30
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
28 31

  
29
public class CoerceToInt implements Coercion {
32
public class CoerceToInt implements CoercionWithLocale {
30 33

  
31
	public Object coerce(Object value) throws CoercionException {
32
    	if( value == null ) {
33
    		return null;
34
    	}
35
		try {
36
			if( ! (value instanceof Integer) ) {
37
				if( value instanceof Number ) {
38
                    value = new Integer(((Number) value).intValue());
39
				} else {
40
					String s = value.toString();
41
                                        if( s == null ) {
42
                                            return null;
43
                                        }
44
                                        s = s.trim().toLowerCase();
45
                                        if( s.length()==0 ) {
46
                                            return null;
47
                                        }
48
					if( s.startsWith("0x")) {
49
						value = Integer.valueOf(s.substring(2), 16);
50
					} else {
51
					    value = Integer.valueOf(s);
52
					}
53
				}
54
			}
55
			return value;
56
		} catch(Exception e) {
57
			throw new CoercionException(e); 
58
		}
59
	}
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
    }
60 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

  
61 111
}

Also available in: Unified diff