ceki 2004/12/23 07:30:40
Modified: src/java/org/apache/log4j/pattern CachedDateFormat.java
tests/src/java/org/apache/log4j/pattern
CachedDateFormatTest.java
Log:
Work in progress
Revision Changes Path
1.4 +33 -22
logging-log4j/src/java/org/apache/log4j/pattern/CachedDateFormat.java
Index: CachedDateFormat.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/pattern/CachedDateFormat.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CachedDateFormat.java 23 Dec 2004 14:07:58 -0000 1.3
+++ CachedDateFormat.java 23 Dec 2004 15:30:40 -0000 1.4
@@ -43,6 +43,7 @@
private int millisecondStart;
private StringBuffer cache = new StringBuffer();
private long slotBegin;
+ private Date slotBeginDate;
private int milliDigits;
private StringBuffer milliBuf = new StringBuffer(3);
private NumberFormat numberFormat;
@@ -79,10 +80,15 @@
long nowTime = now.getTime();
slotBegin = (nowTime / 1000L) * 1000L;
- Date slotBegingDate = new Date(slotBegin);
- String formatted = formatter.format(slotBegingDate);
+ slotBeginDate = new Date(slotBegin);
+ String formatted = formatter.format(slotBeginDate);
cache.append(formatted);
millisecondStart = findMillisecondStart(slotBegin, formatted, formatter);
+// if(millisecondStart == UNRECOGNIZED_MILLISECOND_PATTERN) {
+// System.out.println("UNRECOGNIZED PATTERN");
+// } else {
+// System.out.println("millisecondStart="+millisecondStart);
+// }
}
/**
@@ -111,12 +117,10 @@
//
if ((i + milliDigits) <= formatted.length()) {
for (int j = 0; j < milliDigits; j++) {
- if (
- (formatted.charAt(i + j) != '0')
- || (formatted.charAt(i + j) != ('9' - j))) {
- ;
+ if ((formatted.charAt(i + j) != '0')
+ || (plus987.charAt(i + j) != ('9' - j))) {
+ return UNRECOGNIZED_MILLISECOND_PATTERN;
}
- return UNRECOGNIZED_MILLISECOND_PATTERN;
}
return i;
} else {
@@ -142,6 +146,7 @@
}
long now = date.getTime();
if ((now < (slotBegin + 1000L)) && (now >= slotBegin)) {
+ //System.out.println("Using cached val:"+date);
if (millisecondStart >= 0) {
int millis = (int) (now - slotBegin);
int cacheLength = cache.length();
@@ -152,25 +157,31 @@
cache.setCharAt(millisecondStart+j, milliBuf.charAt(j));
}
}
- sbuf.append(cache);
} else {
+ //System.out.println("Recomputing cache:"+date);
slotBegin = (now / 1000L) * 1000L;
- //
- // if earlier than 1970 and rounded toward 1970
- // then move back one second
- if ((now - slotBegin) < 0) {
- slotBegin -= 1000;
- }
+ int prevLength = cache.length();
cache.setLength(0);
- formatter.format(new Date(slotBegin), cache, fieldPosition);
- millisecondStart =
- findMillisecondStart(slotBegin, cache.toString(), formatter);
- //
- // calling ourself should be safe and faster
- // but why risk it
- formatter.format(date, sbuf, fieldPosition);
+ formatter.format(date, cache, fieldPosition);
+
+ // if the length changed then
+ // recalculate the millisecond position
+ if (cache.length() != prevLength) {
+ //System.out.println("Recomputing cached len changed
oldLen="+prevLength
+ // +", newLen="+cache.length());
+ //
+ // format the previous integral second
+ StringBuffer tempBuffer = new StringBuffer(cache.length());
+ slotBeginDate.setTime(slotBegin);
+ formatter.format(slotBeginDate, tempBuffer, fieldPosition);
+ //
+ // detect the start of the millisecond field
+ millisecondStart = findMillisecondStart(slotBegin,
+ tempBuffer.toString(),
+ formatter);
+ }
}
- return sbuf;
+ return sbuf.append(cache);
}
/**
1.3 +43 -12
logging-log4j/tests/src/java/org/apache/log4j/pattern/CachedDateFormatTest.java
Index: CachedDateFormatTest.java
===================================================================
RCS file:
/home/cvs/logging-log4j/tests/src/java/org/apache/log4j/pattern/CachedDateFormatTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- CachedDateFormatTest.java 23 Dec 2004 14:07:58 -0000 1.2
+++ CachedDateFormatTest.java 23 Dec 2004 15:30:40 -0000 1.3
@@ -16,7 +16,10 @@
package org.apache.log4j.pattern;
+import junit.framework.Test;
import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
import org.apache.log4j.helpers.AbsoluteTimeDateFormat;
import org.apache.log4j.pattern.CachedDateFormat;
@@ -31,8 +34,7 @@
Unit test [EMAIL PROTECTED] AbsoluteTimeDateFormat}.
@author Curt Arnold
@since 1.3.0 */
-public final class CachedDateFormatTest
- extends TestCase {
+public final class CachedDateFormatTest extends TestCase {
/**
* Test constructor
@@ -80,7 +82,9 @@
gmtFormat.setTimeZone(GMT);
long ticks = 12601L * 86400000L;
Date jul1 = new Date(ticks);
- assertEquals("00:00:00,000", gmtFormat.format(jul1));
+ String r = gmtFormat.format(jul1);
+ assertEquals("00:00:00,000", r);
+
Date plus8ms = new Date(ticks + 8);
assertEquals("00:00:00,008", gmtFormat.format(plus8ms));
Date plus17ms = new Date(ticks + 17);
@@ -224,7 +228,7 @@
public void test9() {
String pattern = "yyyy-MMMM-dd HH:mm:ss,SS Z";
DateFormat baseFormat = new SimpleDateFormat(pattern, Locale.US);
- DateFormat cachedFormat = new CachedDateFormat(pattern);
+ CachedDateFormat cachedFormat = new CachedDateFormat(pattern);
TimeZone cet = TimeZone.getTimeZone("GMT+1");
cachedFormat.setTimeZone(cet);
@@ -251,28 +255,55 @@
*/
public void test10() {
String pattern = "MMMM SSS EEEEEE";
- DateFormat baseFormat = new SimpleDateFormat("MMMM SSS EEEEEE",
Locale.US);
DateFormat cachedFormat = new CachedDateFormat(pattern);
- TimeZone cet = TimeZone.getTimeZone("GMT+1");
- cachedFormat.setTimeZone(cet);
Calendar c = Calendar.getInstance();
c.set(2004, Calendar.OCTOBER, 5, 20, 0);
c.set(Calendar.SECOND, 37);
c.set(Calendar.MILLISECOND, 23);
- c.setTimeZone(cet);
String s = cachedFormat.format(c.getTime());
assertEquals("October 023 Tuesday", s);
+ // since we are in a different slot, cachedFormat will return correct
+ // results since it will freshly format
c.set(2004, Calendar.NOVEMBER, 1, 0, 0);
- c.set(Calendar.MILLISECOND, 23);
+ c.set(Calendar.MILLISECOND, 6);
s = cachedFormat.format(c.getTime());
- assertEquals("November 023 Monday", s);
+ assertEquals("November 006 Monday", s);
+ // exercise the cache, this time the result will be wrong.
+ s = cachedFormat.format(c.getTime());
+ if("November 006 Monday".equals(s)) {
+ // Results should be incrrect!
+ fail("CachedDateFormat does not deal properly with MMMM SSS EEEEE
combinations");
+ }
+ }
+
+ public void testS2() {
+ String pattern = "HH:mm:ss,SS";
+ DateFormat cachedFormat = new CachedDateFormat(pattern);
+
+ Calendar c = Calendar.getInstance();
+ c.set(2004, Calendar.OCTOBER, 5, 20, 54);
+ c.set(Calendar.SECOND, 37);
+ c.set(Calendar.MILLISECOND, 3);
- c.set(Calendar.MILLISECOND, 984);
+ String s = cachedFormat.format(c.getTime());
+ System.out.println("---"+s);
+ assertEquals("20:54:37:00", s);
+
+ // excercise the cache
s = cachedFormat.format(c.getTime());
- assertEquals("November 984 Monday", s);
+ System.out.println("--"+s);
+ assertEquals("20:54:37:00", s);
+ }
+
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(new CachedDateFormatTest("test10"));
+ suite.addTest(new CachedDateFormatTest("testS2"));
+ return suite;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]