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 | ||
---|---|---|
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 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToString.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.tools.dataTypes.impl.coercion; |
25 | 25 |
|
26 |
import java.text.NumberFormat; |
|
27 |
import java.util.Locale; |
|
28 |
|
|
26 | 29 |
import org.gvsig.tools.dataTypes.CoercionException; |
27 | 30 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
31 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
|
28 | 32 |
|
29 |
public class CoerceToString implements Coercion { |
|
33 |
public class CoerceToString implements CoercionWithLocale {
|
|
30 | 34 |
|
31 | 35 |
public Object coerce(Object value) throws CoercionException { |
32 | 36 |
if( value == null ) { |
... | ... | |
43 | 47 |
|
44 | 48 |
} |
45 | 49 |
|
50 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
|
51 |
if( value == null ) { |
|
52 |
return null; |
|
53 |
} |
|
54 |
try { |
|
55 |
if( !(value instanceof String )) { |
|
56 |
if( value instanceof Float || value instanceof Double ) { |
|
57 |
NumberFormat nf = NumberFormat.getInstance(locale); |
|
58 |
value = nf.format(((Number)value).doubleValue()); |
|
59 |
} else { |
|
60 |
value = value.toString(); |
|
61 |
} |
|
62 |
} |
|
63 |
return value; |
|
64 |
} catch (Exception e) { |
|
65 |
throw new CoercionException(e); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
46 | 69 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDouble.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 CoerceToDouble implements Coercion { |
|
33 |
public class CoerceToDouble 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 double with locale "+locale.getLanguage()); |
|
76 |
} |
|
77 |
value = new Double(num.doubleValue()); |
|
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 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataTypesManager.java | ||
---|---|---|
24 | 24 |
package org.gvsig.tools.dataTypes; |
25 | 25 |
|
26 | 26 |
import java.util.Iterator; |
27 |
import java.util.Locale; |
|
27 | 28 |
|
28 | 29 |
/** |
29 | 30 |
* Manages data types. |
... | ... | |
38 | 39 |
public Object coerce(Object value) throws CoercionException; |
39 | 40 |
} |
40 | 41 |
|
42 |
public interface CoercionWithLocale extends Coercion { |
|
43 |
|
|
44 |
public Object coerce(Object value, Locale locale) throws CoercionException; |
|
45 |
} |
|
46 |
|
|
41 | 47 |
public DataType get(int type); |
42 | 48 |
|
43 | 49 |
public boolean isValidType(int type); |
Also available in: Unified diff