From 89c64972282f39ecd48b3774963d7a541ca3c0f4 Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Thu, 27 Sep 2012 17:21:12 +0000 Subject: [PATCH] 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 --- .../lang3/text/ExtendedMessageFormat.java | 2 +- .../lang3/time/DurationFormatUtils.java | 14 +++++++----- .../lang3/text/ExtendedMessageFormatTest.java | 4 ++-- .../lang3/time/DateFormatUtilsTest.java | 4 ++-- .../lang3/time/DurationFormatUtilsTest.java | 22 +++++++++---------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java b/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java index 1a7e80386..7c575ce40 100644 --- a/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java +++ b/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java @@ -330,7 +330,7 @@ public class ExtendedMessageFormat extends MessageFormat { private int readArgumentIndex(String pattern, ParsePosition pos) { int start = pos.getIndex(); seekNonWs(pattern, pos); - StringBuffer result = new StringBuffer(); + StringBuilder result = new StringBuilder(); boolean error = false; for (; !error && pos.getIndex() < pattern.length(); next(pos)) { char c = pattern.charAt(pos.getIndex()); diff --git a/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java b/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java index 2853f638c..28f2e4f01 100644 --- a/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java @@ -413,14 +413,14 @@ public class DurationFormatUtils { */ static String format(Token[] tokens, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds, boolean padWithZeros) { - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); boolean lastOutputSeconds = false; int sz = tokens.length; for (int i = 0; i < sz; i++) { Token token = tokens[i]; Object value = token.getValue(); int count = token.getCount(); - if (value instanceof StringBuffer) { + if (value instanceof StringBuilder) { buffer.append(value.toString()); } else { if (value == y) { @@ -485,7 +485,9 @@ public class DurationFormatUtils { ArrayList list = new ArrayList(array.length); 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; int sz = array.length; for(int i=0; i registry, Object[] args, Locale locale) { - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); buffer.append("Pattern=["); buffer.append(pattern); buffer.append("], locale=["); diff --git a/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java index 8a8d836cb..6f3299fa0 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java @@ -47,7 +47,7 @@ public class DateFormatUtilsTest { Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); c.set(2005,0,1,12,0,0); c.setTimeZone(TimeZone.getDefault()); - StringBuffer buffer = new StringBuffer (); + StringBuilder buffer = new StringBuilder (); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH) + 1; int day = c.get(Calendar.DAY_OF_MONTH); @@ -71,7 +71,7 @@ public class DateFormatUtilsTest { Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC")); c.set(2005,0,1,12,0,0); c.setTimeZone(TimeZone.getDefault()); - StringBuffer buffer = new StringBuffer (); + StringBuilder buffer = new StringBuilder (); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH) + 1; int day = c.get(Calendar.DAY_OF_MONTH); diff --git a/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java index 98e3b7634..cba763c62 100644 --- a/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java @@ -347,30 +347,30 @@ public class DurationFormatUtilsTest { // tests the ISO8601-like assertArrayEquals(new DurationFormatUtils.Token[]{ 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(new StringBuffer(":"), 1), + new DurationFormatUtils.Token(new StringBuilder(":"), 1), 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")); // test the iso extended format 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(new StringBuffer("Y"), 1), + new DurationFormatUtils.Token(new StringBuilder("Y"), 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(new StringBuffer("DT"), 1), + new DurationFormatUtils.Token(new StringBuilder("DT"), 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(new StringBuffer("M"), 1), + new DurationFormatUtils.Token(new StringBuilder("M"), 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(new StringBuffer("S"), 1)}, DurationFormatUtils + new DurationFormatUtils.Token(new StringBuilder("S"), 1)}, DurationFormatUtils .lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN)); // test failures in equals