com.mockrunner.util.common
Class StringUtil

java.lang.Object
  extended by com.mockrunner.util.common.StringUtil

public class StringUtil
extends java.lang.Object

Simple util class for String related methods.


Constructor Summary
StringUtil()
           
 
Method Summary
static void appendObjectsAsString(java.lang.StringBuffer buffer, java.util.List data)
          Appends the entries in the specified List as strings with a terminating "\n" after each row.
static void appendTabs(java.lang.StringBuffer buffer, int number)
          Appends number tabs (\t) to the buffer.
static int compare(java.lang.String string1, java.lang.String string2)
          Compares two strings and returns the last index where the two string are equal.
static int countMatches(java.lang.String string, java.lang.String other)
          Returns how many times string contains other.
static java.lang.String emptyStringToNull(java.lang.String string)
          Returns null, if the specified string is null or the empty string.
static java.lang.String fieldToString(java.lang.String fieldName, java.lang.Object field)
          Helper method for toString() implementations.
static boolean isEmptyOrNull(java.lang.String string)
          Returns if the specified string is null or the empty string.
static java.lang.String lowerCase(java.lang.String string, int index)
          Converts the character at the specified index to lowercase and returns the resulting string.
static java.lang.String lowerCase(java.lang.String string, int startIndex, int endIndex)
          Converts the character in the specified index range to lowercase and returns the resulting string.
static boolean matchesContains(java.lang.String source, java.lang.String target, boolean caseSensitive)
          Returns if source contains target, ignoring case, if caseSensitive is false.
static boolean matchesExact(java.lang.String source, java.lang.String target, boolean caseSensitive)
          Returns if the specified strings are equal, ignoring case, if caseSensitive is false.
static boolean matchesPerl5(java.lang.String source, java.lang.String target, boolean caseSensitive)
          Returns if the regular expression target matches source, ignoring case, if caseSensitive is false.
static java.lang.String replaceAll(java.lang.String source, java.lang.String match, java.lang.String replacement)
          Replaces all occurrences of match in source with replacement.
static java.lang.String[] split(java.lang.String string, java.lang.String delim, boolean doTrim)
          Splits a string into tokens.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtil

public StringUtil()
Method Detail

isEmptyOrNull

public static boolean isEmptyOrNull(java.lang.String string)
Returns if the specified string is null or the empty string.

Parameters:
string - the string
Returns:
true if the specified string is null or the empty string, false otherwise

emptyStringToNull

public static java.lang.String emptyStringToNull(java.lang.String string)
Returns null, if the specified string is null or the empty string. Returns the specified string otherwise.

Parameters:
string - the string
Returns:
null if the specified string is null or the empty string, the specified string otherwise

replaceAll

public static java.lang.String replaceAll(java.lang.String source,
                                          java.lang.String match,
                                          java.lang.String replacement)
Replaces all occurrences of match in source with replacement.

Parameters:
source - the source string
match - the string that is searched
replacement - the replacement string
Returns:
the modified string
Throws:
java.lang.IllegalArgumentException - if any argument is null or if match is the empty string

compare

public static int compare(java.lang.String string1,
                          java.lang.String string2)
Compares two strings and returns the last index where the two string are equal. If the first characters of the two string do not match or if at least one of the two strings is empty, -1 is returned.

Parameters:
string1 - the first string
string2 - the second string
Returns:
the last index where the strings are equal

lowerCase

public static java.lang.String lowerCase(java.lang.String string,
                                         int index)
Converts the character at the specified index to lowercase and returns the resulting string.

Parameters:
string - the string to convert
index - the index where the character is set to lowercase
Returns:
the converted string
Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range

lowerCase

public static java.lang.String lowerCase(java.lang.String string,
                                         int startIndex,
                                         int endIndex)
Converts the character in the specified index range to lowercase and returns the resulting string. If the provided endIndex is smaller or equal to startIndex, the endIndex is set to startIndex + 1.

Parameters:
string - the string to convert
startIndex - the index to start, inclusive
endIndex - the index to end, exclusive
Returns:
the converted string
Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range

fieldToString

public static java.lang.String fieldToString(java.lang.String fieldName,
                                             java.lang.Object field)
Helper method for toString() implementations. Returns a string "field name: value". Handles null values, collections and arrays. If the field is a collection or an array, the returned string will be:
"field name 0: value0\nfield name 1: value1"

Parameters:
fieldName - the field name
field - the field value
Returns:
a suitable string for toString() implementations

appendObjectsAsString

public static void appendObjectsAsString(java.lang.StringBuffer buffer,
                                         java.util.List data)
Appends the entries in the specified List as strings with a terminating "\n" after each row.

Parameters:
buffer - the buffer
data - the List with the data

appendTabs

public static void appendTabs(java.lang.StringBuffer buffer,
                              int number)
Appends number tabs (\t) to the buffer.

Parameters:
buffer - the buffer
number - the number of tabs to append

split

public static java.lang.String[] split(java.lang.String string,
                                       java.lang.String delim,
                                       boolean doTrim)
Splits a string into tokens. Similar to StringTokenizer except that empty tokens are recognized and added as null. With a delimiter of ";" the string "a;;b;c;;" will split into ["a"] [null] ["b"] ["c"] [null].

Parameters:
string - the String
delim - the delimiter
doTrim - should each token be trimmed
Returns:
the array of tokens

countMatches

public static int countMatches(java.lang.String string,
                               java.lang.String other)
Returns how many times string contains other.

Parameters:
string - the string to search
other - the string that is searched
Returns:
the number of occurences

matchesExact

public static boolean matchesExact(java.lang.String source,
                                   java.lang.String target,
                                   boolean caseSensitive)
Returns if the specified strings are equal, ignoring case, if caseSensitive is false.

Parameters:
source - the source String
target - the target String
caseSensitive - is the comparison case sensitive
Returns:
true if the strings matches false otherwise

matchesContains

public static boolean matchesContains(java.lang.String source,
                                      java.lang.String target,
                                      boolean caseSensitive)
Returns if source contains target, ignoring case, if caseSensitive is false.

Parameters:
source - the source String
target - the target String
caseSensitive - is the comparison case sensitive
Returns:
true if the strings matches false otherwise

matchesPerl5

public static boolean matchesPerl5(java.lang.String source,
                                   java.lang.String target,
                                   boolean caseSensitive)
Returns if the regular expression target matches source, ignoring case, if caseSensitive is false.

Parameters:
source - the source String
target - the target String
caseSensitive - is the comparison case sensitive
Returns:
true if the strings matches false otherwise