src/share/classes/java/util/StringJoiner.java

Print this page
rev 7571 : 8017231: Add StringJoiner.merge
Reviewed-by:
Contributed-by: brian.goetz@oracle.com, henry.jen@oracle.com

@@ -112,11 +112,12 @@
      * @param  prefix the sequence of characters to be used at the beginning
      * @param  suffix the sequence of characters to be used at the end
      * @throws NullPointerException if {@code prefix}, {@code delimiter}, or
      *         {@code suffix} is {@code null}
      */
-    public StringJoiner(CharSequence delimiter, CharSequence prefix,
+    public StringJoiner(CharSequence delimiter,
+                        CharSequence prefix,
             CharSequence suffix) {
         Objects.requireNonNull(prefix, "The prefix must not be null");
         Objects.requireNonNull(delimiter, "The delimiter must not be null");
         Objects.requireNonNull(suffix, "The suffix must not be null");
         // make defensive copies of arguments

@@ -170,11 +171,11 @@
             }
         }
     }
 
     /**
-     * Add the a copy of the supplied {@code CharSequence} value as the next
+     * Adds a copy of the given {@code CharSequence} value as the next
      * element of the {@code StringJoiner} value. If {@code newElement} is
      * {@code null}, then {@code "null"} is added.
      *
      * @param  newElement The element to add
      * @return a reference to this {@code StringJoiner}

@@ -182,10 +183,40 @@
     public StringJoiner add(CharSequence newElement) {
         prepareBuilder().append(newElement);
         return this;
     }
 
+    /**
+     * Adds the contents of the given {@code StringJoiner} without prefix and
+     * suffix as the next element if it is non-empty. If the given {@code
+     * StringJoiner} is empty, the call has no effect.
+     *
+     * <p>A {@code StringJoiner} is empty if {@link #add(CharSequence) add()}
+     * has never been called, and if {@code merge()} has never been called
+     * with a non-empty {@code StringJoiner} argument.
+     *
+     * <p>If the other {@code StringJoiner} is using a different delimiter,
+     * then elements from the other {@code StringJoiner} are concatenated with
+     * that delimiter and the result is appended to this {@code StringJoiner}
+     * as a single element.
+     *
+     * @param other The {@code StringJoiner} whose contents should be merged
+     *              into this one
+     * @throws NullPointerException if the other {@code StringJoiner} is null
+     */
+    public StringJoiner merge(StringJoiner other) {
+        Objects.requireNonNull(other);
+        if (other.value != null) {
+            StringBuilder builder = prepareBuilder();
+            StringBuilder otherBuilder = other.value;
+            if (other.prefix.length() < otherBuilder.length()) {
+                builder.append(otherBuilder, other.prefix.length(), otherBuilder.length());
+            }
+        }
+        return this;
+    }
+
     private StringBuilder prepareBuilder() {
         if (value != null) {
             value.append(delimiter);
         } else {
             value = new StringBuilder().append(prefix);