Add new method center(String, int, char) to complete API

Rework padding internals
Example javadoc difference()/differenceAt()
Remove dependency on NumberUtils


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137471 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-20 10:29:22 +00:00
parent 5d5af47995
commit 4c1e760dd8
2 changed files with 330 additions and 269 deletions

View File

@ -57,8 +57,6 @@
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.math.NumberUtils;
/**
* <p>Common <code>String</code> manipulation routines that are
* <code>null</code> safe.</p>
@ -98,7 +96,7 @@
* @author Arun Mammen Thomas
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 1.0
* @version $Id: StringUtils.java,v 1.70 2003/07/20 00:37:09 scolebourne Exp $
* @version $Id: StringUtils.java,v 1.71 2003/07/20 10:29:22 scolebourne Exp $
*/
public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -122,19 +120,16 @@ public class StringUtils {
*/
private static int PAD_LIMIT = 8192;
/**
* <p>A <code>String</code> containing all space characters (' ').</p>
*
* <p>Used for efficient space padding. The length of the String expands as needed.</p>
*/
private static String spaces = new String(" ");
/**
* <p>An array of <code>String</code>s used for padding.</p>
*
* <p>Used for efficient space padding. The length of each String expands as needed.</p>
*/
private final static String[] padding = new String[Character.MAX_VALUE];
static {
padding[32] = " ";
}
/**
* <p><code>StringUtils<code> instances should NOT be constructed in
@ -2392,27 +2387,6 @@ public static String repeat(String str, int repeat) {
}
}
/**
* <p>Returns a String containing the requested number of
* space characters (' ').</p>
*
* <pre>
* StringUtils.padding(0) = ""
* StringUtils.padding(3) = " "
* StringUtils.padding(-2) = IndexOutOfBoundsException
* </pre>
*
* @param repeat number of times to repeat space
* @return a String with <code>repeat</code> spaces
* @throws IndexOutOfBoundsException if <code>repeat &lt; 0</code>
*/
private static String padding(int repeat) {
while (spaces.length() < repeat) {
spaces = spaces.concat(spaces);
}
return spaces.substring(0, repeat);
}
/**
* <p>Returns padding using the specified delimiter repeated
* to a given length.</p>
@ -2457,17 +2431,7 @@ private static String padding(int repeat, char padChar) {
* <code>null</code> if null String input
*/
public static String rightPad(String str, int size) {
if (str == null) {
return null;
}
int pads = size - str.length();
if (pads <= 0) {
return str; // returns original String when possible
}
if (pads > PAD_LIMIT) {
return rightPad(str, size, ' ');
}
return str.concat(padding(pads));
return rightPad(str, size, ' ');
}
/**
@ -2517,7 +2481,7 @@ public static String rightPad(String str, int size, char padChar) {
* StringUtils.rightPad("bat", -1, "yz") = "bat"
* StringUtils.rightPad("bat", 1, null) = IllegalArgumentException
* StringUtils.rightPad("bat", 1, "") = IllegalArgumentException
* StringUtils.rightPad(null, 1, "") = IllegalArgumentException
* StringUtils.rightPad(null, 1, "") = null
* </pre>
*
* @param str the String to pad out, may be null
@ -2528,13 +2492,13 @@ public static String rightPad(String str, int size, char padChar) {
* @throws IllegalArgumentException if padStr is the empty String or null
*/
public static String rightPad(String str, int size, String padStr) {
if (str == null) {
return null;
}
int padLen;
if (padStr == null || (padLen = padStr.length()) == 0) {
throw new IllegalArgumentException("Pad String must not be null or empty");
}
if (str == null) {
return null;
}
int strLen = str.length();
int pads = size - strLen;
if (padLen == 1 && pads <= PAD_LIMIT) {
@ -2577,17 +2541,7 @@ public static String rightPad(String str, int size, String padStr) {
* <code>null</code> if null String input
*/
public static String leftPad(String str, int size) {
if (str == null) {
return null;
}
int pads = size - str.length();
if (pads <= 0) {
return str; // returns original String when possible
}
if (pads > PAD_LIMIT) {
return leftPad(str, size, ' ');
}
return padding(pads).concat(str);
return leftPad(str, size, ' ');
}
/**
@ -2637,7 +2591,7 @@ public static String leftPad(String str, int size, char padChar) {
* StringUtils.leftPad("bat", -1, "yz") = "bat"
* StringUtils.leftPad("bat", 1, null) = IllegalArgumentException
* StringUtils.leftPad("bat", 1, "") = IllegalArgumentException
* StringUtils.leftPad(null, 1, "") = IllegalArgumentException
* StringUtils.leftPad(null, 1, "") = null
* </pre>
*
* @param str the String to pad out, may be null
@ -2648,13 +2602,13 @@ public static String leftPad(String str, int size, char padChar) {
* @throws IllegalArgumentException if padStr is the empty String or null
*/
public static String leftPad(String str, int size, String padStr) {
if (str == null) {
return null;
}
int padLen;
if (padStr == null || (padLen = padStr.length()) == 0) {
throw new IllegalArgumentException("Pad String must not be null or empty");
}
if (str == null) {
return null;
}
int strLen = str.length();
int pads = size - strLen;
if (padLen == 1 && pads <= PAD_LIMIT) {
@ -2706,6 +2660,34 @@ public static String leftPad(String str, int size, String padStr) {
* @return centered String, <code>null</code> if null String input
*/
public static String center(String str, int size) {
return center(str, size, ' ');
}
/**
* <p>Centers a String in a larger String of size <code>size</code>.
* Uses a supplied character as the value to pad the String with.</p>
*
* <p>If the size is less than the String length, the String is returned.
* A <code>null</code> String returns <code>null</code>.
* A negative size is treated as zero.</p>
*
* <pre>
* StringUtils.center(null, -1, ' ') = null
* StringUtils.center("ab", -1, ' ') = "ab"
* StringUtils.center(null, 4, ' ') = null
* StringUtils.center("", 4, ' ') = " "
* StringUtils.center("ab", 4, ' ') = " ab"
* StringUtils.center("abcd", 2, ' ') = "abcd"
* StringUtils.center("a", 4, ' ') = " a "
* StringUtils.center("a", 4, 'y') = "yayy"
* </pre>
*
* @param str the String to center, may be null
* @param size the int size of new String, negative treated as zero
* @param padChar the character to pad the new String with
* @return centered String, <code>null</code> if null String input
*/
public static String center(String str, int size, char padChar) {
if (str == null || size <= 0) {
return str;
}
@ -2714,15 +2696,14 @@ public static String center(String str, int size) {
if (pads <= 0) {
return str;
}
str = leftPad(str, strLen + pads / 2, ' ');
str = rightPad(str, size, ' ');
str = leftPad(str, strLen + pads / 2, padChar);
str = rightPad(str, size, padChar);
return str;
}
/**
* <p>Centers a String in a larger String of size <code>size</code>.</p>
*
* <p>Uses a supplied String as the value to pad the String with.</p>
* <p>Centers a String in a larger String of size <code>size</code>.
* Uses a supplied String as the value to pad the String with.</p>
*
* <p>If the size is less than the String length, the String is returned.
* A <code>null</code> String returns <code>null</code>.
@ -2734,12 +2715,12 @@ public static String center(String str, int size) {
* StringUtils.center(null, 4, " ") = null
* StringUtils.center("", 4, " ") = " "
* StringUtils.center("ab", 4, " ") = " ab"
* StringUtils.center("abcd", 2, " ") = " abcd"
* StringUtils.center("abcd", 2, " ") = "abcd"
* StringUtils.center("a", 4, " ") = " a "
* StringUtils.center("a", 4, "yz") = "yayz"
* StringUtils.center("a", 4, "yz") = "yayz"
* StringUtils.center("abc", 4, null) = IllegalArgumentException
* StringUtils.center("abc", 4, "") = IllegalArgumentException
* StringUtils.center(null, 4, "") = IllegalArgumentException
* StringUtils.center(null, 4, "") = null
* </pre>
*
* @param str the String to center, may be null
@ -2749,12 +2730,12 @@ public static String center(String str, int size) {
* @throws IllegalArgumentException if padStr is <code>null</code> or empty
*/
public static String center(String str, int size, String padStr) {
if (padStr == null || padStr.length() == 0) {
throw new IllegalArgumentException("Pad String must not be null or empty");
}
if (str == null || size <= 0) {
return str;
}
if (padStr == null || padStr.length() == 0) {
throw new IllegalArgumentException("Pad String must not be null or empty");
}
int strLen = str.length();
int pads = size - strLen;
if (pads <= 0) {
@ -3494,6 +3475,17 @@ public static String abbreviate(String str, int offset, int maxWidth) {
* <p>For example,
* <code>difference("i am a machine", "i am a robot") -> "robot"</code>.</p>
*
* <pre>
* StringUtils.difference(null, null) = null
* StringUtils.difference("", "") = ""
* StringUtils.difference("", "abc") = "abc"
* StringUtils.difference("abc", "") = ""
* StringUtils.difference("abc", "abc") = ""
* StringUtils.difference("ab", "abxyz") = "xyz"
* StringUtils.difference("abcde", "abxyz") = "xyz"
* StringUtils.difference("abcde", "xyz") = "xyz"
* </pre>
*
* @param str1 the first String, may be null
* @param str2 the second String, may be null
* @return the portion of str2 where it differs from str1; returns the
@ -3520,6 +3512,17 @@ public static String difference(String str1, String str2) {
* <p>For example,
* <code>differenceAt("i am a machine", "i am a robot") -> 7</code></p>
*
* <pre>
* StringUtils.differenceAt(null, null) = -1
* StringUtils.differenceAt("", "") = -1
* StringUtils.differenceAt("", "abc") = 0
* StringUtils.differenceAt("abc", "") = 0
* StringUtils.differenceAt("abc", "abc") = -1
* StringUtils.differenceAt("ab", "abxyz") = 2
* StringUtils.differenceAt("abcde", "abxyz") = 2
* StringUtils.differenceAt("abcde", "xyz") = 0
* </pre>
*
* @param str1 the first String, may be null
* @param str2 the second String, may be null
* @return the index where str2 and str1 begin to differ; -1 if they are equal
@ -3610,7 +3613,7 @@ public static int getLevenshteinDistance(String s, String t) {
}
// Step 6
d[i][j] = NumberUtils.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
}
}
@ -3618,5 +3621,24 @@ public static int getLevenshteinDistance(String s, String t) {
return d[n][m];
}
/**
* <p>Gets the minimum of three <code>int</code> values.</p>
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the smallest of the values
*/
private static int min(int a, int b, int c) {
// Method copied from NumberUtils to avoid dependency on subpackage
if (b < a) {
a = b;
}
if (c < a) {
a = c;
}
return a;
}
}

View File

@ -72,7 +72,7 @@
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
* @author Holger Krauth
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @version $Id: StringUtilsTest.java,v 1.31 2003/07/20 00:17:29 scolebourne Exp $
* @version $Id: StringUtilsTest.java,v 1.32 2003/07/20 10:29:21 scolebourne Exp $
*/
public class StringUtilsTest extends TestCase {
@ -279,7 +279,7 @@ public void testSplit_StringChar() {
assertEquals("a", res[0]);
}
public void testSplit() {
public void testSplit_StringString_StringStringInt() {
assertEquals(null, StringUtils.split(null, "."));
assertEquals(null, StringUtils.split(null, ".", 3));
@ -298,47 +298,43 @@ public void testSplit() {
}
private void innerTestSplit(char separator, String sepStr, char noMatch) {
try {
final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
String[] res;
// (str, sepStr)
res = StringUtils.split(str, sepStr);
assertEquals(3, res.length);
assertEquals("a", res[0]);
assertEquals("b", res[1]);
assertEquals(noMatch + "c", res[2]);
final String str2 = separator + "a" + separator;
res = StringUtils.split(str2, sepStr);
assertEquals(1, res.length);
assertEquals("a", res[0]);
String msg = "Failed on separator hex(" + Integer.toHexString(separator) +
"), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")";
final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
String[] res;
// (str, sepStr)
res = StringUtils.split(str, sepStr);
assertEquals(msg, 3, res.length);
assertEquals(msg, "a", res[0]);
assertEquals(msg, "b", res[1]);
assertEquals(msg, noMatch + "c", res[2]);
final String str2 = separator + "a" + separator;
res = StringUtils.split(str2, sepStr);
assertEquals(msg, 1, res.length);
assertEquals(msg, "a", res[0]);
res = StringUtils.split(str, sepStr, -1);
assertEquals(3, res.length);
assertEquals("a", res[0]);
assertEquals("b", res[1]);
assertEquals(noMatch + "c", res[2]);
res = StringUtils.split(str, sepStr, 0);
assertEquals(3, res.length);
assertEquals("a", res[0]);
assertEquals("b", res[1]);
assertEquals(noMatch + "c", res[2]);
res = StringUtils.split(str, sepStr, 1);
assertEquals(1, res.length);
assertEquals(str, res[0]);
res = StringUtils.split(str, sepStr, 2);
assertEquals(2, res.length);
assertEquals("a", res[0]);
assertEquals(str.substring(2), res[1]);
} catch (AssertionFailedError ex) {
System.out.println("Failed on separator hex(" + Integer.toHexString(separator) +
"), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")");
throw ex;
}
res = StringUtils.split(str, sepStr, -1);
assertEquals(msg, 3, res.length);
assertEquals(msg, "a", res[0]);
assertEquals(msg, "b", res[1]);
assertEquals(msg, noMatch + "c", res[2]);
res = StringUtils.split(str, sepStr, 0);
assertEquals(msg, 3, res.length);
assertEquals(msg, "a", res[0]);
assertEquals(msg, "b", res[1]);
assertEquals(msg, noMatch + "c", res[2]);
res = StringUtils.split(str, sepStr, 1);
assertEquals(msg, 1, res.length);
assertEquals(msg, str, res[0]);
res = StringUtils.split(str, sepStr, 2);
assertEquals(msg, 2, res.length);
assertEquals(msg, "a", res[0]);
assertEquals(msg, str.substring(2), res[1]);
}
public void testDeleteSpace_String() {
@ -433,7 +429,7 @@ public void testOverlayString() {
} catch (IndexOutOfBoundsException ex) {}
}
public void testRepeat() {
public void testRepeat_StringInt() {
assertEquals(null, StringUtils.repeat(null, 2));
assertEquals("", StringUtils.repeat("ab", 0));
assertEquals("", StringUtils.repeat("", 3));
@ -442,46 +438,6 @@ public void testRepeat() {
assertEquals("abcabcabc", StringUtils.repeat("abc", 3));
}
public void testCenter() {
assertEquals(null, StringUtils.center(null, -1));
assertEquals(null, StringUtils.center(null, 4));
assertEquals("ab", StringUtils.center("ab", 0));
assertEquals("ab", StringUtils.center("ab", -1));
assertEquals("ab", StringUtils.center("ab", 1));
assertEquals(" ", StringUtils.center("", 4));
assertEquals(" ab ", StringUtils.center("ab", 4));
assertEquals("abcd", StringUtils.center("abcd", 2));
assertEquals(" a ", StringUtils.center("a", 4));
assertEquals(" a ", StringUtils.center("a", 5));
assertEquals(null, StringUtils.center(null, -1, " "));
assertEquals(null, StringUtils.center(null, 4, " "));
assertEquals("ab", StringUtils.center("ab", 0, " "));
assertEquals("ab", StringUtils.center("ab", -1, " "));
assertEquals("ab", StringUtils.center("ab", 1, " "));
assertEquals(" ", StringUtils.center("", 4, " "));
assertEquals(" ab ", StringUtils.center("ab", 4, " "));
assertEquals("abcd", StringUtils.center("abcd", 2, " "));
assertEquals(" a ", StringUtils.center("a", 4, " "));
assertEquals("yayz", StringUtils.center("a", 4, "yz"));
assertEquals("yzyayzy", StringUtils.center("a", 7, "yz"));
try {
StringUtils.center(null, 4, null);
fail();
} catch (IllegalArgumentException ex) {
}
try {
StringUtils.center("abc", 4, null);
fail();
} catch (IllegalArgumentException ex) {
}
try {
StringUtils.center("abc", 4, "");
fail();
} catch (IllegalArgumentException ex) {
}
}
public void testDeprecatedChompFunctions() {
assertEquals("chompLast(String) failed",
FOO, StringUtils.chompLast(FOO + "\n") );
@ -613,147 +569,213 @@ public void testSliceFunctions() {
}
public void testPadFunctions() {
assertEquals(null, StringUtils.rightPad (null, 8) );
assertEquals("1234 ", StringUtils.rightPad ("1234", 8) );
//-----------------------------------------------------------------------
public void testRightPad_StringInt() {
assertEquals(null, StringUtils.rightPad(null, 5));
assertEquals(" ", StringUtils.rightPad("", 5));
assertEquals("abc ", StringUtils.rightPad("abc", 5));
assertEquals("abc", StringUtils.rightPad("abc", 2));
assertEquals("abc", StringUtils.rightPad("abc", -1));
}
public void testRightPad_StringIntChar() {
assertEquals(null, StringUtils.rightPad(null, 5, ' '));
assertEquals(" ", StringUtils.rightPad("", 5, ' '));
assertEquals("abc ", StringUtils.rightPad("abc", 5, ' '));
assertEquals("abc", StringUtils.rightPad("abc", 2, ' '));
assertEquals("abc", StringUtils.rightPad("abc", -1, ' '));
assertEquals("abcxx", StringUtils.rightPad("abc", 5, 'x'));
}
public void testRightPad_StringIntString() {
assertEquals(null, StringUtils.rightPad(null, 5, "-+"));
assertEquals(" ", StringUtils.rightPad("", 5, " "));
assertEquals(null, StringUtils.rightPad(null, 8, null));
assertEquals("abc-+-+", StringUtils.rightPad("abc", 7, "-+"));
assertEquals("abc-+~", StringUtils.rightPad("abc", 6, "-+~"));
assertEquals("abc-+", StringUtils.rightPad("abc", 5, "-+~"));
assertEquals("abc", StringUtils.rightPad("abc", 2, " "));
assertEquals("abc", StringUtils.rightPad("abc", -1, " "));
try {
StringUtils.rightPad("abc56", 6, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
StringUtils.rightPad("abc56", 6, "");
fail();
} catch (IllegalArgumentException ex) {}
}
assertEquals(null, StringUtils.rightPad (null, 8, "-+") );
assertEquals("1234-+-+", StringUtils.rightPad ("1234", 8, "-+") );
assertEquals("123456-+~", StringUtils.rightPad ("123456", 9, "-+~") );
assertEquals("123456-+", StringUtils.rightPad ("123456", 8, "-+~") );
try {
StringUtils.rightPad(null, 6, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
StringUtils.rightPad("123456", 6, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
StringUtils.rightPad("123456", 6, "");
fail();
} catch (IllegalArgumentException ex) {}
//-----------------------------------------------------------------------
public void testLeftPad_StringInt() {
assertEquals(null, StringUtils.leftPad(null, 5));
assertEquals(" ", StringUtils.leftPad("", 5));
assertEquals(" abc", StringUtils.leftPad("abc", 5));
assertEquals("abc", StringUtils.leftPad("abc", 2));
}
assertEquals(null, StringUtils.leftPad (null, 8) );
assertEquals(" 1234", StringUtils.leftPad("1234", 8) );
public void testLeftPad_StringIntChar() {
assertEquals(null, StringUtils.leftPad(null, 5, ' '));
assertEquals(" ", StringUtils.leftPad("", 5, ' '));
assertEquals(" abc", StringUtils.leftPad("abc", 5, ' '));
assertEquals("xxabc", StringUtils.leftPad("abc", 5, 'x'));
assertEquals("abc", StringUtils.leftPad("abc", 2, ' '));
}
assertEquals(null, StringUtils.leftPad (null, 8, "-+") );
assertEquals("-+-+1234", StringUtils.leftPad("1234", 8, "-+") );
assertEquals("-+~123456", StringUtils.leftPad("123456", 9, "-+~") );
assertEquals("-+123456", StringUtils.leftPad("123456", 8, "-+~") );
public void testLeftPad_StringIntString() {
assertEquals(null, StringUtils.leftPad(null, 5, "-+"));
assertEquals(null, StringUtils.leftPad(null, 5, null));
assertEquals(" ", StringUtils.leftPad("", 5, " "));
assertEquals("-+-+abc", StringUtils.leftPad("abc", 7, "-+"));
assertEquals("-+~abc", StringUtils.leftPad("abc", 6, "-+~"));
assertEquals("-+abc", StringUtils.leftPad("abc", 5, "-+~"));
assertEquals("abc", StringUtils.leftPad("abc", 2, " "));
assertEquals("abc", StringUtils.leftPad("abc", -1, " "));
try {
StringUtils.leftPad(null, 6, null);
StringUtils.leftPad("abc56", 6, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
StringUtils.leftPad("123456", 6, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
StringUtils.leftPad("123456", 6, "");
StringUtils.leftPad("abc56", 6, "");
fail();
} catch (IllegalArgumentException ex) {}
}
public void testReverse() {
//-----------------------------------------------------------------------
public void testCenter_StringInt() {
assertEquals(null, StringUtils.center(null, -1));
assertEquals(null, StringUtils.center(null, 4));
assertEquals(" ", StringUtils.center("", 4));
assertEquals("ab", StringUtils.center("ab", 0));
assertEquals("ab", StringUtils.center("ab", -1));
assertEquals("ab", StringUtils.center("ab", 1));
assertEquals(" ", StringUtils.center("", 4));
assertEquals(" ab ", StringUtils.center("ab", 4));
assertEquals("abcd", StringUtils.center("abcd", 2));
assertEquals(" a ", StringUtils.center("a", 4));
assertEquals(" a ", StringUtils.center("a", 5));
}
public void testCenter_StringIntChar() {
assertEquals(null, StringUtils.center(null, -1, ' '));
assertEquals(null, StringUtils.center(null, 4, ' '));
assertEquals(" ", StringUtils.center("", 4, ' '));
assertEquals("ab", StringUtils.center("ab", 0, ' '));
assertEquals("ab", StringUtils.center("ab", -1, ' '));
assertEquals("ab", StringUtils.center("ab", 1, ' '));
assertEquals(" ", StringUtils.center("", 4, ' '));
assertEquals(" ab ", StringUtils.center("ab", 4, ' '));
assertEquals("abcd", StringUtils.center("abcd", 2, ' '));
assertEquals(" a ", StringUtils.center("a", 4, ' '));
assertEquals(" a ", StringUtils.center("a", 5, ' '));
assertEquals("xxaxx", StringUtils.center("a", 5, 'x'));
}
public void testCenter_StringIntString() {
assertEquals(null, StringUtils.center(null, 4, null));
assertEquals(null, StringUtils.center(null, -1, " "));
assertEquals(null, StringUtils.center(null, 4, " "));
assertEquals(" ", StringUtils.center("", 4, " "));
assertEquals("ab", StringUtils.center("ab", 0, " "));
assertEquals("ab", StringUtils.center("ab", -1, " "));
assertEquals("ab", StringUtils.center("ab", 1, " "));
assertEquals(" ", StringUtils.center("", 4, " "));
assertEquals(" ab ", StringUtils.center("ab", 4, " "));
assertEquals("abcd", StringUtils.center("abcd", 2, " "));
assertEquals(" a ", StringUtils.center("a", 4, " "));
assertEquals("yayz", StringUtils.center("a", 4, "yz"));
assertEquals("yzyayzy", StringUtils.center("a", 7, "yz"));
try {
StringUtils.center("abc", 4, null);
fail();
} catch (IllegalArgumentException ex) {
}
try {
StringUtils.center("abc", 4, "");
fail();
} catch (IllegalArgumentException ex) {
}
}
//-----------------------------------------------------------------------
public void testReverse_String() {
assertEquals(null, StringUtils.reverse(null) );
assertEquals("sdrawkcab", StringUtils.reverse("backwards") );
assertEquals("", StringUtils.reverse("") );
assertEquals("sdrawkcab", StringUtils.reverse("backwards") );
}
public void testReverseDelimitedString() {
public void testReverseDelimitedString_StringChar() {
assertEquals(null, StringUtils.reverseDelimitedString(null, '.') );
assertEquals("", StringUtils.reverseDelimitedString("", '.') );
assertEquals("c.b.a", StringUtils.reverseDelimitedString("a.b.c", '.') );
assertEquals("a b c", StringUtils.reverseDelimitedString("a b c", '.') );
assertEquals("", StringUtils.reverseDelimitedString("", '.') );
assertEquals(null, StringUtils.reverseDelimitedString(null, null) );
assertEquals("a.b.c", StringUtils.reverseDelimitedString("a.b.c", null) );
assertEquals("c b a", StringUtils.reverseDelimitedString("a b c", null) );
assertEquals("org.apache.test",
StringUtils.reverseDelimitedString("test.apache.org", ".") );
assertEquals("reverseDelimitedString(empty-string,'.') failed",
"",
StringUtils.reverseDelimitedString("", ".") );
assertEquals("reverseDelimitedString(String,' ') failed",
"once upon a time",
StringUtils.reverseDelimitedString("time a upon once"," ") );
}
public void testDefaultFunctions() {
public void testReverseDelimitedString_StringString() {
assertEquals(null, StringUtils.reverseDelimitedString(null, null) );
assertEquals("", StringUtils.reverseDelimitedString("", null) );
assertEquals("", StringUtils.reverseDelimitedString("", ".") );
assertEquals("a.b.c", StringUtils.reverseDelimitedString("a.b.c", null) );
assertEquals("c b a", StringUtils.reverseDelimitedString("a b c", null) );
assertEquals("c.b.a", StringUtils.reverseDelimitedString("a.b.c", ".") );
}
//-----------------------------------------------------------------------
public void testDefault_String() {
assertEquals("", StringUtils.defaultString(null) );
assertEquals("", StringUtils.defaultString("") );
assertEquals(FOO, StringUtils.defaultString(FOO) );
assertEquals(BAR, StringUtils.defaultString(null, BAR) );
assertEquals("", StringUtils.defaultString("", BAR) );
assertEquals(FOO, StringUtils.defaultString(FOO, BAR) );
assertEquals("", StringUtils.defaultString((Object) "") );
assertEquals(FOO, StringUtils.defaultString((Object) FOO) );
assertEquals("abc", StringUtils.defaultString("abc") );
}
public void testDefault_StringString() {
assertEquals("xyz", StringUtils.defaultString(null, "xyz") );
assertEquals("", StringUtils.defaultString("", "xyz") );
assertEquals("abc", StringUtils.defaultString("abc", "xyz") );
}
public void testDefault_Object() {
assertEquals("", StringUtils.defaultString((Object) null) );
assertEquals("", StringUtils.defaultString((Object) "", BAR) );
assertEquals(FOO, StringUtils.defaultString((Object) FOO, BAR) );
assertEquals(Boolean.TRUE.toString(), StringUtils.defaultString(Boolean.TRUE) );
}
public void testDefault_ObjectString() {
assertEquals(BAR, StringUtils.defaultString((Object) null, BAR) );
assertEquals(Boolean.TRUE.toString(), StringUtils.defaultString(Boolean.TRUE, BAR) );
}
public void testEscapeFunctions() {
assertEquals("escape(empty-string) failed",
"", StringUtils.escape("") );
assertEquals("escape(String) failed",
FOO, StringUtils.escape(FOO) );
assertEquals("escape(String) failed",
"\\t", StringUtils.escape("\t") );
assertEquals("escape(String) failed",
"\\\\", StringUtils.escape("\\") );
assertEquals("escape(String) failed",
"\\\\\\b\\t\\r", StringUtils.escape("\\\b\t\r") );
assertEquals("escape(String) failed",
"\\u1234", StringUtils.escape("\u1234") );
assertEquals("escape(String) failed",
"\\u0234", StringUtils.escape("\u0234") );
assertEquals("escape(String) failed",
"\\u00FD", StringUtils.escape("\u00fd") );
//-----------------------------------------------------------------------
public void testEscapeFunctions_String() {
assertEquals("", StringUtils.escape("") );
assertEquals("abc", StringUtils.escape("abc") );
assertEquals("\\t", StringUtils.escape("\t") );
assertEquals("\\\\", StringUtils.escape("\\") );
assertEquals("\\\\\\b\\t\\r", StringUtils.escape("\\\b\t\r") );
assertEquals("\\u1234", StringUtils.escape("\u1234") );
assertEquals("\\u0234", StringUtils.escape("\u0234") );
assertEquals("\\u00FD", StringUtils.escape("\u00fd") );
}
public void testGetLevenshteinDistance() {
assertEquals("getLevenshteinDistance(empty-string, empty-string) failed",
0, StringUtils.getLevenshteinDistance("", "") );
assertEquals("getLevenshteinDistance(empty-string, String) failed",
1, StringUtils.getLevenshteinDistance("", "a") );
assertEquals("getLevenshteinDistance(String, empty-string) failed",
7, StringUtils.getLevenshteinDistance("aaapppp", "") );
assertEquals("getLevenshteinDistance(String, String) failed",
1, StringUtils.getLevenshteinDistance("frog", "fog") );
assertEquals("getLevenshteinDistance(String, String) failed",
3, StringUtils.getLevenshteinDistance("fly", "ant") );
assertEquals("getLevenshteinDistance(String, String) failed",
7, StringUtils.getLevenshteinDistance("elephant", "hippo") );
assertEquals("getLevenshteinDistance(String, String) failed",
7, StringUtils.getLevenshteinDistance("hippo", "elephant") );
assertEquals("getLevenshteinDistance(String, String) failed",
1, StringUtils.getLevenshteinDistance("hello", "hallo") );
}
public void testAbbreviate() {
//-----------------------------------------------------------------------
public void testAbbreviate_StringInt() {
assertEquals(null, StringUtils.abbreviate(null, 10));
assertEquals("abbreviate(String,int) failed",
"short", StringUtils.abbreviate("short", 10));
assertEquals("abbreviate(String,int) failed",
"Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
assertEquals("", StringUtils.abbreviate("", 10));
assertEquals("short", StringUtils.abbreviate("short", 10));
assertEquals("Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
String raspberry = "raspberry peach";
assertEquals("abbreviate(String,int) failed (one past limit)",
"raspberry p...", StringUtils.abbreviate(raspberry, 14));
assertEquals("abbreviate(String,int) (at limit)",
"raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
assertEquals("abbreviate(String,int) (one below limit)",
"raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
assertEquals("raspberry p...", StringUtils.abbreviate(raspberry, 14));
assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
}
public void testAbbreviate_StringIntInt() {
assertEquals(null, StringUtils.abbreviate(null, 10, 12));
assertEquals("", StringUtils.abbreviate("", 0, 10));
assertEquals("", StringUtils.abbreviate("", 2, 10));
assertEquals("abbreviate(String,int,int) failed",
"raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
String raspberry = "raspberry peach";
assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
assertEquals(null, StringUtils.abbreviate(null, 7, 14));
assertAbbreviateWithOffset("abcdefg...", -1, 10);
@ -776,11 +798,9 @@ public void testAbbreviate() {
assertAbbreviateWithOffset("...ijklmno", 15, 10);
assertAbbreviateWithOffset("...ijklmno", 16, 10);
assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
}
private void assertAbbreviateWithOffset(String expected, int offset, int maxWidth)
{
private void assertAbbreviateWithOffset(String expected, int offset, int maxWidth) {
String abcdefghijklmno = "abcdefghijklmno";
String message = "abbreviate(String,int,int) failed";
String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
@ -793,23 +813,42 @@ private void assertAbbreviateWithOffset(String expected, int offset, int maxWidt
assertEquals(message, expected, actual);
}
public void testDifference() {
//-----------------------------------------------------------------------
public void testDifference_StringString() {
assertEquals(null, StringUtils.difference(null, null));
assertEquals("", StringUtils.difference("", ""));
assertEquals("abc", StringUtils.difference("", "abc"));
assertEquals("", StringUtils.difference("abc", ""));
assertEquals("i am a robot", StringUtils.difference(null, "i am a robot"));
assertEquals("i am a machine", StringUtils.difference("i am a machine", null));
assertEquals("robot", StringUtils.difference("i am a machine", "i am a robot"));
assertEquals("", StringUtils.difference("foo", "foo"));
assertEquals("", StringUtils.difference("abc", "abc"));
assertEquals("you are a robot", StringUtils.difference("i am a robot", "you are a robot"));
}
public void testDifferenceAt() {
public void testDifferenceAt_StringString() {
assertEquals(-1, StringUtils.differenceAt(null, null));
assertEquals(0, StringUtils.differenceAt(null, "i am a robot"));
assertEquals(-1, StringUtils.differenceAt("", ""));
assertEquals(0, StringUtils.differenceAt("", "abc"));
assertEquals(0, StringUtils.differenceAt("abc", ""));
assertEquals(0, StringUtils.differenceAt("i am a machine", null));
assertEquals(7, StringUtils.differenceAt("i am a machine", "i am a robot"));
assertEquals(-1, StringUtils.differenceAt("foo", "foo"));
assertEquals(0, StringUtils.differenceAt("i am a robot", "you are a robot"));
}
//-----------------------------------------------------------------------
public void testGetLevenshteinDistance_StringString() {
assertEquals(0, StringUtils.getLevenshteinDistance("", "") );
assertEquals(1, StringUtils.getLevenshteinDistance("", "a") );
assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "") );
assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog") );
assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant") );
assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo") );
assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant") );
assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo") );
}
}