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 @ 2208

History | View | Annotate | Download (8.23 KB)

1
package org.gvsig.tools.dataTypes;
2

    
3
import java.math.BigDecimal;
4
import java.time.LocalDate;
5
import java.time.LocalDateTime;
6
import java.time.LocalTime;
7
import java.time.ZoneId;
8
import java.time.temporal.TemporalAccessor;
9
import java.util.Date;
10
import java.util.Locale;
11
import org.gvsig.tools.ToolsLocator;
12

    
13
/**
14
 *
15
 * @author jjdelcerro
16
 */
17
public class DataTypeUtils {
18
    
19
    private static CoercionContextLocale COERCE_CONTEXT_DEFAULT_LOCALE ;
20
    private static CoercionContextDecimal COERCE_CONTEXT_DEFAULT_DECIMAL ;
21

    
22
    protected DataTypeUtils() {
23
        
24
    }
25

    
26
    public static CoercionContextLocale coerceContextLocale(Locale locale) {
27
      return CoercionContextLocale.create(locale);
28
    }
29
    
30
    public static CoercionContextLocale coerceContextDefaultLocale() {
31
      if( COERCE_CONTEXT_DEFAULT_LOCALE==null || 
32
          Locale.getDefault()!=COERCE_CONTEXT_DEFAULT_LOCALE.locale() ) {
33
        COERCE_CONTEXT_DEFAULT_LOCALE = CoercionContextLocale.create(Locale.getDefault());
34
      }
35
      return COERCE_CONTEXT_DEFAULT_LOCALE;
36
    }
37
    
38
    public static CoercionContextDecimal coerceContextDefaultDecimal() {
39
      if( COERCE_CONTEXT_DEFAULT_DECIMAL==null ) {
40
        COERCE_CONTEXT_DEFAULT_DECIMAL = CoercionContextDecimal.create(
41
                Locale.ENGLISH
42
        );
43
      }
44
      return COERCE_CONTEXT_DEFAULT_DECIMAL;
45
    }
46
    
47
    public static CoercionContextDecimal coerceContextDecimal(Locale locale) {
48
      if( COERCE_CONTEXT_DEFAULT_DECIMAL!=null || 
49
          locale==COERCE_CONTEXT_DEFAULT_DECIMAL.locale() ) {
50
          return COERCE_CONTEXT_DEFAULT_DECIMAL;
51
      }
52
      return CoercionContextDecimal.create(locale);
53
    }
54

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

    
79
    public static Object coerce(int type, Object value) throws CoercionException {
80
        Coercion c = getCoercion(type);
81
        Object x = c.coerce(value);
82
        return x;
83
    }
84
    
85
    public static int toInteger(Object value, int defaultValue) {
86
        return (int) coerce(DataTypes.INT, value, defaultValue);
87
    }
88

    
89
    public static short toShort(Object value, short defaultValue) {
90
      try {
91
        return (short) coerce(DataTypes.INT, value, defaultValue);
92
      } catch(Throwable th) {
93
        return defaultValue;
94
      }
95
    }
96

    
97
    public static int toByte(Object value, byte defaultValue) {
98
        return (int) coerce(DataTypes.BYTE, value, defaultValue);
99
    }
100

    
101
    public static long toLong(Object value, long defaultValue) {
102
        return (long) coerce(DataTypes.LONG, value, defaultValue);
103
    }
104

    
105
    public static boolean toBoolean(Object value, boolean defaultValue) {
106
        return (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue);
107
    }
108

    
109
    public static float toFloat(Object value, float defaultValue) {
110
        return (float) coerce(DataTypes.FLOAT, value, defaultValue);
111
    }
112

    
113
    public static double toDouble(Object value, double defaultValue) {
114
        return (double) coerce(DataTypes.DOUBLE, value, defaultValue);
115
    }
116

    
117
    public static String toString(Object value, String defaultValue) {
118
        return (String) coerce(DataTypes.STRING, value, defaultValue);
119
    }
120

    
121
    public static Date toDate(Object value, Date defaultValue) {
122
        return (Date) coerce(DataTypes.DATE, value, defaultValue);
123
    }
124

    
125
    public static Date toTime(Object value, Date defaultValue) {
126
        return (Date) coerce(DataTypes.TIME, value, defaultValue);
127
    }
128

    
129
    public static Date toTimestamp(Object value, Date defaultValue) {
130
        return (Date) coerce(DataTypes.TIMESTAMP, value, defaultValue);
131
    }
132

    
133
    public static LocalDateTime toLocalDateTime(Object value) {
134
        // De momento, dejo esta implementacion para convertir a localdatetime
135
        // rapidamente, pero habria que repasarsela.
136
        if( value instanceof LocalDateTime ) {
137
          return (LocalDateTime) value;
138
        }
139
        if( value instanceof TemporalAccessor ) {
140
          try {
141
            return LocalDateTime.from((TemporalAccessor) value);
142
          } catch(Exception ex) {
143
          }
144
        }
145
        Date d = (Date) coerce(DataTypes.TIMESTAMP, value, null);
146
        if( d == null ) {
147
          return null;
148
        }
149
        return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault());
150
    }
