Subversion Repository Public Repository

litesoft

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

Diff revisions: vs.
  @@ -670,4 +670,377 @@
670 670 }
671 671 return pExisting.trim() + deNull( pSeparator ) + pToAppend.trim();
672 672 }
673 +
674 + public static String trimToLength( String pSource, int pMaxAcceptableLength )
675 + throws IllegalArgumentException
676 + {
677 + pMaxAcceptableLength = Integers.assertNonNegative( "MaxAcceptableLength", pMaxAcceptableLength );
678 + return ((pSource = deNull( pSource )).length() <= pMaxAcceptableLength) ? pSource : pSource.substring( 0, pMaxAcceptableLength );
679 + }
680 +
681 + public static String trimToLength( String pSource, int pMaxAcceptableLength, String pTrimmedSuffix )
682 + throws IllegalArgumentException
683 + {
684 + pMaxAcceptableLength = Integers.assertNonNegative( "MaxAcceptableLength", pMaxAcceptableLength );
685 + if ( (pSource = deNull( pSource )).length() <= pMaxAcceptableLength )
686 + {
687 + return pSource;
688 + }
689 + int zSuffixLength = (pTrimmedSuffix = deNull( pTrimmedSuffix )).length();
690 + if ( zSuffixLength == 0 )
691 + {
692 + return pSource.substring( 0, pMaxAcceptableLength );
693 + }
694 + if ( pMaxAcceptableLength < zSuffixLength )
695 + {
696 + pTrimmedSuffix = pTrimmedSuffix.substring( 0, zSuffixLength = pMaxAcceptableLength );
697 + }
698 + pMaxAcceptableLength -= zSuffixLength;
699 + return pSource.substring( 0, pMaxAcceptableLength ) + pTrimmedSuffix;
700 + }
701 +
702 + public static String combineAsLines( String... pStrings )
703 + {
704 + StringBuilder sb = new StringBuilder();
705 + if ( pStrings != null )
706 + {
707 + for ( String s : pStrings )
708 + {
709 + sb.append( s );
710 + sb.append( '\n' );
711 + }
712 + }
713 + return sb.toString();
714 + }
715 +
716 + public static String[] splitLines( String pString )
717 + {
718 + pString = normalizeNewLines( pString );
719 +
720 + return parseChar( pString, '\n' );
721 + }
722 +
723 + public static String combine( char pSeparator, String... pStrings )
724 + {
725 + if ( Objects.isNullOrEmpty( pStrings ) )
726 + {
727 + return null;
728 + }
729 + StringBuilder sb = new StringBuilder();
730 +
731 + sb.append( pStrings[0] );
732 +
733 + for ( int i = 1; i < pStrings.length; i++ )
734 + {
735 + sb.append( pSeparator );
736 + sb.append( pStrings[i] );
737 + }
738 + return sb.toString();
739 + }
740 +
741 + public static void assertAll7BitAsciiAlpha( String pObjectName, String pToBeAssert )
742 + throws IllegalArgumentException
743 + {
744 + Objects.assertNotNull( pObjectName, pToBeAssert );
745 + for ( int i = 0; i < pToBeAssert.length(); i++ )
746 + {
747 + char c = pToBeAssert.charAt( i );
748 + if ( !(Characters.is7BitAsciiAlpha( c )) )
749 + {
750 + throw new IllegalArgumentException( pObjectName + ": '" + c + "' Not a 7 Bit Ascii Alpha at: " + i );
751 + }
752 + }
753 + }
754 +
755 + public static void assertNotEmptyIfNotNull( String pParamName, String pToBeAsserted )
756 + throws IllegalArgumentException
757 + {
758 + if ( (pToBeAsserted != null) && (pToBeAsserted.length() == 0) )
759 + {
760 + throw new IllegalArgumentException( pParamName + ": Not allowed to be empty ('')" );
761 + }
762 + }
763 +
764 + public static void assertNotNullNotEmptyNoSpaces( String pErrorMessage, String pStringToAssert )
765 + throws IllegalArgumentException
766 + {
767 + if ( isNullOrEmptyOrSpaces( pStringToAssert ) )
768 + {
769 + errorNullOrEmptyOrSpace( pErrorMessage, "String" );
770 + }
771 + }
772 +
773 + public static String assertNotNullNotEmpty( String pErrorMessage, String pStringToAssert )
774 + throws IllegalArgumentException
775 + {
776 + String rv = noEmpty( pStringToAssert );
777 + if ( rv == null )
778 + {
779 + errorNullOrEmpty( pErrorMessage, "String" );
780 + }
781 + return rv;
782 + }
783 +
784 + public static String assertNoLeadingOrTrailingWhiteSpace( String pErrorMessage, String pStringToAssert )
785 + throws IllegalArgumentException
786 + {
787 + if ( pStringToAssert != null )
788 + {
789 + String rv = pStringToAssert.trim();
790 + if ( pStringToAssert.length() != rv.length() )
791 + {
792 + error( "String", pErrorMessage, "No Leading or trailing whitespace allowed" );
793 + }
794 + }
795 + return pStringToAssert;
796 + }
797 +
798 + public static String[] assertNotNullNotEmptyAndNoNullsOrEmptiesAndTrim( String pErrorMessage, String[] pStringArrayToAssert )
799 + throws IllegalArgumentException
800 + {
801 + assertNotNullNotEmpty( pErrorMessage, pStringArrayToAssert );
802 + return assertNoNullsOrEmptiesAndTrim( pErrorMessage, pStringArrayToAssert );
803 + }
804 +
805 + public static void assertNotNullNotEmpty( String pErrorMessage, String[] pStringArrayToAssert )
806 + throws IllegalArgumentException
807 + {
808 + if ( Objects.isNullOrEmpty( pStringArrayToAssert ) )
809 + {
810 + errorNullOrEmpty( pErrorMessage, "String[]" );
811 + }
812 + }
813 +
814 + public static String[] assertNoNullsOrEmptiesAndTrim( String pErrorMessage, String[] pStringArrayToAssert )
815 + throws IllegalArgumentException
816 + {
817 + String[] rv = new String[pStringArrayToAssert.length];
818 + for ( int i = 0; i < pStringArrayToAssert.length; i++ )
819 + {
820 + String s = noEmpty( pStringArrayToAssert[i] );
821 + if ( s == null )
822 + {
823 + errorNullOrEmpty( pErrorMessage + "[" + i + "]", "String[]" );
824 + }
825 + rv[i] = s;
826 + }
827 + return rv;
828 + }
829 +
830 + public static String multiLineHelper( String pLine, String pAppend )
831 + {
832 + return isNullOrEmpty( pLine ) ? "" : (pLine + pAppend);
833 + }
834 +
835 + public static String[] toArray( String pString )
836 + {
837 + return new String[]{pString};
838 + }
839 +
840 + public static String[] toArray( Collection pCollection )
841 + {
842 + return (pCollection == null) ? null : toArray( pCollection.toArray() );
843 + }
844 +
845 + public static String[] toArray( Object[] pObjects )
846 + {
847 + if ( pObjects == null )
848 + {
849 + return null;
850 + }
851 + String[] zStrings = new String[pObjects.length];
852 + for ( int i = 0; i < pObjects.length; i++ )
853 + {
854 + Object o = pObjects[i];
855 + zStrings[i] = (o == null) ? null : o.toString();
856 + }
857 + return zStrings;
858 + }
859 +
860 + public static List<String> toList( Collection pCollection )
861 + {
862 + return (pCollection == null) ? null : toList( pCollection.toArray() );
863 + }
864 +
865 + public static List<String> toList( Object[] pObjects )
866 + {
867 + if ( pObjects == null )
868 + {
869 + return null;
870 + }
871 + List<String> strings = new ArrayList<String>( pObjects.length );
872 + for ( Object o : pObjects )
873 + {
874 + strings.add( (o == null) ? null : o.toString() );
875 + }
876 + return strings;
877 + }
878 +
879 + public static boolean isAll7BitAsciiAlpha( String pString, int pMinLength )
880 + {
881 + if ( (pString == null) || (pString.length() < pMinLength) )
882 + {
883 + return false;
884 + }
885 + for ( int i = 0; i < pString.length(); i++ )
886 + {
887 + if ( !Characters.is7BitAsciiAlpha( pString.charAt( i ) ) )
888 + {
889 + return false;
890 + }
891 + }
892 + return true;
893 + }
894 +
895 + public static boolean isAlphaNumeric( String pString, int pMinLength )
896 + {
897 + if ( (pString == null) || (pString.length() < pMinLength) )
898 + {
899 + return false;
900 + }
901 + for ( int i = 0; i < pString.length(); i++ )
902 + {
903 + if ( !Characters.isAlphaNumeric( pString.charAt( i ) ) )
904 + {
905 + return false;
906 + }
907 + }
908 + return true;
909 + }
910 +
911 + public static boolean isNumeric( String pString, int pMinLength )
912 + {
913 + if ( (pString == null) || (pString.length() < pMinLength) )
914 + {
915 + return false;
916 + }
917 + for ( int i = 0; i < pString.length(); i++ )
918 + {
919 + if ( !Characters.isNumeric( pString.charAt( i ) ) )
920 + {
921 + return false;
922 + }
923 + }
924 + return true;
925 + }
926 +
927 + public static boolean isValidAttributeIdentifier( String pIdentifier )
928 + {
929 + return isValidAttributeIdentifier( pIdentifier, 2 );
930 + }
931 +
932 + public static boolean isValidAttributeIdentifier( String pIdentifier, int pMinLength )
933 + {
934 + if ( (pIdentifier == null) || (pIdentifier.length() < pMinLength) )
935 + {
936 + return false;
937 + }
938 +
939 + if ( !Characters.isValidAttributeIdentifierStartCharacter( pIdentifier.charAt( 0 ) ) )
940 + {
941 + return false;
942 + }
943 + for ( int i = 1; i < pIdentifier.length(); i++ )
944 + {
945 + if ( !Characters.isValidAttributeIdentifierRestCharacter( pIdentifier.charAt( i ) ) )
946 + {
947 + return false;
948 + }
949 + }
950 + return true;
951 + }
952 +
953 + public static String[] parseOptions( String pOptionsAsString )
954 + throws IllegalArgumentException
955 + {
956 + if ( null == (pOptionsAsString = noEmpty( pOptionsAsString )) )
957 + {
958 + return null;
959 + }
960 + char sep = pOptionsAsString.charAt( 0 );
961 + return Character.isLetterOrDigit( sep ) ? //
962 + parseOptions( pOptionsAsString, ',' ) : //
963 + parseOptions( pOptionsAsString.substring( 1 ).trim(), sep );
964 + }
965 +
966 + private static String[] parseOptions( String pStringToParse, char pSeparator )
967 + throws IllegalArgumentException
968 + {
969 + List<String> zParts = new ArrayList<String>();
970 + int from = 0;
971 + for ( int at; -1 != (at = pStringToParse.indexOf( pSeparator, from )); from = at + 1 )
972 + {
973 + addNotEmpty( zParts, pStringToParse.substring( from, at ) );
974 + }
975 + addNotEmpty( zParts, pStringToParse.substring( from ) );
976 + if ( zParts.size() < 2 )
977 + {
978 + throw new IllegalArgumentException( "Not TWO options, given: " + pStringToParse );
979 + }
980 + return toArray( zParts );
981 + }
982 +
983 + private static void addNotEmpty( List<String> pParts, String pPart )
984 + {
985 + if ( (pPart = pPart.trim()).length() != 0 )
986 + {
987 + pParts.add( pPart );
988 + }
989 + }
990 +
991 + public static String combineAsLines( List<String> pStrings )
992 + {
993 + return combineAsLines( (pStrings == null) ? null : pStrings.toArray( new String[pStrings.size()] ) );
994 + }
995 +
996 + public static boolean isAsciiIdentifier( String pToTest )
997 + {
998 + if ( !isNullOrEmpty( pToTest ) )
999 + {
1000 + if ( Characters.isFirstCharAsciiIdentifier( pToTest.charAt( 0 ) ) )
1001 + {
1002 + for ( int i = 1; i < pToTest.length(); i++ )
1003 + {
1004 + if ( !Characters.isNonFirstCharAsciiIdentifier( pToTest.charAt( i ) ) )
1005 + {
1006 + return false;
1007 + }
1008 + }
1009 + return true;
1010 + }
1011 + }
1012 + return false;
1013 + }
1014 +
1015 + public static boolean isNoSpaceAscii( String pToTest )
1016 + {
1017 + if ( !isNullOrEmpty( pToTest ) )
1018 + {
1019 + for ( int i = 0; i < pToTest.length(); i++ )
1020 + {
1021 + if ( !Characters.isNoSpaceAscii( pToTest.charAt( i ) ) )
1022 + {
1023 + return false;
1024 + }
1025 + }
1026 + return true;
1027 + }
1028 + return false;
1029 + }
1030 +
1031 + public static boolean isNoCtrlAscii( String pToTest )
1032 + {
1033 + if ( pToTest != null )
1034 + {
1035 + for ( int i = 0; i < pToTest.length(); i++ )
1036 + {
1037 + if ( !Characters.isNoCtrlAscii( pToTest.charAt( i ) ) )
1038 + {
1039 + return false;
1040 + }
1041 + }
1042 + return true;
1043 + }
1044 + return false;
1045 + }
673 1046 }