You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2018/05/22 14:41:00 UTC

[lang] [LANG-1290] StringUtils.join() with support for List with configurable start/end indices.

Repository: commons-lang
Updated Branches:
  refs/heads/master 09ef69c5b -> 87937b2e7


[LANG-1290] StringUtils.join() with support for List<?> with
configurable start/end indices.

Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/87937b2e
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/87937b2e
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/87937b2e

Branch: refs/heads/master
Commit: 87937b2e7192524306aa58d74c90537eec997cfa
Parents: 09ef69c
Author: Jochen Schalanda <jo...@schalanda.name>
Authored: Tue May 22 08:40:55 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Tue May 22 08:40:55 2018 -0600

----------------------------------------------------------------------
 .../org/apache/commons/lang3/StringUtils.java   | 76 ++++++++++++++++++++
 .../apache/commons/lang3/StringUtilsTest.java   | 38 ++++++++++
 2 files changed, 114 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/87937b2e/src/main/java/org/apache/commons/lang3/StringUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 495e4ec..9387376 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -4722,6 +4722,82 @@ public class StringUtils {
     }
 
     /**
+     * <p>Joins the elements of the provided {@code List} into a single String
+     * containing the provided list of elements.</p>
+     *
+     * <p>No delimiter is added before or after the list.
+     * Null objects or empty strings within the array are represented by
+     * empty strings.</p>
+     *
+     * <pre>
+     * 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"
+     * </pre>
+     *
+     * @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);
+    }
+
+    /**
+     * <p>Joins the elements of the provided {@code List} into a single String
+     * containing the provided list of elements.</p>
+     *
+     * <p>No delimiter is added before or after the list.
+     * Null objects or empty strings within the array are represented by
+     * empty strings.</p>
+     *
+     * <pre>
+     * 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"
+     * </pre>
+     *
+     * @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);
+    }
+
+    /**
      * <p>Joins the elements of the provided varargs into a
      * single String containing the provided elements.</p>
      *

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/87937b2e/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index c4299b8..ee3beeb 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.nio.charset.Charset;
 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 class StringUtilsTest {
     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<String> MIXED_STRING_LIST = Arrays.asList(null, "", "foo");
+    private static final List<Object> MIXED_TYPE_OBJECT_LIST = Arrays.<Object>asList("foo", Long.valueOf(2L));
+    private static final List<String> STRING_LIST = Arrays.asList("foo", "bar", "baz");
+    private static final List<String> EMPTY_STRING_LIST = Collections.emptyList();
+    private static final List<String> NULL_STRING_LIST = Collections.singletonList(null);
 
     private static final String SEPARATOR = ",";
     private static final char SEPARATOR_CHAR = ';';
@@ -367,6 +373,38 @@ public class StringUtilsTest {
     }
 
     @Test
+    public void testJoin_List() {
+        assertNull(StringUtils.join((List<String>) 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, ','));
         assertEquals(TEXT_LIST_CHAR, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR_CHAR));