diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index 9367cf2a4..d4f223369 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -71,8 +71,15 @@ *
  • whitespace - the characters defined by {@link Character#isWhitespace(char)} * * - *

    The StringUtils class varies in its handling of - * null. Each method should be consulted individually.

    + *

    The StringUtils tries to handle null input + * quietly. That is to say that a null will generally return a + * sensible value rather than throw an exception. + * Typically, null in gives null out. + * Each method should be consulted individually for full details.

    + * + *

    A side effect of the null handling is that a + * NullPointerException should be considered a bug in StringUtils. + * (Except for deprecated methods).

    * * @author Apache Jakarta Turbine * @author GenerationJavaCore @@ -90,7 +97,7 @@ * @author Arun Mammen Thomas * @author Gary Gregory * @since 1.0 - * @version $Id: StringUtils.java,v 1.60 2003/07/16 23:45:39 scolebourne Exp $ + * @version $Id: StringUtils.java,v 1.61 2003/07/16 23:56:45 scolebourne Exp $ */ public class StringUtils { @@ -1626,25 +1633,27 @@ public static String escape(String str) { * new string.

    * *
    +     * StringUtils.repeat(null, 2) = null
          * StringUtils.repeat("", 0)   = ""
          * StringUtils.repeat("", 2)   = ""
          * StringUtils.repeat("a", 3)  = "aaa"
          * StringUtils.repeat("ab", 2) = "abab"
    -     * StringUtils.repeat(null, 2) = NullPointerException
    -     * StringUtils.repeat("a", -2) = NegativeArraySizeException
    +     * StringUtils.repeat("a", -2) = ""
          * 
    * - * @param str the String to repeat, must not be null - * @param repeat number of times to repeat str - * @return a new String consisting of the original String repeated - * @throws NegativeArraySizeException if repeat < 0 - * @throws NullPointerException if str is null + * @param str the String to repeat, may be null + * @param repeat number of times to repeat str, negative treated as zero + * @return a new String consisting of the original String repeated, + * null if null string input */ public static String repeat(String str, int repeat) { - int inputLength = str.length(); - if (repeat == 0) { + if (str == null) { + return null; + } + if (repeat <= 0) { return ""; } + int inputLength = str.length(); if (inputLength == 1 && repeat <= PAD_LIMIT) { return padding(repeat, str.charAt(0)); } @@ -1904,8 +1913,8 @@ public static String leftPad(String str, int size, char padChar) { * StringUtils.leftPad("bat", 8, "yz") = "yzyzybat" * StringUtils.leftPad("bat", 1, "yz") = "bat" * StringUtils.leftPad("bat", -1, "yz") = "bat" - * StringUtils.leftPad("bat", 1, null) = NullPointerException - * StringUtils.leftPad("bat", 1, "") = ArithmeticException + * StringUtils.leftPad("bat", 1, null) = IllegalArgumentException + * StringUtils.leftPad("bat", 1, "") = IllegalArgumentException * * * @param str the String to pad out, may be null @@ -2841,7 +2850,7 @@ public static int differenceAt(String str1, String str2) { /** *

    Find the Levenshtein distance between two Strings.

    * - *

    This is the number of changes needed to change one String into + *

    This is the number of changes needed to change one String into * another. Where each change is a single character modification.

    * *

    This implemmentation of the levenshtein distance algorithm diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index 4ccb53eb0..302dfc987 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -70,7 +70,7 @@ * @author Henning P. Schmiedehausen - * @version $Id: StringUtilsTest.java,v 1.24 2003/07/16 23:45:39 scolebourne Exp $ + * @version $Id: StringUtilsTest.java,v 1.25 2003/07/16 23:56:44 scolebourne Exp $ */ public class StringUtilsTest extends TestCase { @@ -279,16 +279,12 @@ public void testOverlayString() { } public void testRepeat() { + assertEquals(null, StringUtils.repeat(null, 2)); assertEquals("", StringUtils.repeat("ab", 0)); assertEquals("", StringUtils.repeat("", 3)); assertEquals("aaa", StringUtils.repeat("a", 3)); assertEquals("ababab", StringUtils.repeat("ab", 3)); assertEquals("abcabcabc", StringUtils.repeat("abc", 3)); - try { - StringUtils.repeat(null, 0); - fail(); - } catch (NullPointerException ex) { - } } public void testCenter() {