org.apache.wicket.util.string
Class StringValue

java.lang.Object
  extended by org.apache.wicket.util.string.StringValue
All Implemented Interfaces:
Serializable, IClusterable
Direct Known Subclasses:
EnumeratedType

public class StringValue
extends Object
implements IClusterable

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:
Serialized Form

Constructor Summary
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.
 
Method Summary
 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 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.
 Object to(Class<?> type)
          Converts this StringValue to a given type.
 boolean toBoolean()
          Convert this text to a boolean.
 boolean toBoolean(boolean defaultValue)
          Convert to primitive types, returning default value if text is null.
 Boolean toBooleanObject()
          Convert this text to a boolean.
 char toChar()
          Convert this text to a char.
 char toChar(char defaultValue)
          Convert to primitive types, returning default value if text is null.
 Character toCharacter()
          Convert this text to a Character.
 double toDouble()
          Convert this text to a double.
 double toDouble(double defaultValue)
          Convert to primitive types, returning default value if text is null.
 Double toDoubleObject()
          Convert this text to a Double.
 Duration toDuration()
          Convert this text to a Duration instance.
 Duration toDuration(Duration defaultValue)
          Convert to primitive types, returning default value if text is null.
 int toInt()
          Convert this text to an int.
 int toInt(int defaultValue)
          Convert to primitive types, returning default value if text is null.
 Integer toInteger()
          Convert this text to an Integer.
 long toLong()
          Convert this text to a long.
 long toLong(long defaultValue)
          Convert to primitive types, returning default value if text is null.
 Long toLongObject()
          Convert this text to a Long.
 Boolean toOptionalBoolean()
          Convert to object types, returning null if text is null.
 Character toOptionalCharacter()
          Convert to object types, returning null if text is null.
 Double toOptionalDouble()
          Convert to object types, returning null if text is null.
 Duration toOptionalDuration()
          Convert to object types, returning null if text is null.
 Integer toOptionalInteger()
          Convert to object types, returning null if text is null.
 Long toOptionalLong()
          Convert to object types, returning null if text is null.
 String toOptionalString()
          Convert to object types, returning null if text is null.
 Time toOptionalTime()
          Convert to object types, returning null if text is null.
 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 primitive types, returning default value if text is null.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringValue

protected StringValue(String text)
Private constructor to force use of static factory methods.

Parameters:
text - The text for this string value

StringValue

protected StringValue(String text,
                      Locale locale)
Private constructor to force use of static factory methods.

Parameters:
text - The text for this string value
locale - the locale for formatting and parsing
Method Detail

repeat

public static StringValue repeat(int times,
                                 char c)
Parameters:
times - Number of times to repeat character
c - Character to repeat
Returns:
Repeated character string

repeat

public static StringValue repeat(int times,
                                 String s)
Parameters:
times - Number of times to repeat string
s - String to repeat
Returns:
Repeated character string

valueOf

public static StringValue valueOf(double value)
Converts the given input to an instance of StringValue.

Parameters:
value - Double precision value
Returns:
String value formatted with one place after decimal

valueOf

public static StringValue valueOf(double value,
                                  int places,
                                  Locale locale)
Converts the given input to an instance of StringValue.

Parameters:
value - Double precision value
places - Number of places after decimal
locale - Locale to be used for formatting
Returns:
String value formatted with the given number of places after decimal

valueOf

public static StringValue valueOf(double value,
                                  Locale locale)
Converts the given input to an instance of StringValue.

Parameters:
value - Double precision value
locale - Locale to be used for formatting
Returns:
String value formatted with one place after decimal

valueOf

public static StringValue valueOf(Object object)
Converts the given input to an instance of StringValue.

Parameters:
object - An object
Returns:
String value for object

valueOf

public static StringValue valueOf(Object object,
                                  Locale locale)
Converts the given input to an instance of StringValue.

Parameters:
object - An object
locale - Locale to be used for formatting
Returns:
String value for object

valueOf

public static StringValue valueOf(String string)
Converts the given input to an instance of StringValue.

Parameters:
string - A string
Returns:
String value for string

valueOf

public static StringValue valueOf(String string,
                                  Locale locale)
Converts the given input to an instance of StringValue.

Parameters:
string - A string
locale - Locale to be used for formatting
Returns:
String value for string

valueOf

public static StringValue valueOf(AppendingStringBuffer buffer)
Converts the given input to an instance of StringValue.

Parameters:
buffer - A string buffer
Returns:
String value

afterFirst

public final String afterFirst(char c)
Gets the substring after the first occurrence given char.

Parameters:
c - char to scan for
Returns:
the substring

afterLast

public final String afterLast(char c)
Gets the substring after the last occurrence given char.

Parameters:
c - char to scan for
Returns:
the substring

beforeFirst

public final String beforeFirst(char c)
Gets the substring before the first occurrence given char.

