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,122 **** * @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, 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 --- 112,123 ---- * @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, 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,180 **** } } } /** ! * Add the a copy of the supplied {@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} --- 171,181 ---- } } } /** ! * Adds a copy of the supplied {@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,191 **** --- 183,221 ---- public StringJoiner add(CharSequence newElement) { prepareBuilder().append(newElement); return this; } + /** + * Adds the contents of the supplied {@code StringJoiner} without prefix + * and suffix as the next element if it is nonempty. If the supplied + * {@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, + * 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 otherBuffer = other.value; + for (int i=other.prefix.length(); i < otherBuffer.length(); i++) + builder.append(otherBuffer.charAt(i)); + } + return this; + } + private StringBuilder prepareBuilder() { if (value != null) { value.append(delimiter); } else { value = new StringBuilder().append(prefix);