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

View file

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 NormalizerBase text = null;
private int[] buffer = null; private int[] buffer = null;
private int expIndex = 0; private int expIndex = 0;
private StringBuffer key = new StringBuffer(5); private StringBuilder key = new StringBuilder(5);
private int swapOrder = 0; private int swapOrder = 0;
private RBCollationTables ordering; private RBCollationTables ordering;
private RuleBasedCollator owner; private RuleBasedCollator owner;

View file

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

View file

@ -101,18 +101,18 @@ final class MergeCollation {
PatternEntry last = findLastWithNoExtension(i-1); PatternEntry last = findLastWithNoExtension(i-1);
for (int j = extList.size() - 1; j >= 0 ; j--) { for (int j = extList.size() - 1; j >= 0 ; j--) {
tmp = extList.get(j); tmp = extList.get(j);
tmp.addToBuffer(result, false, withWhiteSpace, last); tmp.addToBuilder(result, false, withWhiteSpace, last);
} }
extList = null; extList = null;
} }
entry.addToBuffer(result, false, withWhiteSpace, null); entry.addToBuilder(result, false, withWhiteSpace, null);
} }
} }
if (extList != null) { if (extList != null) {
PatternEntry last = findLastWithNoExtension(i-1); PatternEntry last = findLastWithNoExtension(i-1);
for (int j = extList.size() - 1; j >= 0 ; j--) { for (int j = extList.size() - 1; j >= 0 ; j--) {
tmp = extList.get(j); tmp = extList.get(j);
tmp.addToBuffer(result, false, withWhiteSpace, last); tmp.addToBuilder(result, false, withWhiteSpace, last);
} }
extList = null; extList = null;
} }
@ -151,7 +151,7 @@ final class MergeCollation {
{ {
PatternEntry entry = patterns.get(i); PatternEntry entry = patterns.get(i);
if (entry != null) { if (entry != null) {
entry.addToBuffer(result, true, withWhiteSpace, null); entry.addToBuilder(result, true, withWhiteSpace, null);
} }
} }
return result.toString(); return result.toString();
@ -211,7 +211,7 @@ final class MergeCollation {
// This is really used as a local variable inside fixEntry, but we cache // 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. // 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 // 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, private final int findLastEntry(PatternEntry entry,
StringBuffer excessChars) throws ParseException StringBuilder excessChars) throws ParseException
{ {
if (entry == null) if (entry == null)
return 0; return 0;

View file

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

View file

@ -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
*/

View file

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

View file

@ -21,11 +21,6 @@
* questions. * 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 * This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation. * License version 2 only, as published by the Free Software Foundation.
@ -61,13 +56,22 @@
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * 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; package java.text;
import sun.text.IntHashtable; import sun.text.IntHashtable;
/** /**
* This class tests some internal hashCode() functions. * 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() == * break the invariant that if a.equals(b) than a.hashCode() ==
* b.hashCode(). This is because these classes overrode equals() * b.hashCode(). This is because these classes overrode equals()
* but not hashCode(). These are both purely internal classes, and * but not hashCode(). These are both purely internal classes, and
@ -142,15 +146,15 @@ public class Bug4170614Test {
public static void testPatternEntry() throws Exception { public static void testPatternEntry() throws Exception {
PatternEntry fred = new PatternEntry(1, PatternEntry fred = new PatternEntry(1,
new StringBuffer("hello"), new StringBuilder("hello"),
new StringBuffer("up")); new StringBuilder("up"));
PatternEntry barney = new PatternEntry(1, PatternEntry barney = new PatternEntry(1,
new StringBuffer("hello"), new StringBuilder("hello"),
new StringBuffer("down")); new StringBuilder("down"));
// (equals() only considers the "chars" field, so fred and barney are equal) // (equals() only considers the "chars" field, so fred and barney are equal)
PatternEntry homer = new PatternEntry(1, PatternEntry homer = new PatternEntry(1,
new StringBuffer("goodbye"), new StringBuilder("goodbye"),
new StringBuffer("up")); new StringBuilder("up"));
if (fred.equals(barney)) { if (fred.equals(barney)) {
System.out.println("fred.equals(barney)"); System.out.println("fred.equals(barney)");