Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / DataTypeUtils.java @ 2197

History | View | Annotate | Download (6.05 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 CoercionContextLocale.create(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 = CoercionContextLocale.create(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 = CoercionContextDecimal.create(
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 CoercionContextDecimal.create(locale);
48
    }
49

    
50
    public static CoercionContextDecimal coerceContextDecimal(Locale locale, int precision, int scale, int roundMode) {
51
      return CoercionContextDecimal.create(locale,precision, scale, roundMode);
52
    }
53
    
54
    public static Coercion getCoercion(int type) {
55
        DataTypesManager manager = ToolsLocator.getDataTypesManager();
56
        DataType dataType = manager.get(type);
57
        Coercion c = dataType.getCoercion();
58
        return c;
59
    }
60
    
61
    public static Object coerce(int type, Object value, Object defaultValue) {
62
        Coercion coercer = getCoercion(type);
63
        try {
64
            Object x = coercer.coerce(value);
65
            if( x == null ) {
66
                return defaultValue;
67
            }
68
            return x;
69
        } catch (CoercionException ex) {
70
            return defaultValue;
71
        }
72
    }
73

    
74
    public static Object coerce(int type, Object value) throws CoercionException {
75
        Coercion c = getCoercion(type);
76
        Object x = c.coerce(value);
77
        return x;
78
    }
79
    
80
    public static int toInteger(Object value, int defaultValue) {
81
        return (int) coerce(DataTypes.INT, value, defaultValue);
82
    }
83

    
84
    public static short toShort(Object value, short defaultValue) {
85
      try {
86
        return (short) coerce(DataTypes.INT, value, defaultValue);
87
      } catch(Throwable th) {
88
        return defaultValue;
89
      }
90
    }
91

    
92
    public static int toByte(Object value, byte defaultValue) {
93
        return (int) coerce(DataTypes.BYTE, value, defaultValue);
94
    }
95

    
96
    public static long toLong(Object value, long defaultValue) {
97
        return (long) coerce(DataTypes.LONG, value, defaultValue);
98
    }
99

    
100
    public static boolean toBoolean(Object value, boolean defaultValue) {
101
        return (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue);
102
    }
103

    
104
    public static float toFloat(Object value, float defaultValue) {
105
        return (float) coerce(DataTypes.FLOAT, value, defaultValue);
106
    }
107

    
108
    public static double toDouble(Object value, double defaultValue) {
109
        return (double) coerce(DataTypes.DOUBLE, value, defaultValue);
110
    }
111

    
112
    public static String toString(Object value, String defaultValue) {
113
        return (String) coerce(DataTypes.STRING, value, defaultValue);
114
    }
115

    
116
    public static Date toDate(Object value, Date defaultValue) {
117
        return (Date) coerce(DataTypes.DATE, value, defaultValue);
118
    }
119

    
120
    public static Date toTime(Object value, Date defaultValue) {
121
        return (Date) coerce(DataTypes.TIME, value, defaultValue);
122
    }
123

    
124
    public static Date toTimestamp(Object value, Date defaultValue) {
125
        return (Date) coerce(DataTypes.TIMESTAMP, value, defaultValue);
126
    }
127

    
128
    public static String toString(Object value) {
129
        return (String) coerce(DataTypes.STRING, value, null);
130
    }
131

    
132
    public static Date toDate(Object value) {
133
        return (Date) coerce(DataTypes.DATE, value, null);
134
    }
135

    
136
    public static Date toTime(Object value) {
137
        return (Date) coerce(DataTypes.TIME, value, null);
138
    }
139

    
140
    public static Date toTimestamp(Object value) {
141
        return (Date) coerce(DataTypes.TIMESTAMP, value, null);
142
    }
143

    
144
    public static float toFloat(Object value) {
145
        return (float) coerce(DataTypes.FLOAT, value, Float.NaN);
146
    }
147

    
148
    public static double toDouble(Object value) {
149
        return (double) coerce(DataTypes.DOUBLE, value, Double.NaN);
150
    }
151
    
152
    public static int toInteger(Object value) {
153
        return (int) coerce(DataTypes.INT, value, -1);
154
    }
155

    
156
    public static int toByte(Object value) {
157
        return (int) coerce(DataTypes.BYTE, value, -1);
158
    }
159

    
160
    public static short toShort(Object value) {
161
      try {
162
        return (short) coerce(DataTypes.INT, value, -1);
163
      } catch(Throwable th) {
164
        return -1;
165
      }
166
    }
167

    
168
    public static long toLong(Object value) {
169
        return (long) coerce(DataTypes.LONG, value, -1);
170
    }
171

    
172
    public static boolean toBoolean(Object value) {
173
        return (boolean) coerce(DataTypes.BOOLEAN, value, false);
174
    }
175
    
176
    public static BigDecimal toBigDecimal(Object value, int precision, int scale, BigDecimal defaultValue) {
177
      CoercionContextDecimal context = CoercionContextDecimal.create(precision, scale);
178
      Coercion c = getCoercion(DataTypes.DECIMAL);
179
      try {
180
          BigDecimal x = (BigDecimal) c.coerce(value,context);
181
          if( x == null ) {
182
              return defaultValue;
183
          }
184
          return x;
185
      } catch (CoercionException ex) {
186
          return defaultValue;
187
      }
188
    }
189

    
190
    public static BigDecimal toBigDecimal(Object value, int precision, int scale) {
191
      return toBigDecimal(value, precision, scale, null);
192
    }
193
}