Author: bayard
Date: Tue Sep 19 15:55:00 2006
New Revision: 448006
URL: http://svn.apache.org/viewvc?view=rev&rev=448006
Log:
Appying the wished for join(Collection, x) method from Stepan Koltsov in
#LANG-266
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?view=diff&rev=448006&r1=448005&r2=448006
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
(original)
+++
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
Tue Sep 19 15:55:00 2006
@@ -17,6 +17,7 @@
package org.apache.commons.lang;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -2678,6 +2679,48 @@
}
}
return buf.toString();
+ }
+
+ /**
+ * <p>Joins the elements of the provided <code>Collection</code> into
+ * a single String containing the provided elements.</p>
+ *
+ * <p>No delimiter is added before or after the list. Null objects or empty
+ * strings within the iteration are represented by empty strings.</p>
+ *
+ * <p>See the examples here: [EMAIL PROTECTED] #join(Object[],char)}. </p>
+ *
+ * @param collection the <code>Collection</code> of values to join
together, may be null
+ * @param separator the separator character to use
+ * @return the joined String, <code>null</code> if null iterator input
+ * @since 2.3
+ */
+ public static String join(Collection collection, char separator) {
+ if (collection == null) {
+ return null;
+ }
+ return join(collection.iterator(), separator);
+ }
+
+ /**
+ * <p>Joins the elements of the provided <code>Collection</code> into
+ * a single String containing the provided elements.</p>
+ *
+ * <p>No delimiter is added before or after the list.
+ * A <code>null</code> separator is the same as an empty String ("").</p>
+ *
+ * <p>See the examples here: [EMAIL PROTECTED] #join(Object[],String)}.
</p>
+ *
+ * @param collection the <code>Collection</code> of values to join
together, may be null
+ * @param separator the separator character to use, null treated as ""
+ * @return the joined String, <code>null</code> if null iterator input
+ * @since 2.3
+ */
+ public static String join(Collection collection, String separator) {
+ if (collection == null) {
+ return null;
+ }
+ return join(collection.iterator(), separator);
}
// Delete
Modified:
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java?view=diff&rev=448006&r1=448005&r2=448006
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
(original)
+++
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
Tue Sep 19 15:55:00 2006
@@ -20,6 +20,8 @@
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Iterator;
+import java.util.Collection;
+import java.util.Collections;
import junit.framework.Test;
import junit.framework.TestCase;
@@ -243,13 +245,16 @@
assertEquals(TEXT_LIST_CHAR,
StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR_CHAR));
assertEquals("",
StringUtils.join(Arrays.asList(NULL_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
assertEquals("",
StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), SEPARATOR_CHAR));
+ assertEquals("foo",
StringUtils.join(Collections.singleton("foo").iterator(), 'x'));
}
public void testJoin_IteratorString() {
assertEquals(null, StringUtils.join((Iterator) null, null));
assertEquals(TEXT_LIST_NOSEP,
StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), null));
assertEquals(TEXT_LIST_NOSEP,
StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), ""));
-
+ assertEquals("foo",
StringUtils.join(Collections.singleton("foo").iterator(), "x"));
+ assertEquals("foo",
StringUtils.join(Collections.singleton("foo").iterator(), null));
+
assertEquals("",
StringUtils.join(Arrays.asList(NULL_ARRAY_LIST).iterator(), null));
assertEquals("",
StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), null));
@@ -258,7 +263,31 @@
assertEquals(TEXT_LIST,
StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR));
}
-
+
+ public void testJoin_CollectionChar() {
+ assertEquals(null, StringUtils.join((Collection) null, ','));
+ assertEquals(TEXT_LIST_CHAR,
StringUtils.join(Arrays.asList(ARRAY_LIST), SEPARATOR_CHAR));
+ assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST),
SEPARATOR_CHAR));
+ assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST),
SEPARATOR_CHAR));
+ assertEquals("foo", StringUtils.join(Collections.singleton("foo"),
'x'));
+ }
+
+ public void testJoin_CollectionString() {
+ assertEquals(null, StringUtils.join((Collection) null, null));
+ assertEquals(TEXT_LIST_NOSEP,
StringUtils.join(Arrays.asList(ARRAY_LIST), null));
+ assertEquals(TEXT_LIST_NOSEP,
StringUtils.join(Arrays.asList(ARRAY_LIST), ""));
+ assertEquals("foo", StringUtils.join(Collections.singleton("foo"),
"x"));
+ assertEquals("foo", StringUtils.join(Collections.singleton("foo"),
null));
+
+ assertEquals("", StringUtils.join(Arrays.asList(NULL_ARRAY_LIST),
null));
+
+ assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST),
null));
+ assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST),
""));
+ assertEquals("", StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST),
SEPARATOR));
+
+ assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST),
SEPARATOR));
+ }
+
public void testConcatenate_Objectarray() {
assertEquals(null, StringUtils.concatenate(null));
assertEquals("", StringUtils.concatenate(EMPTY_ARRAY_LIST));
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]