net.sf.mmm.util.lang.base
Class StringUtilImpl

java.lang.Object
  extended by net.sf.mmm.util.lang.base.StringUtilImpl
All Implemented Interfaces:
StringUtil

@Singleton
@Named
public class StringUtilImpl
extends Object
implements StringUtil

This is the implementation of the StringUtil interface.

Since:
1.0.0
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)
See Also:
getInstance()

Field Summary
private static StringUtil instance
           
private static char[] SEPARATORS
           
 
Fields inherited from interface net.sf.mmm.util.lang.api.StringUtil
EMPTY_CHAR_ARRAY, EMPTY_STRING_ARRAY, FALSE, LINE_SEPARATOR, LINE_SEPARATOR_CR, LINE_SEPARATOR_CRLF, LINE_SEPARATOR_LF, LINE_SEPARATOR_LFCR, TRUE
 
Constructor Summary
StringUtilImpl()
          The constructor.
 
Method Summary
 String fromCamlCase(String string, char separator)
          This method converts the given string from caml-case syntax to lower-case using the given separator as word-boundary.
static StringUtil getInstance()
          This method gets the singleton instance of this StringUtilImpl.
 boolean isEmpty(String string)
          This method determines if the given string contains no information.
 boolean isEmpty(String string, boolean trim)
          This method determines if the given string is empty.
 boolean isSubstring(char[] string, String substring, int offset)
          This method checks if the given string contains the given substring at the given offset.
 boolean isSubstring(String string, String substring, int offset)
          This method checks if the given string contains the given substring at the given offset.
 String padNumber(long number, int digits)
          This method formats a positive number to a string with at least the given number of digits padding it with leading zeros.
 String padNumber(long number, int digits, int radix)
          This method formats a positive number to a string using the given radix with at least the given number of digits padding it with leading zeros.
 Boolean parseBoolean(String booleanValue)
          This method parses a boolean value given as string.
 void replace(char[] string, char oldChar, char newChar)
          This method replaces all occurrences of oldChar in the char-array given by string with newChar.
 String replace(String string, String match, String replace)
          This method replaces all occurrences of the string match with the string replace in the given string.
 String replaceSuffixWithCase(String string, int suffixLength, String newSuffixLowerCase)
          This method delegates to StringUtil.replaceSuffixWithCase(String, int, String, Locale) using Locale.ENGLISH.
 String replaceSuffixWithCase(String string, int suffixLength, String newSuffixLowerCase, Locale locale)
          This method replaces the last suffixLength number of characters from string with the lower-case string newSuffixLowerCase with respect to the original case of the given string.
 String toCamlCase(String string)
          This method converts the given string to caml-case syntax using the default separators ' ', '-', '_' and '.'
 String toCamlCase(String string, char... separators)
          This method converts the given string to caml-case syntax.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

private static StringUtil instance
See Also:
getInstance()

SEPARATORS

private static final char[] SEPARATORS
See Also:
toCamlCase(String)
Constructor Detail

StringUtilImpl

public StringUtilImpl()
The constructor.

Method Detail

getInstance

public static StringUtil getInstance()
This method gets the singleton instance of this StringUtilImpl.
This design is the best compromise between easy access (via this indirection you have direct, static access to all offered functionality) and IoC-style design which allows extension and customization.
For IoC usage, simply ignore all static getInstance() methods and construct new instances via the container-framework of your choice (like plexus, pico, springframework, etc.). To wire up the dependent components everything is properly annotated using common-annotations (JSR-250). If your container does NOT support this, you should consider using a better one.

Returns:
the singleton instance.

parseBoolean

public Boolean parseBoolean(String booleanValue)
This method parses a boolean value given as string.

Specified by:
parseBoolean in interface StringUtil
Parameters:
booleanValue - is the boolean value as string.
Returns:
true if the given string equals to true, false if it equals to false and null in any other case.
See Also:
Boolean.valueOf(String)

replace

public void replace(char[] string,
                    char oldChar,
                    char newChar)
This method replaces all occurrences of oldChar in the char-array given by string with newChar.

Specified by:
replace in interface StringUtil
Parameters:
string - is the char-array where the replacement should take place.
oldChar - is the character to be replaced.
newChar - is the replacement for oldChar.
See Also:
String.replace(char, char)

replace

public String replace(String string,
                      String match,
                      String replace)
This method replaces all occurrences of the string match with the string replace in the given string.

Specified by:
replace in interface StringUtil
Parameters:
string - is the string where to replace.
match - is the string that is searched and replaced.
replace - is the string match is substituted with.
Returns:
the given string with all occurrences of match replaced by replace.

isSubstring

public boolean isSubstring(String string,
                           String substring,
                           int offset)
This method checks if the given string contains the given substring at the given offset.
This is an efficient implementation of
string.indexOf(substring, offset) == offset
or
string.substring(offset).beginsWith(substring)

Specified by:
isSubstring in interface StringUtil
Parameters:
string - is the string potentially containing substring.
substring - is the substring that should be contained in string at the given offset.
offset - is the offset in string where to check for substring.
Returns:
true if the given string contains the given substring at the given offset and false otherwise.

isSubstring

public boolean isSubstring(char[] string,
                           String substring,
                           int offset)
This method checks if the given string contains the given substring at the given offset.
This is an efficient implementation of
string.indexOf(substring, offset) == offset
or
string.substring(offset).beginsWith(substring)

