gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / DataTypeUtils.java @ 2080
History | View | Annotate | Download (2.84 KB)
1 |
package org.gvsig.tools.dataTypes; |
---|---|
2 |
|
3 |
import java.util.Locale; |
4 |
import org.gvsig.tools.ToolsLocator; |
5 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
6 |
import org.gvsig.tools.dataTypes.CoercionContextDecimal; |
7 |
|
8 |
/**
|
9 |
*
|
10 |
* @author jjdelcerro
|
11 |
*/
|
12 |
public class DataTypeUtils { |
13 |
|
14 |
private static CoercionContextLocale COERCE_CONTEXT_DEFAULT_LOCALE ; |
15 |
private static CoercionContextDecimal COERCE_CONTEXT_DEFAULT_DECIMAL ; |
16 |
|
17 |
protected DataTypeUtils() {
|
18 |
|
19 |
} |
20 |
|
21 |
public static CoercionContextLocale coerceContextLocale(Locale locale) { |
22 |
return new CoercionContextLocaleImpl(locale); |
23 |
} |
24 |
|
25 |
public static CoercionContextLocale coerceContextDefaultLocale() { |
26 |
if( COERCE_CONTEXT_DEFAULT_LOCALE==null || |
27 |
Locale.getDefault()!=COERCE_CONTEXT_DEFAULT_LOCALE.locale() ) {
|
28 |
COERCE_CONTEXT_DEFAULT_LOCALE = new CoercionContextLocaleImpl(Locale.getDefault()); |
29 |
} |
30 |
return COERCE_CONTEXT_DEFAULT_LOCALE;
|
31 |
} |
32 |
|
33 |
public static CoercionContextDecimal coerceContextDefaultDecimal() { |
34 |
if( COERCE_CONTEXT_DEFAULT_DECIMAL==null || |
35 |
Locale.getDefault()!=COERCE_CONTEXT_DEFAULT_DECIMAL.locale() ) {
|
36 |
COERCE_CONTEXT_DEFAULT_DECIMAL = new CoercionContextDecimalImpl(
|
37 |
Locale.getDefault()
|
38 |
); |
39 |
} |
40 |
return COERCE_CONTEXT_DEFAULT_DECIMAL;
|
41 |
} |
42 |
|
43 |
public static CoercionContextDecimal coerceContextDecimal(Locale locale) { |
44 |
if( COERCE_CONTEXT_DEFAULT_DECIMAL!=null || |
45 |
locale==COERCE_CONTEXT_DEFAULT_DECIMAL.locale() ) { |
46 |
return COERCE_CONTEXT_DEFAULT_DECIMAL;
|
47 |
} |
48 |
return new CoercionContextDecimalImpl(locale); |
49 |
} |
50 |
|
51 |
public static CoercionContextDecimal coerceContextDecimal(Locale locale, int precision, int scale, int roundMode) { |
52 |
return new CoercionContextDecimalImpl(locale,precision, scale, roundMode); |
53 |
} |
54 |
|
55 |
public static Object coerce(int type, Object value, Object defaultValue) { |
56 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
57 |
DataType dataType = manager.get(type); |
58 |
try {
|
59 |
Object x = dataType.coerce(value);
|
60 |
if( x == null ) { |
61 |
return defaultValue;
|
62 |
} |
63 |
return x;
|
64 |
} catch (CoercionException ex) {
|
65 |
return defaultValue;
|
66 |
} |
67 |
} |
68 |
|
69 |
public static int toInteger(Object value, int defaultValue) { |
70 |
return (int) coerce(DataTypes.INT, value, defaultValue); |
71 |
} |
72 |
|
73 |
public static double toDouble(Object value, int defaultValue) { |
74 |
return (double) coerce(DataTypes.DOUBLE, value, defaultValue); |
75 |
} |
76 |
|
77 |
public static String toString(Object value, String defaultValue) { |
78 |
return (String) coerce(DataTypes.STRING, value, defaultValue); |
79 |
} |
80 |
|
81 |
public static boolean toBoolean(Object value, boolean defaultValue) { |
82 |
return (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue); |
83 |
} |
84 |
} |