Subversion Repository Public Repository

litesoft

Diff Revisions 820 vs 821 for /trunk/Java/core/Anywhere/src/org/litesoft/core/util/UtilsCommon.java

Diff revisions: vs.
  @@ -1,7 +1,6 @@
1 1 // This Source Code is in the Public Domain per: http://litesoft.org/License.txt
2 2 package org.litesoft.core.util;
3 3
4 - import java.sql.*;
5 4 import java.util.*;
6 5
7 6 import org.litesoft.charstreams.*;
  @@ -12,10 +11,6 @@
12 11
13 12 public class UtilsCommon extends EqualSupport
14 13 {
15 - public static final String MUST_BE_POSITIVE = " must be positive";
16 - public static final String CANNOT_BE_NEGATIVE = " cannot be negative";
17 - public static final String NOT_ALLOWED_TO_BE_NULL = ": Not allowed to be null";
18 -
19 14 protected UtilsCommon()
20 15 {
21 16 }
  @@ -161,55 +156,6 @@
161 156 return Strings.noEmpty( Strings.nullOKtoString( value ) );
162 157 }
163 158
164 - public static String trimToLength( String pSource, int pMaxAcceptableLength )
165 - throws IllegalArgumentException
166 - {
167 - pMaxAcceptableLength = assertNotNegative( "MaxAcceptableLength", pMaxAcceptableLength );
168 - return ((pSource = Strings.deNull( pSource )).length() <= pMaxAcceptableLength) ? pSource : pSource.substring( 0, pMaxAcceptableLength );
169 - }
170 -
171 - public static String trimToLength( String pSource, int pMaxAcceptableLength, String pTrimmedSuffix )
172 - throws IllegalArgumentException
173 - {
174 - pMaxAcceptableLength = assertNotNegative( "MaxAcceptableLength", pMaxAcceptableLength );
175 - if ( (pSource = Strings.deNull( pSource )).length() <= pMaxAcceptableLength )
176 - {
177 - return pSource;
178 - }
179 - int zSuffixLength = (pTrimmedSuffix = Strings.deNull( pTrimmedSuffix )).length();
180 - if ( zSuffixLength == 0 )
181 - {
182 - return pSource.substring( 0, pMaxAcceptableLength );
183 - }
184 - if ( pMaxAcceptableLength < zSuffixLength )
185 - {
186 - pTrimmedSuffix = pTrimmedSuffix.substring( 0, zSuffixLength = pMaxAcceptableLength );
187 - }
188 - pMaxAcceptableLength -= zSuffixLength;
189 - return pSource.substring( 0, pMaxAcceptableLength ) + pTrimmedSuffix;
190 - }
191 -
192 - public static String combineAsLines( String... pStrings )
193 - {
194 - StringBuilder sb = new StringBuilder();
195 - if ( pStrings != null )
196 - {
197 - for ( String s : pStrings )
198 - {
199 - sb.append( s );
200 - sb.append( '\n' );
201 - }
202 - }
203 - return sb.toString();
204 - }
205 -
206 - public static String[] splitLines( String pString )
207 - {
208 - pString = Strings.normalizeNewLines( pString );
209 -
210 - return Strings.parseChar( pString, '\n' );
211 - }
212 -
213 159 public static String combine( String pSeparator, Object... pObjects )
214 160 {
215 161 if ( Objects.isNullOrEmpty( pObjects ) )
  @@ -228,38 +174,6 @@
228 174 return sb.toString();
229 175 }
230 176
231 - public static String combine( char pSeparator, String... pStrings )
232 - {
233 - if ( Objects.isNullOrEmpty( pStrings ) )
234 - {
235 - return null;
236 - }
237 - StringBuilder sb = new StringBuilder();
238 -
239 - sb.append( pStrings[0] );
240 -
241 - for ( int i = 1; i < pStrings.length; i++ )
242 - {
243 - sb.append( pSeparator );
244 - sb.append( pStrings[i] );
245 - }
246 - return sb.toString();
247 - }
248 -
249 - public static void assertAll7BitAsciiAlpha( String pObjectName, String pToBeAssert )
250 - throws IllegalArgumentException
251 - {
252 - Objects.assertNotNull( pObjectName, pToBeAssert );
253 - for ( int i = 0; i < pToBeAssert.length(); i++ )
254 - {
255 - char c = pToBeAssert.charAt( i );
256 - if ( !(Characters.is7BitAsciiAlpha( c )) )
257 - {
258 - throw new IllegalArgumentException( pObjectName + ": '" + c + "' Not a 7 Bit Ascii Alpha at: " + i );
259 - }
260 - }
261 - }
262 -
263 177 public static void assertNotEqual( String pObjectName, long pNotExpected, long pActual )
264 178 throws IllegalArgumentException
265 179 {
  @@ -278,81 +192,6 @@
278 192 }
279 193 }
280 194
281 - public static void assertNotEmptyIfNotNull( String pParamName, String pToBeAsserted )
282 - throws IllegalArgumentException
283 - {
284 - if ( (pToBeAsserted != null) && (pToBeAsserted.length() == 0) )
285 - {
286 - throw new IllegalArgumentException( pParamName + ": Not allowed to be empty ('')" );
287 - }
288 - }
289 -
290 - public static void assertNotNullNotEmptyNoSpaces( String pErrorMessage, String pStringToAssert )
291 - throws IllegalArgumentException
292 - {
293 - if ( Strings.isNullOrEmptyOrSpaces( pStringToAssert ) )
294 - {
295 - Strings.errorNullOrEmptyOrSpace( pErrorMessage, "String" );
296 - }
297 - }
298 -
299 - public static String assertNotNullNotEmpty( String pErrorMessage, String pStringToAssert )
300 - throws IllegalArgumentException
301 - {
302 - String rv = Strings.noEmpty( pStringToAssert );
303 - if ( rv == null )
304 - {
305 - Strings.errorNullOrEmpty( pErrorMessage, "String" );
306 - }
307 - return rv;
308 - }
309 -
310 - public static String assertNoLeadingOrTrailingWhiteSpace( String pErrorMessage, String pStringToAssert )
311 - throws IllegalArgumentException
312 - {
313 - if ( pStringToAssert != null )
314 - {
315 - String rv = pStringToAssert.trim();
316 - if ( pStringToAssert.length() != rv.length() )
317 - {
318 - Strings.error( "String", pErrorMessage, "No Leading or trailing whitespace allowed" );
319 - }
320 - }
321 - return pStringToAssert;
322 - }
323 -
324 - public static String[] assertNotNullNotEmptyAndNoNullsOrEmptiesAndTrim( String pErrorMessage, String[] pStringArrayToAssert )
325 - throws IllegalArgumentException
326 - {
327 - assertNotNullNotEmpty( pErrorMessage, pStringArrayToAssert );
328 - return assertNoNullsOrEmptiesAndTrim( pErrorMessage, pStringArrayToAssert );
329 - }
330 -
331 - public static void assertNotNullNotEmpty( String pErrorMessage, String[] pStringArrayToAssert )
332 - throws IllegalArgumentException
333 - {
334 - if ( Objects.isNullOrEmpty( pStringArrayToAssert ) )
335 - {
336 - Strings.errorNullOrEmpty( pErrorMessage, "String[]" );
337 - }
338 - }
339 -
340 - public static String[] assertNoNullsOrEmptiesAndTrim( String pErrorMessage, String[] pStringArrayToAssert )
341 - throws IllegalArgumentException
342 - {
343 - String[] rv = new String[pStringArrayToAssert.length];
344 - for ( int i = 0; i < pStringArrayToAssert.length; i++ )
345 - {
346 - String s = Strings.noEmpty( pStringArrayToAssert[i] );
347 - if ( s == null )
348 - {
349 - Strings.errorNullOrEmpty( pErrorMessage + "[" + i + "]", "String[]" );
350 - }
351 - rv[i] = s;
352 - }
353 - return rv;
354 - }
355 -
356 195 /**
357 196 * Assert that "value" is not null or when converted to a String is not
358 197 * empty and then return resulting "value" trimmed.
  @@ -389,61 +228,11 @@
389 228 }
390 229 if ( value instanceof String )
391 230 {
392 - value = cast( assertNotNullNotEmpty( what, value.toString() ) );
231 + value = cast( Strings.assertNotNullNotEmpty( what, value.toString() ) );
393 232 }
394 233 return value;
395 234 }
396 235
397 - public static Long assertNotNegative( String pName, Long pValue )
398 - throws IllegalArgumentException
399 - {
400 - if ( pValue != null )
401 - {
402 - assertNotNegative( pName, pValue.longValue() );
403 - }
404 - return pValue;
405 - }
406 -
407 - public static long assertNotNegative( String pName, long pValue )
408 - throws IllegalArgumentException
409 - {
410 - if ( pValue < 0 )
411 - {
412 - throw new IllegalArgumentException( pName + CANNOT_BE_NEGATIVE );
413 - }
414 - return pValue;
415 - }
416 -
417 - public static Integer assertNotNegative( String pName, Integer pValue )
418 - throws IllegalArgumentException
419 - {
420 - if ( pValue != null )
421 - {
422 - assertNotNegative( pName, pValue.intValue() );
423 - }
424 - return pValue;
425 - }
426 -
427 - public static int assertNotNegative( String pName, int pValue )
428 - throws IllegalArgumentException
429 - {
430 - if ( pValue < 0 )
431 - {
432 - throw new IllegalArgumentException( pName + CANNOT_BE_NEGATIVE );
433 - }
434 - return pValue;
435 - }
436 -
437 - public static int assertPositive( String pName, int pValue )
438 - throws IllegalArgumentException
439 - {
440 - if ( pValue <= 0 )
441 - {
442 - throw new IllegalArgumentException( pName + MUST_BE_POSITIVE );
443 - }
444 - return pValue;
445 - }
446 -
447 236 public static String displayFormatFlag( boolean pFlag, String pTrueString )
448 237 {
449 238 return pFlag ? pTrueString : "";
  @@ -474,11 +263,6 @@
474 263 return pPath.replace( '\\', '/' );
475 264 }
476 265
477 - public static String multiLineHelper( String pLine, String pAppend )
478 - {
479 - return Strings.isNullOrEmpty( pLine ) ? "" : (pLine + pAppend);
480 - }
481 -
482 266 // Groups
483 267
484 268 // Object Group:
  @@ -609,183 +393,6 @@
609 393 return sb.toString();
610 394 }
611 395
612 - public static Map<String, String> createHashMap( String... pKeyValuePairs )
613 - throws IllegalArgumentException
614 - {
615 - return populate( new HashMap<String, String>(), pKeyValuePairs );
616 - }
617 -
618 - public static Map<String, String> populate( Map<String, String> pToPopulate, String... pNameValuePairs )
619 - throws IllegalArgumentException
620 - {
621 - if ( pNameValuePairs != null )
622 - {
623 - if ( (pNameValuePairs.length & 1) == 1 )
624 - {
625 - throw new IllegalArgumentException( "NameValuePairs not paired" );
626 - }
627 - for ( int i = 0; i < pNameValuePairs.length; )
628 - {
629 - String name = pNameValuePairs[i++];
630 - String value = pNameValuePairs[i++];
631 - // noinspection unchecked
632 - pToPopulate.put( name, value );
633 - }
634 - }
635 - return pToPopulate;
636 - }
637 -
638 - public static void populate( Map pToPopulate, Map pNewContents )
639 - {
640 - if ( pNewContents != null )
641 - {
642 - //noinspection unchecked
643 - pToPopulate.putAll( pNewContents );
644 - }
645 - }
646 -
647 - public static String[] getSortedKeysAsStrings( Map pMap )
648 - {
649 - String[] names = toStringArray( pMap.keySet().toArray() );
650 - Arrays.sort( names );
651 - return names;
652 - }
653 -
654 - public static String[] arrayOfOne( String pString )
655 - {
656 - return new String[]{pString};
657 - }
658 -
659 - public static String[] toStringArray( Collection pCollection )
660 - {
661 - return (pCollection == null) ? null : toStringArray( pCollection.toArray() );
662 - }
663 -
664 - public static String[] toStringArray( Object[] pObjects )
665 - {
666 - if ( pObjects == null )
667 - {
668 - return null;
669 - }
670 - String[] zStrings = new String[pObjects.length];
671 - for ( int i = 0; i < pObjects.length; i++ )
672 - {
673 - Object o = pObjects[i];
674 - zStrings[i] = (o == null) ? null : o.toString();
675 - }
676 - return zStrings;
677 - }
678 -
679 - public static List<String> toStringList( Collection pCollection )
680 - {
681 - return (pCollection == null) ? null : toStringList( pCollection.toArray() );
682 - }
683 -
684 - public static List<String> toStringList( Object[] pObjects )
685 - {
686 - if ( pObjects == null )
687 - {
688 - return null;
689 - }
690 - List<String> strings = new ArrayList<String>( pObjects.length );
691 - for ( Object o : pObjects )
692 - {
693 - strings.add( (o == null) ? null : o.toString() );
694 - }
695 - return strings;
696 - }
697 -
698 - public static Map<String, String> addPropertiesTo( Map<String, String> pMap, String... pProperty_NameValues )
699 - throws IllegalArgumentException
700 - {
701 - if ( Objects.isNotNullOrEmpty( pProperty_NameValues ) )
702 - {
703 - if ( (pProperty_NameValues.length & 1) != 0 )
704 - {
705 - throw new IllegalArgumentException( "Attempt to add Properties that were NOT Name/Value pairs" );
706 - }
707 - for ( int i = 0; i < pProperty_NameValues.length; i += 2 )
708 - {
709 - pMap.put( pProperty_NameValues[i], pProperty_NameValues[i + 1] );
710 - }
711 - }
712 - return pMap;
713 - }
714 -
715 - public static boolean isAll7BitAsciiAlpha( String pString, int pMinLength )
716 - {
717 - if ( (pString == null) || (pString.length() < pMinLength) )
718 - {
719 - return false;
720 - }
721 - for ( int i = 0; i < pString.length(); i++ )
722 - {
723 - if ( !Characters.is7BitAsciiAlpha( pString.charAt( i ) ) )
724 - {
725 - return false;
726 - }
727 - }
728 - return true;
729 - }
730 -
731 - public static boolean isAlphaNumeric( String pString, int pMinLength )
732 - {
733 - if ( (pString == null) || (pString.length() < pMinLength) )
734 - {
735 - return false;
736 - }
737 - for ( int i = 0; i < pString.length(); i++ )
738 - {
739 - if ( !Characters.isAlphaNumeric( pString.charAt( i ) ) )
740 - {
741 - return false;
742 - }
743 - }
744 - return true;
745 - }
746 -
747 - public static boolean isNumeric( String pString, int pMinLength )
748 - {
749 - if ( (pString == null) || (pString.length() < pMinLength) )
750 - {
751 - return false;
752 - }
753 - for ( int i = 0; i < pString.length(); i++ )
754 - {
755 - if ( !Characters.isNumeric( pString.charAt( i ) ) )
756 - {
757 - return false;
758 - }
759 - }
760 - return true;
761 - }
762 -
763 - public static boolean isValidAttributeIdentifier( String pIdentifier )
764 - {
765 - return isValidAttributeIdentifier( pIdentifier, 2 );
766 - }
767 -
768 - public static boolean isValidAttributeIdentifier( String pIdentifier, int pMinLength )
769 - {
770 - if ( (pIdentifier == null) || (pIdentifier.length() < pMinLength) )
771 - {
772 - return false;
773 - }
774 -
775 - if ( !Characters.isValidAttributeIdentifierStartCharacter( pIdentifier.charAt( 0 ) ) )
776 - {
777 - return false;
778 - }
779 - for ( int i = 1; i < pIdentifier.length(); i++ )
780 - {
781 - if ( !Characters.isValidAttributeIdentifierRestCharacter( pIdentifier.charAt( i ) ) )
782 - {
783 - return false;
784 - }
785 - }
786 - return true;
787 - }
788 -
789 396 @SuppressWarnings({"unchecked", "ManualArrayToCollectionCopy", "MismatchedQueryAndUpdateOfCollection"})
790 397 public static <T> Collection<T> convert( Collection<T> pCollectionToFill, Object[] pObjects )
791 398 {
  @@ -819,54 +426,6 @@
819 426 pBuffer.append( (pValue == null) ? "null" : pValue );
820 427 }
821 428
822 - public static <T> List<T> convert( Enumeration<T> pEnumeration )
823 - {
824 - ArrayList<T> rv = new ArrayList<T>();
825 - while ( pEnumeration.hasMoreElements() )
826 - {
827 - rv.add( pEnumeration.nextElement() );
828 - }
829 - return rv;
830 - }
831 -
832 - public static String[] parseOptions( String pOptionsAsString )
833 - throws IllegalArgumentException
834 - {
835 - if ( null == (pOptionsAsString = Strings.noEmpty( pOptionsAsString )) )
836 - {
837 - return null;
838 - }
839 - char sep = pOptionsAsString.charAt( 0 );
840 - return Character.isLetterOrDigit( sep ) ? //
841 - parseOptions( pOptionsAsString, ',' ) : //
842 - parseOptions( pOptionsAsString.substring( 1 ).trim(), sep );
843 - }
844 -
845 - private static String[] parseOptions( String pStringToParse, char pSeparator )
846 - throws IllegalArgumentException
847 - {
848 - List<String> zParts = new ArrayList<String>();
849 - int from = 0;
850 - for ( int at; -1 != (at = pStringToParse.indexOf( pSeparator, from )); from = at + 1 )
851 - {
852 - addNotEmpty( zParts, pStringToParse.substring( from, at ) );
853 - }
854 - addNotEmpty( zParts, pStringToParse.substring( from ) );
855 - if ( zParts.size() < 2 )
856 - {
857 - throw new IllegalArgumentException( "Not TWO options, given: " + pStringToParse );
858 - }
859 - return toStringArray( zParts );
860 - }
861 -
862 - private static void addNotEmpty( List<String> pParts, String pPart )
863 - {
864 - if ( (pPart = pPart.trim()).length() != 0 )
865 - {
866 - pParts.add( pPart );
867 - }
868 - }
869 -
870 429 public static String printStackTraceToString( Throwable pThrowable )
871 430 {
872 431 ExceptionStackTracePrintStream ps = new ExceptionStackTracePrintStream();
  @@ -1102,11 +661,6 @@
1102 661
1103 662 // Array / varArgs
1104 663
1105 - public static String combineAsLines( List<String> pStrings )
1106 - {
1107 - return combineAsLines( (pStrings == null) ? null : pStrings.toArray( new String[pStrings.size()] ) );
1108 - }
1109 -
1110 664 public static String combine( String pSeparator, List<String> pStrings )
1111 665 {
1112 666 //noinspection SuspiciousToArrayCall
  @@ -1138,57 +692,6 @@
1138 692 return true;
1139 693 }
1140 694
1141 - public static boolean isAsciiIdentifier( String pToTest )
1142 - {
1143 - if ( !Strings.isNullOrEmpty( pToTest ) )
1144 - {
1145 - if ( Characters.isFirstCharAsciiIdentifier( pToTest.charAt( 0 ) ) )
1146 - {
1147 - for ( int i = 1; i < pToTest.length(); i++ )
1148 - {
1149 - if ( !Characters.isNonFirstCharAsciiIdentifier( pToTest.charAt( i ) ) )
1150 - {
1151 - return false;
1152 - }
1153 - }
1154 - return true;
1155 - }
1156 - }
1157 - return false;
1158 - }
1159 -
1160 - public static boolean isNoSpaceAscii( String pToTest )
1161 - {
1162 - if ( !Strings.isNullOrEmpty( pToTest ) )
1163 - {
1164 - for ( int i = 0; i < pToTest.length(); i++ )
1165 - {
1166 - if ( !Characters.isNoSpaceAscii( pToTest.charAt( i ) ) )
1167 - {
1168 - return false;
1169 - }
1170 - }
1171 - return true;
1172 - }
1173 - return false;
1174 - }
1175 -
1176 - public static boolean isNoCtrlAscii( String pToTest )
1177 - {
1178 - if ( pToTest != null )
1179 - {
1180 - for ( int i = 0; i < pToTest.length(); i++ )
1181 - {
1182 - if ( !Characters.isNoCtrlAscii( pToTest.charAt( i ) ) )
1183 - {
1184 - return false;
1185 - }
1186 - }
1187 - return true;
1188 - }
1189 - return false;
1190 - }
1191 -
1192 695 public static boolean hasText( Object pValue )
1193 696 {
1194 697 return (pValue != null) && (pValue.toString().trim().length() != 0);
  @@ -1207,32 +710,6 @@
1207 710 return pSimpleDate;
1208 711 }
1209 712
1210 - public static String singleQuoteToString( List<String> pStrings )
1211 - {
1212 - StringBuilder sb = new StringBuilder();
1213 - if ( pStrings != null )
1214 - {
1215 - char prefix = '[';
1216 - if ( pStrings.size() == 0 )
1217 - {
1218 - sb.append( prefix );
1219 - }
1220 - else
1221 - {
1222 - for ( String zString : pStrings )
1223 - {
1224 - sb.append( prefix );
1225 - sb.append( '\'' );
1226 - sb.append( zString );
1227 - sb.append( '\'' );
1228 - prefix = ',';
1229 - }
1230 - }
1231 - sb.append( ']' );
1232 - }
1233 - return sb.toString();
1234 - }
1235 -
1236 713 @SuppressWarnings({"unchecked"})
1237 714 public static <T> T cast( Object o )
1238 715 {