Package org.openrewrite.internal
Class StringUtils
- java.lang.Object
-
- org.openrewrite.internal.StringUtils
-
public class StringUtils extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Stringcapitalize(java.lang.String value)static java.lang.StringcommonMargin(@Nullable java.lang.CharSequence s1, java.lang.CharSequence s2)static intcountOccurrences(@NonNull java.lang.String text, @NonNull java.lang.String substring)Return the number of times a substring occurs within a target string.static java.lang.StringensureNewlineCountBeforeComment(java.lang.String text, int desiredNewlineCount)Return a copy of the supplied text that contains exactly the desired number of newlines appear before characters that indicate the beginning of a C-style comment, "//" and "/*".static java.lang.StringgreatestCommonMargin(java.lang.String multiline)Locate the greatest common margin of a multi-line stringstatic java.lang.Stringindent(java.lang.String text)static intindexOf(java.lang.String text, java.util.function.Predicate<java.lang.Character> test)static intindexOfNonWhitespace(java.lang.String text)static booleanisBlank(@Nullable java.lang.String string)Check if the String is null or has only whitespaces.static booleanisNullOrEmpty(@Nullable java.lang.String string)Check if the String is empty string or null.static booleanisNumeric(java.lang.String str)static booleanmatchesGlob(@Nullable java.lang.String value, @Nullable java.lang.String globPattern)static intmostCommonIndent(java.util.SortedMap<java.lang.Integer,java.lang.Long> indentFrequencies)static java.lang.StringreadFully(java.io.InputStream inputStream)static java.lang.Stringrepeat(java.lang.String s, int count)static java.lang.StringreplaceFirst(@NonNull java.lang.String text, @NonNull java.lang.String match, @NonNull java.lang.String replacement)This method will search and replace the first occurrence of a matching substring.static java.util.List<java.lang.String>splitCStyleComments(java.lang.String text)Given a prefix comprised of segments that are exclusively whitespace, single line comments, or multi-line comments, return a list of each segment.static java.lang.StringtrimIndent(java.lang.String text)static java.lang.Stringuncapitalize(java.lang.String value)
-
-
-
Method Detail
-
trimIndent
public static java.lang.String trimIndent(java.lang.String text)
-
mostCommonIndent
public static int mostCommonIndent(java.util.SortedMap<java.lang.Integer,java.lang.Long> indentFrequencies)
-
isBlank
public static boolean isBlank(@Nullable @Nullable java.lang.String string)
Check if the String is null or has only whitespaces.Modified from apache commons lang StringUtils.
- Parameters:
string- String to check- Returns:
trueif the String is null or has only whitespaces
-
isNullOrEmpty
public static boolean isNullOrEmpty(@Nullable @Nullable java.lang.String string)
Check if the String is empty string or null.- Parameters:
string- String to check- Returns:
trueif the String is null or empty string
-
readFully
public static java.lang.String readFully(java.io.InputStream inputStream)
-
capitalize
public static java.lang.String capitalize(java.lang.String value)
-
uncapitalize
public static java.lang.String uncapitalize(java.lang.String value)
-
splitCStyleComments
public static java.util.List<java.lang.String> splitCStyleComments(java.lang.String text)
Given a prefix comprised of segments that are exclusively whitespace, single line comments, or multi-line comments, return a list of each segment.Operates on C-style comments: // single line /* multi-line
If the provided input contains anything except whitespace and c-style comments this will probably explode
- Parameters:
text- The space to split- Returns:
- A list of raw whitespace, single line comments, and multi-line comments
-
ensureNewlineCountBeforeComment
public static java.lang.String ensureNewlineCountBeforeComment(java.lang.String text, int desiredNewlineCount)Return a copy of the supplied text that contains exactly the desired number of newlines appear before characters that indicate the beginning of a C-style comment, "//" and "/*".If the supplied text does not contain a comment indicator then this is equivalent to calling ensureNewlineCount()
- Parameters:
text- Original text to add newlines todesiredNewlineCount- The number of newlines that should be before the text- Returns:
- A copy of the supplied text with the desired number of newlines
-
indexOfNonWhitespace
public static int indexOfNonWhitespace(java.lang.String text)
-
indexOf
public static int indexOf(java.lang.String text, java.util.function.Predicate<java.lang.Character> test)- Parameters:
text- Text to scantest- The predicate to match- Returns:
- The index of the first character for which the predicate returns
true, or-1if no character in the string matches the predicate.
-
countOccurrences
public static int countOccurrences(@NonNull @NonNull java.lang.String text, @NonNull @NonNull java.lang.String substring)
Return the number of times a substring occurs within a target string.- Parameters:
text- A target stringsubstring- The substring to search for- Returns:
- the number of times the substring is found in the target. 0 if no occurances are found.
-
replaceFirst
public static java.lang.String replaceFirst(@NonNull @NonNull java.lang.String text, @NonNull @NonNull java.lang.String match, @NonNull @NonNull java.lang.String replacement)
This method will search and replace the first occurrence of a matching substring. There is a a replaceFirst method on the String class but that version leverages regular expressions and is a magnitude slower than this simple replacement.- Parameters:
text- The source string to searchmatch- The substring that is being searched forreplacement- The replacement.- Returns:
- The original string with the first occurrence replaced or the original text if a match is not found.
-
repeat
public static java.lang.String repeat(java.lang.String s, int count)
-
matchesGlob
public static boolean matchesGlob(@Nullable @Nullable java.lang.String value, @Nullable @Nullable java.lang.String globPattern)
-
indent
public static java.lang.String indent(java.lang.String text)
-
greatestCommonMargin
public static java.lang.String greatestCommonMargin(java.lang.String multiline)
Locate the greatest common margin of a multi-line string- Parameters:
multiline- A string of one or more lines.- Returns:
- The greatest common margin consisting only of whitespace characters.
-
commonMargin
public static java.lang.String commonMargin(@Nullable @Nullable java.lang.CharSequence s1, java.lang.CharSequence s2)
-
isNumeric
public static boolean isNumeric(java.lang.String str)
-
-