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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
// 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.*;

import org.litesoft.charstreams.*;
import org.litesoft.core.simpletypes.nonpublic.*;
import org.litesoft.core.simpletypes.temporal.*;
import org.litesoft.core.simpletypes.temporal.nonpublic.*;
import org.litesoft.core.typeutils.*;

public class UtilsCommon extends EqualSupport
{
    public static final String MUST_BE_POSITIVE = " must be positive";
    public static final String CANNOT_BE_NEGATIVE = " cannot be negative";
    public static final String NOT_ALLOWED_TO_BE_NULL = ": Not allowed to be null";

    protected UtilsCommon()
    {
    }

    public static <T extends Enum<T>> T enumFromString( Class<T> pClass, String pEnumAsString )
    {
        if ( pClass != null )
        {
            if ( null != (pEnumAsString = Strings.noEmpty( pEnumAsString )) )
            {
                try
                {
                    return Enum.valueOf( pClass, pEnumAsString );
                }
                catch ( IllegalArgumentException ex )
                {
                    // Bad String
                }
            }
        }
        return null;
    }

    /**
     * Calculate the new "AccumulatedBitFlags" by Overriding the appropriate bit flags in
     * pPreviousAccumulatedBitFlags (based on the bits in pValidBitsOfNewBitFlags) from the pNewBitFlags.
     *
     * @param pPreviousAccumulatedBitFlags Bit Flags to be selectively overriden.
     * @param pValidBitsOfNewBitFlags      The '1' bits will indicate which bids to override.
     * @param pNewBitFlags                 The source of the override Bit Flags.
     *
     * @return The new "AccumulatedBitFlags".
     */
    public static int mutateAccumulatedBitFlags( int pPreviousAccumulatedBitFlags, int pValidBitsOfNewBitFlags, int pNewBitFlags )
    {
        return (pPreviousAccumulatedBitFlags & (~pValidBitsOfNewBitFlags)) //
               | (pValidBitsOfNewBitFlags & pNewBitFlags);
    }

    public static String cvtTextForDisplay( String pText )
    {
        if ( pText == null )
        {
            return "[null]";
        }
        int length = pText.length();
        StringBuilder sb = new StringBuilder( length );
        for ( int i = 0; i < length; i++ )
        {
            char c = pText.charAt( i );
            switch ( c )
            {
                case '[':
                case ']':
                case '\\':
                case '"':
                    sb.append( '\\' );
                    sb.append( c );
                    break;
                case '\n':
                    sb.append( "\\n" );
                    break;
                case '\r':
                    sb.append( "\\r" );
                    break;
                case '\t':
                    sb.append( "\\t" );
                    break;
                case '\f':
                    sb.append( "\\f" );
                    break;
                default:
                    if ( (' ' <= c) && (c < 127) )
                    {
                        sb.append( c );
                        break;
                    }
                    sb.append( '[' );
                    sb.append( Characters.cvtCharForDisplay( c ) );
                    sb.append( ']' );
                    break;
            }
        }
        return sb.toString();
    }

