Package org.hibernate.internal.util
Class StringHelper
- java.lang.Object
-
- org.hibernate.internal.util.StringHelper
-
public final class StringHelper extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceStringHelper.Renderer<T>
-
Field Summary
Fields Modifier and Type Field Description static String[]EMPTY_STRINGSstatic StringWHITESPACE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String[]add(String[] x, String sep, String[] y)static Stringcoalesce(@NonNull String fallbackValue, @NonNull String... values)static Stringcoalesce(@NonNull String fallbackValue, String value)static Stringcollapse(String name)Collapses a name.static StringcollapseQualifier(String qualifier, boolean includeDots)Given a qualifier, collapse it.static StringcollapseQualifierBase(String name, String qualifierBase)Cross betweencollapse(java.lang.String)andpartiallyUnqualify(java.lang.String, java.lang.String).static intcount(String text, char match)static intcount(String text, String match)static intcountUnquoted(String string, char character)static intfirstIndexOfChar(String sqlString, String string, int startindex)static intfirstIndexOfChar(String sqlString, BitSet keys, int startindex)static StringgenerateAlias(String description)static StringgenerateAlias(String description, int unique)Generate a nice alias for the given class name or collection role name and unique integer.static chargetFirstNonWhitespaceCharacter(String str)static chargetLastNonWhitespaceCharacter(String str)static StringgetNonEmptyOrConjunctionIfBothNonEmpty(String firstExpression, String secondExpression)static intindexOfIdentifierWord(String str, String word)Used to find the ordinal parameters (e.g.static booleanisBlank(@Nullable String string)static booleanisEmpty(@Nullable String string)static booleanisNotEmpty(@Nullable String string)static booleanisQuoted(String name)Determine if the given string is quoted (wrapped by '`' characters at beginning and end).static booleanisQuoted(String name, Dialect dialect)Determine if the given name is quoted.static Stringjoin(String separator, Iterable<?> objects)static Stringjoin(String separator, Object[] values)static Stringjoin(String separator, String[] strings)static Stringjoin(String separator, Iterator<?> objects)static <T> Stringjoin(Collection<T> values, StringHelper.Renderer<T> renderer)static StringjoinWithQualifierAndSuffix(String[] values, String qualifier, String suffix, String deliminator)static intlastIndexOfLetter(String string)static StringmoveAndToBeginning(String filter)static String[]multiply(String string, Iterator<String> placeholders, Iterator<String[]> replacements)static StringnullIfEmpty(@Nullable String value)static List<String>parseCommaSeparatedString(String incomingString)static StringpartiallyUnqualify(String name, String qualifierBase)Partially unqualifies a qualified name.static Stringqualifier(String qualifiedName)static Stringqualify(String prefix, String name)static String[]qualify(String prefix, String[] names)static StringqualifyConditionally(String prefix, String name)static StringqualifyConditionallyIfNot(String prefix, String name)Qualifiesnamewithprefixseparated by a '.' ifnameis not already qualifiedprefixis not nullstatic Stringrepeat(char character, int times)static Stringrepeat(String string, int times)static Stringrepeat(String string, int times, String deliminator)static voidrepeat(String string, int times, String separator, StringBuilder buffer)static String[]replace(String[] templates, String placeholder, String replacement)static Stringreplace(String template, String placeholder, String replacement)static Stringreplace(String template, String placeholder, String replacement, boolean wholeWords)static Stringreplace(String template, String placeholder, String replacement, boolean wholeWords, boolean encloseInParensIfNecessary)static Stringreplace(String beforePlaceholder, String afterPlaceholder, String placeholder, String replacement, boolean wholeWords, boolean encloseInParensIfNecessary)static StringreplaceOnce(String template, String placeholder, String replacement)static Stringroot(String qualifiedName)static StringsafeInterning(String string)Return the interned form of a String, or null if the parameter is null.static String[]split(String separators, String list)static String[]split(String separators, String list, boolean include)static String[]splitFull(String separators, String list)static String[]splitTrimmingTokens(String separators, String list, boolean include)static StringsubStringNullIfEmpty(String value, Character startChar)static String[]suffix(String[] columns, String suffix)static StringtoString(Object[] array)static Stringtruncate(String string, int length)static Stringunqualify(String qualifiedName)static StringunqualifyEntityName(String entityName)static Stringunquote(String name)Return the unquoted version of name (stripping the start and end '`' characters if present).static String[]unquote(String[] names, Dialect dialect)Return the unquoted version of name stripping the start and end quote characters.static Stringunquote(String name, Dialect dialect)Return the unquoted version of name stripping the start and end quote characters.static Stringunroot(String qualifiedName)
-
-
-
Field Detail
-
WHITESPACE
public static final String WHITESPACE
- See Also:
- Constant Field Values
-
EMPTY_STRINGS
public static final String[] EMPTY_STRINGS
-
-
Method Detail
-
lastIndexOfLetter
public static int lastIndexOfLetter(String string)
-
joinWithQualifierAndSuffix
public static String joinWithQualifierAndSuffix(String[] values, String qualifier, String suffix, String deliminator)
-
repeat
public static String repeat(char character, int times)
-
repeat
public static void repeat(String string, int times, String separator, StringBuilder buffer)
-
replace
public static String replace(String template, String placeholder, String replacement, boolean wholeWords)
-
replace
public static String replace(String template, String placeholder, String replacement, boolean wholeWords, boolean encloseInParensIfNecessary)
-
replace
public static String replace(String beforePlaceholder, String afterPlaceholder, String placeholder, String replacement, boolean wholeWords, boolean encloseInParensIfNecessary)
-
indexOfIdentifierWord
public static int indexOfIdentifierWord(String str, String word)
Used to find the ordinal parameters (e.g. '?1') in a string.
-
getLastNonWhitespaceCharacter
public static char getLastNonWhitespaceCharacter(String str)
-
getFirstNonWhitespaceCharacter
public static char getFirstNonWhitespaceCharacter(String str)
-
replaceOnce
public static String replaceOnce(String template, String placeholder, String replacement)
-
splitTrimmingTokens
public static String[] splitTrimmingTokens(String separators, String list, boolean include)
-
collapse
public static String collapse(String name)
Collapses a name. Mainly intended for use with classnames, where an example might serve best to explain. Imagine you have a class named 'org.hibernate.internal.util.StringHelper'; calling collapse on that classname will result in 'o.h.u.StringHelper'.- Parameters:
name- The name to collapse.- Returns:
- The collapsed name.
-
collapseQualifier
public static String collapseQualifier(String qualifier, boolean includeDots)
Given a qualifier, collapse it.- Parameters:
qualifier- The qualifier to collapse.includeDots- Should we include the dots in the collapsed form?- Returns:
- The collapsed form.
-
partiallyUnqualify
public static String partiallyUnqualify(String name, String qualifierBase)
Partially unqualifies a qualified name. For example, with a base of 'org.hibernate' the name 'org.hibernate.internal.util.StringHelper' would become 'util.StringHelper'.- Parameters:
name- The (potentially) qualified name.qualifierBase- The qualifier base.- Returns:
- The name itself, or the partially unqualified form if it begins with the qualifier base.
-
collapseQualifierBase
public static String collapseQualifierBase(String name, String qualifierBase)
Cross betweencollapse(java.lang.String)andpartiallyUnqualify(java.lang.String, java.lang.String). Functions much likecollapse(java.lang.String)except that only the qualifierBase is collapsed. For example, with a base of 'org.hibernate' the name 'org.hibernate.internal.util.StringHelper' would become 'o.h.util.StringHelper'.- Parameters:
name- The (potentially) qualified name.qualifierBase- The qualifier base.- Returns:
- The name itself if it does not begin with the qualifierBase, or the properly collapsed form otherwise.
-
multiply
public static String[] multiply(String string, Iterator<String> placeholders, Iterator<String[]> replacements)
-
count
public static int count(String text, char match)
-
countUnquoted
public static int countUnquoted(String string, char character)
-
isNotEmpty
public static boolean isNotEmpty(@Nullable String string)
-
isEmpty
public static boolean isEmpty(@Nullable String string)
-
isBlank
public static boolean isBlank(@Nullable String string)
-
qualifyConditionallyIfNot
public static String qualifyConditionallyIfNot(String prefix, String name)
Qualifiesnamewithprefixseparated by a '.' ifnameis not already qualifiedprefixis not null
- API Note:
- Similar to
qualifyConditionally(java.lang.String, java.lang.String), except that here we explicitly check whethernameis already qualified.
-
firstIndexOfChar
public static int firstIndexOfChar(String sqlString, String string, int startindex)
-
generateAlias
public static String generateAlias(String description, int unique)
Generate a nice alias for the given class name or collection role name and unique integer. Subclasses of Loader do not have to use aliases of this form.- Parameters:
description- The base name (usually an entity-name or collection-role)unique- A uniquing value- Returns:
- an alias of the form foo1_
-
isQuoted
public static boolean isQuoted(String name)
Determine if the given string is quoted (wrapped by '`' characters at beginning and end).- Parameters:
name- The name to check.- Returns:
- True if the given string starts and ends with '`'; false otherwise.
-
unquote
public static String unquote(String name)
Return the unquoted version of name (stripping the start and end '`' characters if present).- Parameters:
name- The name to be unquoted.- Returns:
- The unquoted version.
-
isQuoted
public static boolean isQuoted(String name, Dialect dialect)
Determine if the given name is quoted. It is considered quoted if either:- starts AND ends with backticks (`)
- starts with dialect-specified
open-quoteAND ends with dialect-specifiedclose-quote
- Parameters:
name- The name to checkdialect- The dialect (to determine the "real" quoting chars).- Returns:
- True if quoted, false otherwise
-
unquote
public static String unquote(String name, Dialect dialect)
Return the unquoted version of name stripping the start and end quote characters.- Parameters:
name- The name to be unquoted.dialect- The dialect (to determine the "real" quoting chars).- Returns:
- The unquoted version.
-
unquote
public static String[] unquote(String[] names, Dialect dialect)
Return the unquoted version of name stripping the start and end quote characters.- Parameters:
names- The names to be unquoted.dialect- The dialect (to determine the "real" quoting chars).- Returns:
- The unquoted versions.
-
parseCommaSeparatedString
public static List<String> parseCommaSeparatedString(String incomingString)
-
join
public static <T> String join(Collection<T> values, StringHelper.Renderer<T> renderer)
-
getNonEmptyOrConjunctionIfBothNonEmpty
public static String getNonEmptyOrConjunctionIfBothNonEmpty(String firstExpression, String secondExpression)
- Parameters:
firstExpression- the first expressionsecondExpression- the second expression- Returns:
- if
firstExpressionandsecondExpressionare both non-empty, then "( " +firstExpression+ " ) and ( " +secondExpression+ " )" is returned; iffirstExpressionis non-empty andsecondExpressionis empty, thenfirstExpressionis returned; iffirstExpressionis empty andsecondExpressionis non-empty, thensecondExpressionis returned; if bothfirstExpressionandsecondExpressionare empty, then null is returned.
-
safeInterning
public static String safeInterning(String string)
Return the interned form of a String, or null if the parameter is null.Use with caution: excessive interning is known to cause issues. Best to use only with strings which are known to be long-lived constants, and for which the chances of being actual duplicates is proven. (Even better: avoid needing interning by design changes such as reusing the known reference)
- Parameters:
string- The string to intern.- Returns:
- The interned string.
-
-