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

History | View | Annotate | Download (16.3 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
        if( value == null ) {
112
            return defaultValue;
113
        }
114
        if( value instanceof Boolean ) {
115
            return (boolean) value;
116
        }
117
        return (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue);
118
    }
119

    
120
    public static boolean isTrue(Object value, boolean defaultValue) {
121
        if( value == null ) {
122
            return false;
123
        }
124
        if( value instanceof Boolean ) {
125
            return (boolean) value;
126
        }
127
        return (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue);
128
    }
129

    
130
    public static boolean isFalse(Object value, boolean defaultValue) {
131
        if( value == null ) {
132
            return true;
133
        }
134
        if( value instanceof Boolean ) {
135
            return ! (boolean) value;
136
        }
137
        return ! (boolean) coerce(DataTypes.BOOLEAN, value, defaultValue);
138
    }
139

    
140
    public static float toFloat(Object value, float defaultValue) {
141
        return (float) coerce(DataTypes.FLOAT, value, defaultValue);
142
    }
143

    
144
    public static double toDouble(Object value, double defaultValue) {
145
        return (double) coerce(DataTypes.DOUBLE, value, defaultValue);
146
    }
147

    
148
    public static String toString(Object value, String defaultValue) {
149
        return (String) coerce(DataTypes.STRING, value, defaultValue);
150
    }
151
    
152
    public static String toString(Locale locale, Object value, String defaultValue) {
153
        Coercion coercer = getCoercion(DataTypes.STRING);
154
        try {
155
            String x = (String) coercer.coerce(value, CoercionContextLocale.create(locale));
156
            if( x == null ) {
157
                return defaultValue;
158
            }
159
            return x;
160
        } catch (CoercionException ex) {
161
            return defaultValue;
162
        }
163
    }
164

    
165
    public static Date toDate(Object value, Date defaultValue) {
166
        return (Date) coerce(DataTypes.DATE, value, defaultValue);
167
    }
168

    
169
    public static Date toTime(Object value, Date defaultValue) {
170
        return (Date) coerce(DataTypes.TIME, value, defaultValue);
171
    }
172

    
173
    public static Date toTimestamp(Object value, Date defaultValue) {
174
        return (Date) coerce(DataTypes.TIMESTAMP, value, defaultValue);
175
    }
176

    
177
    public static LocalDate toLocalDate(Object value) {
178
        // De momento, dejo esta implementacion para convertir a localdate
179
        // rapidamente, pero habria que ver de repasarsela.
180
        if( value instanceof LocalDateTime ) {
181
          return (LocalDate) value;
182
        }
183
        if( value instanceof TemporalAccessor ) {
184
          try {
185
            return LocalDate.from((TemporalAccessor) value);
186
          } catch(Exception ex) {
187
          }
188
        }
189
        Date d;
190
        if( value instanceof Date ) {
191
            d = (Date) value;
192
        } else {
193
            d = (Date) coerce(DataTypes.TIMESTAMP, value, null);
194
            if( d == null ) {
195
              return null;
196
            }
197
        }
198
        return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault()).toLocalDate();
199
    }
200

    
201
    public static LocalDate toLocalDate(java.util.Date value) {
202
        return toLocalDate(value.toInstant());
203
    }
204

    
205
    public static LocalDate toLocalDate(ChronoLocalDateTime value) {
206
        return toLocalDate(value.atZone(ZoneId.systemDefault()));
207
    }
