commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r904603 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/StringUtils.java test/java/org/apache/commons/lang/StringUtilsTest.java
Date Fri, 29 Jan 2010 19:26:49 GMT
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 = {



Mime
View raw message