提交 6b6e7b8b 编写于 作者: H henryjen

8020977: StringJoiner merges with itself not as expected

Reviewed-by: psandoz, chegar, mduigou, smarks
上级 0cc1d3f3
......@@ -206,11 +206,12 @@ public final class StringJoiner {
public StringJoiner merge(StringJoiner other) {
Objects.requireNonNull(other);
if (other.value != null) {
final int length = other.value.length();
// lock the length so that we can seize the data to be appended
// before initiate copying to avoid interference, especially when
// merge 'this'
StringBuilder builder = prepareBuilder();
StringBuilder otherBuilder = other.value;
if (other.prefix.length() < otherBuilder.length()) {
builder.append(otherBuilder, other.prefix.length(), otherBuilder.length());
}
builder.append(other.value, other.prefix.length(), length);
}
return this;
}
......
......@@ -23,7 +23,7 @@
/**
* @test
* @bug 8017231
* @bug 8017231 8020977
* @summary test StringJoiner::merge
* @run testng MergeTest
*/
......@@ -121,4 +121,13 @@ public class MergeTest {
sj.merge(other);
assertEquals(sj.toString(), "{a,b,c,d:e:f}");
}
}
\ No newline at end of file
public void testMergeSelf() {
final StringJoiner sj = new StringJoiner(",", "[", "]").add("a").add("b");
assertEquals(sj.merge(sj).toString(), "[a,b,a,b]");
assertEquals(sj.merge(sj).toString(), "[a,b,a,b,a,b,a,b]");
final StringJoiner sj2 = new StringJoiner(",").add("c").add("d");
assertEquals(sj2.merge(sj2).toString(), "c,d,c,d");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册