gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / DataTypeUtils.java @ 2177
History | View | Annotate | Download (5.45 KB)
1 |
package org.gvsig.tools.dataTypes; |
---|---|
2 |
|
3 |
import java.math.BigDecimal; |
4 |
import java.util.Date; |
5 |
import java.util.Locale; |
6 |
import org.gvsig.tools.ToolsLocator; |
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 |
COERCE_CONTEXT_DEFAULT_DECIMAL = new CoercionContextDecimalImpl(
|
36 |
Locale.ENGLISH
|
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 boolean toBoolean(Object value, Boolean defaultValue) { |
84 |
return (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue); |
85 |
} |
86 |
|
87 |
public static float toFloat(Object value, Float defaultValue) { |
88 |
return (float) coerce(DataTypes.FLOAT, value, defaultValue); |
89 |
} |
90 |
|
91 |
public static double toDouble(Object value, Double defaultValue) { |
92 |
return (double) coerce(DataTypes.DOUBLE, value, defaultValue); |
93 |
} |
94 |
|
95 |
public static String toString(Object value, String defaultValue) { |
96 |
return (String) coerce(DataTypes.STRING, 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 |
|
111 |
public static String toString(Object value) { |
112 |
return (String) coerce(DataTypes.STRING, value, null); |
113 |
} |
114 |
|
115 |
public static Date toDate(Object value) { |
116 |
return (Date) coerce(DataTypes.DATE, value, null); |
117 |
} |
118 |
|
119 |
public static Date toTime(Object value) { |
120 |
return (Date) coerce(DataTypes.TIME, value, null); |
121 |
} |
122 |
|
123 |
public static Date toTimestamp(Object value) { |
124 |
return (Date) coerce(DataTypes.TIMESTAMP, value, null); |
125 |
} |
126 |
|
127 |
public static float toFloat(Object value) { |
128 |
return (float) coerce(DataTypes.FLOAT, value, Float.NaN); |
129 |
} |
130 |
|
131 |
public static double toDouble(Object value) { |
132 |
return (double) coerce(DataTypes.DOUBLE, value, Double.NaN); |
133 |
} |
134 |
|
135 |
public static int toInteger(Object value) { |
136 |
return (int) coerce(DataTypes.INT, value, -1); |
137 |
} |
138 |
|
139 |
public static long toLong(Object value) { |
140 |
return (long) coerce(DataTypes.LONG, value, -1); |
141 |
} |
142 |
|
143 |
public static boolean toBoolean(Object value) { |
144 |
return (boolean) coerce(DataTypes.BOOLEAN, value, false); |
145 |
} |
146 |
|
147 |
public static BigDecimal toBigDecimal(Object value, int precision, int scale, BigDecimal defaultValue) { |
148 |
CoercionContextDecimalImpl context = new CoercionContextDecimalImpl(Locale.ENGLISH, precision, scale); |
149 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
150 |
DataType dataType = manager.get(DataTypes.DECIMAL); |
151 |
try {
|
152 |
BigDecimal x = (BigDecimal) dataType.coerce(value,context); |
153 |
if( x == null ) { |
154 |
return defaultValue;
|
155 |
} |
156 |
return x;
|
157 |
} catch (CoercionException ex) {
|
158 |
return defaultValue;
|
159 |
} |
160 |
} |
161 |
|
162 |
public static BigDecimal toBigDecimal(Object value, int precision, int scale) { |
163 |
return toBigDecimal(value, precision, scale, null); |
164 |
} |
165 |
} |