gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToLong.java @ 1588
History | View | Annotate | Download (4.51 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.gvsig.tools.dataTypes.CoercionException; |
30 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
31 |
|
32 |
public class CoerceToLong 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 Long)) { |
41 |
if (value instanceof Number) { |
42 |
value = ((Number) value).longValue();
|
43 |
} else if( value instanceof Boolean ) { |
44 |
return (long)((boolean)value ? 1:0); |
45 |
} else if (value instanceof Date) { |
46 |
value = (long)(((Date)value).getTime()); |
47 |
} else {
|
48 |
String s = value.toString();
|
49 |
if (s == null) { |
50 |
return null; |
51 |
} |
52 |
s = s.trim().toLowerCase(); |
53 |
if (s.length() == 0) { |
54 |
return null; |
55 |
} |
56 |
if (s.startsWith("0x")) { |
57 |
value = Long.valueOf(s.substring(2), 16); |
58 |
} else {
|
59 |
try {
|
60 |
value = Long.valueOf(s);
|
61 |
} catch (NumberFormatException ex) { |
62 |
value = Float.valueOf(s);
|
63 |
value = ((Number) value).longValue();
|
64 |
} |
65 |
} |
66 |
} |
67 |
} |
68 |
return value;
|
69 |
} catch (Exception e) { |
70 |
throw new CoercionException(e); |
71 |
} |
72 |
} |
73 |
|
74 |
@Override
|
75 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
76 |
if (value == null) { |
77 |
return null; |
78 |
} |
79 |
try {
|
80 |
if (!(value instanceof Long)) { |
81 |
if (value instanceof Number) { |
82 |
value = ((Number) value).longValue();
|
83 |
} else if( value instanceof Boolean ) { |
84 |
return (long)((boolean)value ? 1:0); |
85 |
} else if (value instanceof Date) { |
86 |
value = (long)(((Date)value).getTime()); |
87 |
} else {
|
88 |
String s = value.toString();
|
89 |
if (s == null) { |
90 |
return null; |
91 |
} |
92 |
s = s.trim().toLowerCase(); |
93 |
if (s.length() == 0) { |
94 |
return null; |
95 |
} |
96 |
if (s.startsWith("0x")) { |
97 |
value = Long.valueOf(s.substring(2), 16); |
98 |
} else {
|
99 |
try {
|
100 |
value = Long.valueOf(s);
|
101 |
} catch (NumberFormatException ex) { |
102 |
ParsePosition p = new ParsePosition(0); |
103 |
NumberFormat nf = NumberFormat.getInstance(locale); |
104 |
Number num = nf.parse(s, p);
|
105 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
106 |
throw new CoercionException("can't coerce to double with locale " + locale.getLanguage()); |
107 |
} |
108 |
value = ((Number) num).longValue();
|
109 |
} |
110 |
} |
111 |
} |
112 |
} |
113 |
return value;
|
114 |
} catch (Exception e) { |
115 |
throw new CoercionException(e); |
116 |
} |
117 |
} |
118 |
|
119 |
} |