com.mockrunner.util.common.StringUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockrunner-core Show documentation
Show all versions of mockrunner-core Show documentation
Core classes common to all Mockrunner modules
The newest version!
package com.mockrunner.util.common;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import com.mockrunner.base.NestedApplicationException;
/**
* Simple util class for String
related methods.
*/
public class StringUtil
{
/**
* Returns if the specified string is null
or
* the empty string.
* @param string the string
* @return true
if the specified string is null
or
* the empty string, false
otherwise
*/
public static boolean isEmptyOrNull(String string)
{
return (null == string) || (0 >= string.length());
}
/**
* Returns null
, if the specified string is null
or
* the empty string. Returns the specified string otherwise.
* @param string the string
* @return null
if the specified string is null
or
* the empty string, the specified string otherwise
*/
public static String emptyStringToNull(String string)
{
return isEmptyOrNull(string) ? null : string;
}
/**
* Replaces all occurrences of match
in
* source
with replacement
.
* @param source the source string
* @param match the string that is searched
* @param replacement the replacement string
* @return the modified string
* @throws IllegalArgumentException if any argument is null
or
* if match
is the empty string
*/
public static String replaceAll(String source, String match, String replacement)
{
if(null == source || null == match || null == replacement)
{
throw new IllegalArgumentException("null strings not allowed");
}
if(match.length() <= 0)
{
throw new IllegalArgumentException("match must not be empty");
}
StringBuilder buffer = new StringBuilder(source.length() + 10);
int index = 0;
int newIndex = 0;
while((newIndex = source.indexOf(match, index)) >= 0)
{
buffer.append(source.substring(index, newIndex));
buffer.append(replacement);
index = newIndex + match.length();
}
buffer.append(source.substring(index));
return buffer.toString();
}
/**
* 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.
* @param string1 the first string
* @param string2 the second string
* @return the last index where the strings are equal
*/
public static int compare(String string1, String string2)
{
int endIndex = Math.min(string1.length(), string2.length());
for(int ii = 0; ii < endIndex; ii++)
{
if(string1.charAt(ii) != string2.charAt(ii)) return ii - 1;
}
return endIndex - 1;
}
/**
* Converts the character at the specified index to
* lowercase and returns the resulting string.
* @param string the string to convert
* @param index the index where the character is set to lowercase
* @return the converted string
* @throws IndexOutOfBoundsException if the index is out of
* range
*/
public static String lowerCase(String string, int index)
{
return lowerCase(string, index, -1);
}
/**
* 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.
* @param string the string to convert
* @param startIndex the index to start, inclusive
* @param endIndex the index to end, exclusive
* @return the converted string
* @throws IndexOutOfBoundsException if the index is out of
* range
*/
public static String lowerCase(String string, int startIndex, int endIndex)
{
StringBuilder buffer = new StringBuilder(string);
if(endIndex <= startIndex) endIndex = startIndex + 1;
for(int ii = startIndex; ii < endIndex; ii++)
{
char character = buffer.charAt(ii);
buffer.setCharAt(ii, Character.toLowerCase(character));
}
return buffer.toString();
}
/**
* 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"
* @param fieldName the field name
* @param field the field value
* @return a suitable string for toString()
implementations
*/
public static String fieldToString(String fieldName, Object field)
{
StringBuffer buffer = new StringBuffer();
if(null == field)
{
buffer.append(fieldName).append(": ").append("null");
}
else if(field.getClass().isArray())
{
arrayToString(fieldName, field, buffer);
}
else if(field instanceof Collection)
{
collectionToString(fieldName, field, buffer);
}
else if(field instanceof Map)
{
mapToString(fieldName, field, buffer);
}
else
{
buffer.append(fieldName).append(": ").append(field.toString());
}
return buffer.toString();
}
private static void arrayToString(String fieldName, Object field, StringBuffer buffer)
{
int length = Array.getLength(field);
if(0 >= length)
{
buffer.append(fieldName).append(": ").append("empty");
}
else
{
for(int ii = 0; ii < length; ii++)
{
buffer.append(fieldToString(fieldName + " " + ii, Array.get(field, ii)));
if(ii < length - 1)
{
buffer.append("\n");
}
}
}
}
private static void collectionToString(String fieldName, Object field, StringBuffer buffer)
{
@SuppressWarnings("unchecked")
List