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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package org.litesoft.core.typeutils;

import org.litesoft.core.typeutils.Objects;
import java.util.*;

public class Objects
{
    public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
    public static final String NOT_ALLOWED_TO_BE_NULL = ": Not allowed to be null";

    public static void assertNull( String pObjectName, Object pToBeAssert )
            throws IllegalArgumentException
    {
        if ( pToBeAssert != null )
        {
            throw new IllegalArgumentException( pObjectName + ": Expected null, but was'" + pToBeAssert + "'" );
        }
    }

    public static <T> T assertNotNull( String pObjectName, T pToBeAssert )
            throws IllegalArgumentException
    {
        if ( pToBeAssert == null )
        {
            throw new IllegalArgumentException( pObjectName + NOT_ALLOWED_TO_BE_NULL );
        }
        return pToBeAssert;
    }

    public static void assertEqual( String pObjectName, Object pExpected, Object pActual )
            throws IllegalArgumentException
    {
        if ( null == pExpected && null == pActual )
        {
            return;
        }
        if ( null != pExpected && !pExpected.equals( pActual ) )
        {
            throw new IllegalArgumentException( pObjectName + ": Expected '" + pExpected + "', but was '" + pActual + "'" );
        }
    }

    public static boolean areNonArraysEqual( Object pThis, Object pThat )
    {
        if ( pThis == pThat ) // Same or both null
        {
            return true;
        }
        // Both CAN'T be null
        return (pThis != null) ? pThis.equals( pThat ) : pThat.equals( pThis );
    }

    public static <T> T deNull( T pToCheck, T pDefault )
    {
        return (pToCheck != null) ? pToCheck : pDefault;
    }

    public static Object[] appendObjectArrays( Object[] pArray1, Object[] pArray2 )
    {
        if ( isNullOrEmpty( pArray2 ) )
        {
            return pArray1;
        }
        if ( isNullOrEmpty( pArray1 ) )
        {
            return pArray2;
        }
        Object[] joined = new Object[pArray1.length + pArray2.length];
        System.arraycopy( pArray1, 0, joined, 0, pArray1.length );
        System.arraycopy( pArray2, 0, joined, pArray1.length, pArray2.length );
        return joined;
    }

    public static Object[] prependObject( Object pNewFirst, Object[] pTheRest )
    {
        return appendObjectArrays( new Object[]{pNewFirst}, pTheRest );
    }

    public static Object[] appendObject( Object[] pCurArray, Object pNewLast )
    {
        return appendObjectArrays( pCurArray, new Object[]{pNewLast} );
    }

    public static boolean isNotNullOrEmpty( Object[] pArrayToCheck )
    {
        return ((pArrayToCheck != null) && (pArrayToCheck.length != 0));
    }

    public static boolean isNullOrEmpty( Object[] pArrayToCheck )
    {
        return ((pArrayToCheck == null) || (pArrayToCheck.length == 0));
    }

