StringBuffer => StringBuilder where being used as a local-only buffer

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1391112 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-09-27 17:21:12 +00:00
parent 3ab8e13026
commit 89c6497228
5 changed files with 24 additions and 22 deletions

View File

@ -330,7 +330,7 @@ public class ExtendedMessageFormat extends MessageFormat {
private int readArgumentIndex(String pattern, ParsePosition pos) { private int readArgumentIndex(String pattern, ParsePosition pos) {
int start = pos.getIndex(); int start = pos.getIndex();
seekNonWs(pattern, pos); seekNonWs(pattern, pos);
StringBuffer result = new StringBuffer(); StringBuilder result = new StringBuilder();
boolean error = false; boolean error = false;
for (; !error && pos.getIndex() < pattern.length(); next(pos)) { for (; !error && pos.getIndex() < pattern.length(); next(pos)) {
char c = pattern.charAt(pos.getIndex()); char c = pattern.charAt(pos.getIndex());

View File

@ -413,14 +413,14 @@ public class DurationFormatUtils {
*/ */
static String format(Token[] tokens, int years, int months, int days, int hours, int minutes, int seconds, static String format(Token[] tokens, int years, int months, int days, int hours, int minutes, int seconds,
int milliseconds, boolean padWithZeros) { int milliseconds, boolean padWithZeros) {
StringBuffer buffer = new StringBuffer(); StringBuilder buffer = new StringBuilder();
boolean lastOutputSeconds = false; boolean lastOutputSeconds = false;
int sz = tokens.length; int sz = tokens.length;
for (int i = 0; i < sz; i++) { for (int i = 0; i < sz; i++) {
Token token = tokens[i]; Token token = tokens[i];
Object value = token.getValue(); Object value = token.getValue();
int count = token.getCount(); int count = token.getCount();
if (value instanceof StringBuffer) { if (value instanceof StringBuilder) {
buffer.append(value.toString()); buffer.append(value.toString());
} else { } else {
if (value == y) { if (value == y) {
@ -485,7 +485,9 @@ public class DurationFormatUtils {
ArrayList<Token> list = new ArrayList<Token>(array.length); ArrayList<Token> list = new ArrayList<Token>(array.length);
boolean inLiteral = false; boolean inLiteral = false;
StringBuffer buffer = null; // Although the buffer is stored in a Token, the Tokens are only
// used internally, so cannot be accessed by other threads
StringBuilder buffer = null;
Token previous = null; Token previous = null;
int sz = array.length; int sz = array.length;
for(int i=0; i<sz; i++) { for(int i=0; i<sz; i++) {
@ -502,7 +504,7 @@ public class DurationFormatUtils {
buffer = null; buffer = null;
inLiteral = false; inLiteral = false;
} else { } else {
buffer = new StringBuffer(); buffer = new StringBuilder();
list.add(new Token(buffer)); list.add(new Token(buffer));
inLiteral = true; inLiteral = true;
} }
@ -516,7 +518,7 @@ public class DurationFormatUtils {
case 'S' : value = S; break; case 'S' : value = S; break;
default : default :
if(buffer == null) { if(buffer == null) {
buffer = new StringBuffer(); buffer = new StringBuilder();
list.add(new Token(buffer)); list.add(new Token(buffer));
} }
buffer.append(ch); buffer.append(ch);
@ -625,7 +627,7 @@ public class DurationFormatUtils {
if (this.count != tok2.count) { if (this.count != tok2.count) {
return false; return false;
} }
if (this.value instanceof StringBuffer) { if (this.value instanceof StringBuilder) {
return this.value.toString().equals(tok2.value.toString()); return this.value.toString().equals(tok2.value.toString());
} else if (this.value instanceof Number) { } else if (this.value instanceof Number) {
return this.value.equals(tok2.value); return this.value.equals(tok2.value);

View File

@ -111,7 +111,7 @@ public class ExtendedMessageFormatTest {
nf = NumberFormat.getCurrencyInstance(locale); nf = NumberFormat.getCurrencyInstance(locale);
emf = new ExtendedMessageFormat(pattern, locale, registry); emf = new ExtendedMessageFormat(pattern, locale, registry);
} }
StringBuffer expected = new StringBuffer(); StringBuilder expected = new StringBuilder();
expected.append("Name: "); expected.append("Name: ");
expected.append(args[0].toString().toUpperCase()); expected.append(args[0].toString().toUpperCase());
expected.append(" DOB: "); expected.append(" DOB: ");
@ -338,7 +338,7 @@ public class ExtendedMessageFormatTest {
* @param locale Locale * @param locale Locale
*/ */
private void checkBuiltInFormat(String pattern, Map<String, ?> registry, Object[] args, Locale locale) { private void checkBuiltInFormat(String pattern, Map<String, ?> registry, Object[] args, Locale locale) {
StringBuffer buffer = new StringBuffer(); StringBuilder buffer = new StringBuilder();
buffer.append("Pattern=["); buffer.append("Pattern=[");
buffer.append(pattern); buffer.append(pattern);
buffer.append("], locale=["); buffer.append("], locale=[");

View File

@ -47,7 +47,7 @@ public class DateFormatUtilsTest {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.set(2005,0,1,12,0,0); c.set(2005,0,1,12,0,0);
c.setTimeZone(TimeZone.getDefault()); c.setTimeZone(TimeZone.getDefault());
StringBuffer buffer = new StringBuffer (); StringBuilder buffer = new StringBuilder ();
int year = c.get(Calendar.YEAR); int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1; int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH); int day = c.get(Calendar.DAY_OF_MONTH);
@ -71,7 +71,7 @@ public class DateFormatUtilsTest {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.set(2005,0,1,12,0,0); c.set(2005,0,1,12,0,0);
c.setTimeZone(TimeZone.getDefault()); c.setTimeZone(TimeZone.getDefault());
StringBuffer buffer = new StringBuffer (); StringBuilder buffer = new StringBuilder ();
int year = c.get(Calendar.YEAR); int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1; int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH); int day = c.get(Calendar.DAY_OF_MONTH);

View File

@ -347,30 +347,30 @@ public class DurationFormatUtilsTest {
// tests the ISO8601-like // tests the ISO8601-like
assertArrayEquals(new DurationFormatUtils.Token[]{ assertArrayEquals(new DurationFormatUtils.Token[]{
new DurationFormatUtils.Token(DurationFormatUtils.H, 1), new DurationFormatUtils.Token(DurationFormatUtils.H, 1),
new DurationFormatUtils.Token(new StringBuffer(":"), 1), new DurationFormatUtils.Token(new StringBuilder(":"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.m, 2), new DurationFormatUtils.Token(DurationFormatUtils.m, 2),
new DurationFormatUtils.Token(new StringBuffer(":"), 1), new DurationFormatUtils.Token(new StringBuilder(":"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.s, 2), new DurationFormatUtils.Token(DurationFormatUtils.s, 2),
new DurationFormatUtils.Token(new StringBuffer("."), 1), new DurationFormatUtils.Token(new StringBuilder("."), 1),
new DurationFormatUtils.Token(DurationFormatUtils.S, 3)}, DurationFormatUtils.lexx("H:mm:ss.SSS")); new DurationFormatUtils.Token(DurationFormatUtils.S, 3)}, DurationFormatUtils.lexx("H:mm:ss.SSS"));
// test the iso extended format // test the iso extended format
assertArrayEquals(new DurationFormatUtils.Token[]{ assertArrayEquals(new DurationFormatUtils.Token[]{
new DurationFormatUtils.Token(new StringBuffer("P"), 1), new DurationFormatUtils.Token(new StringBuilder("P"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.y, 4), new DurationFormatUtils.Token(DurationFormatUtils.y, 4),
new DurationFormatUtils.Token(new StringBuffer("Y"), 1), new DurationFormatUtils.Token(new StringBuilder("Y"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.M, 1), new DurationFormatUtils.Token(DurationFormatUtils.M, 1),
new DurationFormatUtils.Token(new StringBuffer("M"), 1), new DurationFormatUtils.Token(new StringBuilder("M"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.d, 1), new DurationFormatUtils.Token(DurationFormatUtils.d, 1),
new DurationFormatUtils.Token(new StringBuffer("DT"), 1), new DurationFormatUtils.Token(new StringBuilder("DT"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.H, 1), new DurationFormatUtils.Token(DurationFormatUtils.H, 1),
new DurationFormatUtils.Token(new StringBuffer("H"), 1), new DurationFormatUtils.Token(new StringBuilder("H"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.m, 1), new DurationFormatUtils.Token(DurationFormatUtils.m, 1),
new DurationFormatUtils.Token(new StringBuffer("M"), 1), new DurationFormatUtils.Token(new StringBuilder("M"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.s, 1), new DurationFormatUtils.Token(DurationFormatUtils.s, 1),
new DurationFormatUtils.Token(new StringBuffer("."), 1), new DurationFormatUtils.Token(new StringBuilder("."), 1),
new DurationFormatUtils.Token(DurationFormatUtils.S, 1), new DurationFormatUtils.Token(DurationFormatUtils.S, 1),
new DurationFormatUtils.Token(new StringBuffer("S"), 1)}, DurationFormatUtils new DurationFormatUtils.Token(new StringBuilder("S"), 1)}, DurationFormatUtils
.lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN)); .lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN));
// test failures in equals // test failures in equals