public class StringValue extends Object implements IClusterable
Objects can be constructed directly from Strings or by using the valueOf() static factory methods. The repeat() static factory methods provide a way of generating a String value that repeats a given char or String a number of times.
Conversions to a wide variety of types can be found in the to*() methods. A generic conversion can be achieved with to(Class).
The beforeFirst(), afterFirst(), beforeLast() and afterLast() methods are handy for parsing things like paths and filenames.
Modifier | Constructor and Description |
---|---|
protected |
StringValue(String text)
Private constructor to force use of static factory methods.
|
protected |
StringValue(String text,
Locale locale)
Private constructor to force use of static factory methods.
|
Modifier and Type | Method and Description |
---|---|
String |
afterFirst(char c)
Gets the substring after the first occurrence given char.
|
String |
afterLast(char c)
Gets the substring after the last occurrence given char.
|
String |
beforeFirst(char c)
Gets the substring before the first occurrence given char.
|
String |
beforeLast(char c)
Gets the substring before the last occurrence given char.
|
boolean |
equals(Object obj) |
int |
hashCode() |
boolean |
isEmpty()
Returns whether the text is null or empty
|
boolean |
isNull()
Returns whether the text is null.
|
static StringValue |
repeat(int times,
char c) |
static StringValue |
repeat(int times,
String s) |
CharSequence |
replaceAll(CharSequence searchFor,
CharSequence replaceWith)
Replaces on this text.
|
<T> T |
to(Class<T> type)
Converts this StringValue to a given type.
|
boolean |
toBoolean()
Convert this text to a boolean.
|
boolean |
toBoolean(boolean defaultValue)
Convert to boolean, returning default value if text is inconvertible.
|
Boolean |
toBooleanObject()
Convert this text to a boolean.
|
char |
toChar()
Convert this text to a char.
|
char |
toChar(char defaultValue)
Convert to character, returning default value if text is inconvertible.
|
Character |
toCharacter()
Convert this text to a Character.
|
double |
toDouble()
Convert this text to a double.
|
double |
toDouble(double defaultValue)
Convert to double, returning default value if text is inconvertible.
|
Double |
toDoubleObject()
Convert this text to a Double.
|
Duration |
toDuration()
Convert this text to a Duration instance.
|
Duration |
toDuration(Duration defaultValue)
Convert to duration, returning default value if text is inconvertible.
|
<T extends Enum<T>> |
toEnum(Class<T> eClass)
Convert this text to an enum.
|
<T extends Enum<T>> |
toEnum(Class<T> eClass,
T defaultValue)
Convert this text to an enum.
|
<T extends Enum<T>> |
toEnum(T defaultValue)
Convert this text to an enum.
|
int |
toInt()
Convert this text to an int.
|
int |
toInt(int defaultValue)
Convert to integer, returning default value if text is inconvertible.
|
Integer |
toInteger()
Convert this text to an Integer.
|
long |
toLong()
Convert this text to a long.
|
long |
toLong(long defaultValue)
Convert to long integer, returning default value if text is inconvertible.
|
Long |
toLongObject()
Convert this text to a Long.
|
<T> T |
toOptional(Class<T> type)
Converts this StringValue to a given type or
null if the value is empty. |
Boolean |
toOptionalBoolean()
Convert to object types, returning null if text is null or empty.
|
Character |
toOptionalCharacter()
Convert to object types, returning null if text is null or empty.
|
Double |
toOptionalDouble()
Convert to object types, returning null if text is null or empty.
|
Duration |
toOptionalDuration()
Convert to object types, returning null if text is null or empty.
|
<T extends Enum<T>> |
toOptionalEnum(Class<T> eClass)
Convert to enum, returning null if text is null or empty.
|
Integer |
toOptionalInteger()
Convert to object types, returning null if text is null or empty.
|
Long |
toOptionalLong()
Convert to object types, returning null if text is null or empty.
|
String |
toOptionalString()
Convert to object types, returning null if text is null.
|
Time |
toOptionalTime()
Convert to object types, returning null if text is null or empty.
|
String |
toString() |
String |
toString(String defaultValue)
Convert to primitive types, returning default value if text is null.
|
Time |
toTime()
Convert this text to a time instance.
|
Time |
toTime(Time defaultValue)
Convert to time, returning default value if text is inconvertible.
|
static StringValue |
valueOf(AppendingStringBuffer buffer)
Converts the given input to an instance of StringValue.
|
static StringValue |
valueOf(double value)
Converts the given input to an instance of StringValue.
|
static StringValue |
valueOf(double value,
int places,
Locale locale)
Converts the given input to an instance of StringValue.
|
static StringValue |
valueOf(double value,
Locale locale)
Converts the given input to an instance of StringValue.
|
static StringValue |
valueOf(Object object)
Converts the given input to an instance of StringValue.
|
static StringValue |
valueOf(Object object,
Locale locale)
Converts the given input to an instance of StringValue.
|
static StringValue |
valueOf(String string)
Converts the given input to an instance of StringValue.
|
static StringValue |
valueOf(String string,
Locale locale)
Converts the given input to an instance of StringValue.
|
protected StringValue(String text)
text
- The text for this string valuepublic static StringValue repeat(int times, char c)
times
- Number of times to repeat characterc
- Character to repeatpublic static StringValue repeat(int times, String s)
times
- Number of times to repeat strings
- String to repeatpublic static StringValue valueOf(double value)
value
- Double precision valuepublic static StringValue valueOf(double value, int places, Locale locale)
value
- Double precision valueplaces
- Number of places after decimallocale
- Locale to be used for formattingpublic static StringValue valueOf(double value, Locale locale)
value
- Double precision valuelocale
- Locale to be used for formattingpublic static StringValue valueOf(Object object)
object
- An objectpublic static StringValue valueOf(Object object, Locale locale)
object
- An objectlocale
- Locale to be used for formattingpublic static StringValue valueOf(String string)
string
- A stringpublic static StringValue valueOf(String string, Locale locale)
string
- A stringlocale
- Locale to be used for formattingpublic static StringValue valueOf(AppendingStringBuffer buffer)
buffer
- A string bufferpublic final String afterFirst(char c)
c
- char to scan forpublic final String afterLast(char c)
c
- char to scan forpublic final String beforeFirst(char c)
c
- char to scan forpublic final String beforeLast(char c)
c
- char to scan forpublic final CharSequence replaceAll(CharSequence searchFor, CharSequence replaceWith)
searchFor
- What to search forreplaceWith
- What to replace withpublic final <T> T to(Class<T> type) throws StringValueConversionException
type
- The type to convert toStringValueConversionException
public final <T> T toOptional(Class<T> type) throws StringValueConversionException
null
if the value is empty.type
- The type to convert toStringValueConversionException
public final boolean toBoolean() throws StringValueConversionException
StringValueConversionException
public final boolean toBoolean(boolean defaultValue)
defaultValue
- the default valueStrings.isTrue(String)
public final Boolean toBooleanObject() throws StringValueConversionException
StringValueConversionException
public final char toChar() throws StringValueConversionException
StringValueConversionException
public final char toChar(char defaultValue)
defaultValue
- the default valuepublic final Character toCharacter() throws StringValueConversionException
StringValueConversionException
public final double toDouble() throws StringValueConversionException
StringValueConversionException
public final double toDouble(double defaultValue)
defaultValue
- the default valuepublic final Double toDoubleObject() throws StringValueConversionException
StringValueConversionException
public final Duration toDuration() throws StringValueConversionException
StringValueConversionException
Duration.valueOf(String, java.util.Locale)
public final Duration toDuration(Duration defaultValue)
defaultValue
- the default valueDuration.valueOf(String, java.util.Locale)
public final int toInt() throws StringValueConversionException
StringValueConversionException
public final int toInt(int defaultValue)
defaultValue
- the default valuepublic final Integer toInteger() throws StringValueConversionException
StringValueConversionException
public final long toLong() throws StringValueConversionException
StringValueConversionException
public final long toLong(long defaultValue)
defaultValue
- the default valuepublic final Long toLongObject() throws StringValueConversionException
StringValueConversionException
public final Boolean toOptionalBoolean() throws StringValueConversionException
StringValueConversionException
public final Character toOptionalCharacter() throws StringValueConversionException
StringValueConversionException
public final Double toOptionalDouble() throws StringValueConversionException
StringValueConversionException
public final Duration toOptionalDuration() throws StringValueConversionException
StringValueConversionException
public final Integer toOptionalInteger() throws StringValueConversionException
StringValueConversionException
public final Long toOptionalLong() throws StringValueConversionException
StringValueConversionException
public final String toOptionalString()
public final Time toOptionalTime() throws StringValueConversionException
StringValueConversionException
public final String toString()
public final String toString(String defaultValue)
defaultValue
- the default value to return of text is nullpublic final Time toTime() throws StringValueConversionException
StringValueConversionException
public final Time toTime(Time defaultValue)
defaultValue
- the default valuepublic final <T extends Enum<T>> T toEnum(Class<T> eClass) throws StringValueConversionException
eClass
- enum typeStringValueConversionException
public final <T extends Enum<T>> T toEnum(T defaultValue)
defaultValue
- This will be returned if there is an error converting the valuepublic final <T extends Enum<T>> T toEnum(Class<T> eClass, T defaultValue)
eClass
- enum typedefaultValue
- This will be returned if there is an error converting the valuepublic final <T extends Enum<T>> T toOptionalEnum(Class<T> eClass) throws StringValueConversionException
eClass
- enum typeStringValueConversionException
public boolean isNull()
true
if the text is null
, false
otherwise.public boolean isEmpty()
true
if the text is null
or
.trim().length()==0
, false
otherwise.Copyright © 2006–2022 Apache Software Foundation. All rights reserved.