Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.util;

import java.sql.*;
import java.util.Date;

import org.litesoft.commonfoundation.typeutils.*;
import org.litesoft.core.simpletypes.*;
import org.litesoft.core.simpletypes.currency.Currency;
import org.litesoft.core.simpletypes.currency.*;
import org.litesoft.core.simpletypes.temporal.*;
import org.litesoft.core.simpletypes.temporal.nonpublic.*;

public class TypeConverter
{
    public static String to_String( Object pObject )
    {
        return (pObject == null) ? null : pObject.toString();
    }

    public static Boolean to_Boolean( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof Boolean )
        {
            return (Boolean) pObject;
        }
        Boolean rv = Booleans.fromString( pObject.toString() );
        if ( rv != null )
        {
            return rv;
        }
        throw noConversion( Boolean.class, pObject, null );
    }

    public static boolean to_boolean( Object pObject )
    {
        Boolean b = to_Boolean( pObject );
        return (b != null) && b;
    }

    public static Long to_Long( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? null : zNumber.longValue();
    }

    public static long to_long( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? 0 : zNumber.longValue();
    }

    public static Integer to_Integer( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? null : zNumber.intValue();
    }

    public static int to_int( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? 0 : zNumber.intValue();
    }

    public static Short to_Short( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? null : zNumber.shortValue();
    }

    public static short to_short( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? 0 : zNumber.shortValue();
    }

    public static Double to_Double( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? null : zNumber.doubleValue();
    }

    public static double to_double( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? 0 : zNumber.doubleValue();
    }

    public static Float to_Float( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? null : zNumber.floatValue();
    }

    public static float to_float( Object pObject )
    {
        Number zNumber = toNumber( pObject );
        return (null == zNumber) ? 0 : zNumber.floatValue();
    }

    public static TextLines to_TextLines( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof TextLines )
        {
            return (TextLines) pObject;
        }
        if ( pObject instanceof TextLine )
        {
            return new TextLines( (TextLine) pObject );
        }
        if ( pObject instanceof String[] )
        {
            return new TextLines( (String[]) pObject );
        }
        if ( pObject instanceof TextLine[] )
        {
            return new TextLines( (TextLine[]) pObject );
        }
        if ( pObject instanceof Object[] )
        {
            return new TextLines( Strings.toArray( (Object[]) pObject ) );
        }
        if ( pObject instanceof java.util.Collection )
        {
            return new TextLines( Strings.toArray( (java.util.Collection) pObject ) );
        }
        return new TextLines( Strings.splitLines( pObject.toString() ) );
    }

    public static byte[] to_bytes( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof byte[] )
        {
            return (byte[]) pObject;
        }
        throw noConversion( "byte[]", pObject, null );
    }

    private static Number toNumber( Object pObject )
    {
        if ( (pObject == null) || (pObject instanceof Number) )
        {
            return (Number) pObject;
        }
        String s = pObject.toString().trim();
        if ( s.length() == 0 )
        {
            return null;
        }
        try
        {
            return Long.parseLong( s );
        }
        catch ( NumberFormatException e )
        {
            // Ignore
        }
        return Double.parseDouble( s ); // Will blow up...
    }

    public static Money to_Money( Currency pSC, Object pObject )
    {
        Objects.assertNotNull( "Currency", pSC );
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof Money )
        {
            Money zMoney = (Money) pObject;
            Currency zNewSC = zMoney.getCurrency();
            if ( zNewSC.equals( pSC ) )
            {
                return zMoney;
            }
            throw new IllegalArgumentException( "Currency convertion ('" + zNewSC + "'->'" + pSC + "') is not supported" );
        }
        return Money.fromString( pSC, pObject.toString() );
    }

    public static FixedPointLong to_FixedPointLong( int pDecimalPlaces, //
                                                    NumericFormatControl pFormatControl, Object pObject )
    {
        if ( pDecimalPlaces < 0 )
        {
            throw new IllegalArgumentException( "Decimal places not allowed to be negative" );
        }
        Objects.assertNotNull( "NumericFormatControl", pFormatControl );
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof FixedPointLong )
        {
            return FixedPointLong.getInstance( pDecimalPlaces, pFormatControl, (FixedPointLong) pObject );
        }
        return FixedPointLong.fromString( pDecimalPlaces, pFormatControl, pObject.toString() );
    }

    public static FloatingPointLong to_FloatingPointLong( NumericFormatControl pFormatControl, Object pObject )
    {
        Objects.assertNotNull( "NumericFormatControl", pFormatControl );
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof FloatingPointLong )
        {
            return FloatingPointLong.getInstance( pFormatControl, (FloatingPointLong) pObject );
        }
        return FloatingPointLong.fromString( pFormatControl, pObject.toString() );
    }

    // TODO: Temporal!!!

    public static java.sql.Timestamp to_Timestamp( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof java.sql.Timestamp )
        {
            return (java.sql.Timestamp) pObject;
        }
        return new java.sql.Timestamp( to_UtilDate( pObject ).getTime() );
    }

    public static java.sql.Time to_SqlTime( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof java.sql.Time )
        {
            return (java.sql.Time) pObject;
        }
        SimpleTime time = to_SimpleTime( TimeRes.ToSEC, pObject );
        return new java.sql.Time( time.getSqlTimeOffset() );
    }

    public static java.sql.Date to_SqlDate( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof java.sql.Date )
        {
            return (java.sql.Date) pObject;
        }
        CalendarYMD date = to_CalendarYMD( pObject );
        return to_SqlDate( date );
    }

    public static java.sql.Date to_SqlDate( CalendarYMD pDate )
    {
        return new java.sql.Date( pDate.toUtilDate().getTime() );
    }

    public static Date to_UtilDate( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof Date )
        {
            return (Date) pObject;
        }
        // TODO: xxx - use convertToCalendarAccessor(pObject)
        if ( pObject instanceof Number )
        {
            return new Date( ((Number) pObject).longValue() );
        }
        if ( pObject instanceof CalendarYMD )
        {
            return ((CalendarYMD) pObject).toUtilDate();
        }
        if ( pObject instanceof SimpleTimestamp )
        {
            return ((SimpleTimestamp) pObject).getDate();
        }
        if ( pObject instanceof SimpleTime )
        {
            return new Date( ((SimpleTime) pObject).getSqlTimeOffset() );
        }
        String s = pObject.toString();
        try
        {
            return new Date( Long.parseLong( s ) );
        }
        catch ( NumberFormatException e )
        {
            // Ignore
        }
        try
        {
            return UtilDateAdaptor.fromUtilDateToString( s ).getWallDate();
        }
        catch ( IllegalArgumentException e )
        {
            // Ignore
        }
        try
        {
            return new Date( java.sql.Date.valueOf( s ).getTime() );
        }
        catch ( IllegalArgumentException e )
        {
            // Ignore
        }
        try
        {
            return new Date( java.sql.Time.valueOf( s ).getTime() );
        }
        catch ( IllegalArgumentException e )
        {
            // Ignore
        }
        return SimpleTimestamp.fromString( s ).getDate();
    }

    public static SimpleTime to_SimpleTime( Object pObject )
    {
        return to_SimpleTime( TimeRes.ToMIN, pObject );
    }

    public static SimpleTime to_SimpleTime( TimeRes pTimeRes, Object pObject )
    {
        Objects.assertNotNull( "TimeRes", pTimeRes );
        if ( null == pObject )
        {
            return null;
        }
        SimpleTime temporal = (pObject instanceof SimpleTime) ? //
                              (SimpleTime) pObject : //
                              SimpleTime.fromString( pObject.toString() );
        if ( pTimeRes.getIndex() == temporal.getResolutionIndex() )
        {
            return temporal;
        }
        return temporal.copyWithUpdatedResolution( pTimeRes );
    }

    public static SimpleTimestamp to_SimpleTimestamp( Object pObject )
    {
        return to_SimpleTimestamp( TimeRes.ToMSEC, pObject );
    }

    public static SimpleTimestamp to_SimpleTimestamp( TimeRes pTimeRes, Object pObject )
    {
        Objects.assertNotNull( "TimeRes", pTimeRes );
        if ( null == pObject )
        {
            return null;
        }
        if ( !(pObject instanceof SimpleTimestamp) )
        {
            Date date = to_UtilDate( pObject );
            return new SimpleTimestamp( pTimeRes, date );
        }
        SimpleTimestamp temporal = (SimpleTimestamp) pObject;
        if ( pTimeRes.getIndex() == temporal.getResolutionIndex() )
        {
            return temporal;
        }
        return temporal.copyWithUpdatedResolution( pTimeRes );
    }

    public static CalendarY to_CalendarY( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof CalendarY )
        {
            return (CalendarY) pObject;
        }
        RuntimeException zProblem = null;
        try
        {
            Object zCalendarAccessor = convertToCalendarAccessor( pObject );
            if ( zCalendarAccessor instanceof CalendarAccessorY )
            {
                return new CalendarY( (CalendarAccessorY) pObject );
            }
        }
        catch ( RuntimeException e )
        {
            zProblem = e;
        }
        throw noConversion( CalendarY.class, pObject, zProblem );
    }

    public static CalendarYM to_CalendarYM( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof CalendarYM )
        {
            return (CalendarYM) pObject;
        }
        RuntimeException zProblem = null;
        try
        {
            Object zCalendarAccessor = convertToCalendarAccessor( pObject );
            if ( zCalendarAccessor instanceof CalendarAccessorYM )
            {
                return new CalendarYM( (CalendarAccessorYM) pObject );
            }
        }
        catch ( RuntimeException e )
        {
            zProblem = e;
        }
        throw noConversion( CalendarYM.class, pObject, zProblem );
    }

    public static CalendarYMD to_CalendarYMD( Object pObject )
    {
        if ( null == pObject )
        {
            return null;
        }
        if ( pObject instanceof CalendarYMD )
        {
            return (CalendarYMD) pObject;
        }
        RuntimeException zProblem = null;
        try
        {
            Object zCalendarAccessor = convertToCalendarAccessor( pObject );
            if ( zCalendarAccessor instanceof CalendarAccessorYMD )
            {
                return new CalendarYMD( (CalendarAccessorYMD) zCalendarAccessor );
            }
        }
        catch ( RuntimeException e )
        {
            zProblem = e;
        }
        throw noConversion( CalendarYMD.class, pObject, zProblem );
    }

    private static IllegalArgumentException noConversion( Class pTarget, Object pObject, RuntimeException pProblem )
    {
        return noConversion( Objects.justClassName( pTarget ), pObject, pProblem );
    }

    private static IllegalArgumentException noConversion( String pTarget, Object pObject, RuntimeException pProblem )
    {
        return new IllegalArgumentException(
                "No conversion supported to '" + pTarget + "' from '" + Objects.justClassNameOf( pObject ) + "': " + pObject, pProblem );
    }

    private static Object convertToCalendarAccessor( Object pObject )
    {
        if ( pObject instanceof Time )
        {
            return null;
        }
        if ( pObject instanceof CalendarAccessorY )
        {
            return pObject;
        }
        if ( pObject instanceof Date )
        {
            return new UtilDateAdaptor( (Date) pObject );
        }
        String s = Strings.noEmpty( pObject.toString() );
        if ( s == null )
        {
            return null;
        }
        try
        {
            return UtilDateAdaptor.fromUtilDateToString( s );
        }
        catch ( IllegalArgumentException e )
        {
            // Ignore
        }
        return TemporalParser.YMD.parse( s );
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/TypeConverter.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

914 Diff Diff GeorgeS picture GeorgeS Thu 01 Aug, 2013 13:24:53 +0000
864 Diff Diff GeorgeS picture GeorgeS Mon 19 Nov, 2012 01:48:25 +0000
862 Diff Diff GeorgeS picture GeorgeS Thu 15 Nov, 2012 02:23:36 +0000

On the Way...

860 Diff Diff GeorgeS picture GeorgeS Mon 05 Nov, 2012 01:39:02 +0000
855 Diff Diff GeorgeS picture GeorgeS Sun 04 Nov, 2012 15:32:03 +0000
854 Diff Diff GeorgeS picture GeorgeS Sun 04 Nov, 2012 15:28:27 +0000
853 Diff Diff GeorgeS picture GeorgeS Sun 04 Nov, 2012 15:18:21 +0000
851 Diff Diff GeorgeS picture GeorgeS Mon 08 Oct, 2012 00:05:32 +0000

Breaking the code as Temporal changes are implemented...

821 GeorgeS picture GeorgeS Sun 19 Aug, 2012 00:08:41 +0000