Skip to content

Commit 87acfee

Browse files
justin-curtis-lunaotoj
authored andcommitted
8294397: Replace StringBuffer with StringBuilder within java.text
Reviewed-by: lancea, naoto, bchristi
1 parent f2c5718 commit 87acfee

File tree

7 files changed

+55
-80
lines changed

7 files changed

+55
-80
lines changed

src/java.base/share/classes/java/text/CollationElementIterator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -776,7 +776,7 @@ private int prevContractChar(int ch)
776776
private NormalizerBase text = null;
777777
private int[] buffer = null;
778778
private int expIndex = 0;
779-
private StringBuffer key = new StringBuffer(5);
779+
private StringBuilder key = new StringBuilder(5);
780780
private int swapOrder = 0;
781781
private RBCollationTables ordering;
782782
private RuleBasedCollator owner;

src/java.base/share/classes/java/text/DigitList.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ public final double getDouble() {
161161
return 0.0;
162162
}
163163

164-
StringBuffer temp = getStringBuffer();
165-
temp.append('.');
166-
temp.append(digits, 0, count);
167-
temp.append('E');
168-
temp.append(decimalAt);
169-
return Double.parseDouble(temp.toString());
164+
return Double.parseDouble(getStringBuilder()
165+
.append('.')
166+
.append(digits, 0, count)
167+
.append('E')
168+
.append(decimalAt)
169+
.toString());
170170
}
171171

172172
/**
@@ -187,7 +187,7 @@ public final long getLong() {
187187
return Long.MIN_VALUE;
188188
}
189189

190-
StringBuffer temp = getStringBuffer();
190+
StringBuilder temp = getStringBuilder();
191191
temp.append(digits, 0, count);
192192
for (int i = count; i < decimalAt; ++i) {
193193
temp.append('0');
@@ -736,7 +736,7 @@ public Object clone() {
736736
char[] newDigits = new char[digits.length];
737737
System.arraycopy(digits, 0, newDigits, 0, digits.length);
738738
other.digits = newDigits;
739-
other.tempBuffer = null;
739+
other.tempBuilder = null;
740740
return other;
741741
} catch (CloneNotSupportedException e) {
742742
throw new InternalError(e);
@@ -788,23 +788,24 @@ public String toString() {
788788
if (isZero()) {
789789
return "0";
790790
}
791-
StringBuffer buf = getStringBuffer();
792-
buf.append("0.");
793-
buf.append(digits, 0, count);
794-
buf.append("x10^");
795-
buf.append(decimalAt);
796-
return buf.toString();
791+
792+
return getStringBuilder()
793+
.append("0.")
794+
.append(digits, 0, count)
795+
.append("x10^")
796+
.append(decimalAt)
797+
.toString();
797798
}
798799

799-
private StringBuffer tempBuffer;
800+
private StringBuilder tempBuilder;
800801

801-
private StringBuffer getStringBuffer() {
802-
if (tempBuffer == null) {
803-
tempBuffer = new StringBuffer(MAX_COUNT);
802+
private StringBuilder getStringBuilder() {
803+
if (tempBuilder == null) {
804+
tempBuilder = new StringBuilder(MAX_COUNT);
804805
} else {
805-
tempBuffer.setLength(0);
806+
tempBuilder.setLength(0);
806807
}
807-
return tempBuffer;
808+
return tempBuilder;
808809
}
809810

810811
private void extendDigits(int len) {

src/java.base/share/classes/java/text/MergeCollation.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,18 @@ public String getPattern(boolean withWhiteSpace) {
101101
PatternEntry last = findLastWithNoExtension(i-1);
102102
for (int j = extList.size() - 1; j >= 0 ; j--) {
103103
tmp = extList.get(j);
104-
tmp.addToBuffer(result, false, withWhiteSpace, last);
104+
tmp.addToBuilder(result, false, withWhiteSpace, last);
105105
}
106106
extList = null;
107107
}
108-
entry.addToBuffer(result, false, withWhiteSpace, null);
108+
entry.addToBuilder(result, false, withWhiteSpace, null);
109109
}
110110
}
111111
if (extList != null) {
112112
PatternEntry last = findLastWithNoExtension(i-1);
113113
for (int j = extList.size() - 1; j >= 0 ; j--) {
114114
tmp = extList.get(j);
115-
tmp.addToBuffer(result, false, withWhiteSpace, last);
115+
tmp.addToBuilder(result, false, withWhiteSpace, last);
116116
}
117117
extList = null;
118118
}
@@ -151,7 +151,7 @@ public String emitPattern(boolean withWhiteSpace) {
151151
{
152152
PatternEntry entry = patterns.get(i);
153153
if (entry != null) {
154-
entry.addToBuffer(result, true, withWhiteSpace, null);
154+
entry.addToBuilder(result, true, withWhiteSpace, null);
155155
}
156156
}
157157
return result.toString();
@@ -211,7 +211,7 @@ public PatternEntry getItemAt(int index) {
211211

212212
// This is really used as a local variable inside fixEntry, but we cache
213213
// it here to avoid newing it up every time the method is called.
214-
private transient StringBuffer excess = new StringBuffer();
214+
private transient StringBuilder excess = new StringBuilder();
215215

216216
//
217217
// When building a MergeCollation, we need to do lots of searches to see
@@ -300,7 +300,7 @@ private final void fixEntry(PatternEntry newEntry) throws ParseException
300300
}
301301

302302
private final int findLastEntry(PatternEntry entry,
303-
StringBuffer excessChars) throws ParseException
303+
StringBuilder excessChars) throws ParseException
304304
{
305305
if (entry == null)
306306
return 0;

src/java.base/share/classes/java/text/PatternEntry.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public int hashCode() {
8484
*/
8585
public String toString() {
8686
StringBuilder result = new StringBuilder();
87-
addToBuffer(result, true, false, null);
87+
addToBuilder(result, true, false, null);
8888
return result.toString();
8989
}
9090

