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

View differences:

CoerceToLong.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;
26 28
import org.gvsig.tools.dataTypes.CoercionException;
27
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
29
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
28 30

  
29
public class CoerceToLong implements Coercion {
31
public class CoerceToLong implements CoercionWithLocale {
30 32

  
31
	public Object coerce(Object value) throws CoercionException {
32
    	if( value == null ) {
33
    		return null;
34
    	}
35
		try {
36
			if( ! (value instanceof Long) ) {
37
				if( value instanceof Number ) {
38
					value = new Long(((Number)value).longValue());
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 = Long.valueOf(s.substring(2), 16);
50
					} else {
51
					    value = Long.valueOf(s);
52
					}
53
				}
54
			}
55
			return value;
56
		} catch(Exception e) {
57
			throw new CoercionException(e); 
58
		}
59
	}
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
    }
60 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

  
61 110
}

Also available in: Unified diff