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 interface
StringHelper.Renderer<T>
-
Field Summary
Fields Modifier and Type Field Description static String[]
EMPTY_STRINGS
static String
WHITESPACE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String[]
add(String[] x, String sep, String[] y)
static String
coalesce(@NonNull String fallbackValue, @NonNull String... values)
static String
coalesce(@NonNull String fallbackValue, String value)
static String
collapse(String name)
Collapses a name.static String
collapseQualifier(String qualifier, boolean includeDots)
Given a qualifier, collapse it.static String
collapseQualifierBase(String name, String qualifierBase)
Cross betweencollapse(java.lang.String)
andpartiallyUnqualify(java.lang.String, java.lang.String)
.static int
count(String text, char match)
static int
count(String text, String match)
static int
countUnquoted(String string, char character)
static int
firstIndexOfChar(String sqlString, String string, int startindex)
static int
firstIndexOfChar(String sqlString, BitSet keys, int startindex)
static String
generateAlias(String description)
static String
generateAlias(String description, int unique)
Generate a nice alias for the given class name or collection role name and unique integer.static char
getFirstNonWhitespaceCharacter(String str)
static char
getLastNonWhitespaceCharacter(String str)
static String
getNonEmptyOrConjunctionIfBothNonEmpty(String firstExpression, String secondExpression)
static int
indexOfIdentifierWord(String str, String word)
Used to find the ordinal parameters (e.g.static boolean
isBlank(@Nullable String string)
static boolean
isEmpty(@Nullable String string)
static boolean
isNotEmpty(@Nullable String string)
static boolean
isQuoted(String name)
Determine if the given string is quoted (wrapped by '`' characters at beginning and end).static boolean
isQuoted(String name, Dialect dialect)
Determine if the given name is quoted.static String
join(String separator, Iterable<?> objects)
static String
join(String separator, Object[] values)
static String
join(String separator, String[] strings)
static String
join(String separator, Iterator<?> objects)
static <T> String
join(Collection<T> values, StringHelper.Renderer<T> renderer)
static String
joinWithQualifierAndSuffix(String[] values, String qualifier, String suffix, String deliminator)
static int
lastIndexOfLetter(String string)
static String
moveAndToBeginning(String filter)
static String[]
multiply(String string, Iterator<String> placeholders, Iterator<String[]> replacements)
static String
nullIfEmpty(@Nullable String value)
static List<String>
parseCommaSeparatedString(String incomingString)
static String
partiallyUnqualify(String name, String qualifierBase)
Partially unqualifies a qualified name.static String
qualifier(String qualifiedName)
static String
qualify(String prefix, String name)
static String[]
qualify(String prefix, String[] names)
static String
qualifyConditionally(String prefix, String name)
static String
qualifyConditionallyIfNot(String prefix, String name)
Qualifiesname
withprefix
separated by a '.' ifname
is not already qualifiedprefix
is not nullstatic String
repeat(char character, int times)
static String
repeat(String string, int times)
static String
repeat(String string, int times, String deliminator)
static void
repeat(String string, int times, String separator, StringBuilder buffer)
static String[]
replace(String[] templates, String placeholder, String replacement)
static String
replace(String template, String placeholder, String replacement)
static String
replace(String template, String placeholder, String replacement, boolean wholeWords)
static String
replace(String template, String placeholder, String replacement, boolean wholeWords, boolean encloseInParensIfNecessary)
static String
replace(String beforePlaceholder, String afterPlaceholder, String placeholder, String replacement, boolean wholeWords, boolean encloseInParensIfNecessary)
static String
replaceOnce(String template, String placeholder, String replacement)
static String
root(String qualifiedName)
static String
safeInterning(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 String
subStringNullIfEmpty(String value, Character startChar)
static String[]
suffix(String[] columns, String suffix)
static String
toString(Object[] array)
static String
truncate(String string, int length)
static String
unqualify(String qualifiedName)
static String
unqualifyEntityName(String entityName)
static String
unquote(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 String
unquote(String name, Dialect dialect)
Return the unquoted version of name stripping the start and end quote characters.static String
unroot(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)
Qualifiesname
withprefix
separated by a '.' ifname
is not already qualifiedprefix
is not null
- API Note:
- Similar to
qualifyConditionally(java.lang.String, java.lang.String)
, except that here we explicitly check whethername
is 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-quote
AND 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
firstExpression
andsecondExpression
are both non-empty, then "( " +firstExpression
+ " ) and ( " +secondExpression
+ " )" is returned; iffirstExpression
is non-empty andsecondExpression
is empty, thenfirstExpression
is returned; iffirstExpression
is empty andsecondExpression
is non-empty, thensecondExpression
is returned; if bothfirstExpression
andsecondExpression
are 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.
-
-