@@ -111,10 +111,10 @@ final String getChars() {
111111

112112
// ===== privates =====
113113

114-
void addToBuffer(StringBuilder toAddTo,
115-
boolean showExtension,
116-
boolean showWhiteSpace,
117-
PatternEntry lastEntry)
114+
void addToBuilder(StringBuilder toAddTo,
115+
boolean showExtension,
116+
boolean showWhiteSpace,
117+
PatternEntry lastEntry)
118118
{
119119
if (showWhiteSpace && toAddTo.length() > 0)
120120
if (strength == Collator.PRIMARY || lastEntry != null)
@@ -190,8 +190,8 @@ private static void appendQuoted(String chars, StringBuilder toAddTo) {
190190
//========================================================================
191191

192192
PatternEntry(int strength,
193-
StringBuffer chars,
194-
StringBuffer extension)
193+
StringBuilder chars,
194+
StringBuilder extension)
195195
{
196196
this.strength = strength;
197197
this.chars = chars.toString();
@@ -287,8 +287,8 @@ public PatternEntry next() throws ParseException {
287287
}
288288

289289
// We re-use these objects in order to improve performance
290-
private StringBuffer newChars = new StringBuffer();
291-
private StringBuffer newExtension = new StringBuffer();
290+
private StringBuilder newChars = new StringBuilder();
291+
private StringBuilder newExtension = new StringBuilder();
292292

293293
}
294294

test/jdk/sun/text/IntHashtable/Bug4170614TestRun.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/jdk/sun/text/IntHashtable/Bug4705389.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* questions.
2222
*/
2323

24-
/**
24+
/*
2525
* @test
2626
* @bug 4705389
2727
* @summary Make sure to find removed slots, which test case will be timed out without the fix.

test/jdk/sun/text/IntHashtable/patch-src/java.base/java/text/Bug4170614Test.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
* questions.
2222
*/
2323

24-
/*
25-
(this test doesn't have an at-test tag because it's run by Bug4170614TestRun.java
26-
instead of directly by the test harness)
27-
*/
28-
2924
/*
3025
* This file is available under and governed by the GNU General Public
3126
* License version 2 only, as published by the Free Software Foundation.
@@ -61,13 +56,22 @@
6156
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
6257
*
6358
*/
59+
60+
/* @test
61+
* @bug 4170614
62+
* @summary Test internal hashCode() and equals() functions
63+
* @library ../../../../patch-src
64+
* @build java.base/java.text.Bug4170614Test
65+
* @run main java.base/java.text.Bug4170614Test
66+
*/
67+
6468
package java.text;
6569
import sun.text.IntHashtable;
6670

6771

6872
/**
6973
* This class tests some internal hashCode() functions.
70-
* Bug #4170614 complained that we had two iternal classes that
74+
* Bug #4170614 complained that we had two internal classes that
7175
* break the invariant that if a.equals(b) than a.hashCode() ==
7276
* b.hashCode(). This is because these classes overrode equals()
7377
* but not hashCode(). These are both purely internal classes, and
@@ -142,15 +146,15 @@ public static void testIntHashtable() throws Exception {
142146

143147
public static void testPatternEntry() throws Exception {
144148
PatternEntry fred = new PatternEntry(1,
145-
new StringBuffer("hello"),
146-
new StringBuffer("up"));
149+
new StringBuilder("hello"),
150+
new StringBuilder("up"));
147151
PatternEntry barney = new PatternEntry(1,
148-
new StringBuffer("hello"),
149-
new StringBuffer("down"));
152+
new StringBuilder("hello"),
153+
new StringBuilder("down"));
150154
// (equals() only considers the "chars" field, so fred and barney are equal)
151155
PatternEntry homer = new PatternEntry(1,
152-
new StringBuffer("goodbye"),
153-
new StringBuffer("up"));
156+
new StringBuilder("goodbye"),
157+
new StringBuilder("up"));
154158

155159
if (fred.equals(barney)) {
156160
System.out.println("fred.equals(barney)");

0 commit comments

Comments
 (0)