java.lang.Object
org.apache.wicket.util.string.StringValue
- All Implemented Interfaces:
Serializable,IClusterable
- Direct Known Subclasses:
EnumeratedType
Holds an immutable String value and optionally a Locale, with methods to convert to various
types. Also provides some handy parsing methods and a variety of static factory methods.
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.
- Author:
- Jonathan Locke
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedStringValue(String text) Private constructor to force use of static factory methods.protectedStringValue(String text, Locale locale) Private constructor to force use of static factory methods. -
Method Summary
Modifier and TypeMethodDescriptionfinal StringafterFirst(char c) Gets the substring after the first occurrence given char.final StringafterLast(char c) Gets the substring after the last occurrence given char.final StringbeforeFirst(char c) Gets the substring before the first occurrence given char.final StringbeforeLast(char c) Gets the substring before the last occurrence given char.booleaninthashCode()booleanisEmpty()Returns whether the text is null or emptybooleanisNull()Returns whether the text is null.static StringValuerepeat(int times, char c) static StringValuefinal CharSequencereplaceAll(CharSequence searchFor, CharSequence replaceWith) Replaces on this text.final <T> TConverts this StringValue to a given type.final booleanConvert this text to a boolean.final booleantoBoolean(boolean defaultValue) Convert to boolean, returning default value if text is inconvertible.final BooleanConvert this text to a boolean.final chartoChar()Convert this text to a char.final chartoChar(char defaultValue) Convert to character, returning default value if text is inconvertible.final CharacterConvert this text to a Character.final doubletoDouble()Convert this text to a double.final doubletoDouble(double defaultValue) Convert to double, returning default value if text is inconvertible.final DoubleConvert this text to a Double.final DurationConvert this text to a Duration instance.final DurationtoDuration(Duration defaultValue) Convert to duration, returning default value if text is inconvertible.final <T extends Enum<T>>
TConvert this text to an enum.final <T extends Enum<T>>
TConvert this text to an enum.final <T extends Enum<T>>
TtoEnum(T defaultValue) Convert this text to an enum.final InstantConvert this text to anInstantinstance.final InstantConvert toInstant, returning default value if text is inconvertible.final inttoInt()Convert this text to an int.final inttoInt(int defaultValue) Convert to integer, returning default value if text is inconvertible.final IntegerConvert this text to an Integer.final longtoLong()Convert this text to a long.final longtoLong(long defaultValue) Convert to long integer, returning default value if text is inconvertible.final LongConvert this text to a Long.final <T> TtoOptional(Class<T> type) Converts this StringValue to a given type ornullif the value is empty.final BooleanConvert to object types, returning null if text is null or empty.final CharacterConvert to object types, returning null if text is null or empty.final DoubleConvert to object types, returning null if text is null or empty.final DurationConvert to object types, returning null if text is null or empty.final <T extends Enum<T>>
TtoOptionalEnum(Class<T> eClass) Convert to enum, returning null if text is null or empty.final InstantConvert to object types, returning null if text is null or empty.final IntegerConvert to object types, returning null if text is null or empty.final LongConvert to object types, returning null if text is null or empty.final StringConvert to object types, returning null if text is null.final StringtoString()final StringConvert to primitive types, returning default value if text is null.static StringValuevalueOf(double value) Converts the given input to an instance of StringValue.static StringValueConverts the given input to an instance of StringValue.static StringValueConverts the given input to an instance of StringValue.static StringValueConverts the given input to an instance of StringValue.static StringValueConverts the given input to an instance of StringValue.static StringValueConverts the given input to an instance of StringValue.static StringValueConverts the given input to an instance of StringValue.static StringValuevalueOf(AppendingStringBuffer buffer) Converts the given input to an instance of StringValue.
-
Constructor Details
-
StringValue
Private constructor to force use of static factory methods.- Parameters:
text- The text for this string value
-
StringValue
Private constructor to force use of static factory methods.- Parameters:
text- The text for this string valuelocale- the locale for formatting and parsing
-
-
Method Details
-
repeat
- Parameters:
times- Number of times to repeat characterc- Character to repeat- Returns:
- Repeated character string
-
repeat
- Parameters:
times- Number of times to repeat strings- String to repeat- Returns:
- Repeated character string
-
valueOf
Converts the given input to an instance of StringValue.- Parameters:
value- Double precision value- Returns:
- String value formatted with one place after decimal
-
valueOf
Converts the given input to an instance of StringValue.- Parameters:
value- Double precision valueplaces- Number of places after decimallocale- Locale to be used for formatting- Returns:
- String value formatted with the given number of places after decimal
-
valueOf
Converts the given input to an instance of StringValue.- Parameters:
value- Double precision valuelocale- Locale to be used for formatting- Returns:
- String value formatted with one place after decimal
-
valueOf
Converts the given input to an instance of StringValue.- Parameters:
object- An object- Returns:
- String value for object
-
valueOf
Converts the given input to an instance of StringValue.- Parameters:
object- An objectlocale- Locale to be used for formatting- Returns:
- String value for object
-
valueOf
Converts the given input to an instance of StringValue.- Parameters:
string- A string- Returns:
- String value for string
-
valueOf
Converts the given input to an instance of StringValue.- Parameters:
string- A stringlocale- Locale to be used for formatting- Returns:
- String value for string
-
valueOf
Converts the given input to an instance of StringValue.- Parameters:
buffer- A string buffer- Returns:
- String value
-
afterFirst
Gets the substring after the first occurrence given char.- Parameters:
c- char to scan for- Returns:
- the substring
-
afterLast
Gets the substring after the last occurrence given char.- Parameters:
c- char to scan for- Returns:
- the substring
-
beforeFirst
Gets the substring before the first occurrence given char.- Parameters:
c- char to scan for- Returns:
- the substring
-
beforeLast
Gets the substring before the last occurrence given char.- Parameters:
c- char to scan for- Returns:
- the substring
-
replaceAll
Replaces on this text.- Parameters:
searchFor- What to search forreplaceWith- What to replace with- Returns:
- This string value with searchFor replaces with replaceWith
-
to
Converts this StringValue to a given type.- Parameters:
type- The type to convert to- Returns:
- The converted value
- Throws:
StringValueConversionException
-
toOptional
Converts this StringValue to a given type ornullif the value is empty.- Parameters:
type- The type to convert to- Returns:
- The converted value
- Throws:
StringValueConversionException
-
toBoolean
Convert this text to a boolean.- Returns:
- This string value as a boolean
- Throws:
StringValueConversionException
-
toBoolean
Convert to boolean, returning default value if text is inconvertible.- Parameters:
defaultValue- the default value- Returns:
- the converted text as a boolean or the default value if text is empty or inconvertible
- See Also:
-
toBooleanObject
Convert this text to a boolean.- Returns:
- Converted text
- Throws:
StringValueConversionException
-
toChar
Convert this text to a char.- Returns:
- This string value as a character
- Throws:
StringValueConversionException
-
toChar
Convert to character, returning default value if text is inconvertible.- Parameters:
defaultValue- the default value- Returns:
- the converted text as a primitive char or the default value if text is not a single character
-
toCharacter
Convert this text to a Character.- Returns:
- Converted text
- Throws:
StringValueConversionException
-
toDouble
Convert this text to a double.- Returns:
- Converted text
- Throws:
StringValueConversionException
-
toDouble
Convert to double, returning default value if text is inconvertible.- Parameters:
defaultValue- the default value- Returns:
- the converted text as a double or the default value if text is empty or inconvertible
-
toDoubleObject
Convert this text to a Double.- Returns:
- Converted text
- Throws:
StringValueConversionException
-
toDuration
Convert this text to a Duration instance.- Returns:
- Converted text
- Throws:
StringValueConversionException- See Also:
-
toDuration
Convert to duration, returning default value if text is inconvertible.- Parameters:
defaultValue- the default value- Returns:
- the converted text as a duration or the default value if text is empty or inconvertible
- See Also:
-
toInt
Convert this text to an int.- Returns:
- Converted text
- Throws:
StringValueConversionException
-
toInt
Convert to integer, returning default value if text is inconvertible.- Parameters:
defaultValue- the default value- Returns:
- the converted text as an integer or the default value if text is not an integer
-
toInteger
Convert this text to an Integer.- Returns:
- Converted text
- Throws:
StringValueConversionException
-
toLong
Convert this text to a long.- Returns:
- Converted text
- Throws:
StringValueConversionException
-
toLong
Convert to long integer, returning default value if text is inconvertible.- Parameters:
defaultValue- the default value- Returns:
- the converted text as a long integer or the default value if text is empty or inconvertible
-
toLongObject
Convert this text to a Long.- Returns:
- Converted text
- Throws:
StringValueConversionException
-
toOptionalBoolean
Convert to object types, returning null if text is null or empty.- Returns:
- converted
- Throws:
StringValueConversionException
-
toOptionalCharacter
Convert to object types, returning null if text is null or empty.- Returns:
- converted
- Throws:
StringValueConversionException
-
toOptionalDouble
Convert to object types, returning null if text is null or empty.- Returns:
- converted
- Throws:
StringValueConversionException
-
toOptionalDuration
Convert to object types, returning null if text is null or empty.- Returns:
- converted
- Throws:
StringValueConversionException
-
toOptionalInteger
Convert to object types, returning null if text is null or empty.- Returns:
- converted
- Throws:
StringValueConversionException
-
toOptionalLong
Convert to object types, returning null if text is null or empty.- Returns:
- converted
- Throws:
StringValueConversionException
-
toOptionalString
Convert to object types, returning null if text is null.- Returns:
- converted
-
toOptionalInstant
Convert to object types, returning null if text is null or empty.- Returns:
- converted
- Throws:
StringValueConversionException
-
toString
-
toString
Convert to primitive types, returning default value if text is null.- Parameters:
defaultValue- the default value to return of text is null- Returns:
- the converted text as a primitive or the default if text is null
-
toInstant
Convert this text to anInstantinstance.- Returns:
- Converted text
- Throws:
StringValueConversionException
-
toInstant
Convert toInstant, returning default value if text is inconvertible.- Parameters:
defaultValue- the default value- Returns:
- the converted text as a
Instantor the default value if text is inconvertible.
-
toEnum
Convert this text to an enum.- Parameters:
eClass- enum type- Returns:
- The value as an enum
- Throws:
StringValueConversionException
-
toEnum
Convert this text to an enum.- Parameters:
defaultValue- This will be returned if there is an error converting the value- Returns:
- The value as an enum
-
toEnum
Convert this text to an enum.- Parameters:
eClass- enum typedefaultValue- This will be returned if there is an error converting the value- Returns:
- The value as an enum
-
toOptionalEnum
public final <T extends Enum<T>> T toOptionalEnum(Class<T> eClass) throws StringValueConversionException Convert to enum, returning null if text is null or empty.- Parameters:
eClass- enum type- Returns:
- converted
- Throws:
StringValueConversionException
-
isNull
Returns whether the text is null.- Returns:
trueif the text isnull,falseotherwise.
-
isEmpty
Returns whether the text is null or empty- Returns:
trueif the text isnullor.trim().length()==0,falseotherwise.
-
hashCode
-
equals
-