8294397: Replace StringBuffer with StringBuilder within java.text

Reviewed-by: lancea, naoto, bchristi
This commit is contained in:
Justin Lu 2022-10-05 18:42:00 +00:00 committed by Naoto Sato
parent f2c57186a4
commit 87acfee3c3
7 changed files with 55 additions and 80 deletions
src/java.base/share/classes/java/text
test/jdk/sun/text/IntHashtable

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -776,7 +776,7 @@ public final class CollationElementIterator
private NormalizerBase text = null;
private int[] buffer = null;
private int expIndex = 0;
private StringBuffer key = new StringBuffer(5);
private StringBuilder key = new StringBuilder(5);
private int swapOrder = 0;
private RBCollationTables ordering;
private RuleBasedCollator owner;

@ -161,12 +161,12 @@ final class DigitList implements Cloneable {
return 0.0;
}
StringBuffer temp = getStringBuffer();
temp.append('.');
temp.append(digits, 0, count);
temp.append('E');
temp.append(decimalAt);
return Double.parseDouble(temp.toString());
return Double.parseDouble(getStringBuilder()
.append('.')
.append(digits, 0, count)
.append('E')
.append(decimalAt)
.toString());
}
/**
@ -187,7 +187,7 @@ final class DigitList implements Cloneable {
return Long.MIN_VALUE;
}
StringBuffer temp = getStringBuffer();
StringBuilder temp = getStringBuilder();
temp.append(digits, 0, count);
for (int i = count; i < decimalAt; ++i) {
temp.append('0');
@ -736,7 +736,7 @@ final class DigitList implements Cloneable {
char[] newDigits = new char[digits.length];
System.arraycopy(digits, 0, newDigits, 0, digits.length);
other.digits = newDigits;
other.tempBuffer = null;
other.tempBuilder = null;
return other;
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
@ -788,23 +788,24 @@ final class DigitList implements Cloneable {
if (isZero()) {
return "0";
}
StringBuffer buf = getStringBuffer();
buf.append("0.");
buf.append(digits, 0, count);
buf.append("x10^");
buf.append(decimalAt);
return buf.toString();
return getStringBuilder()
.append("0.")
.append(digits, 0, count)
.append("x10^")
.append(decimalAt)
.toString();
}
private StringBuffer tempBuffer;
private StringBuilder tempBuilder;
private StringBuffer getStringBuffer() {
if (tempBuffer == null) {
tempBuffer = new StringBuffer(MAX_COUNT);
private StringBuilder getStringBuilder() {
if (tempBuilder == null) {
tempBuilder = new StringBuilder(MAX_COUNT);
} else {
tempBuffer.setLength(0);
tempBuilder.setLength(0);
}
return tempBuffer;
return tempBuilder;
}
private void extendDigits(int len) {

@ -101,18 +101,18 @@ final class MergeCollation {
PatternEntry last = findLastWithNoExtension(i-1);
for (int j = extList.size() - 1; j >= 0 ; j--) {
tmp = extList.get(j);
tmp.addToBuffer(result, false, withWhiteSpace, last);
tmp.addToBuilder(result, false, withWhiteSpace, last);
}
extList = null;
}
entry.addToBuffer(result, false, withWhiteSpace, null);
entry.addToBuilder(result, false, withWhiteSpace, null);
}
}
if (extList != null) {
PatternEntry last = findLastWithNoExtension(i-1);
for (int j = extList.size() - 1; j >= 0 ; j--) {
tmp = extList.get(j);
tmp.addToBuffer(result, false, withWhiteSpace, last);
tmp.addToBuilder(result, false, withWhiteSpace, last);
}
extList = null;
}
@ -151,7 +151,7 @@ final class MergeCollation {
{
PatternEntry entry = patterns.get(i);
if (entry != null) {
entry.addToBuffer(result, true, withWhiteSpace, null);
entry.addToBuilder(result, true, withWhiteSpace, null);
}
}
return result.toString();
@ -211,7 +211,7 @@ final class MergeCollation {
// This is really used as a local variable inside fixEntry, but we cache
// it here to avoid newing it up every time the method is called.
private transient StringBuffer excess = new StringBuffer();
private transient StringBuilder excess = new StringBuilder();
//
// When building a MergeCollation, we need to do lots of searches to see
@ -300,7 +300,7 @@ final class MergeCollation {
}
private final int findLastEntry(PatternEntry entry,
StringBuffer excessChars) throws ParseException
StringBuilder excessChars) throws ParseException
{
if (entry == null)
return 0;

@ -84,7 +84,7 @@ class PatternEntry {
*/
public String toString() {
StringBuilder result = new StringBuilder();
addToBuffer(result, true, false, null);
addToBuilder(result, true, false, null);
return result.toString();
}
@ -111,10 +111,10 @@ class PatternEntry {
// ===== privates =====
void addToBuffer(StringBuilder toAddTo,
boolean showExtension,
boolean showWhiteSpace,
PatternEntry lastEntry)
void addToBuilder(StringBuilder toAddTo,
boolean showExtension,
boolean showWhiteSpace,
PatternEntry lastEntry)
{
if (showWhiteSpace && toAddTo.length() > 0)
if (strength == Collator.PRIMARY || lastEntry != null)
@ -190,8 +190,8 @@ class PatternEntry {
//========================================================================
PatternEntry(int strength,
StringBuffer chars,
StringBuffer extension)
StringBuilder chars,
StringBuilder extension)
{
this.strength = strength;
this.chars = chars.toString();
@ -287,8 +287,8 @@ class PatternEntry {
}
// We re-use these objects in order to improve performance
private StringBuffer newChars = new StringBuffer();
private StringBuffer newExtension = new StringBuffer();
private StringBuilder newChars = new StringBuilder();
private StringBuilder newExtension = new StringBuilder();
}

@ -1,30 +0,0 @@
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4170614
* @summary Test internal hashCode() and equals() functions
* @library patch-src
* @build java.base/java.text.Bug4170614Test
* @run main java.base/java.text.Bug4170614Test
*/

@ -21,7 +21,7 @@
* questions.
*/
/**
/*
* @test
* @bug 4705389
* @summary Make sure to find removed slots, which test case will be timed out without the fix.

@ -21,11 +21,6 @@
* questions.
*/
/*
(this test doesn't have an at-test tag because it's run by Bug4170614TestRun.java
instead of directly by the test harness)
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
@ -61,13 +56,22 @@
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
/* @test
* @bug 4170614
* @summary Test internal hashCode() and equals() functions
* @library ../../../../patch-src
* @build java.base/java.text.Bug4170614Test
* @run main java.base/java.text.Bug4170614Test
*/
package java.text;
import sun.text.IntHashtable;
/**
* This class tests some internal hashCode() functions.
* Bug #4170614 complained that we had two iternal classes that
* Bug #4170614 complained that we had two internal classes that
* break the invariant that if a.equals(b) than a.hashCode() ==
* b.hashCode(). This is because these classes overrode equals()
* but not hashCode(). These are both purely internal classes, and
@ -142,15 +146,15 @@ public class Bug4170614Test {
public static void testPatternEntry() throws Exception {
PatternEntry fred = new PatternEntry(1,
new StringBuffer("hello"),
new StringBuffer("up"));
new StringBuilder("hello"),
new StringBuilder("up"));
PatternEntry barney = new PatternEntry(1,
new StringBuffer("hello"),
new StringBuffer("down"));
new StringBuilder("hello"),
new StringBuilder("down"));
// (equals() only considers the "chars" field, so fred and barney are equal)
PatternEntry homer = new PatternEntry(1,
new StringBuffer("goodbye"),
new StringBuffer("up"));
new StringBuilder("goodbye"),
new StringBuilder("up"));
if (fred.equals(barney)) {
System.out.println("fred.equals(barney)");