Relax exceptions in leftPad(), rightPad() and center()

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137553 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-08-01 23:20:06 +00:00
parent 9a51cf5efc
commit 0dce2b5285
2 changed files with 23 additions and 48 deletions

View File

@ -144,7 +144,7 @@ import java.util.List;
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a> * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author Phil Steitz * @author Phil Steitz
* @since 1.0 * @since 1.0
* @version $Id: StringUtils.java,v 1.88 2003/08/01 23:11:55 scolebourne Exp $ * @version $Id: StringUtils.java,v 1.89 2003/08/01 23:20:06 scolebourne Exp $
*/ */
public class StringUtils { public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne) // Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -3022,26 +3022,24 @@ public class StringUtils {
* StringUtils.rightPad("bat", 8, "yz") = "batyzyzy" * StringUtils.rightPad("bat", 8, "yz") = "batyzyzy"
* StringUtils.rightPad("bat", 1, "yz") = "bat" * StringUtils.rightPad("bat", 1, "yz") = "bat"
* StringUtils.rightPad("bat", -1, "yz") = "bat" * StringUtils.rightPad("bat", -1, "yz") = "bat"
* StringUtils.rightPad("bat", 1, null) = IllegalArgumentException * StringUtils.rightPad("bat", 5, null) = "bat "
* StringUtils.rightPad("bat", 1, "") = IllegalArgumentException * StringUtils.rightPad("bat", 5, "") = "bat "
* StringUtils.rightPad(null, 1, "") = null
* </pre> * </pre>
* *
* @param str the String to pad out, may be null * @param str the String to pad out, may be null
* @param size the size to pad to * @param size the size to pad to
* @param padStr the String to pad with, must not be null * @param padStr the String to pad with, null or empty treated as single space
* @return right padded String or original String if no padding is necessary, * @return right padded String or original String if no padding is necessary,
* <code>null</code> if null String input * <code>null</code> if null String input
* @throws IllegalArgumentException if padStr is the empty String or null
*/ */
public static String rightPad(String str, int size, String padStr) { public static String rightPad(String str, int size, String padStr) {
if (str == null) { if (str == null) {
return null; return null;
} }
int padLen; if (padStr == null || padStr.length() == 0) {
if (padStr == null || (padLen = padStr.length()) == 0) { padStr = " ";
throw new IllegalArgumentException("Pad String must not be null or empty");
} }
int padLen = padStr.length();
int strLen = str.length(); int strLen = str.length();
int pads = size - strLen; int pads = size - strLen;
if (pads <= 0) { if (pads <= 0) {
@ -3135,26 +3133,24 @@ public class StringUtils {
* StringUtils.leftPad("bat", 8, "yz") = "yzyzybat" * StringUtils.leftPad("bat", 8, "yz") = "yzyzybat"
* StringUtils.leftPad("bat", 1, "yz") = "bat" * StringUtils.leftPad("bat", 1, "yz") = "bat"
* StringUtils.leftPad("bat", -1, "yz") = "bat" * StringUtils.leftPad("bat", -1, "yz") = "bat"
* StringUtils.leftPad("bat", 1, null) = IllegalArgumentException * StringUtils.leftPad("bat", 5, null) = " bat"
* StringUtils.leftPad("bat", 1, "") = IllegalArgumentException * StringUtils.leftPad("bat", 5, "") = " bat"
* StringUtils.leftPad(null, 1, "") = null
* </pre> * </pre>
* *
* @param str the String to pad out, may be null * @param str the String to pad out, may be null
* @param size the size to pad to * @param size the size to pad to
* @param padStr the String to pad with, must not be null * @param padStr the String to pad with, null or empty treated as single space
* @return left padded String or original String if no padding is necessary, * @return left padded String or original String if no padding is necessary,
* <code>null</code> if null String input * <code>null</code> if null String input
* @throws IllegalArgumentException if padStr is the empty String or null
*/ */
public static String leftPad(String str, int size, String padStr) { public static String leftPad(String str, int size, String padStr) {
if (str == null) { if (str == null) {
return null; return null;
} }
int padLen; if (padStr == null || padStr.length() == 0) {
if (padStr == null || (padLen = padStr.length()) == 0) { padStr = " ";
throw new IllegalArgumentException("Pad String must not be null or empty");
} }
int padLen = padStr.length();
int strLen = str.length(); int strLen = str.length();
int pads = size - strLen; int pads = size - strLen;
if (pads <= 0) { if (pads <= 0) {
@ -3260,9 +3256,8 @@ public class StringUtils {
* StringUtils.center("abcd", 2, " ") = "abcd" * StringUtils.center("abcd", 2, " ") = "abcd"
* StringUtils.center("a", 4, " ") = " a " * 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", 7, null) = " abc "
* StringUtils.center("abc", 4, "") = IllegalArgumentException * StringUtils.center("abc", 7, "") = " abc "
* StringUtils.center(null, 4, "") = null
* </pre> * </pre>
* *
* @param str the String to center, may be null * @param str the String to center, may be null
@ -3276,7 +3271,7 @@ public class StringUtils {
return str; return str;
} }
if (padStr == null || padStr.length() == 0) { if (padStr == null || padStr.length() == 0) {
throw new IllegalArgumentException("Pad String must not be null or empty"); padStr = " ";
} }
int strLen = str.length(); int strLen = str.length();
int pads = size - strLen; int pads = size - strLen;

View File

@ -74,7 +74,7 @@ import junit.textui.TestRunner;
* @author Holger Krauth * @author Holger Krauth
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a> * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author Phil Steitz * @author Phil Steitz
* @version $Id: StringUtilsTest.java,v 1.43 2003/08/01 23:01:52 scolebourne Exp $ * @version $Id: StringUtilsTest.java,v 1.44 2003/08/01 23:20:06 scolebourne Exp $
*/ */
public class StringUtilsTest extends TestCase { public class StringUtilsTest extends TestCase {
@ -703,14 +703,8 @@ public class StringUtilsTest extends TestCase {
assertEquals("abc-+", StringUtils.rightPad("abc", 5, "-+~")); assertEquals("abc-+", StringUtils.rightPad("abc", 5, "-+~"));
assertEquals("abc", StringUtils.rightPad("abc", 2, " ")); assertEquals("abc", StringUtils.rightPad("abc", 2, " "));
assertEquals("abc", StringUtils.rightPad("abc", -1, " ")); assertEquals("abc", StringUtils.rightPad("abc", -1, " "));
try { assertEquals("abc ", StringUtils.rightPad("abc", 5, null));
StringUtils.rightPad("abc56", 6, null); assertEquals("abc ", StringUtils.rightPad("abc", 5, ""));
fail();
} catch (IllegalArgumentException ex) {}
try {
StringUtils.rightPad("abc56", 6, "");
fail();
} catch (IllegalArgumentException ex) {}
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -741,14 +735,8 @@ public class StringUtilsTest extends TestCase {
assertEquals("-+abc", StringUtils.leftPad("abc", 5, "-+~")); assertEquals("-+abc", StringUtils.leftPad("abc", 5, "-+~"));
assertEquals("abc", StringUtils.leftPad("abc", 2, " ")); assertEquals("abc", StringUtils.leftPad("abc", 2, " "));
assertEquals("abc", StringUtils.leftPad("abc", -1, " ")); assertEquals("abc", StringUtils.leftPad("abc", -1, " "));
try { assertEquals(" abc", StringUtils.leftPad("abc", 5, null));
StringUtils.leftPad("abc56", 6, null); assertEquals(" abc", StringUtils.leftPad("abc", 5, ""));
fail();
} catch (IllegalArgumentException ex) {}
try {
StringUtils.leftPad("abc56", 6, "");
fail();
} catch (IllegalArgumentException ex) {}
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -795,16 +783,8 @@ public class StringUtilsTest extends TestCase {
assertEquals(" a ", StringUtils.center("a", 4, " ")); assertEquals(" a ", StringUtils.center("a", 4, " "));
assertEquals("yayz", StringUtils.center("a", 4, "yz")); assertEquals("yayz", StringUtils.center("a", 4, "yz"));
assertEquals("yzyayzy", StringUtils.center("a", 7, "yz")); assertEquals("yzyayzy", StringUtils.center("a", 7, "yz"));
try { assertEquals(" abc ", StringUtils.center("abc", 7, null));
StringUtils.center("abc", 4, null); assertEquals(" abc ", StringUtils.center("abc", 7, ""));
fail();
} catch (IllegalArgumentException ex) {
}
try {
StringUtils.center("abc", 4, "");
fail();
} catch (IllegalArgumentException ex) {
}
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------