Class StringUtil

java.lang.Object
nl.pim16aap2.animatedarchitecture.core.util.StringUtil

public final class StringUtil extends Object
Utility class for strings.
  • Method Details

    • toString

      public static String toString(ExecutorService executor)
      Converts an ExecutorService to a string.

      This method is used for logging/debugging purposes.

      Parameters:
      executor - The ExecutorService to convert.
      Returns:
      A string representation of the ExecutorService.
    • randomString

      public static String randomString(int length)
      Generate an (insecure) random alphanumeric string of a given length.
      Parameters:
      length - Length of the resulting string
      Returns:
      An insecure random alphanumeric string.
    • removeTrailingNewlines

      @Contract("_ -> param1") public static StringBuilder removeTrailingNewlines(StringBuilder sb)
      Removes all trailing newlines from a StringBuilder.
      Parameters:
      sb - The StringBuilder to remove the trailing newlines from.

      This object is modified in place.

      If the StringBuilder is empty or does not end with a newline, nothing is done.

      Returns:
      The StringBuilder for chaining.
    • removeTrailingNewlines

      public static String removeTrailingNewlines(CharSequence string)
      Removes all trailing newlines from a String.
      Parameters:
      string - The String to remove the trailing newlines from.

      If the String is empty or does not end with a newline, the same object is returned.

      Returns:
      The String without trailing newlines.
    • capitalizeFirstLetter

      public static String capitalizeFirstLetter(String string)
      Capitalizes the first letter. The rest of the String is left intact.
      Parameters:
      string - The string for which to capitalize the first letter.
      Returns:
      The same string that it received as input, but with a capizalized first letter.
    • countPatternOccurrences

      public static int countPatternOccurrences(Pattern pattern, String statement)
      Obtains the numbers of question marks in a String.
      Parameters:
      statement - The String.
      Returns:
      The number of question marks in the String.
    • isValidStructureName

      public static boolean isValidStructureName(@Nullable String name)
      Check if a given string is a valid structure name. The following input is not allowed:
      • Numerical names (numerical values are reserved for UIDs).
      • Empty input.
      • Input containing spaces.
      Parameters:
      name - The name to test for validity,
      Returns:
      True if the name is allowed.
    • formatCollection

      public static <T> String formatCollection(Collection<T> collection, Function<T,String> mapper, int indent, boolean printSize)
      Formats a collection of objects into a string.

      The objects are formatted using the provided mapper function. The resulting strings are joined with a delimiter and indented by the provided amount.

      For example, given a collection of Strings ["A", "B", "C"], a mapper that returns the input string, and an indent of 0, the resulting string would be:

      - A
      - B
      - C
      

      Note that there is no trailing delimiter or new line.

      Type Parameters:
      T - The type of the objects in the collection.
      Parameters:
      collection - The collection of objects to format.
      mapper - The function to map the objects to strings.
      indent - The amount of spaces to indent the lines. This is the number of spaces before each '-'.
      printSize - Whether to print the size of the collection in parentheses before the formatted strings.
      Returns:
      A formatted string.
    • formatCollection

      public static <T> String formatCollection(Collection<T> collection, Function<T,String> mapper, int indent)
      Formats a collection of objects into a string.

      This method is a shortcut for formatCollection(Collection, Function, int, boolean) with an indent of 0 and printSize enabled.

      Type Parameters:
      T - The type of the objects in the collection.
      Parameters:
      collection - The collection of objects to format.
      mapper - The function to map the objects to strings.
      Returns:
      A formatted string.
    • formatCollection

      public static <T> String formatCollection(Collection<T> collection, Function<T,String> mapper)
      Formats a collection of objects into a string.

      This method is a shortcut for formatCollection(Collection, Function, int, boolean) with an indent of 0 and printSize enabled.

      Type Parameters:
      T - The type of the objects in the collection.
      Parameters:
      collection - The collection of objects to format.
      mapper - The function to map the objects to strings.
      Returns:
      A formatted string.
    • formatCollection

      public static String formatCollection(Collection<?> collection, int indent)
      Formats a collection of objects into a string.

      This method is a shortcut for formatCollection(Collection, Function, int, boolean) with a mapper that calls Object.toString() and printSize enabled.

      Parameters:
      collection - The collection of objects to format.
      Returns:
      A formatted string.
    • formatCollection

      public static String formatCollection(Collection<?> collection)
      Formats a collection of objects into a string.

      This method is a shortcut for formatCollection(Collection, Function, int, boolean) with an indent of 0 and the mapper that calls Object.toString() and printSize enabled.

      Parameters:
      collection - The collection of objects to format.
      Returns:
      A formatted string.
    • stringCollector

      public static Collector<String,?,String> stringCollector(String delimiter, String emptyString)
      Creates a collector that collects a stream of strings into a formatted string.

      For example, given a delimiter of "\n - " and an empty String of "{}", and a stream with values "1", "2", and "3", the resulting string would be:

      - 1
      - 2
      - 3
      

      Given the same delimiter and empty string, but an empty stream, the resulting string would be:

      {}

      Note that there is no trailing delimiter or new line.

      Example usage:

      List<String> list = List.of("1", "2", "3");
      String formatted = list.stream().collect(StringUtil.stringCollector("\n  - ", "{}"));
      
      Parameters:
      delimiter - The delimiter to prepend to each string in the stream.
      emptyString - The string representation when the stream is empty.
      Returns:
      A collector that collects the stream into a formatted string.
    • stringCollector

      public static Collector<String,?,String> stringCollector(String delimiter)
      Creates a collector that collects a stream of strings into a formatted string.

      This method is a shortcut for stringCollector(String, String) with the empty string "{}".

      Parameters:
      delimiter - The delimiter to prepend to each string in the stream.
      Returns:
      A collector that collects the stream into a formatted string.
    • stringCollector

      public static Collector<String,?,String> stringCollector()
      Creates a collector that collects a stream of strings into a formatted string.

      This method is a shortcut for stringCollector(String, String) with the empty string "{}" and the delimiter "\n- ".

      Returns:
      A collector that collects the stream into a formatted string.
    • getVariableIndices

      public static it.unimi.dsi.fastutil.ints.IntImmutableList getVariableIndices(String string, char variable)
      Gets all indices of a given character in a string.

      For example, given the string "Hello, World!" and the character 'o', this method would return the list [4, 8].

      Parameters:
      string - The string to search in.
      Returns:
      A list of all indices of the given character in the string.