208

    
209
    public static LocalDate toLocalDate(ChronoLocalDate value) {
210
        return toLocalDate(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
211
    }
212

    
213
    public static LocalDate toLocalDate(LocalTime value) {
214
        return toLocalDate(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
215
    }
216

    
217
    public static LocalDate toLocalDate(ChronoZonedDateTime value) {
218
        return toLocalDate(((ChronoZonedDateTime)value).toInstant());
219
    }
220

    
221
    public static LocalDate toLocalDate(Instant value) {
222
        return LocalDateTime.ofInstant(value, ZoneId.systemDefault()).toLocalDate();
223
    }
224
    
225
    public static LocalDateTime toLocalDateTime(Object value) {
226
        // De momento, dejo esta implementacion para convertir a localdatetime
227
        // rapidamente, pero habria que repasarsela.
228
        if( value instanceof LocalDateTime ) {
229
          return (LocalDateTime) value;
230
        }
231
        if( value instanceof TemporalAccessor ) {
232
          try {
233
            return LocalDateTime.from((TemporalAccessor) value);
234
          } catch(Exception ex) {
235
          }
236
        }
237
        Date d;
238
        if( value instanceof Date ) {
239
            d = (Date) value;
240
        } else {
241
            d = (Date) coerce(DataTypes.TIMESTAMP, value, null);
242
            if( d == null ) {
243
              return null;
244
            }
245
        }
246
        return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault());
247
    }
248

    
249
    public static LocalDateTime toLocalDateTime(java.util.Date value) {
250
        return toLocalDateTime(value.toInstant());
251
    }
252

    
253
    public static LocalDateTime toLocalDateTime(ChronoLocalDateTime value) {
254
        return toLocalDateTime(value.atZone(ZoneId.systemDefault()));
255
    }
256

    
257
    public static LocalDateTime toLocalDateTime(ChronoLocalDate value) {
258
        return toLocalDateTime(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
259
    }
260

    
261
    public static LocalDateTime toLocalDateTime(LocalTime value) {
262
        return toLocalDateTime(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
263
    }
264

    
265
    public static LocalDateTime toLocalDateTime(ChronoZonedDateTime value) {
266
        return toLocalDateTime(((ChronoZonedDateTime)value).toInstant());
267
    }
268

    
269
    public static LocalDateTime toLocalDateTime(Instant value) {
270
        return LocalDateTime.ofInstant(value, ZoneId.systemDefault());
271
    }
272
    
273
    public static LocalTime toLocalTime(Object value) {
274
        // De momento, dejo esta implementacion para convertir a localtime
275
        // rapidamente, pero habria que ver de repasarsela.
276
        if( value instanceof LocalTime ) {
277
          return (LocalTime) value;
278
        }
279
        if( value instanceof TemporalAccessor ) {
280
          try {
281
            return LocalTime.from((TemporalAccessor) value);
282
          } catch(Exception ex) {
283
          }
284
        }
285
        Date d;
286
        if( value instanceof Date ) {
287
            d = (Date) value;
288
        } else {
289
            d = (Date) coerce(DataTypes.TIMESTAMP, value, null);
290
            if( d == null ) {
291
              return null;
292
            }
293
        }
294
        return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault()).toLocalTime();
295
    }
296

    
297
    public static LocalTime toLocalTime(java.util.Date value) {
298
        return toLocalTime(value.toInstant());
299
    }
300

    
301
    public static LocalTime toLocalTime(ChronoLocalDateTime value) {
302
        return toLocalTime(value.atZone(ZoneId.systemDefault()));
303
    }
304

    
305
    public static LocalTime toLocalTime(ChronoLocalDate value) {
306
        return toLocalTime(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
307
    }
308

    
309
    public static LocalTime toLocalTime(LocalTime value) {
310
        return toLocalTime(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
311
    }
312

    
313
    public static LocalTime toLocalTime(ChronoZonedDateTime value) {
314
        return toLocalTime(((ChronoZonedDateTime)value).toInstant());
315
    }
316

    
317
    public static LocalTime toLocalTime(Instant value) {
318
        return LocalDateTime.ofInstant(value, ZoneId.systemDefault()).toLocalTime();
319
    }
320

    
321
    public static String toString(Object value) {
322
        return (String) coerce(DataTypes.STRING, value, null);
323
    }
324

    
325

    
326
    public static java.sql.Date toDate(Object value) {
327
        if( value == null ) {
328
            return null;
329
        }
330
        return (java.sql.Date) coerce(DataTypes.DATE, value, null);
331
    }
332

    
333
    public static java.sql.Date toDate(ChronoLocalDateTime value) {
334
        if( value == null ) {
335
            return null;
336
        }
337
        return toDate(value.atZone(ZoneId.systemDefault()));
338
    }
339

    
340
    public static java.sql.Date toDate(ChronoLocalDate value) {
341
        if( value == null ) {
342
            return null;
343
        }
344
        return toDate(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
345
    }
346

    
347
    public static java.sql.Date toDate(LocalTime value) {
348
        if( value == null ) {
349
            return null;
350
        }
351
        return toDate(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
352
    }
353

    
354
    public static java.sql.Date toDate(ChronoZonedDateTime value) {
355
        if( value == null ) {
356
            return null;
357
        }
358
        return toDate(((ChronoZonedDateTime)value).toInstant());
359
    }
360

    
361
    public static java.sql.Date toDate(Instant value) {
362
        if( value == null ) {
363
            return null;
364
        }
365
        return (java.sql.Date) java.sql.Date.from(value);
366
    }
367

    
368
    public static java.sql.Time toTime(Object value) {
369
        if( value == null ) {
370
            return null;
371
        }
372
        return (java.sql.Time) coerce(DataTypes.TIME, value, null);
373
    }
374

    
375
    public static java.sql.Time toTime(ChronoLocalDateTime value) {
376
        if( value == null ) {
377
            return null;
378
        }
379
        return toTime(value.atZone(ZoneId.systemDefault()));
380
    }
381

    
382
    public static java.sql.Time toTime(ChronoLocalDate value) {
383
        if( value == null ) {
384
            return null;
385
        }
386
        return toTime(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
387
    }
388

    
389
    public static java.sql.Time toTime(LocalTime value) {
390
        if( value == null ) {
391
            return null;
392
        }
393
        return toTime(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
394
    }
395

    
396
    public static java.sql.Time toTime(ChronoZonedDateTime value) {
397
        if( value == null ) {
398
            return null;
399
        }
400
        return toTime(((ChronoZonedDateTime)value).toInstant());
401
    }
402

    
403
    public static java.sql.Time toTime(Instant value) {
404
        if( value == null ) {
405
            return null;
406
        }
407
        return (java.sql.Time) java.sql.Time.from(value);
408
    }
409
    
410
    public static java.sql.Timestamp toTimestamp(Object value) {
411
        if( value == null ) {
412
            return null;
413
        }
414
        return (java.sql.Timestamp) coerce(DataTypes.TIMESTAMP, value, null);
415
    }
416
    
417
    public static java.sql.Timestamp toTimestamp(java.util.Date value) {
418
        if( value == null ) {
419
            return null;
420
        }
421
        return (java.sql.Timestamp) new Timestamp(value.getTime());
422
    }
423
    
424
    public static java.sql.Timestamp toTimestamp(java.sql.Timestamp value) {
425
        return value;
426
    }
427
    
428
    public static java.sql.Timestamp toTimestamp(ChronoLocalDateTime value) {
429
        if( value == null ) {
430
            return null;
431
        }
432
        return toTimestamp(value.atZone(ZoneId.systemDefault()));
433
    }
434

    
435
    public static java.sql.Timestamp toTimestamp(ChronoLocalDate value) {
436
        if( value == null ) {
437
            return null;
438
        }
439
        return toTimestamp(value.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()));
440
    }
441

    
442
    public static java.sql.Timestamp toTimestamp(LocalTime value) {
443
        if( value == null ) {
444
            return null;
445
        }
446
        return toTimestamp(Instant.ofEpochSecond(value.toSecondOfDay(), value.getSecond()));
447
    }
448

    
449
    public static java.sql.Timestamp toTimestamp(ChronoZonedDateTime value) {
450
        if( value == null ) {
451
            return null;
452
        }
453
        return toTimestamp(((ChronoZonedDateTime)value).toInstant());
454
    }
455

    
456
    public static java.sql.Timestamp toTimestamp(Instant value) {
457
        if( value == null ) {
458
            return null;
459
        }
460
        return (java.sql.Timestamp) java.sql.Timestamp.from(value);
461
    }
462

    
463
    public static float toFloat(Object value) {
464
        return (float) coerce(DataTypes.FLOAT, value, Float.NaN);
465
    }
466

    
467
    public static double toDouble(Object value) {
468
        return (double) coerce(DataTypes.DOUBLE, value, Double.NaN);
469
    }
470
    
471
    public static int toInteger(Object value) {
472
        return (int) coerce(DataTypes.INT, value, -1);
473
    }
474

    
475
    public static int toByte(Object value) {
476
        return (int) coerce(DataTypes.BYTE, value, -1);
477
    }
478

    
479
    public static short toShort(Object value) {
480
      try {
481
        return (short) coerce(DataTypes.INT, value, -1);
482
      } catch(Throwable th) {
483
        return -1;
484
      }
485
    }
486

    
487
    public static long toLong(Object value) {
488
        return (long) coerce(DataTypes.LONG, value, -1);
489
    }
490

    
491
    public static boolean toBoolean(Object value) {
492
        return (boolean) coerce(DataTypes.BOOLEAN, value, false);
493
    }
494
    
495
    public static BigDecimal toBigDecimal(Object value, int precision, int scale, BigDecimal defaultValue) {
496
      CoercionContextDecimal context = CoercionContextDecimal.create(precision, scale);
497
      Coercion c = getCoercion(DataTypes.DECIMAL);
498
      try {
499
          BigDecimal x = (BigDecimal) c.coerce(value,context);
500
          if( x == null ) {
501
              return defaultValue;
502
          }
503
          return x;
504
      } catch (CoercionException ex) {
505
          return defaultValue;
506
      }
507
    }
508

    
509
    public static BigDecimal toBigDecimal(Object value, int precision, int scale) {
510
      return toBigDecimal(value, precision, scale, null);
511
    }
512
}