Class DateFormatCache


  • public class DateFormatCache
    extends java.lang.Object

    Cache structure for SimpleDateFormat formatted timestamps based on seconds.

    Millisecond formatting using S is not supported. You should add the millisecond information after getting back the second formatting.

    The cache consists of entries for a consecutive range of seconds. The length of the range is configurable. It is implemented based on a cyclic buffer. New entries shift the range.

    The cache is not thread safe. It can be used without synchronization via thread local instances, or with synchronization as a global cache.

    The cache can be created with a parent cache to build a cache hierarchy. Access to the parent cache is thread safe.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static char MSEC_PATTERN
      Pattern character used to replace 'S' (milliseconds) in format strings to make timestamps cacheable.
    • Constructor Summary

      Constructors 
      Constructor Description
      DateFormatCache​(int size, java.lang.String format, DateFormatCache parent)
      Create a new DateFormatCache.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getFormat​(long time)
      Get the formatted timestamp for the given time.
      java.lang.String getTimeFormat()
      Get the time format string used by this cache.
      protected static java.lang.String tidyFormat​(java.lang.String format)
      Replace the millisecond formatting character 'S' by some dummy characters in order to make the resulting formatted time stamps cacheable.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MSEC_PATTERN

        public static final char MSEC_PATTERN
        Pattern character used to replace 'S' (milliseconds) in format strings to make timestamps cacheable.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DateFormatCache

        public DateFormatCache​(int size,
                               java.lang.String format,
                               DateFormatCache parent)
        Create a new DateFormatCache.
        Parameters:
        size - The number of cached entries
        format - The timestamp format string
        parent - The parent cache for building a cache hierarchy
    • Method Detail

      • tidyFormat

        protected static java.lang.String tidyFormat​(java.lang.String format)
        Replace the millisecond formatting character 'S' by some dummy characters in order to make the resulting formatted time stamps cacheable. Our consumer might choose to replace the dummy chars with the actual milliseconds because that's relatively cheap.
        Parameters:
        format - the format
        Returns:
        the replacement format
      • getFormat

        public java.lang.String getFormat​(long time)
        Get the formatted timestamp for the given time.
        Parameters:
        time - The time in milliseconds
        Returns:
        the formatted timestamp string
      • getTimeFormat

        public java.lang.String getTimeFormat()
        Get the time format string used by this cache.
        Returns:
        the time format string