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

Print this page
rev 7730 : 8020977: StringJoiner merges with itself not as expected
Reviewed-by:


 189      * Adds the contents of the given {@code StringJoiner} without prefix and
 190      * suffix as the next element if it is non-empty. If the given {@code
 191      * StringJoiner} is empty, the call has no effect.
 192      *
 193      * <p>A {@code StringJoiner} is empty if {@link #add(CharSequence) add()}
 194      * has never been called, and if {@code merge()} has never been called
 195      * with a non-empty {@code StringJoiner} argument.
 196      *
 197      * <p>If the other {@code StringJoiner} is using a different delimiter,
 198      * then elements from the other {@code StringJoiner} are concatenated with
 199      * that delimiter and the result is appended to this {@code StringJoiner}
 200      * as a single element.
 201      *
 202      * @param other The {@code StringJoiner} whose contents should be merged
 203      *              into this one
 204      * @throws NullPointerException if the other {@code StringJoiner} is null
 205      */
 206     public StringJoiner merge(StringJoiner other) {
 207         Objects.requireNonNull(other);
 208         if (other.value != null) {




 209             StringBuilder builder = prepareBuilder();
 210             StringBuilder otherBuilder = other.value;
 211             if (other.prefix.length() < otherBuilder.length()) {
 212                 builder.append(otherBuilder, other.prefix.length(), otherBuilder.length());
 213             }
 214         }
 215         return this;
 216     }
 217 
 218     private StringBuilder prepareBuilder() {
 219         if (value != null) {
 220             value.append(delimiter);
 221         } else {
 222             value = new StringBuilder().append(prefix);
 223         }
 224         return value;
 225     }
 226 
 227     /**
 228      * The length of the {@code StringJoiner} value, i.e. the length of
 229      * {@code String} representation of the {@code StringJoiner}. Note that if
 230      * no add methods have been called, then the length of the {@code String}
 231      * representation (either {@code prefix + suffix} or {@code emptyValue})
 232      * will be returned. The value should be equivalent to


 189      * Adds the contents of the given {@code StringJoiner} without prefix and
 190      * suffix as the next element if it is non-empty. If the given {@code
 191      * StringJoiner} is empty, the call has no effect.
 192      *
 193      * <p>A {@code StringJoiner} is empty if {@link #add(CharSequence) add()}
 194      * has never been called, and if {@code merge()} has never been called
 195      * with a non-empty {@code StringJoiner} argument.
 196      *
 197      * <p>If the other {@code StringJoiner} is using a different delimiter,
 198      * then elements from the other {@code StringJoiner} are concatenated with
 199      * that delimiter and the result is appended to this {@code StringJoiner}
 200      * as a single element.
 201      *
 202      * @param other The {@code StringJoiner} whose contents should be merged
 203      *              into this one
 204      * @throws NullPointerException if the other {@code StringJoiner} is null
 205      */
 206     public StringJoiner merge(StringJoiner other) {
 207         Objects.requireNonNull(other);
 208         if (other.value != null) {
 209             final StringBuilder otherBuilder = other.value;
 210             final int length = otherBuilder.length();
 211             // seize the data to be appended before actually copying to avoid
 212             // interference, especially when merge 'this'
 213             StringBuilder builder = prepareBuilder();
 214             if (other.prefix.length() < length) {
 215                 builder.append(otherBuilder, other.prefix.length(), length);

 216             }
 217         }
 218         return this;
 219     }
 220 
 221     private StringBuilder prepareBuilder() {
 222         if (value != null) {
 223             value.append(delimiter);
 224         } else {
 225             value = new StringBuilder().append(prefix);
 226         }
 227         return value;
 228     }
 229 
 230     /**
 231      * The length of the {@code StringJoiner} value, i.e. the length of
 232      * {@code String} representation of the {@code StringJoiner}. Note that if
 233      * no add methods have been called, then the length of the {@code String}
 234      * representation (either {@code prefix + suffix} or {@code emptyValue})
 235      * will be returned. The value should be equivalent to