2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j.pattern;
20 import java.text.DateFormat;
21 import java.text.FieldPosition;
22 import java.text.NumberFormat;
23 import java.text.ParsePosition;
24 import java.util.Date;
25 import java.util.TimeZone;
29 * CachedDateFormat optimizes the performance of a wrapped
30 * DateFormat. The implementation is not thread-safe.
31 * If the millisecond pattern is not recognized,
32 * the class will only use the cache if the
33 * same value is requested.
36 public final class CachedDateFormat extends DateFormat {
38 * Serialization version.
40 private static final long serialVersionUID = 1;
42 * Constant used to represent that there was no change
43 * observed when changing the millisecond count.
45 public static final int NO_MILLISECONDS = -2;
48 * Supported digit set. If the wrapped DateFormat uses
49 * a different unit set, the millisecond pattern
50 * will not be recognized and duplicate requests
53 private static final String DIGITS = "0123456789";
56 * Constant used to represent that there was an
57 * observed change, but was an expected change.
59 public static final int UNRECOGNIZED_MILLISECONDS = -1;
62 * First magic number used to detect the millisecond position.
64 private static final int MAGIC1 = 654;
67 * Expected representation of first magic number.
69 private static final String MAGICSTRING1 = "654";
72 * Second magic number used to detect the millisecond position.
74 private static final int MAGIC2 = 987;
77 * Expected representation of second magic number.
79 private static final String MAGICSTRING2 = "987";
82 * Expected representation of 0 milliseconds.
84 private static final String ZERO_STRING = "000";
89 private final DateFormat formatter;
92 * Index of initial digit of millisecond pattern or
93 * UNRECOGNIZED_MILLISECONDS or NO_MILLISECONDS.
95 private int millisecondStart;
98 * Integral second preceding the previous convered Date.
100 private long slotBegin;
103 * Cache of previous conversion.
105 private StringBuffer cache = new StringBuffer(50);
108 * Maximum validity period for the cache.
109 * Typically 1, use cache for duplicate requests only, or
110 * 1000, use cache for requests within the same integral second.
112 private final int expiration;
115 * Date requested in previous conversion.
117 private long previousTime;
120 * Scratch date object used to minimize date object creation.
122 private final Date tmpDate = new Date(0);
125 * Creates a new CachedDateFormat object.
126 * @param dateFormat Date format, may not be null.
127 * @param expiration maximum cached range in milliseconds.
128 * If the dateFormat is known to be incompatible with the
129 * caching algorithm, use a value of 0 to totally disable
130 * caching or 1 to only use cache for duplicate requests.
132 public CachedDateFormat(final DateFormat dateFormat, final int expiration) {
133 if (dateFormat == null) {
134 throw new IllegalArgumentException("dateFormat cannot be null");
137 if (expiration < 0) {
138 throw new IllegalArgumentException("expiration must be non-negative");
141 formatter = dateFormat;
142 this.expiration = expiration;
143 millisecondStart = 0;
146 // set the previousTime so the cache will be invalid
147 // for the next request.
148 previousTime = Long.MIN_VALUE;
149 slotBegin = Long.MIN_VALUE;
153 * Finds start of millisecond field in formatted time.
154 * @param time long time, must be integral number of seconds
155 * @param formatted String corresponding formatted string
156 * @param formatter DateFormat date format
157 * @return int position in string of first digit of milliseconds,
158 * -1 indicates no millisecond field, -2 indicates unrecognized
159 * field (likely RelativeTimeDateFormat)
161 public static int findMillisecondStart(
162 final long time, final String formatted, final DateFormat formatter) {
163 long slotBegin = (time / 1000) * 1000;
165 if (slotBegin > time) {
169 int millis = (int) (time - slotBegin);
172 String magicString = MAGICSTRING1;
174 if (millis == MAGIC1) {
176 magicString = MAGICSTRING2;
179 String plusMagic = formatter.format(new Date(slotBegin + magic));
182 * If the string lengths differ then
183 * we can't use the cache except for duplicate requests.
185 if (plusMagic.length() != formatted.length()) {
186 return UNRECOGNIZED_MILLISECONDS;
188 // find first difference between values
189 for (int i = 0; i < formatted.length(); i++) {
190 if (formatted.charAt(i) != plusMagic.charAt(i)) {
192 // determine the expected digits for the base time
193 StringBuffer formattedMillis = new StringBuffer("ABC");
194 millisecondFormat(millis, formattedMillis, 0);
196 String plusZero = formatter.format(new Date(slotBegin));
198 // If the next 3 characters match the magic
199 // string and the expected string
201 (plusZero.length() == formatted.length())
202 && magicString.regionMatches(
203 0, plusMagic, i, magicString.length())
204 && formattedMillis.toString().regionMatches(
205 0, formatted, i, magicString.length())
206 && ZERO_STRING.regionMatches(
207 0, plusZero, i, ZERO_STRING.length())) {
210 return UNRECOGNIZED_MILLISECONDS;
216 return NO_MILLISECONDS;
220 * Formats a Date into a date/time string.
222 * @param date the date to format.
223 * @param sbuf the string buffer to write to.
224 * @param fieldPosition remains untouched.
225 * @return the formatted time string.
227 public StringBuffer format(
228 Date date, StringBuffer sbuf, FieldPosition fieldPosition) {
229 format(date.getTime(), sbuf);
235 * Formats a millisecond count into a date/time string.
237 * @param now Number of milliseconds after midnight 1 Jan 1970 GMT.
238 * @param buf the string buffer to write to.
239 * @return the formatted time string.
241 public StringBuffer format(long now, StringBuffer buf) {
243 // If the current requested time is identical to the previously
244 // requested time, then append the cache contents.
246 if (now == previousTime) {
253 // If millisecond pattern was not unrecognized
254 // (that is if it was found or milliseconds did not appear)
256 if (millisecondStart != UNRECOGNIZED_MILLISECONDS &&
257 // Check if the cache is still valid.
258 // If the requested time is within the same integral second
259 // as the last request and a shorter expiration was not requested.
260 (now < (slotBegin + expiration)) && (now >= slotBegin)
261 && (now < (slotBegin + 1000L))) {
263 // if there was a millisecond field then update it
265 if (millisecondStart >= 0) {
266 millisecondFormat((int) (now - slotBegin), cache, millisecondStart);
270 // update the previously requested time
271 // (the slot begin should be unchanged)
279 // could not use previous value.
280 // Call underlying formatter to format date.
282 tmpDate.setTime(now);
283 cache.append(formatter.format(tmpDate));
286 slotBegin = (previousTime / 1000) * 1000;
288 if (slotBegin > previousTime) {
293 // if the milliseconds field was previous found
294 // then reevaluate in case it moved.
296 if (millisecondStart >= 0) {
298 findMillisecondStart(now, cache.toString(), formatter);
305 * Formats a count of milliseconds (0-999) into a numeric representation.
306 * @param millis Millisecond coun between 0 and 999.
307 * @param buf String buffer, may not be null.
308 * @param offset Starting position in buffer, the length of the
309 * buffer must be at least offset + 3.
311 private static void millisecondFormat(
312 final int millis, final StringBuffer buf, final int offset) {
313 buf.setCharAt(offset, DIGITS.charAt(millis / 100));
314 buf.setCharAt(offset + 1, DIGITS.charAt((millis / 10) % 10));
315 buf.setCharAt(offset + 2, DIGITS.charAt(millis % 10));
321 * Setting the timezone using getCalendar().setTimeZone()
322 * will likely cause caching to misbehave.
323 * @param timeZone TimeZone new timezone
325 public void setTimeZone(final TimeZone timeZone) {
326 formatter.setTimeZone(timeZone);
327 previousTime = Long.MIN_VALUE;
328 slotBegin = Long.MIN_VALUE;
332 * This method is delegated to the formatter which most
333 * likely returns null.
334 * @param s string representation of date.
335 * @param pos field position, unused.
336 * @return parsed date, likely null.
338 public Date parse(String s, ParsePosition pos) {
339 return formatter.parse(s, pos);
343 * Gets number formatter.
345 * @return NumberFormat number formatter
347 public NumberFormat getNumberFormat() {
348 return formatter.getNumberFormat();
352 * Gets maximum cache validity for the specified SimpleDateTime
353 * conversion pattern.
354 * @param pattern conversion pattern, may not be null.
355 * @return Duration in milliseconds from an integral second
356 * that the cache will return consistent results.
358 public static int getMaximumCacheValidity(final String pattern) {
360 // If there are more "S" in the pattern than just one "SSS" then
361 // (for example, "HH:mm:ss,SSS SSS"), then set the expiration to
362 // one millisecond which should only perform duplicate request caching.
364 int firstS = pattern.indexOf('S');
366 if ((firstS >= 0) && (firstS != pattern.lastIndexOf("SSS"))) {