diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index a190d36e2..464fcd367 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -77,7 +77,7 @@ * @author Alexander Day Chaffee * @author Henning P. Schmiedehausen * @since 1.0 - * @version $Id: StringUtils.java,v 1.34 2003/03/23 04:58:47 bayard Exp $ + * @version $Id: StringUtils.java,v 1.35 2003/03/23 05:26:23 bayard Exp $ */ public class StringUtils { @@ -523,7 +523,7 @@ public static String[] split(String str, String separator, int max) { * @return the concatenated string. */ public static String concatenate(Object[] array) { - return join(array, ""); + return join(array, null); } /** @@ -538,12 +538,40 @@ public static String concatenate(Object[] array) { * @return the joined String */ public static String join(Object[] array, String separator) { - if (separator == null) { - separator = ""; - } int arraySize = array.length; - int bufSize = (arraySize == 0 ? 0 : (array[0].toString().length() + - separator.length()) * arraySize); + + // ArraySize == 0: Len = 0 + // ArraySize > 0: Len = NofStrings *(len(firstString) + len(separator)) + // (Assuming that all strings are roughly equally long) + int bufSize + = ((arraySize == 0) ? 0 + : arraySize * (array[0].toString().length() + + ((separator != null) ? separator.length(): 0))); + + StringBuffer buf = new StringBuffer(bufSize); + + for (int i = 0; i < arraySize; i++) { + if ((separator != null) && (i > 0)) { + buf.append(separator); + } + buf.append(array[i]); + } + return buf.toString(); + } + + /** + *

Joins the elements of the provided array into a single String + * containing the provided list of elements.

+ * + *

No delimiter is added before or after the list. A + * + * @param array the array of values to join together + * @param separator the separator character to use + * @return the joined String + */ + public static String join(Object[] array, char separator) { + int arraySize = array.length; + int bufSize = (arraySize == 0 ? 0 : (array[0].toString().length() + 1) * arraySize); StringBuffer buf = new StringBuffer(bufSize); for (int i = 0; i < arraySize; i++) { @@ -567,9 +595,27 @@ public static String join(Object[] array, String separator) { * @return the joined String */ public static String join(Iterator iterator, String separator) { - if (separator == null) { - separator = ""; - } + StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small + while (iterator.hasNext()) { + buf.append(iterator.next()); + if ((separator != null) && iterator.hasNext()) { + buf.append(separator); + } + } + return buf.toString(); + } + + /** + *

Joins the elements of the provided Iterator into + * a single String containing the provided elements.

+ * + *

No delimiter is added before or after the list. A + * + * @param iterator the Iterator of values to join together + * @param separator the separator character to use + * @return the joined String + */ + public static String join(Iterator iterator, char separator) { StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small while (iterator.hasNext()) { buf.append(iterator.next()); diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index ec83385ec..0121fb0df 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -68,15 +68,20 @@ * @author Ringo De Smet * @author Henning P. Schmiedehausen + * @version $Id: StringUtilsTest.java,v 1.15 2003/03/23 05:26:23 bayard Exp $ */ public class StringUtilsTest extends TestCase { private static final String[] ARRAY_LIST = { "foo", "bar", "baz" }; + private static final String[] EMPTY_ARRAY_LIST = {}; private static final String SEPARATOR = ","; + private static final char SEPARATOR_CHAR = ';'; private static final String TEXT_LIST = "foo,bar,baz"; + private static final String TEXT_LIST_CHAR = "foo;bar;baz"; + private static final String TEXT_LIST_NOSEP = "foobarbaz"; private static final String FOO = "foo"; private static final String BAR = "bar"; @@ -149,12 +154,44 @@ public void testCaseFunctions() { public void testJoin() { assertEquals("concatenate(Object[]) failed", - "foobarbaz", StringUtils.concatenate(ARRAY_LIST)); + TEXT_LIST_NOSEP, StringUtils.concatenate(ARRAY_LIST)); assertEquals("join(Object[], String) failed", TEXT_LIST, StringUtils.join(ARRAY_LIST, SEPARATOR)); assertEquals("join(Iterator, String) failed", TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR)); + + assertEquals("join(Object[], char) failed", TEXT_LIST_CHAR, + StringUtils.join(ARRAY_LIST, SEPARATOR_CHAR)); + assertEquals("join(Iterator, char) failed", TEXT_LIST_CHAR, + StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), + SEPARATOR_CHAR)); + + assertEquals("join(Object[], null) failed", TEXT_LIST_NOSEP, + StringUtils.join(ARRAY_LIST, null)); + assertEquals("join(Iterator, null) failed", TEXT_LIST_NOSEP, + StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), + null)); + + assertEquals("concatenate(Object[]) failed", + "", StringUtils.concatenate(EMPTY_ARRAY_LIST)); + assertEquals("join(Object[], String) failed", "", + StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR)); + assertEquals("join(Iterator, String) failed", "", + StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), + SEPARATOR)); + + assertEquals("join(Object[], char) failed", "", + StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR_CHAR)); + assertEquals("join(Iterator, char) failed", "", + StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), + SEPARATOR_CHAR)); + + assertEquals("join(Object[], null) failed", "", + StringUtils.join(EMPTY_ARRAY_LIST, null)); + assertEquals("join(Iterator, null) failed", "", + StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(), + null)); } public void testSplit() {