commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang StringUtils.java
Date Fri, 01 Aug 2003 23:20:06 GMT
scolebourne    2003/08/01 16:20:06

  Modified:    lang/src/test/org/apache/commons/lang StringUtilsTest.java
               lang/src/java/org/apache/commons/lang StringUtils.java
  Log:
  Relax exceptions in leftPad(), rightPad() and center()
  
  Revision  Changes    Path
  1.44      +7 -27     jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java
  
  Index: StringUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v
  retrieving revision 1.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- StringUtilsTest.java	1 Aug 2003 23:01:52 -0000	1.43
  +++ StringUtilsTest.java	1 Aug 2003 23:20:06 -0000	1.44
  @@ -703,14 +703,8 @@
           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 @@
           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 @@
           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, ""));
       }
   
       //-----------------------------------------------------------------------
  
  
  
  1.89      +16 -21    jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
  retrieving revision 1.88
  retrieving revision 1.89
  diff -u -r1.88 -r1.89
  --- StringUtils.java	1 Aug 2003 23:11:55 -0000	1.88
  +++ StringUtils.java	1 Aug 2003 23:20:06 -0000	1.89
  @@ -3022,26 +3022,24 @@
        * 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  "
        * </pre>
        *
        * @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,
        *  <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) {
           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 @@
        * 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"
        * </pre>
        *
        * @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,
        *  <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) {
           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 @@
        * 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  "
        * </pre>
        * 
        * @param str  the String to center, may be null
  @@ -3276,7 +3271,7 @@
               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;
  
  
  

Mime
View raw message