Revision 989 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToFloat.java
CoerceToFloat.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.tools.dataTypes.impl.coercion; |
25 | 25 |
|
26 |
import java.text.NumberFormat; |
|
27 |
import java.text.ParsePosition; |
|
28 |
import java.util.Locale; |
|
29 |
|
|
26 | 30 |
import org.gvsig.tools.dataTypes.CoercionException; |
27 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
|
31 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
|
|
28 | 32 |
|
29 |
public class CoerceToFloat implements Coercion { |
|
33 |
public class CoerceToFloat implements CoercionWithLocale {
|
|
30 | 34 |
|
31 | 35 |
public Object coerce(Object value) throws CoercionException { |
32 | 36 |
if( value == null ) { |
... | ... | |
49 | 53 |
} catch (Exception e) { |
50 | 54 |
throw new CoercionException(e); |
51 | 55 |
} |
56 |
} |
|
52 | 57 |
|
58 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
|
59 |
if( value == null ) { |
|
60 |
return null; |
|
61 |
} |
|
62 |
try { |
|
63 |
if (!(value instanceof Double)) { |
|
64 |
if (value instanceof Number) { |
|
65 |
value = new Double(((Number) value).doubleValue()); |
|
66 |
} else { |
|
67 |
String s = value.toString().trim().toLowerCase(); |
|
68 |
if( s.startsWith("0x")) { |
|
69 |
value = new Double(Long.parseLong(s.substring(2), 16)); |
|
70 |
} else { |
|
71 |
ParsePosition p = new ParsePosition(0); |
|
72 |
NumberFormat nf = NumberFormat.getInstance(locale); |
|
73 |
Number num = nf.parse(s,p); |
|
74 |
if( p.getErrorIndex()>0 || p.getIndex()<s.length() ) { |
|
75 |
throw new CoercionException("can't coerce to float with locale "+locale.getLanguage()); |
|
76 |
} |
|
77 |
value = new Float(num.floatValue()); |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
return value; |
|
82 |
} catch (CoercionException e) { |
|
83 |
throw e; |
|
84 |
} catch (Exception e) { |
|
85 |
throw new CoercionException(e); |
|
86 |
} |
|
53 | 87 |
} |
54 | 88 |
|
55 | 89 |
} |
Also available in: Unified diff