    public static Object[] convert( int pEntries, Enumeration<Object> pEnumeration )
    {
        Object[] them = new Object[pEntries];
        for ( int i = 0; (i < pEntries) && pEnumeration.hasMoreElements(); i++ )
        {
            them[i] = pEnumeration.nextElement();
        }
        return them;
    }

    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 ( Objects.areNonArraysEqual( pThis, pThat ) )
        {
            return true;
        }
        // Both CAN'T be null
        if ( (pThis instanceof Object[]) && (pThat instanceof Object[]) )
        {
            return Objects.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 trimToLength( String pSource, int pMaxAcceptableLength )
            throws IllegalArgumentException
    {
        pMaxAcceptableLength = assertNotNegative( "MaxAcceptableLength", pMaxAcceptableLength );
        return ((pSource = Strings.deNull( pSource )).length() <= pMaxAcceptableLength) ? pSource : pSource.substring( 0, pMaxAcceptableLength );
    }

    public static String trimToLength( String pSource, int pMaxAcceptableLength, String pTrimmedSuffix )
            throws IllegalArgumentException
    {
        pMaxAcceptableLength = assertNotNegative( "MaxAcceptableLength", pMaxAcceptableLength );
        if ( (pSource = Strings.deNull( pSource )).length() <= pMaxAcceptableLength )
        {
            return pSource;
        }
        int zSuffixLength = (pTrimmedSuffix = Strings.deNull( pTrimmedSuffix )).length();
        if ( zSuffixLength == 0 )
        {
            return pSource.substring( 0, pMaxAcceptableLength );
        }
        if ( pMaxAcceptableLength < zSuffixLength )
        {
            pTrimmedSuffix = pTrimmedSuffix.substring( 0, zSuffixLength = pMaxAcceptableLength );
        }
        pMaxAcceptableLength -= zSuffixLength;
        return pSource.substring( 0, pMaxAcceptableLength ) + pTrimmedSuffix;
    }

    public static String combineAsLines( String... pStrings )
    {
        StringBuilder sb = new StringBuilder();
        if ( pStrings != null )
        {
            for ( String s : pStrings )
            {
                sb.append( s );
                sb.append( '\n' );
            }
        }
        return sb.toString();
    }

    public static String[] splitLines( String pString )
    {
        pString = Strings.normalizeNewLines( pString );

        return Strings.parseChar( pString, '\n' );
    }

    public static String combine( String pSeparator, Object... pObjects )
    {
        if ( Objects.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();
    }

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

        sb.append( pStrings[0] );

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

    public static void assertAll7BitAsciiAlpha( String pObjectName, String pToBeAssert )
            throws IllegalArgumentException
    {
        Objects.assertNotNull( pObjectName, pToBeAssert );
        for ( int i = 0; i < pToBeAssert.length(); i++ )
        {
            char c = pToBeAssert.charAt( i );
            if ( !(Characters.is7BitAsciiAlpha( c )) )
            {
                throw new IllegalArgumentException( pObjectName + ": '" + c + "' Not a 7 Bit Ascii Alpha at: " + i );
            }
        }
    }

    public static void assertNotEqual( String pObjectName, long pNotExpected, long pActual )
            throws IllegalArgumentException
    {
        if ( pNotExpected == pActual )
        {
            throw new IllegalArgumentException( pObjectName + ": '" + pNotExpected + "' Not allowed" );
        }
    }

    public static void assertTrue( String pValueDescription, boolean pActual )
            throws IllegalArgumentException
    {
        if ( !pActual )
        {
            throw new IllegalArgumentException( pValueDescription + ": Expected to be true" );
        }
    }

    public static void assertNotEmptyIfNotNull( String pParamName, String pToBeAsserted )
            throws IllegalArgumentException
    {
        if ( (pToBeAsserted != null) && (pToBeAsserted.length() == 0) )
        {
            throw new IllegalArgumentException( pParamName + ": Not allowed to be empty ('')" );
        }
    }

    public static void assertNotNullNotEmptyNoSpaces( String pErrorMessage, String pStringToAssert )
            throws IllegalArgumentException
    {
        if ( Strings.isNullOrEmptyOrSpaces( pStringToAssert ) )
        {
            Strings.errorNullOrEmptyOrSpace( pErrorMessage, "String" );
        }
    }

    public static String assertNotNullNotEmpty( String pErrorMessage, String pStringToAssert )
            throws IllegalArgumentException
    {
        String rv = Strings.noEmpty( pStringToAssert );
        if ( rv == null )
        {
            Strings.errorNullOrEmpty( pErrorMessage, "String" );
        }
        return rv;
    }

    public static String assertNoLeadingOrTrailingWhiteSpace( String pErrorMessage, String pStringToAssert )
            throws IllegalArgumentException
    {
        if ( pStringToAssert != null )
        {
            String rv = pStringToAssert.trim();
            if ( pStringToAssert.length() != rv.length() )
            {
                Strings.error( "String", pErrorMessage, "No Leading or trailing whitespace allowed" );
            }
        }
        return pStringToAssert;
    }

    public static String[] assertNotNullNotEmptyAndNoNullsOrEmptiesAndTrim( String pErrorMessage, String[] pStringArrayToAssert )
            throws IllegalArgumentException
    {
        assertNotNullNotEmpty( pErrorMessage, pStringArrayToAssert );
        return assertNoNullsOrEmptiesAndTrim( pErrorMessage, pStringArrayToAssert );
    }

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

    public static String[] assertNoNullsOrEmptiesAndTrim( String pErrorMessage, String[] pStringArrayToAssert )
            throws IllegalArgumentException
    {
        String[] rv = new String[pStringArrayToAssert.length];
        for ( int i = 0; i < pStringArrayToAssert.length; i++ )
        {
            String s = Strings.noEmpty( pStringArrayToAssert[i] );
            if ( s == null )
            {
                Strings.errorNullOrEmpty( pErrorMessage + "[" + i + "]", "String[]" );
            }
            rv[i] = s;
        }
        return rv;
    }

    /**
     * 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( Objects.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( Objects.justClassNameOf( value ) ) );
        }
        if ( value instanceof String )
        {
            value = cast( assertNotNullNotEmpty( what, value.toString() ) );
        }
        return value;
    }

    public static Long assertNotNegative( String pName, Long pValue )
            throws IllegalArgumentException
    {
        if ( pValue != null )
        {
            assertNotNegative( pName, pValue.longValue() );
        }
        return pValue;
    }

    public static long assertNotNegative( String pName, long pValue )
            throws IllegalArgumentException
    {
        if ( pValue < 0 )
        {
            throw new IllegalArgumentException( pName + CANNOT_BE_NEGATIVE );
        }
        return pValue;
    }

    public static Integer assertNotNegative( String pName, Integer pValue )
            throws IllegalArgumentException
    {
        if ( pValue != null )
        {
            assertNotNegative( pName, pValue.intValue() );
        }
        return pValue;
    }

    public static int assertNotNegative( String pName, int pValue )
            throws IllegalArgumentException
    {
        if ( pValue < 0 )
        {
            throw new IllegalArgumentException( pName + CANNOT_BE_NEGATIVE );
        }
        return pValue;
    }

    public static int assertPositive( String pName, int pValue )
            throws IllegalArgumentException
    {
        if ( pValue <= 0 )
        {
            throw new IllegalArgumentException( pName + MUST_BE_POSITIVE );
        }
        return pValue;
    }

    public static String displayFormatFlag( boolean pFlag, String pTrueString )
    {
        return pFlag ? pTrueString : "";
    }

    public static boolean endsWithPathSep( String pPath )
    {
        return (pPath != null) && Characters.isPathSep( pPath.charAt( pPath.length() - 1 ) );
    }

    public static int lastPathSepAt( String pPath )
    {
        if ( pPath != null )
        {
            for ( int at = pPath.length(); --at >= 0; )
            {
                if ( Characters.isPathSep( pPath.charAt( at ) ) )
                {
                    return at;
                }
            }
        }
        return -1;
    }

    public static String forwardSlashPath( String pPath )
    {
        return pPath.replace( '\\', '/' );
    }

    public static String multiLineHelper( String pLine, String pAppend )
    {
        return Strings.isNullOrEmpty( pLine ) ? "" : (pLine + pAppend);
    }

    // Groups

    // Object Group:

    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 isAnyTrue( Boolean... pToSearch )
    {
        if ( pToSearch != null )
        {
            for ( Boolean zBoolean : pToSearch )
            {
                if ( Boolean.TRUE.equals( zBoolean ) )
                {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isOneOf( String pToFind, String[] pToSearch )
    {
        return isObjectOneOf( pToFind, pToSearch );
    }

    public static boolean isOneOf( Object pToFind, Object[] pToSearch )
    {
        return isObjectOneOf( pToFind, pToSearch );
    }

    private static boolean isObjectOneOf( 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 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 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 Map<String, String> createHashMap( String... pKeyValuePairs )
            throws IllegalArgumentException
    {
        return populate( new HashMap<String, String>(), pKeyValuePairs );
    }

    public static Map<String, String> populate( Map<String, String> pToPopulate, String... pNameValuePairs )
            throws IllegalArgumentException
    {
        if ( pNameValuePairs != null )
        {
            if ( (pNameValuePairs.length & 1) == 1 )
            {
                throw new IllegalArgumentException( "NameValuePairs not paired" );
            }
            for ( int i = 0; i < pNameValuePairs.length; )
            {
                String name = pNameValuePairs[i++];
                String value = pNameValuePairs[i++];
                // noinspection unchecked
                pToPopulate.put( name, value );
            }
        }
        return pToPopulate;
    }

    public static void populate( Map pToPopulate, Map pNewContents )
    {
        if ( pNewContents != null )
        {
            //noinspection unchecked
            pToPopulate.putAll( pNewContents );
        }
    }

    public static String[] getSortedKeysAsStrings( Map pMap )
    {
        String[] names = toStringArray( pMap.keySet().toArray() );
        Arrays.sort( names );
        return names;
    }

    public static String[] arrayOfOne( String pString )
    {
        return new String[]{pString};
    }

    public static String[] toStringArray( Collection pCollection )
    {
        return (pCollection == null) ? null : toStringArray( pCollection.toArray() );
    }

    public static String[] toStringArray( Object[] pObjects )
    {
        if ( pObjects == null )
        {
            return null;
        }
        String[] zStrings = new String[pObjects.length];
        for ( int i = 0; i < pObjects.length; i++ )
        {
            Object o = pObjects[i];
            zStrings[i] = (o == null) ? null : o.toString();
        }
        return zStrings;
    }

    public static List<String> toStringList( Collection pCollection )
    {
        return (pCollection == null) ? null : toStringList( pCollection.toArray() );
    }

    public static List<String> toStringList( Object[] pObjects )
    {
        if ( pObjects == null )
        {
            return null;
        }
        List<String> strings = new ArrayList<String>( pObjects.length );
        for ( Object o : pObjects )
        {
            strings.add( (o == null) ? null : o.toString() );
        }
        return strings;
    }

    public static Map<String, String> addPropertiesTo( Map<String, String> pMap, String... pProperty_NameValues )
            throws IllegalArgumentException
    {
        if ( Objects.isNotNullOrEmpty( pProperty_NameValues ) )
        {
            if ( (pProperty_NameValues.length & 1) != 0 )
            {
                throw new IllegalArgumentException( "Attempt to add Properties that were NOT Name/Value pairs" );
            }
            for ( int i = 0; i < pProperty_NameValues.length; i += 2 )
            {
                pMap.put( pProperty_NameValues[i], pProperty_NameValues[i + 1] );
            }
        }
        return pMap;
    }

    public static boolean isAll7BitAsciiAlpha( String pString, int pMinLength )
    {
        if ( (pString == null) || (pString.length() < pMinLength) )
        {
            return false;
        }
        for ( int i = 0; i < pString.length(); i++ )
        {
            if ( !Characters.is7BitAsciiAlpha( pString.charAt( i ) ) )
            {
                return false;
            }
        }
        return true;
    }

    public static boolean isAlphaNumeric( String pString, int pMinLength )
    {
        if ( (pString == null) || (pString.length() < pMinLength) )
        {
            return false;
        }
        for ( int i = 0; i < pString.length(); i++ )
        {
            if ( !Characters.isAlphaNumeric( pString.charAt( i ) ) )
            {
                return false;
            }
        }
        return true;
    }

    public static boolean isNumeric( String pString, int pMinLength )
    {
        if ( (pString == null) || (pString.length() < pMinLength) )
        {
            return false;
        }
        for ( int i = 0; i < pString.length(); i++ )
        {
            if ( !Characters.isNumeric( pString.charAt( i ) ) )
            {
                return false;
            }
        }
        return true;
    }

    public static boolean isValidAttributeIdentifier( String pIdentifier )
    {
        return isValidAttributeIdentifier( pIdentifier, 2 );
    }

    public static boolean isValidAttributeIdentifier( String pIdentifier, int pMinLength )
    {
        if ( (pIdentifier == null) || (pIdentifier.length() < pMinLength) )
        {
            return false;
        }

        if ( !Characters.isValidAttributeIdentifierStartCharacter( pIdentifier.charAt( 0 ) ) )
        {
            return false;
        }
        for ( int i = 1; i < pIdentifier.length(); i++ )
        {
            if ( !Characters.isValidAttributeIdentifierRestCharacter( pIdentifier.charAt( i ) ) )
            {
                return false;
            }
        }
        return true;
    }

    @SuppressWarnings({"unchecked", "ManualArrayToCollectionCopy", "MismatchedQueryAndUpdateOfCollection"})
    public static <T> Collection<T> convert( Collection<T> pCollectionToFill, Object[] pObjects )
    {
        Collection<Object> toFill = (Collection<Object>) pCollectionToFill;
        for ( Object object : pObjects )
        {
            toFill.add( object );
        }
        return pCollectionToFill;
    }

    public static void appendClassName( StringBuilder pBuffer, int pTabLevel, String pLabel, Object pValue )
    {
        append( pBuffer, pTabLevel, pLabel, (pValue == null) ? null : pValue.getClass().getName() );
    }

    public static void append( StringBuilder pBuffer, int pTabLevel, String pLabel, Object pValue )
    {
        append( pBuffer, pTabLevel, pLabel );
        pBuffer.append( ':' );
        pBuffer.append( (pValue == null) ? "null" : pValue.toString() );
    }

    public static void append( StringBuilder pBuffer, int pTabLevel, String pValue )
    {
        pBuffer.append( '\n' );
        while ( pTabLevel-- >= 1 )
        {
            pBuffer.append( '\t' );
        }
        pBuffer.append( (pValue == null) ? "null" : pValue );
    }

    public static <T> List<T> convert( Enumeration<T> pEnumeration )
    {
        ArrayList<T> rv = new ArrayList<T>();
        while ( pEnumeration.hasMoreElements() )
        {
            rv.add( pEnumeration.nextElement() );
        }
        return rv;
    }

    public static String[] parseOptions( String pOptionsAsString )
            throws IllegalArgumentException
    {
        if ( null == (pOptionsAsString = Strings.noEmpty( pOptionsAsString )) )
        {
            return null;
        }
        char sep = pOptionsAsString.charAt( 0 );
        return Character.isLetterOrDigit( sep ) ? //
               parseOptions( pOptionsAsString, ',' ) : //
               parseOptions( pOptionsAsString.substring( 1 ).trim(), sep );
    }

    private static String[] parseOptions( String pStringToParse, char pSeparator )
            throws IllegalArgumentException
    {
        List<String> zParts = new ArrayList<String>();
        int from = 0;
        for ( int at; -1 != (at = pStringToParse.indexOf( pSeparator, from )); from = at + 1 )
        {
            addNotEmpty( zParts, pStringToParse.substring( from, at ) );
        }
        addNotEmpty( zParts, pStringToParse.substring( from ) );
        if ( zParts.size() < 2 )
        {
            throw new IllegalArgumentException( "Not TWO options, given: " + pStringToParse );
        }
        return toStringArray( zParts );
    }

    private static void addNotEmpty( List<String> pParts, String pPart )
    {
        if ( (pPart = pPart.trim()).length() != 0 )
        {
            pParts.add( pPart );
        }
    }

    public static String printStackTraceToString( Throwable pThrowable )
    {
        ExceptionStackTracePrintStream ps = new ExceptionStackTracePrintStream();
        pThrowable.printStackTrace( ps );
        return ps.toString();
    }

    public static String normalizeCtrlChars( String pText )
    {
        if ( pText != null )
        {
            for ( int i = pText.length(); i-- > 0; )
            {
                if ( pText.charAt( i ) < ' ' )
                {
                    StringBuilder sb = new StringBuilder( pText );
                    for (; i >= 0; i-- )
                    {
                        char c = sb.charAt( i );
                        if ( c < ' ' )
                        {
                            switch ( c )
                            {
                                case '\n':
                                case '\r':
                                case '\f':
                                    break;
                                case '\t':
                                    sb.setCharAt( i, ' ' );
                                    break;
                                default:
                                    sb.setCharAt( i, '.' );
                                    break;
                            }
                        }
                        pText = sb.toString();
                    }
                    break;
                }
            }
        }
        return pText;
    }

    /**
     * Given path(s) that may be system dependent, create a path
     * that is converted to a system independent form (forward Slashes).
     * Note: Ignore null entries (null, or all nulls returns null).
     */
    public static String forwardSlashPath( String... pPaths )
    {
        StringBuilder sb = null;
        if ( pPaths != null )
        {
            for ( String path : pPaths )
            {
                if ( path != null )
                {
                    if ( sb == null )
                    {
                        sb = new StringBuilder();
                    }
                    if ( path.length() != 0 )
                    {
                        String fsp = forwardSlashPath( path );
                        if ( sb.length() == 0 )
                        {
                            sb.append( fsp );
                        }
                        else
                        {
                            boolean pStartsWithSlash = (fsp.charAt( 0 ) == '/');
                            if ( sb.charAt( sb.length() - 1 ) == '/' )
                            {
                                sb.append( pStartsWithSlash ? fsp.substring( 1 ) : fsp );
                            }
                            else
                            {
                                if ( !pStartsWithSlash )
                                {
                                    sb.append( '/' );
                                }
                                sb.append( fsp );
                            }
                        }
                    }
                }
            }
        }
        return (sb == null) ? null : sb.toString();
    }

    /**
     * @param pText !null
     */
    public static void formatToJavaSourceString( StringBuilder pSB, String pText )
    {
        LowLevelEncode( pSB, new CharSourceFromSequence( pText ), true );
    }

    public static String encodeToConstrainedASCII( String pText )
    {
        if ( Strings.isNullOrEmpty( pText ) )
        {
            return pText;
        }
        return LowLevelEncode( new StringBuilder(), new CharSourceFromSequence( pText, 0 ), true ).toString();
    }

    public static String decodeFromConstrainedASCII( String pText )
    {
        if ( Strings.isNullOrEmpty( pText ) )
        {
            return pText;
        }
        return LowLevelDecode( new StringBuilder(), new CharSourceFromSequence( pText, 0 ), false ).toString();
    }

    private static StringBuilder LowLevelEncode( StringBuilder pSB, CharSource pSource, boolean pDoubleQuoteSpecial )
    {
        for ( int c; -1 != (c = pSource.get()); )
        {
            if ( pDoubleQuoteSpecial && (c == '"') )
            {
                pSB.append( '\\' );
                pSB.append( (char) c );
            }
            else
            {
                switch ( c )
                {
                    case '\\':
                        pSB.append( '\\' );
                        pSB.append( '\\' );
                        break;
                    case '\n':
                        pSB.append( '\\' );
                        pSB.append( 'n' );
                        break;
                    case '\r':
                        pSB.append( '\\' );
                        pSB.append( 'r' );
                        break;
                    case '\t':
                        pSB.append( '\\' );
                        pSB.append( 't' );
                        break;
                    default:
                        if ( (' ' <= c) && (c < 127) )
                        {
                            pSB.append( (char) c );
                            break;
                        }
                        pSB.append( '\\' );
                        pSB.append( 'x' );
                        pSB.append( Hex.toChar( c >> 24 ) );
                        pSB.append( Hex.toChar( c >> 16 ) );
                        pSB.append( Hex.toChar( c >> 8 ) );
                        pSB.append( Hex.toChar( c ) );
                        break;
                }
            }
        }
        return pSB;
    }

    private static StringBuilder LowLevelDecode( StringBuilder pSB, CharSource pSource, boolean pDoubleQuoteSpecial )
    {
        for ( int c; -1 != (c = pSource.get()); )
        {
            if ( (c < ' ') || (127 <= c) )
            {
                throw new IllegalArgumentException( "Decoding Error: Char[" + pSource.getLastOffset() + "] Not between ' ' & Ascii 126 (inclusive)" );
            }
            if ( c != '\\' )
            {
                pSB.append( (char) c );
                continue;
            }
            if ( -1 == (c = pSource.get()) )
            {
                throw new IllegalArgumentException( "Decoding Error: No Char after escape \\" );
            }
            if ( pDoubleQuoteSpecial && (c == '"') )
            {
                pSB.append( (char) c );
            }
            else
            {
                switch ( c )
                {
                    case '\\':
                        pSB.append( (char) c );
                        break;
                    case 'n':
                        pSB.append( '\n' );
                        break;
                    case 'r':
                        pSB.append( '\r' );
                        break;
                    case 't':
                        pSB.append( '\t' );
                        break;
                    case 'x':
                        int c1 = pSource.get();
                        int c2 = pSource.get();
                        int c3 = pSource.get();
                        int c4 = pSource.get();
                        if ( c4 == -1 )
                        {
                            throw new IllegalArgumentException( "Decoding Error: Not enough Chars after \\x" );
                        }
                        try
                        {
                            int zChar = (Hex.fromCharChecked( (char) c1 ) << 24) + //
                                        (Hex.fromCharChecked( (char) c2 ) << 16) + //
                                        (Hex.fromCharChecked( (char) c3 ) << 8) + //
                                        Hex.fromCharChecked( (char) c4 );
                            pSB.append( (char) zChar );
                        }
                        catch ( IllegalArgumentException e )
                        {
                            throw new IllegalArgumentException( "Decoding Error: After \\x expected 4 hex digits: " + e.getMessage() );
                        }
                        break;
                    default:
                        throw new IllegalArgumentException( "Decoding Error: Invalid Char '" + c + "' after escape \\" );
                }
            }
        }
        return pSB;
    }

    // Array / varArgs

    public static String combineAsLines( List<String> pStrings )
    {
        return combineAsLines( (pStrings == null) ? null : pStrings.toArray( new String[pStrings.size()] ) );
    }

    public static String combine( String pSeparator, List<String> pStrings )
    {
        //noinspection SuspiciousToArrayCall
        return combine( pSeparator, (Object[]) pStrings.toArray( new String[pStrings.size()] ) );
    }

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

    // Objects:

    // Object Group:

    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 isAsciiIdentifier( String pToTest )
    {
        if ( !Strings.isNullOrEmpty( pToTest ) )
        {
            if ( Characters.isFirstCharAsciiIdentifier( pToTest.charAt( 0 ) ) )
            {
                for ( int i = 1; i < pToTest.length(); i++ )
                {
                    if ( !Characters.isNonFirstCharAsciiIdentifier( pToTest.charAt( i ) ) )
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }

    public static boolean isNoSpaceAscii( String pToTest )
    {
        if ( !Strings.isNullOrEmpty( pToTest ) )
        {
            for ( int i = 0; i < pToTest.length(); i++ )
            {
                if ( !Characters.isNoSpaceAscii( pToTest.charAt( i ) ) )
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public static boolean isNoCtrlAscii( String pToTest )
    {
        if ( pToTest != null )
        {
            for ( int i = 0; i < pToTest.length(); i++ )
            {
                if ( !Characters.isNoCtrlAscii( pToTest.charAt( i ) ) )
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

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

    public static SimpleDate forceEndOfMonth( SimpleDate pSimpleDate )
    {
        if ( pSimpleDate != null )
        {
            int zDaysInMonth = CalendarSupport.daysInMonth( pSimpleDate.getYear(), pSimpleDate.getMonth() );
            if ( pSimpleDate.getDay() != zDaysInMonth )
            {
                pSimpleDate.setDay( zDaysInMonth );
            }
        }
        return pSimpleDate;
    }

    public static String singleQuoteToString( List<String> pStrings )
    {
        StringBuilder sb = new StringBuilder();
        if ( pStrings != null )
        {
            char prefix = '[';
            if ( pStrings.size() == 0 )
            {
                sb.append( prefix );
            }
            else
            {
                for ( String zString : pStrings )
                {
                    sb.append( prefix );
                    sb.append( '\'' );
                    sb.append( zString );
                    sb.append( '\'' );
                    prefix = ',';
                }
            }
            sb.append( ']' );
        }
        return sb.toString();
    }

    @SuppressWarnings({"unchecked"})
    public static <T> T cast( Object o )
    {
        return (T) o;
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
819 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 18:09:40 +0000
811 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 13:45:18 +0000
810 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:16:07 +0000
809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
806 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 03:48:22 +0000
805 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:53:09 +0000
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
803 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:08:34 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
801 GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000