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 2021/01/06 16:01:19 UTC

[commons-lang] branch master updated: LANG-1636 - Boolean Join Function (#686)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 8dc37dc  LANG-1636 - Boolean Join Function (#686)
8dc37dc is described below

commit 8dc37dc4a7b516a1c70002081df13f0ad781ff3a
Author: Arturo Bernal <ar...@gmail.com>
AuthorDate: Wed Jan 6 17:01:13 2021 +0100

    LANG-1636 - Boolean Join Function (#686)
---
 .../java/org/apache/commons/lang3/StringUtils.java | 78 ++++++++++++++++++++++
 .../org/apache/commons/lang3/StringUtilsTest.java  | 19 ++++++
 2 files changed, 97 insertions(+)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 49ffda4..45ff9ce 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -3869,6 +3869,84 @@ public class StringUtils {
      * </p>
      *
      * <pre>
+     * StringUtils.join(null, *)              = null
+     * StringUtils.join([], *)                = ""
+     * StringUtils.join([null], *)            = ""
+     * StringUtils.join([false, false], ';')  = "false;false"
+     * </pre>
+     *
+     * @param array
+     *            the array of values to join together, may be null
+     * @param separator
+     *            the separator character to use
+     * @return the joined String, {@code null} if null array input
+     * @since 3.12
+     */
+    public static String join(final boolean[] array, final char separator) {
+        if (array == null) {
+            return null;
+        }
+        return join(array, separator, 0, array.length);
+    }
+
+    /**
+     * <p>
+     * Joins the elements of the provided array 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([true, false, true], ';')  = "true;false;true"
+     * </pre>
+     *
+     * @param array
+     *            the array 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 a start index past the end of the
+     *            array
+     * @param endIndex
+     *            the index to stop joining from (exclusive). It is an error to pass in an end index past the end of
+     *            the array
+     * @return the joined String, {@code null} if null array input
+     * @since 3.12
+     */
+    public static String join(final boolean[] array, final char separator, final int startIndex, final int endIndex) {
+        if (array == null) {
+            return null;
+        }
+        final int noOfItems = endIndex - startIndex;
+        if (noOfItems <= 0) {
+            return EMPTY;
+        }
+        final StringBuilder buf = newStringBuilder(noOfItems);
+        buf.append(array[startIndex]);
+        for (int i = startIndex + 1; i < endIndex; i++) {
+            buf.append(separator);
+            buf.append(array[i]);
+        }
+        return buf.toString();
+    }
+
+    /**
+     * <p>
+     * Joins the elements of the provided array 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], *)             = ""
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 0ecf3f0..6182c2c 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -114,6 +114,7 @@ public class StringUtilsTest {
 
     private static final String SEPARATOR = ",";
     private static final char SEPARATOR_CHAR = ';';
+    private static final char COMMA_SEPARATOR_CHAR = ',';
 
     private static final String TEXT_LIST = "foo,bar,baz";
     private static final String TEXT_LIST_CHAR = "foo;bar;baz";
@@ -125,6 +126,11 @@ public class StringUtilsTest {
     private static final String SENTENCE_UNCAP = "foo bar baz";
     private static final String SENTENCE_CAP = "Foo Bar Baz";
 
+    private static final boolean[] EMPTY = {};
+    private static final boolean[] ARRAY_FALSE_FALSE = {false, false};
+    private static final boolean[] ARRAY_FALSE_TRUE = {false, true};
+    private static final boolean[] ARRAY_FALSE_TRUE_FALSE = {false, true, false};
+
     private void assertAbbreviateWithAbbrevMarkerAndOffset(final String expected, final String abbrevMarker, final int offset, final int maxWidth) {
         final String abcdefghijklmno = "abcdefghijklmno";
         final String message = "abbreviate(String,String,int,int) failed";
@@ -1155,6 +1161,19 @@ public class StringUtilsTest {
         assertEquals(StringUtils.EMPTY, StringUtils.join(BYTE_PRIM_LIST, SEPARATOR_CHAR, 1, 0));
     }
 
+
+    @Test
+    public void testJoin_ArrayOfBooleans() {
+        assertNull(StringUtils.join((boolean[]) null, COMMA_SEPARATOR_CHAR));
+        assertEquals("false;false", StringUtils.join(ARRAY_FALSE_FALSE, SEPARATOR_CHAR));
+        assertEquals("", StringUtils.join(EMPTY, SEPARATOR_CHAR));
+        assertEquals("false,true,false", StringUtils.join(ARRAY_FALSE_TRUE_FALSE, COMMA_SEPARATOR_CHAR));
+        assertEquals("true", StringUtils.join(ARRAY_FALSE_TRUE, SEPARATOR_CHAR, 1, 2));
+        assertNull(StringUtils.join((boolean[]) null, SEPARATOR_CHAR, 0, 1));
+        assertEquals(StringUtils.EMPTY, StringUtils.join(ARRAY_FALSE_FALSE, SEPARATOR_CHAR, 0, 0));
+        assertEquals(StringUtils.EMPTY, StringUtils.join(ARRAY_FALSE_TRUE_FALSE, SEPARATOR_CHAR, 1, 0));
+    }
+
     @Test
     public void testJoin_ArrayOfChars() {
         assertNull(StringUtils.join((char[]) null, ','));