LANG-1057

Micro optimization: Replace StringBuilder with String concatenation so that the compiler can better optimize the code
This commit is contained in:
Chas Honton 2015-07-14 21:02:49 -07:00
parent fb7784fa14
commit cc1aed9bdf
7 changed files with 12 additions and 30 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-1057" type="update" dev="chas" dute-to="Otávio Santana">Replace StringBuilder with String concatenation for better optimization</action>
<action issue="LANG-1075" type="update" dev="chas">Deprecate SystemUtils.FILE_SEPARATOR and SystemUtils.PATH_SEPARATOR</action>
<action issue="LANG-1154" type="add" dev="chas" due-to="Gary Gregory">FastDateFormat APIs that use a StringBuilder</action>
<action issue="LANG-1149" type="add" dev="chas" due-to="Gregory Zak">Ability to throw checked exceptions without declaring them</action>

View File

@ -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;
}
}

View File

@ -445,18 +445,10 @@ public final class Range<T> 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;
}
/**

View File

@ -897,7 +897,7 @@ public final class Fraction extends Number implements Comparable<Fraction> {
@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<Fraction> {
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;

View File

@ -138,8 +138,7 @@ public abstract class Triple<L, M, R> implements Comparable<Triple<L, M, R>>, Se
*/
@Override
public String toString() {
return new StringBuilder().append('(').append(getLeft()).append(',').append(getMiddle()).append(',')
.append(getRight()).append(')').toString();
return "(" + getLeft() + "," + getMiddle() + "," + getRight() + ")";
}
/**

View File

@ -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"));

View File

@ -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);
}