org.x4juli.formatter.pattern
Class CachedDateFormat

java.lang.Object
  extended byjava.text.Format
      extended byjava.text.DateFormat
          extended byorg.x4juli.formatter.pattern.CachedDateFormat
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class CachedDateFormat
extends java.text.DateFormat

CachedDateFormat optimizes the performance of a wrapped DateFormat. The implementation is not thread-safe. If the millisecond pattern is not recognized, the class will only use the cache if the same value is requested.

Logging API as a whole was originally done for Apache log4j. Juli is a port of main parts of that to complete the Java Logging APIs. All credits for initial idea, design, implementation, documentation belong to the log4j crew. This file was originally published by Curt Arnold. Please use exclusively the appropriate mailing lists for questions, remarks and contribution.

Since:
0.5
Author:
Boris Unckel
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class java.text.DateFormat
java.text.DateFormat.Field
 
Nested classes inherited from class java.text.Format
 
Field Summary
private  java.lang.StringBuffer cache
          Cache of previous conversion.
private static java.lang.String DIGITS
          Supported digit set.
private  int expiration
          Maximum validity period for the cache.
private  java.text.DateFormat formatter
          Wrapped formatter.
private static int MAGIC1
          First magic number used to detect the millisecond position.
private static int MAGIC2
          Second magic number used to detect the millisecond position.
private static java.lang.String MAGICSTRING1
          Expected representation of first magic number.
private static java.lang.String MAGICSTRING2
          Expected representation of second magic number.
private  int millisecondStart
          Index of initial digit of millisecond pattern or UNRECOGNIZED_MILLISECONDS or NO_MILLISECONDS.
static int NO_MILLISECONDS
          Constant used to represent that there was no change observed when changing the millisecond count.
private static int ONE_HUNDRED
           
private static int ONE_THOUSAND
           
private static long ONE_THOUSAND_L
           
private  long previousTime
          Date requested in previous conversion.
private static long serialVersionUID
           
private  long slotBegin
          Integral second preceding the previous convered Date.
private static int TEN
           
private  java.util.Date tmpDate
          Scratch date object used to minimize date object creation.
static int UNRECOGNIZED_MILLISECONDS
          Constant used to represent that there was an observed change, but was an expected change.
private static java.lang.String ZERO_STRING
          Expected representation of 0 milliseconds.
 
Fields inherited from class java.text.DateFormat
AM_PM_FIELD, calendar, DATE_FIELD, DAY_OF_WEEK_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD, DAY_OF_YEAR_FIELD, DEFAULT, ERA_FIELD, FULL, HOUR_OF_DAY0_FIELD, HOUR_OF_DAY1_FIELD, HOUR0_FIELD, HOUR1_FIELD, LONG, MEDIUM, MILLISECOND_FIELD, MINUTE_FIELD, MONTH_FIELD, numberFormat, SECOND_FIELD, SHORT, TIMEZONE_FIELD, WEEK_OF_MONTH_FIELD, WEEK_OF_YEAR_FIELD, YEAR_FIELD
 
Constructor Summary
CachedDateFormat(java.text.DateFormat dateFormat, int expiration)
          Creates a new CachedDateFormat object.
 
Method Summary
static int findMillisecondStart(long time, java.lang.String formatted, java.text.DateFormat formatter)
          Finds start of millisecond field in formatted time.
 java.lang.StringBuffer format(java.util.Date date, java.lang.StringBuffer sbuf, java.text.FieldPosition fieldPosition)
          Formats a Date into a date/time string.
 java.lang.StringBuffer format(long now, java.lang.StringBuffer buf)
          Formats a millisecond count into a date/time string.
static int getMaximumCacheValidity(java.lang.String pattern)
          Gets maximum cache validity for the specified SimpleDateTime conversion pattern.
 java.text.NumberFormat getNumberFormat()
          Gets number formatter.
private static void millisecondFormat(int millis, java.lang.StringBuffer buf, int offset)
          Formats a count of milliseconds (0-999) into a numeric representation.
 java.util.Date parse(java.lang.String s, java.text.ParsePosition pos)
          This method is delegated to the formatter which most likely returns null.
 void setTimeZone(java.util.TimeZone timeZone)
          Set timezone.
 java.lang.String toString()
          
 
Methods inherited from class java.text.DateFormat
clone, equals, format, format, getAvailableLocales, getCalendar, getDateInstance, getDateInstance, getDateInstance, getDateTimeInstance, getDateTimeInstance, getDateTimeInstance, getInstance, getTimeInstance, getTimeInstance, getTimeInstance, getTimeZone, hashCode, isLenient, parse, parseObject, setCalendar, setLenient, setNumberFormat
 
Methods inherited from class java.text.Format
format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_MILLISECONDS

public static final int NO_MILLISECONDS
Constant used to represent that there was no change observed when changing the millisecond count.
Value is -2.

See Also:
Constant Field Values

UNRECOGNIZED_MILLISECONDS

public static final int UNRECOGNIZED_MILLISECONDS
Constant used to represent that there was an observed change, but was an expected change.
Value is -1.

