[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-commons-dev
Subject:    svn commit: r904603 - in /commons/proper/lang/branches/LANG_2_X/src:
From:       niallp () apache ! org
Date:       2010-01-29 19:26:49
Message-ID: 20100129192650.0457623889F7 () eris ! apache ! org
[Download RAW message or body]

Author: niallp
Date: Fri Jan 29 19:26:48 2010
New Revision: 904603

URL: http://svn.apache.org/viewvc?rev=904603&view=rev
Log:
Port LANG-348 to 2.x branch - add repeat() method to StringUtils

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
  commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java


Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
                
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/ \
org/apache/commons/lang/StringUtils.java?rev=904603&r1=904602&r2=904603&view=diff \
                ==============================================================================
                
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java \
                (original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java \
Fri Jan 29 19:26:48 2010 @@ -4539,6 +4539,35 @@
     }
 
     /**
+     * <p>Repeat a String <code>repeat</code> times to form a
+     * new String, with a String separator injected each time. </p>
+     *
+     * <pre>
+     * StringUtils.repeat(null, null, 2) = null
+     * StringUtils.repeat(null, "x", 2)  = null
+     * StringUtils.repeat("", null, 0)   = ""
+     * StringUtils.repeat("", "", 2)     = ""
+     * StringUtils.repeat("", "x", 3)    = "xxx"
+     * StringUtils.repeat("?", ", ", 3)  = "?, ?, ?"
+     * </pre>
+     *
+     * @param str        the String to repeat, may be null
+     * @param separator  the String to inject, 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,
+     *  <code>null</code> if null String input
+     */
+    public static String repeat(String str, String separator, int repeat) {
+        if(str == null || separator == null) {
+            return repeat(str, repeat);
+        } else {
+            // given that repeat(String, int) is quite optimized, better to rely on \
it than try and splice this into it +            String result = repeat(str + \
separator, repeat); +            return removeEnd(result, separator);
+        }
+    }
+
+    /**
      * <p>Returns padding using the specified delimiter repeated
      * to a given length.</p>
      *

Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java
                
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/ \
org/apache/commons/lang/StringUtilsTest.java?rev=904603&r1=904602&r2=904603&view=diff \
                ==============================================================================
                
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java \
                (original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java \
Fri Jan 29 19:26:48 2010 @@ -1248,6 +1248,19 @@
                      FOO_UNCAP, StringUtils.chopNewline(FOO_UNCAP + "\r\n") );
     }
 
+    public void testRepeat_StringStringInt() {
+        assertEquals(null, StringUtils.repeat(null, null, 2));
+        assertEquals(null, StringUtils.repeat(null, "x", 2));
+        assertEquals("", StringUtils.repeat("", null, 2));
+
+        assertEquals("", StringUtils.repeat("ab", "", 0));
+        assertEquals("", StringUtils.repeat("", "", 2));
+
+        assertEquals("xx", StringUtils.repeat("", "x", 3));
+
+        assertEquals("?, ?, ?", StringUtils.repeat("?", ", ", 3));
+    }
+
     public void testChop() {
 
         String[][] chopCases = {


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic