From 0dce2b5285c551c610499f28968790f6dc2fd25b Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Fri, 1 Aug 2003 23:20:06 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang/StringUtils.java | 37 ++++++++----------- .../apache/commons/lang/StringUtilsTest.java | 34 ++++------------- 2 files changed, 23 insertions(+), 48 deletions(-) diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index da0536d43..c92fa32fd 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -144,7 +144,7 @@ import java.util.List; * @author Gary Gregory * @author Phil Steitz * @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 { // Performance testing notes (JDK 1.4, Jul03, scolebourne) @@ -3022,26 +3022,24 @@ public class StringUtils { * StringUtils.rightPad("bat", 8, "yz") = "batyzyzy" * StringUtils.rightPad("bat", 1, "yz") = "bat" * StringUtils.rightPad("bat", -1, "yz") = "bat" - * StringUtils.rightPad("bat", 1, null) = IllegalArgumentException - * StringUtils.rightPad("bat", 1, "") = IllegalArgumentException - * StringUtils.rightPad(null, 1, "") = null + * StringUtils.rightPad("bat", 5, null) = "bat " + * StringUtils.rightPad("bat", 5, "") = "bat " * * * @param str the String to pad out, may be null * @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, * null if null String input - * @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 (padStr == null || padStr.length() == 0) { + padStr = " "; } + int padLen = padStr.length(); int strLen = str.length(); int pads = size - strLen; if (pads <= 0) { @@ -3135,26 +3133,24 @@ public class StringUtils { * StringUtils.leftPad("bat", 8, "yz") = "yzyzybat" * StringUtils.leftPad("bat", 1, "yz") = "bat" * StringUtils.leftPad("bat", -1, "yz") = "bat" - * StringUtils.leftPad("bat", 1, null) = IllegalArgumentException - * StringUtils.leftPad("bat", 1, "") = IllegalArgumentException - * StringUtils.leftPad(null, 1, "") = null + * StringUtils.leftPad("bat", 5, null) = " bat" + * StringUtils.leftPad("bat", 5, "") = " bat" * * * @param str the String to pad out, may be null * @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, * null if null String input - * @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 (padStr == null || padStr.length() == 0) { + padStr = " "; } + int padLen = padStr.length(); int strLen = str.length(); int pads = size - strLen; if (pads <= 0) { @@ -3260,9 +3256,8 @@ public class StringUtils { * StringUtils.center("abcd", 2, " ") = "abcd" * StringUtils.center("a", 4, " ") = " a " * StringUtils.center("a", 4, "yz") = "yayz" - * StringUtils.center("abc", 4, null) = IllegalArgumentException - * StringUtils.center("abc", 4, "") = IllegalArgumentException - * StringUtils.center(null, 4, "") = null + * StringUtils.center("abc", 7, null) = " abc " + * StringUtils.center("abc", 7, "") = " abc " * * * @param str the String to center, may be null @@ -3276,7 +3271,7 @@ public class StringUtils { return str; } if (padStr == null || padStr.length() == 0) { - throw new IllegalArgumentException("Pad String must not be null or empty"); + padStr = " "; } int strLen = str.length(); int pads = size - strLen; diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index 5f988006a..a75046d18 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -74,7 +74,7 @@ import junit.textui.TestRunner; * @author Holger Krauth * @author Henning P. Schmiedehausen * @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 { @@ -703,14 +703,8 @@ public class StringUtilsTest extends TestCase { 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("abc ", StringUtils.rightPad("abc", 5, null)); + assertEquals("abc ", StringUtils.rightPad("abc", 5, "")); } //----------------------------------------------------------------------- @@ -741,14 +735,8 @@ public class StringUtilsTest extends TestCase { assertEquals("-+abc", StringUtils.leftPad("abc", 5, "-+~")); assertEquals("abc", StringUtils.leftPad("abc", 2, " ")); assertEquals("abc", StringUtils.leftPad("abc", -1, " ")); - try { - StringUtils.leftPad("abc56", 6, null); - fail(); - } catch (IllegalArgumentException ex) {} - try { - StringUtils.leftPad("abc56", 6, ""); - fail(); - } catch (IllegalArgumentException ex) {} + assertEquals(" abc", StringUtils.leftPad("abc", 5, null)); + assertEquals(" abc", StringUtils.leftPad("abc", 5, "")); } //----------------------------------------------------------------------- @@ -795,16 +783,8 @@ public class StringUtilsTest extends TestCase { 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) { - } + assertEquals(" abc ", StringUtils.center("abc", 7, null)); + assertEquals(" abc ", StringUtils.center("abc", 7, "")); } //-----------------------------------------------------------------------