gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / DataTypeUtils.java @ 2105
History | View | Annotate | Download (3.72 KB)
1 |
package org.gvsig.tools.dataTypes; |
---|---|
2 |
|
3 |
import java.util.Date; |
4 |
import java.util.Locale; |
5 |
import org.gvsig.tools.ToolsLocator; |
6 |
|
7 |
/**
|
8 |
*
|
9 |
* @author jjdelcerro
|
10 |
*/
|
11 |
public class DataTypeUtils { |
12 |
|
13 |
private static CoercionContextLocale COERCE_CONTEXT_DEFAULT_LOCALE ; |
14 |
private static CoercionContextDecimal COERCE_CONTEXT_DEFAULT_DECIMAL ; |
15 |
|
16 |
protected DataTypeUtils() {
|
17 |
|
18 |
} |
19 |
|
20 |
public static CoercionContextLocale coerceContextLocale(Locale locale) { |
21 |
return new CoercionContextLocaleImpl(locale); |
22 |
} |
23 |
|
24 |
public static CoercionContextLocale coerceContextDefaultLocale() { |
25 |
if( COERCE_CONTEXT_DEFAULT_LOCALE==null || |
26 |
Locale.getDefault()!=COERCE_CONTEXT_DEFAULT_LOCALE.locale() ) {
|
27 |
COERCE_CONTEXT_DEFAULT_LOCALE = new CoercionContextLocaleImpl(Locale.getDefault()); |
28 |
} |
29 |
return COERCE_CONTEXT_DEFAULT_LOCALE;
|
30 |
} |
31 |
|
32 |
public static CoercionContextDecimal coerceContextDefaultDecimal() { |
33 |
if( COERCE_CONTEXT_DEFAULT_DECIMAL==null || |
34 |
Locale.getDefault()!=COERCE_CONTEXT_DEFAULT_DECIMAL.locale() ) {
|
35 |
COERCE_CONTEXT_DEFAULT_DECIMAL = new CoercionContextDecimalImpl(
|
36 |
Locale.getDefault()
|
37 |
); |
38 |
} |
39 |
return COERCE_CONTEXT_DEFAULT_DECIMAL;
|
40 |
} |
41 |
|
42 |
public static CoercionContextDecimal coerceContextDecimal(Locale locale) { |
43 |
if( COERCE_CONTEXT_DEFAULT_DECIMAL!=null || |
44 |
locale==COERCE_CONTEXT_DEFAULT_DECIMAL.locale() ) { |
45 |
return COERCE_CONTEXT_DEFAULT_DECIMAL;
|
46 |
} |
47 |
return new CoercionContextDecimalImpl(locale); |
48 |
} |
49 |
|
50 |
public static CoercionContextDecimal coerceContextDecimal(Locale locale, int precision, int scale, int roundMode) { |
51 |
return new CoercionContextDecimalImpl(locale,precision, scale, roundMode); |
52 |
} |
53 |
|
54 |
public static Object coerce(int type, Object value, Object defaultValue) { |
55 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
56 |
DataType dataType = manager.get(type); |
57 |
try {
|
58 |
Object x = dataType.coerce(value);
|
59 |
if( x == null ) { |
60 |
return defaultValue;
|
61 |
} |
62 |
return x;
|
63 |
} catch (CoercionException ex) {
|
64 |
return defaultValue;
|
65 |
} |
66 |
} |
67 |
|
68 |
public static Object coerce(int type, Object value) throws CoercionException { |
69 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
70 |
DataType dataType = manager.get(type); |
71 |
Object x = dataType.coerce(value);
|
72 |
return x;
|
73 |
} |
74 |
|
75 |
public static int toInteger(Object value, Integer defaultValue) { |
76 |
return (int) coerce(DataTypes.INT, value, defaultValue); |
77 |
} |
78 |
|
79 |
public static long toLong(Object value, Long defaultValue) { |
80 |
return (long) coerce(DataTypes.LONG, value, defaultValue); |
81 |
} |
82 |
|
83 |
public static float toFloat(Object value, Float defaultValue) { |
84 |
return (float) coerce(DataTypes.FLOAT, value, defaultValue); |
85 |
} |
86 |
|
87 |
public static double toDouble(Object value, Double defaultValue) { |
88 |
return (double) coerce(DataTypes.DOUBLE, value, defaultValue); |
89 |
} |
90 |
|
91 |
public static String toString(Object value, String defaultValue) { |
92 |
return (String) coerce(DataTypes.STRING, value, defaultValue); |
93 |
} |
94 |
|
95 |
public static boolean toBoolean(Object value, Boolean defaultValue) { |
96 |
return (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue); |
97 |
} |
98 |
|
99 |
public static Date toDate(Object value, Date defaultValue) { |
100 |
return (Date) coerce(DataTypes.DATE, value, defaultValue); |
101 |
} |
102 |
|
103 |
public static Date toTime(Object value, Date defaultValue) { |
104 |
return (Date) coerce(DataTypes.TIME, value, defaultValue); |
105 |
} |
106 |
|
107 |
public static Date toTimestamp(Object value, Date defaultValue) { |
108 |
return (Date) coerce(DataTypes.TIMESTAMP, value, defaultValue); |
109 |
} |
110 |
} |