    public static boolean areArraysEqual( Object[] pThis, Object[] pThat )
    {
        if ( pThis == pThat ) // handles if both are null
        {
            return true;
        }
        if ( (pThis != null) && (pThat != null) && (pThis.length == pThat.length) )
        {
            for ( int i = pThis.length; --i >= 0; )
            {
                if ( !areEqual( pThis[i], pThat[i] ) )
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    private static void assertElementsNotNull( String pErrorMessage, Object[] pArrayToAssert )
            throws IllegalArgumentException
    {
        for ( int i = pArrayToAssert.length; --i >= 0; )
        {
            if ( pArrayToAssert[i] == null )
            {
                Strings.errorNullOrEmpty( pErrorMessage, "Object[" + i + "]" );
            }
        }
    }

    public static void assertNotNullAndElementsNotNull( String pErrorMessage, Object[] pArrayToAssert )
            throws IllegalArgumentException
    {
        if ( pArrayToAssert == null )
        {
            Strings.errorNullOrEmpty( pErrorMessage, "Object[]" );
        }
        assertElementsNotNull( pErrorMessage, pArrayToAssert );
    }

    public static void assertNotNullNotEmptyAndElementsNotNull( String pErrorMessage, Object[] pArrayToAssert )
            throws IllegalArgumentException
    {
        assertNotNullNotEmpty( pErrorMessage, pArrayToAssert );
        assertElementsNotNull( pErrorMessage, pArrayToAssert );
    }

    public static void assertNotNullNotEmpty( String pErrorMessage, Object[] pArrayToAssert )
            throws IllegalArgumentException
    {
        if ( isNullOrEmpty( pArrayToAssert ) )
        {
            Strings.errorNullOrEmpty( pErrorMessage, "Object[]" );
        }
    }

    public static boolean isNotNull( Object pToCheck )
    {
        return (pToCheck != null);
    }

    public static int getNonNullEntryCount( Object[] pArrayToCheck )
    {
        int rv = 0;
        if ( pArrayToCheck != null )
        {
            for ( int i = pArrayToCheck.length; --i >= 0; )
            {
                if ( pArrayToCheck[i] != null )
                {
                    rv++;
                }
            }
        }
        return rv;
    }

    public static boolean hasEntries( Object[] pArrayToCheck )
    {
        if ( pArrayToCheck != null )
        {
            for ( int i = pArrayToCheck.length; --i >= 0; )
            {
                if ( pArrayToCheck[i] != null )
                {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * This method strips the package name off a fully qualified class name returning just the substring
     * beginning one character beyond the last ".".
     *
     * @return the substring beginning one character beyond the last "."; null or no "." just returns pFullyQualifiedClassName
     */
    public static String justClassName( String pFullyQualifiedClassName )
    {
        int zAt = (pFullyQualifiedClassName != null) ? pFullyQualifiedClassName.lastIndexOf( '.' ) : -1;
        return (zAt != -1) ? pFullyQualifiedClassName.substring( zAt + 1 ) : pFullyQualifiedClassName;
    }

    /**
     * This method strips the package name off a fully qualified class name returning just the substring
     * beginning one character beyond the last ".".
     *
     * @return the substring beginning one character beyond the last "."; null or no "." just returns
     */
    public static String justClassName( Class<?> pClass )
    {
        return (pClass != null) ? justClassName( pClass.getName() ) : null;
    }

    /**
     * This method strips the package name off the class name of the pObject returning just the substring
     * beginning one character beyond the last ".".
     *
     * @see Objects#justClassName(Class
     */
    public static String justClassNameOf( Object pObject )
    {
        return (pObject != null) ? justClassName( pObject.getClass() ) : null;
    }

    /**
     * This method strips the package name off the class name of the pObject returning just the substring
     * beginning one character beyond the last ".", and if it ends with "Impl" remove that.
     *
     * @see Objects#justClassName(Class
     */
    public static String justClassNameNoImplOf( Object pObject )
    {
        String zName = justClassNameOf( pObject );
        return ((zName != null) && zName.endsWith( "Impl" )) ? zName.substring( 0, zName.length() - 4 ) : zName;
    }

    public static String justClassNameIfPackage( String pClassName, String pPackage )
    {
        if ( pClassName != null )
        {
            if ( (pPackage != null) && pPackage.endsWith( "." ) && (pPackage.length() < pClassName.length()) )
            {
                String zSimpleName = justClassName( pClassName );
                if ( pClassName.equals( pPackage + zSimpleName ) )
                {
                    return zSimpleName;
                }
            }
        }
        return pClassName;
    }

    public static String justClassNameIfPackage( Class<?> pClass, String pPackage )
    {
        return (pClass != null) ? justClassNameIfPackage( pClass.getName(), pPackage ) : null;
    }

    /**
     * This method strips the package name off a fully qualified class name returning just the substring
     * beginning one character beyond the last "." (And removes the wrapping Class names if Any).
     *
     * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName )
     */
    public static String justSimpleName( String pFullyQualifiedClassName )
    {
        int zAt = (pFullyQualifiedClassName != null) ? pFullyQualifiedClassName.lastIndexOf( '$' ) : -1;
        return (zAt != -1) ? pFullyQualifiedClassName.substring( zAt + 1 ) : justClassName( pFullyQualifiedClassName );
    }

    /**
     * This method strips the package name off the fully qualified class name of the pObject returning just the substring
     * beginning one character beyond the last "." (And removes the wrapping Class names if Any).
     *
     * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName )
     */
    public static String justSimpleName( Class pClass )
    {
        return justSimpleName( (pClass != null) ? pClass.getName() : null );
    }

    /**
     * This method strips the package name off the fully qualified class name of the pObject returning just the substring
     * beginning one character beyond the last "." (And removes the wrapping Class names if Any).
     *
     * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName )
     */
    public static String justSimpleName( Object pObject )
    {
        return justSimpleName( (pObject != null) ? pObject.getClass() : null );
    }

    public static String classNameOf( Object pObject )
    {
        return (pObject != null) ? pObject.getClass().getName() : null;
    }

    /**
     * @param pCount     - < 1 means copy nothing
     * @param pTo        - is NOT extended
     * @param pFromIndex - 0 based, < 0 indicates from end (eg -1 == Last)
     * @param pToIndex   - 0 based, < 0 indicates from end (eg -1 == Last)
     *
     * @return pTo
     */
    public static Object[] copySubArrayTo( int pCount, //
                                           Object[] pFrom, int pFromIndex, //
                                           Object[] pTo, int pToIndex )
    {
        if ( (pCount > 0) && //
             isNotNullOrEmpty( pFrom ) && (pFromIndex < pFrom.length) && //
             isNotNullOrEmpty( pTo ) && (pToIndex < pTo.length) )
        {
            pCount = lesserOf( pCount, pTo, pToIndex = unNegateIndex( pTo, pToIndex ) );
            pCount = lesserOf( pCount, pFrom, pFromIndex = unNegateIndex( pFrom, pFromIndex ) );
            while ( pCount-- > 0 )
            {
                pTo[pToIndex++] = pFrom[pFromIndex++];
            }
        }
        return pTo;
    }

    private static int lesserOf( int pCount, Object[] pObjects, int pIndex )
    {
        if ( pCount > 0 )
        {
            int zMoveLen = pObjects.length - pIndex;
            if ( zMoveLen < pCount )
            {
                return zMoveLen;
            }
        }
        return pCount;
    }

    private static int unNegateIndex( Object[] pFrom, int pFromIndex )
    {
        if ( pFromIndex < 0 )
        {
            if ( (pFromIndex = pFrom.length - pFromIndex) < 0 )
            {
                pFromIndex = 0;
            }
        }
        return pFromIndex;
    }

    /**
     * @param pTo      - is NOT extended
     * @param pToIndex - 0 based, < 0 indicates from end (eg -1 == Last)
     *
     * @return pTo
     */
    public static Object[] copyArrayTo( Object[] pFrom, Object[] pTo, int pToIndex )
    {
        if ( (pFrom != null) && (pTo != null) )
        {
            copySubArrayTo( Math.min( pFrom.length, pTo.length - pToIndex ), pFrom, 0, pTo, pToIndex );
        }
        return pTo;
    }

    /**
     * @param pTo - is NOT extended
     *
     * @return pTo
     */
    public static Object[] copyArrayTo( Object[] pFrom, Object[] pTo )
    {
        if ( (pFrom != null) && (pTo != null) )
        {
            copySubArrayTo( Math.min( pFrom.length, pTo.length ), pFrom, 0, pTo, 0 );
        }
        return pTo;
    }

    public static boolean isEmptyString( Object pObject )
    {
        return pObject == null || ((pObject instanceof String) && Strings.isBlank( (String) pObject ));
    }

    public static boolean areBothEmptyStrings( Object pThis, Object pThat )
    {
        return isEmptyString( pThis ) && isEmptyString( pThat );
    }

    public static boolean areBothBooleanNotTrue( Object pThis, Object pThat )
    {
        return Booleans.isBooleanNotTrue( pThis ) && Booleans.isBooleanNotTrue( pThat );
    }

    public static boolean isNullEquivalent( Object pObject )
    {
        return isEmptyString( pObject ) || Booleans.isBooleanNotTrue( pObject );
    }

    public static boolean areBothNullEquivalent( Object pThis, Object pThat )
    {
        return isNullEquivalent( pThis ) && isNullEquivalent( pThat );
    }

    public static boolean areEqual( Object pThis, Object pThat )
    {
        if ( areNonArraysEqual( pThis, pThat ) )
        {
            return true;
        }
        // Both CAN'T be null
        if ( (pThis instanceof Object[]) && (pThat instanceof Object[]) )
        {
            return areArraysEqual( (Object[]) pThis, (Object[]) pThat );
        }
        return (pThis instanceof int[]) && (pThat instanceof int[]) && Integers.areArraysEqual( (int[]) pThis, (int[]) pThat );
    }

    public static String deNullToString( Object value, Object defaultValue )
    {
        return Strings.deNull( noEmptyToString( value ), noEmptyToString( defaultValue ) );
    }

    public static String noEmptyToString( Object value )
    {
        return Strings.noEmpty( Strings.nullOKtoString( value ) );
    }

    public static String combine( String pSeparator, List<?> pObjects )
    {
        return combine( pSeparator, pObjects.toArray() );
    }

    public static String combine( String pSeparator, Object... pObjects )
    {
        if ( isNullOrEmpty( pObjects ) )
        {
            return null;
        }
        StringBuilder sb = new StringBuilder();

        sb.append( pObjects[0] );

        for ( int i = 1; i < pObjects.length; i++ )
        {
            sb.append( pSeparator );
            sb.append( pObjects[i] );
        }
        return sb.toString();
    }

    /**
     * Assert that "value" is not null or when converted to a String is not
     * empty and then return resulting "value" trimmed.
     *
     * @param what  "field" was the problem on
     * @param value to check
     *
     * @return value converted to a String minus any leading and trailing spaces
     */
    public static String assertNoEmptyToString( String what, Object value )
    {
        String strValue = noEmptyToString( value );
        if ( strValue == null )
        {
            Strings.errorNullOrEmpty( what, String.valueOf( justClassNameOf( value ) ) );
        }
        return strValue;
    }

    /**
     * Assert that "value" is not null or empty (if it is a String) and return
     * "value" trimmed (if it is a String).
     *
     * @param what  "field" was the problem on
     * @param value to check
     *
     * @return value minus any leading and trailing spaces
     */
    public static <T> T assertNotNullOrEmpty( String what, T value )
    {
        if ( value == null )
        {
            Strings.errorNullOrEmpty( what, String.valueOf( justClassNameOf( value ) ) );
        }
        if ( value instanceof String )
        {
            value = Cast.it( Strings.assertNotNullNotEmpty( what, value.toString() ) );
        }
        return value;
    }

    public static String toStringNullToEmpty( Object pObject )
    {
        if ( pObject != null )
        {
            String rv = pObject.toString();
            if ( rv != null )
            {
                return rv;
            }
        }
        return "";
    }

    public static String toStringOrNull( Object pObject )
    {
        return Strings.noEmpty( (pObject != null) ? pObject.toString() : null );
    }

    public static String toString( Object pObject )
    {
        if ( pObject != null )
        {
            String rv = pObject.toString();
            if ( rv != null )
            {
                return rv;
            }
        }
        return null;
    }

    public static boolean isOneOf( Object pToFind, Object[] pToSearch )
    {
        if ( pToSearch != null )
        {
            for ( int i = pToSearch.length; --i >= 0; )
            {
                if ( areEqual( pToFind, pToSearch[i] ) )
                {
                    return true;
                }
            }
        }
        return false;
    }

    public static <T> T assertOneOf( T pToFind, T[] pToSearch )
    {
        if ( isOneOf( pToFind, pToSearch ) )
        {
            return pToFind;
        }
        throw new IllegalArgumentException( "Expected one of (" + optionsToString( pToSearch ) + "), but was '" + pToFind + "'" );
    }

    public static String optionsToString( Object[] pObjects )
    {
        if ( pObjects == null )
        {
            return "No Options Available";
        }
        StringBuilder sb = new StringBuilder( "Valid Options are" );
        String prefix = ": ";
        //noinspection ForLoopReplaceableByForEach
        for ( int i = 0; i < pObjects.length; i++ )
        {
            sb.append( prefix ).append( pObjects[i] );
            prefix = ", ";
        }
        return sb.toString();
    }

    public static String toString( Object[] pObjects )
    {
        if ( pObjects == null )
        {
            return "null";
        }
        return new StringBuilder().append( '<' ).append( pObjects.length ).append( ':' ).append( toString( pObjects, "," ) ).append( '>' ).toString();
    }

    public static String toString( Object[] pObjects, String separator )
    {
        if ( pObjects == null )
        {
            return "null";
        }
        StringBuilder sb = new StringBuilder();
        if ( pObjects.length > 0 )
        {
            sb.append( pObjects[0] );
            //noinspection ForLoopReplaceableByForEach
            for ( int i = 1; i < pObjects.length; i++ )
            {
                sb.append( separator ).append( pObjects[i] );
            }
        }
        return sb.toString();
    }

    public static String padIt( int pMinDesiredLength, Object pIt )
    {
        return Strings.padIt( pMinDesiredLength, "" + pIt );
    }

    public static String iTpad( Object pIt, int pMinDesiredLength )
    {
        return Strings.iTpad( "" + pIt, pMinDesiredLength );
    }

    public static boolean areNotNull( Object... pToChecks )
    {
        if ( pToChecks == null )
        {
            return false;
        }
        for ( Object toCheck : pToChecks )
        {
            if ( toCheck == null )
            {
                return false;
            }
        }
        return true;
    }

    public static boolean hasText( Object pValue )
    {
        return (pValue != null) && (pValue.toString().trim().length() != 0);
    }
}

Commits for litesoft/trunk/DeviceDesktopTest/src/org/litesoft/core/typeutils/Objects.java

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

Extracting commonfoundation

936 GeorgeS picture GeorgeS Sun 01 Jun, 2014 20:19:38 +0000

Language Support