svn commit: r981533 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util: StringUtil.java test/StringUtilTests.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r981533 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util: StringUtil.java test/StringUtilTests.java

doogie-3
Author: doogie
Date: Mon Aug  2 14:46:21 2010
New Revision: 981533

URL: http://svn.apache.org/viewvc?rev=981533&view=rev
Log:
Add appendTo/append variant that allows for a prefix and suffix around
the separator.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/StringUtil.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/StringUtilTests.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/StringUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/StringUtil.java?rev=981533&r1=981532&r2=981533&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/StringUtil.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/StringUtil.java Mon Aug  2 14:46:21 2010
@@ -651,23 +651,39 @@ public class StringUtil {
     }
 
     public static StringBuilder appendTo(StringBuilder sb, Iterable<? extends Appender<StringBuilder>> iterable, String prefix, String suffix, String sep) {
+        return appendTo(sb, iterable, prefix, suffix, null, sep, null);
+    }
+
+    public static StringBuilder appendTo(StringBuilder sb, Iterable<? extends Appender<StringBuilder>> iterable, String prefix, String suffix, String sepPrefix, String sep, String sepSuffix) {
         Iterator<? extends Appender<StringBuilder>> it = iterable.iterator();
         while (it.hasNext()) {
             if (prefix != null) sb.append(prefix);
             it.next().appendTo(sb);
             if (suffix != null) sb.append(suffix);
-            if (it.hasNext() && sep != null) sb.append(sep);
+            if (it.hasNext() && sep != null) {
+                if (sepPrefix != null) sb.append(sepPrefix);
+                sb.append(sep);
+                if (sepSuffix != null) sb.append(sepSuffix);
+            }
         }
         return sb;
     }
 
     public static StringBuilder append(StringBuilder sb, Iterable<? extends Object> iterable, String prefix, String suffix, String sep) {
+        return append(sb, iterable, prefix, suffix, null, sep, null);
+    }
+
+    public static StringBuilder append(StringBuilder sb, Iterable<? extends Object> iterable, String prefix, String suffix, String sepPrefix, String sep, String sepSuffix) {
         Iterator<? extends Object> it = iterable.iterator();
         while (it.hasNext()) {
             if (prefix != null) sb.append(prefix);
             sb.append(it.next());
             if (suffix != null) sb.append(suffix);
-            if (it.hasNext() && sep != null) sb.append(sep);
+            if (it.hasNext() && sep != null) {
+                if (sepPrefix != null) sb.append(sepPrefix);
+                sb.append(sep);
+                if (sepSuffix != null) sb.append(sepSuffix);
+            }
         }
         return sb;
     }

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/StringUtilTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/StringUtilTests.java?rev=981533&r1=981532&r2=981533&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/StringUtilTests.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/StringUtilTests.java Mon Aug  2 14:46:21 2010
@@ -338,6 +338,12 @@ public class StringUtilTests extends Gen
         assertEquals("011", "1],2]", StringUtil.appendTo(new StringBuilder(), list(new TestAppender("1"), new TestAppender("2")), null, "]", ",").toString());
         assertEquals("101", "[1,[2", StringUtil.appendTo(new StringBuilder(), list(new TestAppender("1"), new TestAppender("2")), "[", null, ",").toString());
         assertEquals("110", "[1][2]", StringUtil.appendTo(new StringBuilder(), list(new TestAppender("1"), new TestAppender("2")), "[", "]", null).toString());
+        assertEquals("11111", "[1]<,>[2]", StringUtil.appendTo(new StringBuilder(), list(new TestAppender("1"), new TestAppender("2")), "[", "]", "<", ",", ">").toString());
+        assertEquals("01111", "1]<,>2]", StringUtil.appendTo(new StringBuilder(), list(new TestAppender("1"), new TestAppender("2")), null, "]", "<", ",", ">").toString());
+        assertEquals("10111", "[1<,>[2", StringUtil.appendTo(new StringBuilder(), list(new TestAppender("1"), new TestAppender("2")), "[", null, "<", ",", ">").toString());
+        assertEquals("11011", "[1],>[2]", StringUtil.appendTo(new StringBuilder(), list(new TestAppender("1"), new TestAppender("2")), "[", "]", null, ",", ">").toString());
+        assertEquals("11101", "[1][2]", StringUtil.appendTo(new StringBuilder(), list(new TestAppender("1"), new TestAppender("2")), "[", "]", "<", null, ">").toString());
+        assertEquals("11110", "[1]<,[2]", StringUtil.appendTo(new StringBuilder(), list(new TestAppender("1"), new TestAppender("2")), "[", "]", "<", ",", null).toString());
     }
 
     public void testAppend() {
@@ -345,5 +351,11 @@ public class StringUtilTests extends Gen
         assertEquals("011", "1],2]", StringUtil.append(new StringBuilder(), list("1", "2"), null, "]", ",").toString());
         assertEquals("101", "[1,[2", StringUtil.append(new StringBuilder(), list("1", "2"), "[", null, ",").toString());
         assertEquals("110", "[1][2]", StringUtil.append(new StringBuilder(), list("1", "2"), "[", "]", null).toString());
+        assertEquals("11111", "[1]<,>[2]", StringUtil.append(new StringBuilder(), list("1", "2"), "[", "]", "<", ",", ">").toString());
+        assertEquals("01111", "1]<,>2]", StringUtil.append(new StringBuilder(), list("1", "2"), null, "]", "<", ",", ">").toString());
+        assertEquals("10111", "[1<,>[2", StringUtil.append(new StringBuilder(), list("1", "2"), "[", null, "<", ",", ">").toString());
+        assertEquals("11011", "[1],>[2]", StringUtil.append(new StringBuilder(), list("1", "2"), "[", "]", null, ",", ">").toString());
+        assertEquals("11101", "[1][2]", StringUtil.append(new StringBuilder(), list("1", "2"), "[", "]", "<", null, ">").toString());
+        assertEquals("11110", "[1]<,[2]", StringUtil.append(new StringBuilder(), list("1", "2"), "[", "]", "<", ",", null).toString());
     }
 }