Class StringUtils

java.lang.Object
org.openrewrite.internal.StringUtils

public class StringUtils extends Object
  • Method Details

    • trimIndentPreserveCRLF

      public static String trimIndentPreserveCRLF(@Nullable String text)
    • trimIndent

      public static String trimIndent(String text)
      Detects a common minimal indent of all the input lines and removes the indent from each line.

      This is modeled after Kotlin's trimIndent and is useful for pruning the indent from multi-line text blocks.

      Note: Blank lines do not affect the detected indent level.

      Parameters:
      text - A string that have a common indention
      Returns:
      A mutated version of the string that removed the common indention.
    • mostCommonIndent

      public static int mostCommonIndent(SortedMap<Integer,Long> indentFrequencies)
    • isBlank

      public static boolean isBlank(@Nullable String string)
      Check if the String is null or has only whitespaces.

      Modified from apache commons lang StringUtils.

      Parameters:
      string - String to check
      Returns:
      true if the String is null or has only whitespaces
    • isNullOrEmpty

      public static boolean isNullOrEmpty(@Nullable String string)
      Check if the String is empty string or null.
      Parameters:
      string - String to check
      Returns:
      true if the String is null or empty string
    • isNotEmpty

      public static boolean isNotEmpty(@Nullable String string)
    • readFully

      public static String readFully(@Nullable InputStream inputStream)
    • readFully

      public static String readFully(InputStream inputStream, Charset charset)
      If the input stream is coming from a stream with an unknown encoding, use EncodingDetectingInputStream.readFully() instead.
      Parameters:
      inputStream - An input stream.
      Returns:
      the full contents of the input stream interpreted as a string of the specified encoding
    • capitalize

      public static String capitalize(String value)
    • uncapitalize

      public static String uncapitalize(String value)
    • containsOnlyWhitespaceAndComments

      public static boolean containsOnlyWhitespaceAndComments(String text)
    • indexOfNonWhitespace

      public static int indexOfNonWhitespace(String text)
    • indexOf

      public static int indexOf(String text, Predicate<Character> test)
      Parameters:
      text - Text to scan
      test - The predicate to match
      Returns:
      The index of the first character for which the predicate returns true, or -1 if no character in the string matches the predicate.
    • countOccurrences

      public static int countOccurrences(@NonNull String text, @NonNull String substring)
      Return the number of times a substring occurs within a target string.
      Parameters:
      text - A target string
      substring - The substring to search for
      Returns:
      the number of times the substring is found in the target. 0 if no occurrences are found.
    • replaceFirst

      public static String replaceFirst(@NonNull String text, @NonNull String match, @NonNull 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 search
      match - The substring that is being searched for
      replacement - The replacement.
      Returns:
      The original string with the first occurrence replaced or the original text if a match is not found.
    • repeat

      public static String repeat(String s, int count)
    • matchesGlob

      public static boolean matchesGlob(@Nullable String value, @Nullable String globPattern)
    • indent

      public static String indent(String text)
    • greatestCommonMargin

      public static String greatestCommonMargin(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 String commonMargin(@Nullable CharSequence s1, CharSequence s2)
    • isNumeric

      public static boolean isNumeric(@Nullable String str)
    • aspectjNameToPattern

      public static String aspectjNameToPattern(String name)
      See https://eclipse.org/aspectj/doc/next/progguide/semantics-pointcuts.html#type-patterns

      An embedded * in an identifier matches any sequence of characters, but does not match the package (or inner-type) separator ".".

      The ".." wildcard matches any sequence of characters that start and end with a ".", so it can be used to pick out all types in any subpackage, or all inner types. e.g. within(com.xerox..*) picks out all join points where the code is in any declaration of a type whose name begins with "com.xerox.".

    • greatestCommonSubstringLength

      public static int greatestCommonSubstringLength(String s1, String s2)
      Parameters:
      s1 - first string
      s2 - second string
      Returns:
      length of the longest substring common to both strings
      See Also:
    • indexOfNextNonWhitespace

      public static int indexOfNextNonWhitespace(int cursor, String source)
      Returns:
      Considering C-style comments to be whitespace, return the index of the next non-whitespace character.
    • formatUriForPropertiesFile

      public static String formatUriForPropertiesFile(String uri)