From 87937b2e7192524306aa58d74c90537eec997cfa Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Tue, 22 May 2018 08:40:55 -0600 Subject: [PATCH] [LANG-1290] StringUtils.join() with support for List with configurable start/end indices. --- .../org/apache/commons/lang3/StringUtils.java | 76 +++++++++++++++++++ .../apache/commons/lang3/StringUtilsTest.java | 38 ++++++++++ 2 files changed, 114 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 495e4ec8ebf..9387376c7b9 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -4721,6 +4721,82 @@ public static String join(final Iterable iterable, final String separator) { return join(iterable.iterator(), separator); } + /** + *

Joins the elements of the provided {@code List} into a single String + * containing the provided list of elements.

+ * + *

No delimiter is added before or after the list. + * Null objects or empty strings within the array are represented by + * empty strings.

+ * + *
+     * StringUtils.join(null, *)               = null
+     * StringUtils.join([], *)                 = ""
+     * StringUtils.join([null], *)             = ""
+     * StringUtils.join(["a", "b", "c"], ';')  = "a;b;c"
+     * StringUtils.join(["a", "b", "c"], null) = "abc"
+     * StringUtils.join([null, "", "a"], ';')  = ";;a"
+     * 
+ * + * @param list the {@code List} of values to join together, may be null + * @param separator the separator character to use + * @param startIndex the first index to start joining from. It is + * an error to pass in an end index past the end of the list + * @param endIndex the index to stop joining from (exclusive). It is + * an error to pass in an end index past the end of the list + * @return the joined String, {@code null} if null list input + * @since 3.8 + */ + public static String join(final List list, final char separator, final int startIndex, final int endIndex) { + if (list == null) { + return null; + } + final int noOfItems = endIndex - startIndex; + if (noOfItems <= 0) { + return EMPTY; + } + final List subList = list.subList(startIndex, endIndex); + return join(subList.iterator(), separator); + } + + /** + *

Joins the elements of the provided {@code List} into a single String + * containing the provided list of elements.

+ * + *

No delimiter is added before or after the list. + * Null objects or empty strings within the array are represented by + * empty strings.

+ * + *
+     * StringUtils.join(null, *)               = null
+     * StringUtils.join([], *)                 = ""
+     * StringUtils.join([null], *)             = ""
+     * StringUtils.join(["a", "b", "c"], ';')  = "a;b;c"
+     * StringUtils.join(["a", "b", "c"], null) = "abc"
+     * StringUtils.join([null, "", "a"], ';')  = ";;a"
+     * 
+ * + * @param list the {@code List} of values to join together, may be null + * @param separator the separator character to use + * @param startIndex the first index to start joining from. It is + * an error to pass in an end index past the end of the list + * @param endIndex the index to stop joining from (exclusive). It is + * an error to pass in an end index past the end of the list + * @return the joined String, {@code null} if null list input + * @since 3.8 + */ + public static String join(final List list, final String separator, final int startIndex, final int endIndex) { + if (list == null) { + return null; + } + final int noOfItems = endIndex - startIndex; + if (noOfItems <= 0) { + return EMPTY; + } + final List subList = list.subList(startIndex, endIndex); + return join(subList.iterator(), separator); + } + /** *

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

diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index c4299b8d1e3..ee3beeb656e 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -34,6 +34,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Iterator; +import java.util.List; import java.util.Locale; import java.util.Objects; import java.util.regex.PatternSyntaxException; @@ -100,6 +101,11 @@ public String toString() { private static final char[] CHAR_PRIM_LIST = {'1', '2'}; private static final float[] FLOAT_PRIM_LIST = {1, 2}; private static final double[] DOUBLE_PRIM_LIST = {1, 2}; + private static final List MIXED_STRING_LIST = Arrays.asList(null, "", "foo"); + private static final List MIXED_TYPE_OBJECT_LIST = Arrays.asList("foo", Long.valueOf(2L)); + private static final List STRING_LIST = Arrays.asList("foo", "bar", "baz"); + private static final List EMPTY_STRING_LIST = Collections.emptyList(); + private static final List NULL_STRING_LIST = Collections.singletonList(null); private static final String SEPARATOR = ","; private static final char SEPARATOR_CHAR = ';'; @@ -366,6 +372,38 @@ public void testJoin_ArrayString() { assertEquals("", StringUtils.join(MIXED_TYPE_LIST, "/", 2, 1)); } + @Test + public void testJoin_List() { + assertNull(StringUtils.join((List) null, null)); + assertEquals(TEXT_LIST_NOSEP, StringUtils.join(STRING_LIST, null)); + assertEquals(TEXT_LIST_NOSEP, StringUtils.join(STRING_LIST, "")); + + assertEquals("", StringUtils.join(NULL_STRING_LIST, null)); + + assertEquals("", StringUtils.join(EMPTY_STRING_LIST, null)); + assertEquals("", StringUtils.join(EMPTY_STRING_LIST, "")); + assertEquals("", StringUtils.join(EMPTY_STRING_LIST, SEPARATOR)); + + assertEquals(TEXT_LIST, StringUtils.join(STRING_LIST, SEPARATOR)); + assertEquals(",,foo", StringUtils.join(MIXED_STRING_LIST, SEPARATOR)); + assertEquals("foo,2", StringUtils.join(MIXED_TYPE_OBJECT_LIST, SEPARATOR)); + + assertEquals("/", StringUtils.join(MIXED_STRING_LIST, "/", 0, MIXED_STRING_LIST.size() - 1)); + assertEquals("", StringUtils.join(MIXED_STRING_LIST, "", 0, MIXED_STRING_LIST.size()- 1)); + assertEquals("foo", StringUtils.join(MIXED_TYPE_OBJECT_LIST, "/", 0, 1)); + assertEquals("foo/2", StringUtils.join(MIXED_TYPE_OBJECT_LIST, "/", 0, 2)); + assertEquals("2", StringUtils.join(MIXED_TYPE_OBJECT_LIST, "/", 1, 2)); + assertEquals("", StringUtils.join(MIXED_TYPE_OBJECT_LIST, "/", 2, 1)); + assertNull(null, StringUtils.join((List) null, "/", 0, 1)); + + assertEquals("/", StringUtils.join(MIXED_STRING_LIST, '/', 0, MIXED_STRING_LIST.size() - 1)); + assertEquals("foo", StringUtils.join(MIXED_TYPE_OBJECT_LIST, '/', 0, 1)); + assertEquals("foo/2", StringUtils.join(MIXED_TYPE_OBJECT_LIST, '/', 0, 2)); + assertEquals("2", StringUtils.join(MIXED_TYPE_OBJECT_LIST, '/', 1, 2)); + assertEquals("", StringUtils.join(MIXED_TYPE_OBJECT_LIST, '/', 2, 1)); + assertNull(null, StringUtils.join((List) null, '/', 0, 1)); + } + @Test public void testJoin_IteratorChar() { assertNull(StringUtils.join((Iterator) null, ','));