151

    
152
    public static LocalDate toLocalDate(Object value) {
153
        // De momento, dejo esta implementacion para convertir a localdate
154
        // rapidamente, pero habria que ver de repasarsela.
155
        if( value instanceof LocalDateTime ) {
156
          return (LocalDate) value;
157
        }
158
        if( value instanceof TemporalAccessor ) {
159
          try {
160
            return LocalDate.from((TemporalAccessor) value);
161
          } catch(Exception ex) {
162
          }
163
        }
164
        Date d = (Date) coerce(DataTypes.TIMESTAMP, value, null);
165
        if( d == null ) {
166
          return null;
167
        }
168
        return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault()).toLocalDate();
169
    }
170

    
171
    public static LocalTime toLocalTime(Object value) {
172
        // De momento, dejo esta implementacion para convertir a localtime
173
        // rapidamente, pero habria que ver de repasarsela.
174
        if( value instanceof LocalTime ) {
175
          return (LocalTime) value;
176
        }
177
        if( value instanceof TemporalAccessor ) {
178
          try {
179
            return LocalTime.from((TemporalAccessor) value);
180
          } catch(Exception ex) {
181
          }
182
        }
183
        Date d = (Date) coerce(DataTypes.TIMESTAMP, value, null);
184
        if( d == null ) {
185
          return null;
186
        }
187
        return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault()).toLocalTime();
188
    }
189

    
190
    public static String toString(Object value) {
191
        return (String) coerce(DataTypes.STRING, value, null);
192
    }
193

    
194
    public static Date toDate(Object value) {
195
        return (Date) coerce(DataTypes.DATE, value, null);
196
    }
197

    
198
    public static Date toTime(Object value) {
199
        return (Date) coerce(DataTypes.TIME, value, null);
200
    }
201

    
202
    public static Date toTimestamp(Object value) {
203
        return (Date) coerce(DataTypes.TIMESTAMP, value, null);
204
    }
205

    
206
    public static float toFloat(Object value) {
207
        return (float) coerce(DataTypes.FLOAT, value, Float.NaN);
208
    }
209

    
210
    public static double toDouble(Object value) {
211
        return (double) coerce(DataTypes.DOUBLE, value, Double.NaN);
212
    }
213
    
214
    public static int toInteger(Object value) {
215
        return (int) coerce(DataTypes.INT, value, -1);
216
    }
217

    
218
    public static int toByte(Object value) {
219
        return (int) coerce(DataTypes.BYTE, value, -1);
220
    }
221

    
222
    public static short toShort(Object value) {
223
      try {
224
        return (short) coerce(DataTypes.INT, value, -1);
225
      } catch(Throwable th) {
226
        return -1;
227
      }
228
    }
229

    
230
    public static long toLong(Object value) {
231
        return (long) coerce(DataTypes.LONG, value, -1);
232
    }
233

    
234
    public static boolean toBoolean(Object value) {
235
        return (boolean) coerce(DataTypes.BOOLEAN, value, false);
236
    }
237
    
238
    public static BigDecimal toBigDecimal(Object value, int precision, int scale, BigDecimal defaultValue) {
239
      CoercionContextDecimal context = CoercionContextDecimal.create(precision, scale);
240
      Coercion c = getCoercion(DataTypes.DECIMAL);
241
      try {
242
          BigDecimal x = (BigDecimal) c.coerce(value,context);
243
          if( x == null ) {
244
              return defaultValue;
245
          }
246
          return x;
247
      } catch (CoercionException ex) {
248
          return defaultValue;
249
      }
250
    }
251

    
252
    public static BigDecimal toBigDecimal(Object value, int precision, int scale) {
253
      return toBigDecimal(value, precision, scale, null);
254
    }
255
}