diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index b716711c065..49a979a8a65 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -188,6 +188,11 @@ public class StringUtils { */ private static final Pattern STRIP_ACCENTS_PATTERN = Pattern.compile("\\p{InCombiningDiacriticalMarks}+"); //$NON-NLS-1$ + /** + * The filter mode for join operations. + */ + private enum JoinFilterMode { NOT_EMPTY, NOT_BLANK } + /** *
Abbreviates a String using ellipses. This will turn * "Now is the time for all good men" into "Now is the time for..."
@@ -4887,6 +4892,97 @@ public static String joinWith(final String delimiter, final Object... array) { return join(array, delimiter); } + /** + *Joins the elements of the provided varargs into a + * single String containing the filtered provided elements.
+ * + *Element that do not match the filter criteria is not added. + * No delimiter is added between elements when at least one of them is filtered. + * No delimiter is added before or after the list. + * {@code null} elements and separator are treated as empty Strings ("").
+ * + * @param filterMode the filter mode, must not be null + * @param delimiter the separator character to use, null treated as "" + * @param array the varargs providing the values to join together. {@code null} elements are treated as "" + * @return the joined String. + * @throws java.lang.IllegalArgumentException if filterMode is null + * @throws java.lang.IllegalArgumentException if a null varargs is provided + * @since ??? + */ + private static String joinWith(final JoinFilterMode filterMode, final String delimiter, final Object... array) { + if (filterMode == null) { + throw new IllegalArgumentException("Filter mode must not be null"); + } + + if (array == null) { + throw new IllegalArgumentException("Object varargs must not be null"); + } + + final StringJoiner joiner = new StringJoiner(toStringOrEmpty(delimiter)); + for (Object o : array) { + String s = toStringOrEmpty(o); + if (filterMode == JoinFilterMode.NOT_EMPTY && isNotEmpty(s) + || filterMode == JoinFilterMode.NOT_BLANK && isNotBlank(s)) { + joiner.add(s); + } + } + return joiner.toString(); + } + + /** + *Joins the elements of the provided varargs into a + * single String containing the filtered provided elements.
+ * + *Elements for which the result of isNotEmpty() is false are skipped. + * No delimiter is added between elements when at least one of them is filtered. + * No delimiter is added before or after the list. + * {@code null} elements and separator are treated as empty Strings ("").
+ * + *+ * StringUtils.joinNotEmptyWith(",", {"a", "b"}) = "a,b" + * StringUtils.joinNotEmptyWith(",", {"a", "b", " "}) = "a,b, " + * StringUtils.joinNotEmptyWith(",", {"", "a", null, "b"}) = "a,b" + * StringUtils.joinNotEmptyWith(null, {"a", "b"}) = "ab" + * StringUtils.joinNotEmptyWith(null, {null, "a", " ", "b"}) = "a b" + *+ * + * @param delimiter the separator character to use, null treated as "" + * @param array the varargs providing the values to join together. {@code null} elements are treated as "" + * @return the joined String. + * @throws java.lang.IllegalArgumentException if a null varargs is provided + * @since ??? + */ + public static String joinNotEmptyWith(final String delimiter, final Object... array) { + return joinWith(JoinFilterMode.NOT_EMPTY, delimiter, array); + } + + /** + *
Joins the elements of the provided varargs into a + * single String containing the filtered provided elements.
+ * + *Elements for which the result of isNotBlank() is false are skipped. + * No delimiter is added between elements when at least one of them is filtered. + * No delimiter is added before or after the list. + * {@code null} elements and separator are treated as empty Strings ("").
+ * + *+ * StringUtils.joinNotBlankWith(",", {"a", "b"}) = "a,b" + * StringUtils.joinNotBlankWith(",", {"a", "b", " "}) = "a,b" + * StringUtils.joinNotBlankWith(",", {"", "a", null, "b"}) = "a,b" + * StringUtils.joinNotBlankWith(null, {"a", "b"}) = "ab" + * StringUtils.joinNotBlankWith(null, {null, "a", " ", "b"}) = "ab" + *+ * + * @param delimiter the separator character to use, null treated as "" + * @param array the varargs providing the values to join together. {@code null} elements are treated as "" + * @return the joined String. + * @throws java.lang.IllegalArgumentException if a null varargs is provided + * @since ??? + */ + public static String joinNotBlankWith(final String delimiter, final Object... array) { + return joinWith(JoinFilterMode.NOT_BLANK, delimiter, array); + } + /** *
Finds the last index within a CharSequence, handling {@code null}. * This method uses {@link String#lastIndexOf(String)} if possible.
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 5750f8b0185..3d20f27d8ef 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -1384,6 +1384,32 @@ public void testJoinWith() { assertEquals("ab", StringUtils.joinWith(null, "a", "b")); } + @Test + public void testJoinNotEmptyWith() { + assertEquals("", StringUtils.joinNotEmptyWith(",")); // empty array + assertEquals("", StringUtils.joinNotEmptyWith(",", (Object[]) NULL_ARRAY_LIST)); + assertEquals("", StringUtils.joinNotEmptyWith(",", NULL_TO_STRING_LIST)); + + assertEquals("a,b", StringUtils.joinNotEmptyWith(",", "a", "b")); + assertEquals("a,b, ", StringUtils.joinNotEmptyWith(",", "a", "b", " ")); + assertEquals("a,b", StringUtils.joinNotEmptyWith(",", "", "a", null, "b")); + assertEquals("ab", StringUtils.joinNotEmptyWith(null, "a", "b")); + assertEquals("a b", StringUtils.joinNotEmptyWith(null, "a", " ", "b")); + } + + @Test + public void testJoinNotBlankWith() { + assertEquals("", StringUtils.joinNotBlankWith(",")); // empty array + assertEquals("", StringUtils.joinNotBlankWith(",", (Object[]) NULL_ARRAY_LIST)); + assertEquals("", StringUtils.joinNotBlankWith(",", NULL_TO_STRING_LIST)); + + assertEquals("a,b", StringUtils.joinNotBlankWith(",", "a", "b")); + assertEquals("a,b", StringUtils.joinNotBlankWith(",", "a", "b", " ")); + assertEquals("a,b", StringUtils.joinNotBlankWith(",", "", "a", null, "b")); + assertEquals("ab", StringUtils.joinNotBlankWith(null, "a", "b")); + assertEquals("ab", StringUtils.joinNotBlankWith(null, "a", " ", "b")); + } + @Test public void testJoinWithThrowsException() { assertThrows(IllegalArgumentException.class, () -> StringUtils.joinWith(",", (Object[]) null));