Parameters:
c - char to scan for
Returns:
the substring

beforeLast

public final String beforeLast(char c)
Gets the substring before the last occurrence given char.

Parameters:
c - char to scan for
Returns:
the substring

replaceAll

public final CharSequence replaceAll(CharSequence searchFor,
                                     CharSequence replaceWith)
Replaces on this text.

Parameters:
searchFor - What to search for
replaceWith - What to replace with
Returns:
This string value with searchFor replaces with replaceWith

to

public final Object to(Class<?> type)
                throws StringValueConversionException
Converts this StringValue to a given type.

Parameters:
type - The type to convert to
Returns:
The converted value
Throws:
StringValueConversionException

toBoolean

public final boolean toBoolean()
                        throws StringValueConversionException
Convert this text to a boolean.

Returns:
This string value as a boolean
Throws:
StringValueConversionException

toBoolean

public final boolean toBoolean(boolean defaultValue)
                        throws StringValueConversionException
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
Throws:
StringValueConversionException

toBooleanObject

public final Boolean toBooleanObject()
                              throws StringValueConversionException
Convert this text to a boolean.

Returns:
Converted text
Throws:
StringValueConversionException

toChar

public final char toChar()
                  throws StringValueConversionException
Convert this text to a char.

Returns:
This string value as a character
Throws:
StringValueConversionException

toChar

public final char toChar(char defaultValue)
                  throws StringValueConversionException
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
Throws:
StringValueConversionException

toCharacter

public final Character toCharacter()
                            throws StringValueConversionException
Convert this text to a Character.

Returns:
Converted text
Throws:
StringValueConversionException

toDouble

public final double toDouble()
                      throws StringValueConversionException
Convert this text to a double.

Returns:
Converted text
Throws:
StringValueConversionException

toDouble

public final double toDouble(double defaultValue)
                      throws StringValueConversionException
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
Throws:
StringValueConversionException

toDoubleObject

public final Double toDoubleObject()
                            throws StringValueConversionException
Convert this text to a Double.

Returns:
Converted text
Throws:
StringValueConversionException

toDuration

public final Duration toDuration()
                          throws StringValueConversionException
Convert this text to a Duration instance.

Returns:
Converted text
Throws:
StringValueConversionException

toDuration

public final Duration toDuration(Duration defaultValue)
                          throws StringValueConversionException
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
Throws:
StringValueConversionException

toInt

public final int toInt()
                throws StringValueConversionException
Convert this text to an int.

Returns:
Converted text
Throws:
StringValueConversionException

toInt

public final int toInt(int defaultValue)
                throws StringValueConversionException
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
Throws:
StringValueConversionException

toInteger

public final Integer toInteger()
                        throws StringValueConversionException
Convert this text to an Integer.

Returns:
Converted text
Throws:
StringValueConversionException

toLong

public final long toLong()
                  throws StringValueConversionException
Convert this text to a long.

Returns:
Converted text
Throws:
StringValueConversionException

toLong

public final long toLong(long defaultValue)
                  throws StringValueConversionException
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
Throws:
StringValueConversionException

toLongObject

public final Long toLongObject()
                        throws StringValueConversionException
Convert this text to a Long.

Returns:
Converted text
Throws:
StringValueConversionException

toOptionalBoolean

public final Boolean toOptionalBoolean()
                                throws StringValueConversionException
Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException

toOptionalCharacter

public final Character toOptionalCharacter()
                                    throws StringValueConversionException
Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException

toOptionalDouble

public final Double toOptionalDouble()
                              throws StringValueConversionException
Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException

toOptionalDuration

public final Duration toOptionalDuration()
                                  throws StringValueConversionException
Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException

toOptionalInteger

public final Integer toOptionalInteger()
                                throws StringValueConversionException
Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException

toOptionalLong

public final Long toOptionalLong()
                          throws StringValueConversionException
Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException

toOptionalString

public final String toOptionalString()
Convert to object types, returning null if text is null.

Returns:
converted

toOptionalTime

public final Time toOptionalTime()
                          throws StringValueConversionException
Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException

toString

public final String toString()
Overrides:
toString in class Object
Returns:
The string value

toString

public final String toString(String defaultValue)
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

toTime

public final Time toTime()
                  throws StringValueConversionException
Convert this text to a time instance.

Returns:
Converted text
Throws:
StringValueConversionException

toTime

public final Time toTime(Time defaultValue)
                  throws StringValueConversionException
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
Throws:
StringValueConversionException

isNull

public boolean isNull()
Returns whether the text is null.

Returns:
true if the text is null, false otherwise.

isEmpty

public boolean isEmpty()
Returns whether the text is null or empty

Returns:
true if the text is null or .trim().length()==0, false otherwise.


Copyright © 2004-2011 Apache Software Foundation. All Rights Reserved.