Class KiwiStrings2

java.lang.Object
org.kiwiproject.beta.base.KiwiStrings2

@Beta public final class KiwiStrings2 extends Object
Utilities related to strings.

These utilities could be considered for kiwi's KiwiStrings class. Or somewhere else. Or nowhere.

  • Field Details

  • Method Details

    • camelToSnakeCase

      public static String camelToSnakeCase(String value)
      Convert a camelCase value to snake_case.
      Parameters:
      value - the camelCase value; must not be blank
      Returns:
      the converted snake_case value
      Throws:
      IllegalArgumentException - if value is blank
    • camelToSnakeCaseOrEmpty

      public static Optional<String> camelToSnakeCaseOrEmpty(@Nullable String value)
      Convert a camelCase value to snake_case.
      Parameters:
      value - the camelCase value
      Returns:
      Optional containing the converted snake_case value, or an empty Optional if the input value is blank
    • camelToSnakeCaseOrNull

      public static String camelToSnakeCaseOrNull(@Nullable String value)
      Convert a camelCase value to snake_case.
      Parameters:
      value - the camelCase value
      Returns:
      the converted snake_case value, or null if the input value is blank
    • replaceNullCharactersWithEmpty

      public static String replaceNullCharactersWithEmpty(@Nullable String str)
      Replace null characters (Unicode U+0000) in str with an empty string.
      Parameters:
      str - the string to replace within
      Returns:
      a string with null characters replaced, or the original string if no null characters exist in it
    • replaceNullCharacters

      public static String replaceNullCharacters(@Nullable String str, String replacement, @Nullable String defaultValue)
      Replace null characters (Unicode U+0000) in str with the given replacement string. If the input string is null, the default value is returned.
      Parameters:
      str - the string to replace within
      replacement - the replacement string
      defaultValue - the value to return if str is null
      Returns:
      a string with null characters replaced, or the original string if no null characters exist in it
    • randomCaseVariants

      public static Set<String> randomCaseVariants(@NonNull String input)
      Generate a set of three strings (or two when the input is only one character) whose characters match the input string but are random upper and lower case using the default Locale.
      Parameters:
      input - the input string to generate random case variants for
      Returns:
      an unmodifiable set containing three random case variants of the input string
    • randomCaseVariants

      public static Set<String> randomCaseVariants(@NonNull String input, @NonNull Locale locale)
      Generate a set of three strings (or two when the input is only one character) whose characters match the input string but are random upper and lower case using the given Locale.
      Parameters:
      input - the input string to generate random case variants for
      locale - the Locale to use to conversion to upper and lower cases
      Returns:
      an unmodifiable set containing three random case variants of the input string
    • randomCaseGenerationLimit

      public static int randomCaseGenerationLimit()
      Get the current maximum number of random strings that can be generated using randomCaseVariants(String, int) and randomCaseVariants(String, int, Locale).
      Returns:
      the maximum number of random strings that can be generated
    • setRandomCaseGenerationLimit

      public static void setRandomCaseGenerationLimit(int limit)
      Set the maximum number of random strings that can be generated using randomCaseVariants(String, int) and randomCaseVariants(String, int, Locale) to the given limit.

      Setting this too high (e.g., to Integer.MAX_VALUE) will probably result in bad things happening such as extremely long run times, out of memory errors, etc. Why? Because if the input string is very long, and the strings are generated using random capitalization, it could take a prohibitively long time (i.e., near infinity) to generate the desired number of unique variants.

      Parameters:
      limit - the maximum number of random strings
      Implementation Note:
      This is expected to be called rarely, but it still uses an internal lock to provide thread-safe access.
    • resetRandomCaseGenerationLimit

      public static void resetRandomCaseGenerationLimit()
      Reset the maximum number of random strings that can be generated using randomCaseVariants(String, int) to the default value (DEFAULT_RANDOM_CASE_GENERATION_LIMIT).
      Implementation Note:
      This is expected to be called rarely, but it still uses an internal lock to provide thread-safe access.
    • randomCaseVariants

      public static Set<String> randomCaseVariants(@NonNull String input, int desiredSize)
      Generate a set of strings whose characters match the input string but are randomly upper and lower case using the default Locale.

      The maximum number of distinct strings that can be generated is 2^N where N is the length of the input string. If desiredSize is greater than this maximum, only the maximum will be generated. In addition, there is a hard maximum limit for the number of strings that will be generated. By default, this is DEFAULT_RANDOM_CASE_GENERATION_LIMIT, but its value can be changed using setRandomCaseGenerationLimit(int).

      Parameters:
      input - the input string to generate random case variants for
      desiredSize - the number of desired strings in the returned set
      Returns:
      an unmodifiable set containing random case variants of the input string
    • randomCaseVariants

      public static Set<String> randomCaseVariants(@NonNull String input, int desiredSize, @NonNull Locale locale)
      Generate a set of strings whose characters match the input string but are random upper and lower case using the given Locale.

      See additional notes in randomCaseVariants(String, int) regarding the maximum number of strings that can be generated based on the input string length as well as a limit imposed by this class.

      Parameters:
      input - the input string to generate random case variants for
      desiredSize - the number of desired strings in the returned set
      locale - the Locale to use during conversion to upper and lower cases
      Returns:
      an unmodifiable set containing random case variants of the input string
    • randomlyCapitalize

      public static String randomlyCapitalize(@NonNull String input, @NonNull Locale locale)
      For the given input string, return a string whose characters match the input string but are random upper and lower case using the given Locale.
      Parameters:
      input - the input string to randomly capitalize
      locale - the Locale to use during conversion to upper and lower cases
      Returns:
      a string marching the input string but with random capitalization
    • standardCaseVariants

      public static Set<String> standardCaseVariants(@NonNull String input)
      Generate a set of the three "standard" case variants: all uppercase, all lowercase, and sentence case (only the first letter is capitalized, and the rest are lowercase).

      Uses the default Locale to perform case conversions.

      Parameters:
      input - the input string to generate "standard" case variants for
      Returns:
      an unmodifiable set containing the three "standard" case variants of the input string
    • standardCaseVariants

      public static Set<String> standardCaseVariants(@NonNull String input, @NonNull Locale locale)
      Generate a set of the three "standard" case variants: all uppercase, all lowercase, and sentence case (only the first letter is capitalized, and the rest are lowercase). If the input string has only one character, then the returned set contains only two strings: uppercase and lowercase.

      Uses the given Locale to perform case conversions.

      Parameters:
      input - the input string to generate "standard" case variants for
      locale - the Locale to use to conversion during upper and lower cases
      Returns:
      an unmodifiable set containing the three "standard" case variants of the input string