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

History | View | Annotate | Download (15.4 KB)

1
package org.gvsig.tools.dataTypes;
2

    
3
import java.math.BigDecimal;
4
import java.sql.Timestamp;
5
import java.time.Instant;
6
import java.time.LocalDate;
7
import java.time.LocalDateTime;
8
import java.time.LocalTime;
9
import java.time.ZoneId;
10
import java.time.chrono.ChronoLocalDate;
11
import java.time.chrono.ChronoLocalDateTime;
12
import java.time.chrono.ChronoZonedDateTime;
13
import java.time.temporal.TemporalAccessor;
14
import java.util.Date;
15
import java.util.Locale;
16
import org.gvsig.tools.ToolsLocator;
17

    
18
/**
19
 *
20
 * @author jjdelcerro
21
 */
22
public class DataTypeUtils {
23
    
24
    private static CoercionContextLocale COERCE_CONTEXT_DEFAULT_LOCALE ;
25
    private static CoercionContextDecimal COERCE_CONTEXT_DEFAULT_DECIMAL ;
26

    
27
    protected DataTypeUtils() {
28
        
29
    }
30

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

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

    
84
    public static Object coerce(int type, Object value) throws CoercionException {
85
        Coercion c = getCoercion(type);
86
        Object x = c.coerce(value);
87
        return x;
88
    }
89
    
90
    public static int toInteger(Object value, int defaultValue) {
91
        return (int) coerce(DataTypes.INT, value, defaultValue);
92
    }
93

    
94
    public static short toShort(Object value, short defaultValue) {
95
      try {
96
        return (short) coerce(DataTypes.INT, value, defaultValue);
97
      } catch(Throwable th) {
98
        return defaultValue;
99
      }
100
    }
101

    
102
    public static int toByte(Object value, byte defaultValue) {
103
        return (int) coerce(DataTypes.BYTE, value, defaultValue);
104
    }
105

    
106
    public static long toLong(Object value, long defaultValue) {
107
        return (long) coerce(DataTypes.LONG, value, defaultValue);
108
    }
109

    
110
    public static boolean toBoolean(Object value, boolean defaultValue) {
111
        return (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue);
112
    }
113

    
114
    public static boolean isTrue(Object value, boolean defaultValue) {
115
        return (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue);
116
    }
117

    
118
    public static boolean isFalse(Object value, boolean defaultValue) {
119
        return ! (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue);
120
    }
121

    
122
    public static float toFloat(Object value, float defaultValue) {
123
        return (float) coerce(DataTypes.FLOAT, value, defaultValue);
124
    }
125

    
126
    public static double toDouble(Object value, double defaultValue) {
127
        return (double) coerce(DataTypes.DOUBLE, value, defaultValue);
128
    }
129

    
130
    public static String toString(Object value, String defaultValue) {
131
        return (String) coerce(DataTypes.STRING, value, defaultValue);
132
    }
133

    
134
    public static Date toDate(Object value, Date defaultValue) {
135
        return (Date) coerce(DataTypes.DATE, value, defaultValue);
136
    }
137

    
138
    public static Date toTime(Object value, Date defaultValue) {
139
        return (Date) coerce(DataTypes.TIME, value, defaultValue);
140
    }
141

    
142
    public static Date toTimestamp(Object value, Date defaultValue) {
143
        return (Date) coerce(DataTypes.TIMESTAMP, value, defaultValue);
144
    }
145

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

    
170
    public static LocalDate toLocalDate(java.util.Date value) {
171
        return toLocalDate(value.toInstant());
172
    }
173

    
174
    public static LocalDate toLocalDate(ChronoLocalDateTime value) {
175
        return toLocalDate(value.atZone(ZoneId.systemDefault()));
176
    }
177

    
178
    public static LocalDate toLocalDate(ChronoLocalDate value) {
179
        return toLocalDate(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
180
    }
181

    
182
    public static LocalDate toLocalDate(LocalTime value) {
183
        return toLocalDate(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
184
    }
185

    
186
    public static LocalDate toLocalDate(ChronoZonedDateTime value) {
187
        return toLocalDate(((ChronoZonedDateTime)value).toInstant());
188
    }
189

    
190
    public static LocalDate toLocalDate(Instant value) {
191
        return LocalDateTime.ofInstant(value, ZoneId.systemDefault()).toLocalDate();
192
    }
193
    
194
    public static LocalDateTime toLocalDateTime(Object value) {
195
        // De momento, dejo esta implementacion para convertir a localdatetime
196
        // rapidamente, pero habria que repasarsela.
197
        if( value instanceof LocalDateTime ) {
198
          return (LocalDateTime) value;
199
        }
200
        if( value instanceof TemporalAccessor ) {
201
          try {
202
            return LocalDateTime.from((TemporalAccessor) value);
203
          } catch(Exception ex) {
204
          }
205
        }
206
        Date d;
207
        if( value instanceof Date ) {
208
            d = (Date) value;
209
        } else {
210
            d = (Date) coerce(DataTypes.TIMESTAMP, value, null);
211
            if( d == null ) {
212
              return null;
213
            }
214
        }
215
        return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault());
216
    }
217

    
218
    public static LocalDateTime toLocalDateTime(java.util.Date value) {
219
        return toLocalDateTime(value.toInstant());
220
    }
221

    
222
    public static LocalDateTime toLocalDateTime(ChronoLocalDateTime value) {
223
        return toLocalDateTime(value.atZone(ZoneId.systemDefault()));
224
    }
225

    
226
    public static LocalDateTime toLocalDateTime(ChronoLocalDate value) {
227
        return toLocalDateTime(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
228
    }
229

    
230
    public static LocalDateTime toLocalDateTime(LocalTime value) {
231
        return toLocalDateTime(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
232
    }
233

    
234
    public static LocalDateTime toLocalDateTime(ChronoZonedDateTime value) {
235
        return toLocalDateTime(((ChronoZonedDateTime)value).toInstant());
236
    }
237

    
238
    public static LocalDateTime toLocalDateTime(Instant value) {
239
        return LocalDateTime.ofInstant(value, ZoneId.systemDefault());
240
    }
241
    
242
    public static LocalTime toLocalTime(Object value) {
243
        // De momento, dejo esta implementacion para convertir a localtime
244
        // rapidamente, pero habria que ver de repasarsela.
245
        if( value instanceof LocalTime ) {
246
          return (LocalTime) value;
247
        }
248
        if( value instanceof TemporalAccessor ) {
249
          try {
250
            return LocalTime.from((TemporalAccessor) value);
251
          } catch(Exception ex) {
252
          }
253
        }
254
        Date d;
255
        if( value instanceof Date ) {
256
            d = (Date) value;
257
        } else {
258
            d = (Date) coerce(DataTypes.TIMESTAMP, value, null);
259
            if( d == null ) {
260
              return null;
261
            }
262
        }
263
        return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault()).toLocalTime();
264
    }
265

    
266
    public static LocalTime toLocalTime(java.util.Date value) {
267
        return toLocalTime(value.toInstant());
268
    }
269

    
270
    public static LocalTime toLocalTime(ChronoLocalDateTime value) {
271
        return toLocalTime(value.atZone(ZoneId.systemDefault()));
272
    }
273

    
274
    public static LocalTime toLocalTime(ChronoLocalDate value) {
275
        return toLocalTime(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
276
    }
277

    
278
    public static LocalTime toLocalTime(LocalTime value) {
279
        return toLocalTime(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
280
    }
281

    
282
    public static LocalTime toLocalTime(ChronoZonedDateTime value) {
283
        return toLocalTime(((ChronoZonedDateTime)value).toInstant());
284
    }
285

    
286
    public static LocalTime toLocalTime(Instant value) {
287
        return LocalDateTime.ofInstant(value, ZoneId.systemDefault()).toLocalTime();
288
    }
289

    
290
    public static String toString(Object value) {
291
        return (String) coerce(DataTypes.STRING, value, null);
292
    }
293

    
294

    
295
    public static java.sql.Date toDate(Object value) {
296
        if( value == null ) {
297
            return null;
298
        }
299
        return (java.sql.Date) coerce(DataTypes.DATE, value, null);
300
    }
301

    
302
    public static java.sql.Date toDate(ChronoLocalDateTime value) {
303
        if( value == null ) {
304
            return null;
305
        }
306
        return toDate(value.atZone(ZoneId.systemDefault()));
307
    }
308

    
309
    public static java.sql.Date toDate(ChronoLocalDate value) {
310
        if( value == null ) {
311
            return null;
312
        }
313
        return toDate(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
314
    }
315

    
316
    public static java.sql.Date toDate(LocalTime value) {
317
        if( value == null ) {
318
            return null;
319
        }
320
        return toDate(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
321
    }
322

    
323
    public static java.sql.Date toDate(ChronoZonedDateTime value) {
324
        if( value == null ) {
325
            return null;
326
        }
327
        return toDate(((ChronoZonedDateTime)value).toInstant());
328
    }
329

    
330
    public static java.sql.Date toDate(Instant value) {
331
        if( value == null ) {
332
            return null;
333
        }
334
        return (java.sql.Date) java.sql.Date.from(value);
335
    }
336

    
337
    public static java.sql.Time toTime(Object value) {
338
        if( value == null ) {
339
            return null;
340
        }
341
        return (java.sql.Time) coerce(DataTypes.TIME, value, null);
342
    }
343

    
344
    public static java.sql.Time toTime(ChronoLocalDateTime value) {
345
        if( value == null ) {
346
            return null;
347
        }
348
        return toTime(value.atZone(ZoneId.systemDefault()));
349
    }
350

    
351
    public static java.sql.Time toTime(ChronoLocalDate value) {
352
        if( value == null ) {
353
            return null;
354
        }
355
        return toTime(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
356
    }
357

    
358
    public static java.sql.Time toTime(LocalTime value) {
359
        if( value == null ) {
360
            return null;
361
        }
362
        return toTime(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
363
    }
364

    
365
    public static java.sql.Time toTime(ChronoZonedDateTime value) {
366
        if( value == null ) {
367
            return null;
368
        }
369
        return toTime(((ChronoZonedDateTime)value).toInstant());
370
    }
371

    
372
    public static java.sql.Time toTime(Instant value) {
373
        if( value == null ) {
374
            return null;
375
        }
376
        return (java.sql.Time) java.sql.Time.from(value);
377
    }
378
    
379
    public static java.sql.Timestamp toTimestamp(Object value) {
380
        if( value == null ) {
381
            return null;
382
        }
383
        return (java.sql.Timestamp) coerce(DataTypes.TIMESTAMP, value, null);
384
    }
385
    
386
    public static java.sql.Timestamp toTimestamp(java.util.Date value) {
387
        if( value == null ) {
388
            return null;
389
        }
390
        return (java.sql.Timestamp) new Timestamp(value.getTime());
391
    }
392
    
393
    public static java.sql.Timestamp toTimestamp(java.sql.Timestamp value) {
394
        return value;
395
    }
396
    
397
    public static java.sql.Timestamp toTimestamp(ChronoLocalDateTime value) {
398
        if( value == null ) {
399
            return null;
400
        }
401
        return toTimestamp(value.atZone(ZoneId.systemDefault()));
402
    }
403

    
404
    public static java.sql.Timestamp toTimestamp(ChronoLocalDate value) {
405
        if( value == null ) {
406
            return null;
407
        }
408
        return toTimestamp(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
409
    }
410

    
411
    public static java.sql.Timestamp toTimestamp(LocalTime value) {
412
        if( value == null ) {
413
            return null;
414
        }
415
        return toTimestamp(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
416
    }
417

    
418
    public static java.sql.Timestamp toTimestamp(ChronoZonedDateTime value) {
419
        if( value == null ) {
420
            return null;
421
        }
422
        return toTimestamp(((ChronoZonedDateTime)value).toInstant());
423
    }
424

    
425
    public static java.sql.Timestamp toTimestamp(Instant value) {
426
        if( value == null ) {
427
            return null;
428
        }
429
        return (java.sql.Timestamp) java.sql.Timestamp.from(value);
430
    }
431

    
432
    public static float toFloat(Object value) {
433
        return (float) coerce(DataTypes.FLOAT, value, Float.NaN);
434
    }
435

    
436
    public static double toDouble(Object value) {
437
        return (double) coerce(DataTypes.DOUBLE, value, Double.NaN);
438
    }
439
    
440
    public static int toInteger(Object value) {
441
        return (int) coerce(DataTypes.INT, value, -1);
442
    }
443

    
444
    public static int toByte(Object value) {
445
        return (int) coerce(DataTypes.BYTE, value, -1);
446
    }
447

    
448
    public static short toShort(Object value) {
449
      try {
450
        return (short) coerce(DataTypes.INT, value, -1);
451
      } catch(Throwable th) {
452
        return -1;
453
      }
454
    }
455

    
456
    public static long toLong(Object value) {
457
        return (long) coerce(DataTypes.LONG, value, -1);
458
    }
459

    
460
    public static boolean toBoolean(Object value) {
461
        return (boolean) coerce(DataTypes.BOOLEAN, value, false);
462
    }
463
    
464
    public static BigDecimal toBigDecimal(Object value, int precision, int scale, BigDecimal defaultValue) {
465
      CoercionContextDecimal context = CoercionContextDecimal.create(precision, scale);
466
      Coercion c = getCoercion(DataTypes.DECIMAL);
467
      try {
468
          BigDecimal x = (BigDecimal) c.coerce(value,context);
469
          if( x == null ) {
470
              return defaultValue;
471
          }
472
          return x;
473
      } catch (CoercionException ex) {
474
          return defaultValue;
475
      }
476
    }
477

    
478
    public static BigDecimal toBigDecimal(Object value, int precision, int scale) {
479
      return toBigDecimal(value, precision, scale, null);
480
    }
481
}