See Also:
Constant Field Values

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

TEN

private static final int TEN
See Also:
Constant Field Values

ONE_HUNDRED

private static final int ONE_HUNDRED
See Also:
Constant Field Values

ONE_THOUSAND_L

private static final long ONE_THOUSAND_L
See Also:
Constant Field Values

ONE_THOUSAND

private static final int ONE_THOUSAND
See Also:
Constant Field Values

DIGITS

private static final java.lang.String DIGITS
Supported digit set. If the wrapped DateFormat uses a different unit set, the millisecond pattern will not be recognized and duplicate requests will use the cache.

See Also:
Constant Field Values

MAGIC1

private static final int MAGIC1
First magic number used to detect the millisecond position.

See Also:
Constant Field Values

MAGICSTRING1

private static final java.lang.String MAGICSTRING1
Expected representation of first magic number.

See Also:
Constant Field Values

MAGIC2

private static final int MAGIC2
Second magic number used to detect the millisecond position.

See Also:
Constant Field Values

MAGICSTRING2

private static final java.lang.String MAGICSTRING2
Expected representation of second magic number.

See Also:
Constant Field Values

ZERO_STRING

private static final java.lang.String ZERO_STRING
Expected representation of 0 milliseconds.

See Also:
Constant Field Values

formatter

private final java.text.DateFormat formatter
Wrapped formatter.


millisecondStart

private int millisecondStart
Index of initial digit of millisecond pattern or UNRECOGNIZED_MILLISECONDS or NO_MILLISECONDS.


slotBegin

private long slotBegin
Integral second preceding the previous convered Date.


cache

private java.lang.StringBuffer cache
Cache of previous conversion.


expiration

private final int expiration
Maximum validity period for the cache. Typically 1, use cache for duplicate requests only, or 1000, use cache for requests within the same integral second.


previousTime

private long previousTime
Date requested in previous conversion.


tmpDate

private final java.util.Date tmpDate
Scratch date object used to minimize date object creation.

Constructor Detail

CachedDateFormat

public CachedDateFormat(java.text.DateFormat dateFormat,
                        int expiration)
Creates a new CachedDateFormat object.

Parameters:
dateFormat - Date format, may not be null.
expiration - maximum cached range in milliseconds. If the dateFormat is known to be incompatible with the caching algorithm, use a value of 0 to totally disable caching or 1 to only use cache for duplicate requests.
Since:
0.5
Method Detail

findMillisecondStart

public static int findMillisecondStart(long time,
                                       java.lang.String formatted,
                                       java.text.DateFormat formatter)
Finds start of millisecond field in formatted time.

Parameters:
time - long time, must be integral number of seconds
formatted - String corresponding formatted string
formatter - DateFormat date format
Returns:
int position in string of first digit of milliseconds, -1 indicates no millisecond field, -2 indicates unrecognized field (likely RelativeTimeDateFormat)
Since:
0.5

format

public java.lang.StringBuffer format(java.util.Date date,
                                     java.lang.StringBuffer sbuf,
                                     java.text.FieldPosition fieldPosition)
Formats a Date into a date/time string.

Parameters:
date - the date to format.
sbuf - the string buffer to write to.
fieldPosition - remains untouched.
Returns:
the formatted time string.
Since:
0.5

format

public java.lang.StringBuffer format(long now,
                                     java.lang.StringBuffer buf)
Formats a millisecond count into a date/time string.

Parameters:
now - Number of milliseconds after midnight 1 Jan 1970 GMT.
buf - the string buffer to write to.
Returns:
the formatted time string.
Since:
0.5

millisecondFormat

private static void millisecondFormat(int millis,
                                      java.lang.StringBuffer buf,
                                      int offset)
Formats a count of milliseconds (0-999) into a numeric representation.

Parameters:
millis - Millisecond coun between 0 and 999.
buf - String buffer, may not be null.
offset - Starting position in buffer, the length of the buffer must be at least offset + 3.
Since:
0.5

setTimeZone

public void setTimeZone(java.util.TimeZone timeZone)
Set timezone. Setting the timezone using getCalendar().setTimeZone() will likely cause caching to misbehave.

Parameters:
timeZone - TimeZone new timezone
Since:
0.5

parse

public java.util.Date parse(java.lang.String s,
                            java.text.ParsePosition pos)
This method is delegated to the formatter which most likely returns null.

Parameters:
s - string representation of date.
pos - field position, unused.
Returns:
parsed date, likely null.
Since:
0.5

getNumberFormat

public java.text.NumberFormat getNumberFormat()
Gets number formatter.

Returns:
NumberFormat number formatter
Since:
0.5

getMaximumCacheValidity

public static int getMaximumCacheValidity(java.lang.String pattern)
Gets maximum cache validity for the specified SimpleDateTime conversion pattern.

Parameters:
pattern - conversion pattern, may not be null.
Returns:
Duration in milliseconds from an integral second that the cache will return consistent results.
Since:
0.5

toString

public java.lang.String toString()

Since:
0.5


${license_html}