Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.wicket.util.string;
 
 
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(s):
Jonathan Locke
 
 public class StringValue implements IClusterable
 {
 	private static final long serialVersionUID = 1L;

Locale to be used for formatting and parsing.
 
 	private final Locale locale;

The underlying string.
 
 	private final String text;

Parameters:
times Number of times to repeat character
c Character to repeat
Returns:
Repeated character string
 
 	public static StringValue repeat(final int timesfinal char c)
 	{
 		final AppendingStringBuffer buffer = new AppendingStringBuffer(times);
 
 		for (int i = 0; i < timesi++)
 		{
 			buffer.append(c);
 		}
 
 		return valueOf(buffer);
 	}

Parameters:
times Number of times to repeat string
s String to repeat
Returns:
Repeated character string
 
 	public static StringValue repeat(final int timesfinal String s)
 	{
 		final AppendingStringBuffer buffer = new AppendingStringBuffer(times);
 
 		for (int i = 0; i < timesi++)
 		{
 			buffer.append(s);
 		}
 
 		return valueOf(buffer);
 	}

Converts the given input to an instance of StringValue.

Parameters:
value Double precision value
Returns:
String value formatted with one place after decimal
	public static StringValue valueOf(final double value)
	{
		return valueOf(value, Locale.getDefault());
	}

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
	public static StringValue valueOf(final double valuefinal int placesfinal Locale locale)
	{
		if (Double.isNaN(value) || Double.isInfinite(value))
		{
			return valueOf("N/A");
		}
		else
		{
			final DecimalFormat format = new DecimalFormat("#." + repeat(places'#'),
				new DecimalFormatSymbols(locale));
			return valueOf(format.format(value));
		}
	}

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
	public static StringValue valueOf(final double valuefinal Locale locale)
	{
		return valueOf(value, 1, locale);
	}

Converts the given input to an instance of StringValue.

Parameters:
object An object
Returns:
String value for object
	public static StringValue valueOf(final Object object)
	{
		return valueOf(Strings.toString(object));
	}

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
	public static StringValue valueOf(final Object objectfinal Locale locale)
	{
		return valueOf(Strings.toString(object), locale);
	}

Converts the given input to an instance of StringValue.

Parameters:
string A string
Returns:
String value for string
	public static StringValue valueOf(final String string)
	{
		return new StringValue(string);
	}

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
	public static StringValue valueOf(final String stringfinal Locale locale)
	{
		return new StringValue(stringlocale);
	}

Converts the given input to an instance of StringValue.

Parameters:
buffer A string buffer
Returns:
String value
	public static StringValue valueOf(final AppendingStringBuffer buffer)
	{
		return valueOf(buffer.toString());
	}

Private constructor to force use of static factory methods.

Parameters:
text The text for this string value
	protected StringValue(final String text)
	{
		this. = text;
		 = Locale.getDefault();
	}

Private constructor to force use of static factory methods.

Parameters:
text The text for this string value
locale the locale for formatting and parsing
	protected StringValue(final String textfinal Locale locale)
	{
		this. = text;
		this. = locale;
	}

Gets the substring after the first occurrence given char.

Parameters:
c char to scan for
Returns:
the substring
	public final String afterFirst(final char c)
	{
		return Strings.afterFirst(c);
	}

Gets the substring after the last occurrence given char.

Parameters:
c char to scan for
Returns:
the substring
	public final String afterLast(final char c)
	{
		return Strings.afterLast(c);
	}

Gets the substring before the first occurrence given char.

Parameters:
c char to scan for
Returns:
the substring
	public final String beforeFirst(final char c)
	{
		return Strings.beforeFirst(c);
	}

Gets the substring before the last occurrence given char.

Parameters:
c char to scan for
Returns:
the substring
	public final String beforeLast(final char c)
	{
		return Strings.afterLast(c);
	}

Replaces on this text.

Parameters:
searchFor What to search for
replaceWith What to replace with
Returns:
This string value with searchFor replaces with replaceWith
	public final CharSequence replaceAll(final CharSequence searchFor,
		final CharSequence replaceWith)
	{
		return Strings.replaceAll(searchForreplaceWith);
	}

Converts this StringValue to a given type.

Parameters:
type The type to convert to
Returns:
The converted value
Throws:
StringValueConversionException
	public final Object to(final Class typethrows StringValueConversionException
	{
		if (type == String.class)
		{
			return toString();
		}
		if (type == . || type == Integer.class)
		{
			return toInteger();
		}
		if (type == . || type == Long.class)
		{
			return toLongObject();
		}
		if (type == . || type == Boolean.class)
		{
			return toBooleanObject();
		}
		if (type == . || type == Double.class)
		{
			return toDoubleObject();
		}
		if (type == . || type == Character.class)
		{
			return toCharacter();
		}
		if (type == Time.class)
		{
			return toTime();
		}
		if (type == Duration.class)
		{
			return toDuration();
		}
		throw new StringValueConversionException("Cannot convert '" + toString() + "'to type " +
			type);
	}

Convert this text to a boolean.

Returns:
This string value as a boolean
Throws:
StringValueConversionException
	public final boolean toBoolean() throws StringValueConversionException
	{
		return Strings.isTrue();
	}

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
	public final boolean toBoolean(final boolean defaultValue)
	{
		return ( == null) ? defaultValue : toBoolean();
	}

Convert this text to a boolean.

Returns:
Converted text
Throws:
StringValueConversionException
	{
		return Strings.toBoolean();
	}

Convert this text to a char.

Returns:
This string value as a character
Throws:
StringValueConversionException
	public final char toChar() throws StringValueConversionException
	{
		return Strings.toChar();
	}

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
	public final char toChar(final char defaultValuethrows StringValueConversionException
	{
		return ( == null) ? defaultValue : toChar();
	}

Convert this text to a Character.

Returns:
Converted text
Throws:
StringValueConversionException
	{
		return new Character(toChar());
	}

Convert this text to a double.

Returns:
Converted text
Throws:
StringValueConversionException
	public final double toDouble() throws StringValueConversionException
	{
		try
		{
			return NumberFormat.getNumberInstance().parse().doubleValue();
		}
		catch (ParseException e)
		{
			throw new StringValueConversionException("Unable to convert '" +  +
				"' to a double value"e);
		}
	}

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
	public final double toDouble(final double defaultValuethrows StringValueConversionException
	{
		return ( == null) ? defaultValue : toDouble();
	}

Convert this text to a Double.

Returns:
Converted text
Throws:
StringValueConversionException
	{
		return new Double(toDouble());
	}

Convert this text to a Duration instance.

Returns:
Converted text
Throws:
StringValueConversionException
	{
		return Duration.valueOf();
	}

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
	public final Duration toDuration(final Duration defaultValue)
	{
		return ( == null) ? defaultValue : toDuration();
	}

Convert this text to an int.

Returns:
Converted text
Throws:
StringValueConversionException
	public final int toInt() throws StringValueConversionException
	{
		try
		{
			return Integer.parseInt();
		}
		{
			throw new StringValueConversionException("Unable to convert '" +  +
				"' to an int value"e);
		}
	}

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
	public final int toInt(final int defaultValuethrows StringValueConversionException
	{
		return ( == null) ? defaultValue : toInt();
	}

Convert this text to an Integer.

Returns:
Converted text
Throws:
StringValueConversionException
	{
		try
		{
			return new Integer();
		}
		{
			throw new StringValueConversionException("Unable to convert '" +  +
				"' to an Integer value"e);
		}
	}

Convert this text to a long.

Returns:
Converted text
Throws:
StringValueConversionException
	public final long toLong() throws StringValueConversionException
	{
		try
		{
			return Long.parseLong();
		}
		{
			throw new StringValueConversionException("Unable to convert '" +  +
				"' to a long value"e);
		}
	}

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
	public final long toLong(final long defaultValuethrows StringValueConversionException
	{
		return ( == null) ? defaultValue : toLong();
	}

Convert this text to a Long.

Returns:
Converted text
Throws:
StringValueConversionException
	{
		try
		{
			return new Long();
		}
		{
			throw new StringValueConversionException("Unable to convert '" +  +
				"' to a Long value"e);
		}
	}

Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException
	{
		return ( == null) ? null : toBooleanObject();
	}

Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException
	{
		return ( == null) ? null : toCharacter();
	}

Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException
	{
		return ( == null) ? null : toDoubleObject();
	}

Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException
	{
		return ( == null) ? null : toDuration();
	}

Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException
	{
		return ( == null) ? null : toInteger();
	}

Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException
	{
		return ( == null) ? null : toLongObject();
	}

Convert to object types, returning null if text is null.

Returns:
converted
	public final String toOptionalString()
	{
		return ;
	}

Convert to object types, returning null if text is null.

Returns:
converted
Throws:
StringValueConversionException
	{
		return ( == null) ? null : toTime();
	}

Returns:
The string value
	public final String toString()
	{
		return ;
	}

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
	public final String toString(final String defaultValue)
	{
		return ( == null) ? defaultValue : ;
	}

Convert this text to a time instance.

Returns:
Converted text
Throws:
StringValueConversionException
	public final Time toTime() throws StringValueConversionException
	{
		try
		{
			return Time.valueOf();
		}
		catch (ParseException e)
		{
			throw new StringValueConversionException("Unable to convert '" +  +
				"' to a Time value"e);
		}
	}

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
	public final Time toTime(final Time defaultValuethrows StringValueConversionException
	{
		return ( == null) ? defaultValue : toTime();
	}

Returns whether the text is null.

Returns:
true if the text is null, false otherwise.
	public boolean isNull()
	{
		return  == null;
	}

Returns whether the text is null or empty

Returns:
true if the text is null or .trim().length()==0, false otherwise.
	public boolean isEmpty()
	{
		return Strings.isEmpty();
	}
New to GrepCode? Check out our FAQ X