Class KiwiStrings2
These utilities could be considered for kiwi's KiwiStrings class.
Or somewhere else.
Or nowhere.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default maximum number of strings that will be generated byrandomCaseVariants(String, int)andrandomCaseVariants(String, int, Locale). -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcamelToSnakeCase(String value) Convert a camelCase value to snake_case.camelToSnakeCaseOrEmpty(@Nullable String value) Convert a camelCase value to snake_case.static StringcamelToSnakeCaseOrNull(@Nullable String value) Convert a camelCase value to snake_case.static intGet the current maximum number of random strings that can be generated usingrandomCaseVariants(String, int)andrandomCaseVariants(String, int, Locale).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 defaultLocale.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 defaultLocale.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 givenLocale.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 givenLocale.static StringrandomlyCapitalize(@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 givenLocale.static StringreplaceNullCharacters(@Nullable String str, String replacement, @Nullable String defaultValue) Replace null characters (Unicode U+0000) instrwith the given replacement string.static StringreplaceNullCharactersWithEmpty(@Nullable String str) Replace null characters (Unicode U+0000) instrwith an empty string.static voidReset the maximum number of random strings that can be generated usingrandomCaseVariants(String, int)to the default value (DEFAULT_RANDOM_CASE_GENERATION_LIMIT).static voidsetRandomCaseGenerationLimit(int limit) Set the maximum number of random strings that can be generated usingrandomCaseVariants(String, int)andrandomCaseVariants(String, int, Locale)to the given limit.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).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).
-
Field Details
-
DEFAULT_RANDOM_CASE_GENERATION_LIMIT
public static final int DEFAULT_RANDOM_CASE_GENERATION_LIMITThe default maximum number of strings that will be generated byrandomCaseVariants(String, int)andrandomCaseVariants(String, int, Locale).- See Also:
-
-
Method Details
-
camelToSnakeCase
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 the value is blank
-
camelToSnakeCaseOrEmpty
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
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
Replace null characters (Unicode U+0000) instrwith 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) instrwith the given replacement string. If the input string is null, the default value is returned.- Parameters:
str- the string to replace withinreplacement- the replacement stringdefaultValue- the value to return ifstris null- Returns:
- a string with null characters replaced, or the original string if no null characters exist in it
-
randomCaseVariants
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 defaultLocale.- 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
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 givenLocale.- Parameters:
input- the input string to generate random case variants forlocale- 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 usingrandomCaseVariants(String, int)andrandomCaseVariants(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 usingrandomCaseVariants(String, int)andrandomCaseVariants(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.Note that changing this affects every caller, i.e., all code that calls the
randomCaseVariantsmethods will use the limit set when calling this method.- 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 usingrandomCaseVariants(String, int)to the default value (DEFAULT_RANDOM_CASE_GENERATION_LIMIT).Note that changing this affects every caller, i.e., all code that calls the
randomCaseVariantsmethods will use the limit set when calling this method.- Implementation Note:
- This is expected to be called rarely, but it still uses an internal lock to provide thread-safe access.
-
randomCaseVariants
Generate a set of strings whose characters match the input string but are randomly upper and lower case using the defaultLocale.The maximum number of distinct strings that can be generated is 2^N where N is the length of the input string. If
desiredSizeis 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 isDEFAULT_RANDOM_CASE_GENERATION_LIMIT, but its value can be changed usingsetRandomCaseGenerationLimit(int).- Parameters:
input- the input string to generate random case variants fordesiredSize- 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 givenLocale.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 fordesiredSize- the number of desired strings in the returned setlocale- the Locale to use during conversion to upper and lower cases- Returns:
- an unmodifiable set containing random case variants of the input string
-
randomlyCapitalize
For the given input string, return a string whose characters match the input string but are random upper and lower case using the givenLocale.- Parameters:
input- the input string to randomly capitalizelocale- the Locale to use during conversion to upper and lower cases- Returns:
- a string marching the input string but with random capitalization
-
standardCaseVariants
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
Localeto 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
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
Localeto perform case conversions.- Parameters:
input- the input string to generate "standard" case variants forlocale- 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
-