Specified by:
isSubstring in interface StringUtil
Parameters:
string - is the char[] representing the string potentially containing substring.
substring - is the substring that should be contained in string at the given offset.
offset - is the offset in string where to check for substring.
Returns:
true if the given string contains the given substring at the given offset and false otherwise.

replaceSuffixWithCase

public String replaceSuffixWithCase(String string,
                                    int suffixLength,
                                    String newSuffixLowerCase)
This method delegates to StringUtil.replaceSuffixWithCase(String, int, String, Locale) using Locale.ENGLISH.

Specified by:
replaceSuffixWithCase in interface StringUtil
Parameters:
string - is the string to replace.
suffixLength - is the length of the suffix from string to replace.
newSuffixLowerCase - is the new suffix for the given string in lower-case.
Returns:
the given string with the last suffixLength characters cut off and replaced by newSuffixLowerCase with respect to the original case of string.
See Also:
StringUtil.replaceSuffixWithCase(String, int, String, Locale)

replaceSuffixWithCase

public String replaceSuffixWithCase(String string,
                                    int suffixLength,
                                    String newSuffixLowerCase,
                                    Locale locale)
This method replaces the last suffixLength number of characters from string with the lower-case string newSuffixLowerCase with respect to the original case of the given string.
Here are some examples for Locale.ENGLISH:
string suffixLength newSuffixLowerCase StringUtil.replaceSuffixWithCase(String, int, String)
foobar 3 foo foofoo
FOOBAR 3 foo FOOFOO
FooBar 3 foo FooFoo
FooBar 2 foo FooBfoo

Specified by:
replaceSuffixWithCase in interface StringUtil
Parameters:
string - is the string to replace.
suffixLength - is the length of the suffix from string to replace.
newSuffixLowerCase - is the new suffix for the given string in lower-case.
locale - is the locale used for case transformation.
Returns:
the given string with the last suffixLength characters cut off and replaced by newSuffixLowerCase with respect to the original case of string.

isEmpty

public boolean isEmpty(String string)
This method determines if the given string contains no information.

Specified by:
isEmpty in interface StringUtil
Parameters:
string - is the string to check.
Returns:
true if the given string is null or has a trimmed length of zero, false otherwise.
See Also:
StringUtil.isEmpty(String, boolean)

isEmpty

public boolean isEmpty(String string,
                       boolean trim)
This method determines if the given string is empty.

Specified by:
isEmpty in interface StringUtil
Parameters:
string - is the string to check.
trim - if whitespaces should be ignored and a string with a trimmed length of zero is considered as empty.
Returns:
true if the given string is null or has a (trimmed) length of zero, false otherwise.

padNumber

public String padNumber(long number,
                        int digits)
This method formats a positive number to a string with at least the given number of digits padding it with leading zeros.
Examples:

Specified by:
padNumber in interface StringUtil
Parameters:
number - is the positive number to format.
digits - is the (minimum) number of digits required.
Returns:
the number as string with the length of (at least) digits. If the number is less, leading zeros are appended.

padNumber

public String padNumber(long number,
                        int digits,
                        int radix)
This method formats a positive number to a string using the given radix with at least the given number of digits padding it with leading zeros.
Examples: To pad a number to a constant length you can use the following value for digits:
Radix byte short int long
2 8 16 32 64
8 3 6 9 12
10 3 5 9 19
16 2 4 8 16

Specified by:
padNumber in interface StringUtil
Parameters:
number - is the positive number to format.
digits - is the (minimum) number of digits required.
radix - is the radix to use.
Returns:
the number as string with the length of (at least) digits. If the number is less, leading zeros are appended.

toCamlCase

public String toCamlCase(String string)
This method converts the given string to caml-case syntax using the default separators ' ', '-', '_' and '.'.

Specified by:
toCamlCase in interface StringUtil
Parameters:
string - is the string to convert.
Returns:
the given string in caml-case syntax.
See Also:
StringUtil.toCamlCase(String, char[])

toCamlCase

public String toCamlCase(String string,
                         char... separators)
This method converts the given string to caml-case syntax.
In caml-case syntax words are written without a separator but each new word starts with a capitalized letter. This method removes all characters from the given string that are in the list given by separators and capitalizes the first character of the following word.
Examples for separators ' ', '-', '_', '.':
string toCamlCase(string)
foo bar fooBar
aAa-bBB-CcC aAaBBBCcC
X--m_._l.. XML

Specified by:
toCamlCase in interface StringUtil
Parameters:
string - is the string to convert.
separators - is the list of characters that are treated as word-separators.
Returns:
the given string in caml-case syntax.

fromCamlCase

public String fromCamlCase(String string,
                           char separator)
This method converts the given string from caml-case syntax to lower-case using the given separator as word-boundary.
Example:
string separator fromCamlCase(string, separator)
FooBar - foo-bar
someWordMix . some.word.mix
AbbreviationsLikeXMLshouldNotBeCapitalized _ abbreviations_like_xmlshould_not_be_capitalized
FOO_BAR * foo_bar

Specified by:
fromCamlCase in interface StringUtil
Parameters:
string - is the string to convert.
separator - is the character to insert at word-boundaries indicated by a switch from lower- to upper-case.
Returns:
the given string in lower-case with the given separator inserted at word-boundaries.
See Also:
StringUtil.toCamlCase(String, char...)


Copyright © 2001-2010 mmm-Team. All Rights Reserved.