From cc1aed9bdf196403c673a886bbf723101171a9bf Mon Sep 17 00:00:00 2001 From: Chas Honton Date: Tue, 14 Jul 2015 21:02:49 -0700 Subject: [PATCH] LANG-1057 Micro optimization: Replace StringBuilder with String concatenation so that the compiler can better optimize the code --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/ClassPathUtils.java | 12 ++---------- src/main/java/org/apache/commons/lang3/Range.java | 14 +++----------- .../org/apache/commons/lang3/math/Fraction.java | 8 +++----- .../org/apache/commons/lang3/tuple/Triple.java | 3 +-- .../org/apache/commons/lang3/BooleanUtilsTest.java | 2 +- .../commons/lang3/StringEscapeUtilsTest.java | 2 +- 7 files changed, 12 insertions(+), 30 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1271d5f0c..fa53e127f 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + Replace StringBuilder with String concatenation for better optimization Deprecate SystemUtils.FILE_SEPARATOR and SystemUtils.PATH_SEPARATOR FastDateFormat APIs that use a StringBuilder Ability to throw checked exceptions without declaring them diff --git a/src/main/java/org/apache/commons/lang3/ClassPathUtils.java b/src/main/java/org/apache/commons/lang3/ClassPathUtils.java index 2a9f16f0e..df3773a34 100644 --- a/src/main/java/org/apache/commons/lang3/ClassPathUtils.java +++ b/src/main/java/org/apache/commons/lang3/ClassPathUtils.java @@ -79,11 +79,7 @@ public class ClassPathUtils { public static String toFullyQualifiedName(final Package context, final String resourceName) { Validate.notNull(context, "Parameter '%s' must not be null!", "context" ); Validate.notNull(resourceName, "Parameter '%s' must not be null!", "resourceName"); - final StringBuilder sb = new StringBuilder(); - sb.append(context.getName()); - sb.append("."); - sb.append(resourceName); - return sb.toString(); + return context.getName() + "." + resourceName; } /** @@ -128,11 +124,7 @@ public class ClassPathUtils { public static String toFullyQualifiedPath(final Package context, final String resourceName) { Validate.notNull(context, "Parameter '%s' must not be null!", "context" ); Validate.notNull(resourceName, "Parameter '%s' must not be null!", "resourceName"); - final StringBuilder sb = new StringBuilder(); - sb.append(context.getName().replace('.', '/')); - sb.append("/"); - sb.append(resourceName); - return sb.toString(); + return context.getName().replace('.', '/') + "/" + resourceName; } } diff --git a/src/main/java/org/apache/commons/lang3/Range.java b/src/main/java/org/apache/commons/lang3/Range.java index 450897f65..fb0ccdb52 100644 --- a/src/main/java/org/apache/commons/lang3/Range.java +++ b/src/main/java/org/apache/commons/lang3/Range.java @@ -445,18 +445,10 @@ public final class Range implements Serializable { */ @Override public String toString() { - String result = toString; - if (result == null) { - final StringBuilder buf = new StringBuilder(32); - buf.append('['); - buf.append(minimum); - buf.append(".."); - buf.append(maximum); - buf.append(']'); - result = buf.toString(); - toString = result; + if (toString == null) { + toString = "[" + minimum + ".." + maximum + "]"; } - return result; + return toString; } /** diff --git a/src/main/java/org/apache/commons/lang3/math/Fraction.java b/src/main/java/org/apache/commons/lang3/math/Fraction.java index ee1d638e1..3ff964fc3 100644 --- a/src/main/java/org/apache/commons/lang3/math/Fraction.java +++ b/src/main/java/org/apache/commons/lang3/math/Fraction.java @@ -897,7 +897,7 @@ public final class Fraction extends Number implements Comparable { @Override public String toString() { if (toString == null) { - toString = new StringBuilder(32).append(getNumerator()).append('/').append(getDenominator()).toString(); + toString = getNumerator() + "/" + getDenominator(); } return toString; } @@ -928,12 +928,10 @@ public final class Fraction extends Number implements Comparable { if (properNumerator == 0) { toProperString = Integer.toString(getProperWhole()); } else { - toProperString = new StringBuilder(32).append(getProperWhole()).append(' ').append(properNumerator) - .append('/').append(getDenominator()).toString(); + toProperString = getProperWhole() + " " + properNumerator + "/" + getDenominator(); } } else { - toProperString = new StringBuilder(32).append(getNumerator()).append('/').append(getDenominator()) - .toString(); + toProperString = getNumerator() + "/" + getDenominator(); } } return toProperString; diff --git a/src/main/java/org/apache/commons/lang3/tuple/Triple.java b/src/main/java/org/apache/commons/lang3/tuple/Triple.java index 196580aee..37d1b15ba 100644 --- a/src/main/java/org/apache/commons/lang3/tuple/Triple.java +++ b/src/main/java/org/apache/commons/lang3/tuple/Triple.java @@ -138,8 +138,7 @@ public abstract class Triple implements Comparable>, Se */ @Override public String toString() { - return new StringBuilder().append('(').append(getLeft()).append(',').append(getMiddle()).append(',') - .append(getRight()).append(')').toString(); + return "(" + getLeft() + "," + getMiddle() + "," + getRight() + ")"; } /** diff --git a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java index fb7687e85..c189cde24 100644 --- a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java @@ -319,7 +319,7 @@ public class BooleanUtilsTest { assertFalse(BooleanUtils.toBoolean("false")); assertFalse(BooleanUtils.toBoolean("a")); assertTrue(BooleanUtils.toBoolean("true")); // interned handled differently - assertTrue(BooleanUtils.toBoolean(new StringBuffer("tr").append("ue").toString())); + assertTrue(BooleanUtils.toBoolean(new StringBuilder("tr").append("ue").toString())); assertTrue(BooleanUtils.toBoolean("truE")); assertTrue(BooleanUtils.toBoolean("trUe")); assertTrue(BooleanUtils.toBoolean("trUE")); diff --git a/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java index 1eff6ed9e..28be43771 100644 --- a/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java @@ -571,7 +571,7 @@ public class StringEscapeUtilsTest { @Test @SuppressWarnings( "deprecation" ) // escapeXml(String) has been replaced by escapeXml10(String) and escapeXml11(String) in 3.3 public void testLang720() { - final String input = new StringBuilder("\ud842\udfb7").append("A").toString(); + final String input = "\ud842\udfb7" + "A"; final String escaped = StringEscapeUtils.escapeXml(input); assertEquals(input, escaped); }