8020977: StringJoiner merges with itself not as expected

Reviewed-by: psandoz, chegar, mduigou, smarks
This commit is contained in:
Henry Jen 2013-07-30 15:47:35 -07:00
parent b7c322c198
commit e8e95eec3a
2 changed files with 16 additions and 6 deletions

View File

@ -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;
}

View File

@ -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}");
}
}
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");
}
}