LANG-1100: Avoid memory allocation when using date formating to StringBuffer

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1668517 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chas Honton 2015-03-23 05:13:20 +00:00
parent 8bc91a95f9
commit 10e18ae9b4
2 changed files with 25 additions and 26 deletions

View File

@ -32,8 +32,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.commons.lang3.Validate;
/**
* <p>FastDatePrinter is a fast and thread-safe version of
* {@link java.text.SimpleDateFormat}.</p>
@ -778,10 +776,9 @@ public final void appendTo(final StringBuffer buffer, final int value) {
if (value < 10) {
buffer.append((char)(value + '0'));
} else if (value < 100) {
buffer.append((char)(value / 10 + '0'));
buffer.append((char)(value % 10 + '0'));
appendDigits(buffer, value);
} else {
buffer.append(Integer.toString(value));
buffer.append(value);
}
}
}
@ -856,7 +853,7 @@ private static class PaddedNumberField implements NumberRule {
*/
@Override
public int estimateLength() {
return 4;
return mSize;
}
/**
@ -871,24 +868,15 @@ public void appendTo(final StringBuffer buffer, final Calendar calendar) {
* {@inheritDoc}
*/
@Override
public final void appendTo(final StringBuffer buffer, final int value) {
if (value < 100) {
for (int i = mSize; --i >= 2; ) {
buffer.append('0');
}
appendDigits(buffer, value);
} else {
int digits;
if (value < 1000) {
digits = 3;
} else {
Validate.isTrue(value > -1, "Negative values should not be possible", value);
digits = Integer.toString(value).length();
}
for (int i = mSize; --i >= digits; ) {
buffer.append('0');
}
buffer.append(Integer.toString(value));
public final void appendTo(final StringBuffer buffer, int value) {
// pad the buffer with adequate zeros
for(int digit = 0; digit<mSize; ++digit) {
buffer.append('0');
}
// backfill the buffer with non-zero digits
int index = buffer.length();
for( ; value>0; value /= 10) {
buffer.setCharAt(--index, (char)('0' + value % 10));
}
}
}
@ -932,7 +920,7 @@ public final void appendTo(final StringBuffer buffer, final int value) {
if (value < 100) {
appendDigits(buffer, value);
} else {
buffer.append(Integer.toString(value));
buffer.append(value);
}
}
}

View File

@ -310,7 +310,6 @@ private Expected1806(TimeZone zone, String one, String two, String three) {
final String three;
}
@Test
public void test1806() throws ParseException {
for (Expected1806 trial : Expected1806.values()) {
@ -326,4 +325,16 @@ public void test1806() throws ParseException {
assertEquals(trial.three, printer.format(cal));
}
}
@Test
public void testLang1103() throws ParseException {
Calendar cal = Calendar.getInstance(SWEDEN);
cal.set(Calendar.DAY_OF_MONTH, 2);
assertEquals("2", getInstance("d", SWEDEN).format(cal));
assertEquals("02", getInstance("dd", SWEDEN).format(cal));
assertEquals("002", getInstance("ddd", SWEDEN).format(cal));
assertEquals("0002", getInstance("dddd", SWEDEN).format(cal));
assertEquals("00002", getInstance("ddddd", SWEDEN).format(cal));
}
}