Revision 2092 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToLong.java
CoerceToLong.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package org.gvsig.tools.dataTypes.impl.coercion; |
24 | 24 |
|
25 |
import java.text.DecimalFormat; |
|
26 |
import java.text.NumberFormat; |
|
27 |
import java.text.ParsePosition; |
|
25 | 28 |
import java.util.Date; |
26 | 29 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
27 | 30 |
import org.gvsig.tools.dataTypes.CoercionException; |
28 | 31 |
import org.gvsig.tools.dataTypes.CoercionContext; |
32 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
|
33 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
|
29 | 34 |
|
30 | 35 |
@SuppressWarnings("UseSpecificCatch") |
31 | 36 |
public class CoerceToLong extends AbstractCoercion { |
... | ... | |
35 | 40 |
if (value == null || value instanceof Long) { |
36 | 41 |
return value; |
37 | 42 |
} |
38 |
Long num; |
|
43 |
CoercionContextLocale theContext; |
|
44 |
if (context instanceof CoercionContextLocale) { |
|
45 |
theContext = (CoercionContextLocale) context; |
|
46 |
} else { |
|
47 |
theContext = DataTypeUtils.coerceContextDefaultLocale(); |
|
48 |
} |
|
49 |
Number num; |
|
39 | 50 |
try { |
40 | 51 |
if (value instanceof Number) { |
41 |
num = ((Number) value).longValue();
|
|
52 |
num = (Number) value;
|
|
42 | 53 |
|
43 | 54 |
} else if (value instanceof Boolean) { |
44 | 55 |
num = ((boolean) value ? 1l : 0l); |
... | ... | |
62 | 73 |
if (s.startsWith("+")) { |
63 | 74 |
s = s.substring(1); |
64 | 75 |
} |
65 |
num = Long.valueOf(s); |
|
76 |
ParsePosition p = new ParsePosition(0); |
|
77 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
78 |
num = nf.parse(s, p); |
|
79 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
80 |
throw new CoercionException("Can't coerce '" + s + "' to long with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
81 |
} |
|
66 | 82 |
} |
67 | 83 |
} |
68 |
return num; |
|
84 |
return num.longValue();
|
|
69 | 85 |
} catch (Exception e) { |
70 | 86 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to long.", e); |
71 | 87 |
} |
Also available in: Unified diff