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:
parent
8bc91a95f9
commit
10e18ae9b4
|
@ -32,8 +32,6 @@ import java.util.TimeZone;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
import org.apache.commons.lang3.Validate;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>FastDatePrinter is a fast and thread-safe version of
|
* <p>FastDatePrinter is a fast and thread-safe version of
|
||||||
* {@link java.text.SimpleDateFormat}.</p>
|
* {@link java.text.SimpleDateFormat}.</p>
|
||||||
|
@ -778,10 +776,9 @@ public class FastDatePrinter implements DatePrinter, Serializable {
|
||||||
if (value < 10) {
|
if (value < 10) {
|
||||||
buffer.append((char)(value + '0'));
|
buffer.append((char)(value + '0'));
|
||||||
} else if (value < 100) {
|
} else if (value < 100) {
|
||||||
buffer.append((char)(value / 10 + '0'));
|
appendDigits(buffer, value);
|
||||||
buffer.append((char)(value % 10 + '0'));
|
|
||||||
} else {
|
} else {
|
||||||
buffer.append(Integer.toString(value));
|
buffer.append(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -856,7 +853,7 @@ public class FastDatePrinter implements DatePrinter, Serializable {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int estimateLength() {
|
public int estimateLength() {
|
||||||
return 4;
|
return mSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -871,24 +868,15 @@ public class FastDatePrinter implements DatePrinter, Serializable {
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public final void appendTo(final StringBuffer buffer, final int value) {
|
public final void appendTo(final StringBuffer buffer, int value) {
|
||||||
if (value < 100) {
|
// pad the buffer with adequate zeros
|
||||||
for (int i = mSize; --i >= 2; ) {
|
for(int digit = 0; digit<mSize; ++digit) {
|
||||||
buffer.append('0');
|
buffer.append('0');
|
||||||
}
|
}
|
||||||
appendDigits(buffer, value);
|
// backfill the buffer with non-zero digits
|
||||||
} else {
|
int index = buffer.length();
|
||||||
int digits;
|
for( ; value>0; value /= 10) {
|
||||||
if (value < 1000) {
|
buffer.setCharAt(--index, (char)('0' + value % 10));
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -932,7 +920,7 @@ public class FastDatePrinter implements DatePrinter, Serializable {
|
||||||
if (value < 100) {
|
if (value < 100) {
|
||||||
appendDigits(buffer, value);
|
appendDigits(buffer, value);
|
||||||
} else {
|
} else {
|
||||||
buffer.append(Integer.toString(value));
|
buffer.append(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -310,7 +310,6 @@ public class FastDatePrinterTest {
|
||||||
final String three;
|
final String three;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test1806() throws ParseException {
|
public void test1806() throws ParseException {
|
||||||
for (Expected1806 trial : Expected1806.values()) {
|
for (Expected1806 trial : Expected1806.values()) {
|
||||||
|
@ -326,4 +325,16 @@ public class FastDatePrinterTest {
|
||||||
assertEquals(trial.three, printer.format(cal));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue