Class InstantPatternDynamicFormatter
- java.lang.Object
-
- org.apache.logging.log4j.core.util.internal.instant.InstantPatternDynamicFormatter
-
- All Implemented Interfaces:
InstantFormatter,InstantPatternFormatter
final class InstantPatternDynamicFormatter extends java.lang.Object implements InstantPatternFormatter
AnInstantPatternFormatterthat usesDateTimeFormatterunder the hood. The pattern is analyzed and parts that require a precision lower than or equal to are precomputed, cached, and updated once every . The rest is computed dynamically.For instance, given the pattern
yyyy-MM-dd'T'HH:mm:ss.SSSX, the generated formatter will- Sequence the pattern and assign a time precision to each part (e.g.,
MMis of month precision) - Precompute and cache the output for parts that are of precision lower than or equal to (i.e.,
yyyy-MM-dd'T'HH:mm:andX) and cache it - Upon a formatting request, combine the cached outputs with the dynamic parts (i.e.,
ss.SSS)
- Since:
- 2.25.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classInstantPatternDynamicFormatter.AbstractFormatter(package private) static classInstantPatternDynamicFormatter.DynamicPatternSequenceCreates formatters that useDateTimeFormatter.(package private) static classInstantPatternDynamicFormatter.PatternSequence(package private) static classInstantPatternDynamicFormatter.SecondPatternSequence(package private) static classInstantPatternDynamicFormatter.StaticPatternSequenceprivate static classInstantPatternDynamicFormatter.TimestampedFormatter-
Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.util.internal.instant.InstantPatternFormatter
InstantPatternFormatter.Builder
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.time.temporal.ChronoUnitPRECISION_THRESHOLDprivate java.util.concurrent.atomic.AtomicReference<InstantPatternDynamicFormatter.TimestampedFormatter>timestampedFormatterRef-
Fields inherited from interface org.apache.logging.log4j.core.util.internal.instant.InstantPatternFormatter
LEGACY_FORMATTERS_ENABLED
-
-
Constructor Summary
Constructors Constructor Description InstantPatternDynamicFormatter(java.lang.String pattern, java.util.Locale locale, java.util.TimeZone timeZone)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static InstantPatternFormattercreateFormatter(java.lang.String pattern, java.util.Locale locale, java.util.TimeZone timeZone, java.time.temporal.ChronoUnit precisionThreshold, Instant creationInstant)private static InstantPatternDynamicFormatter.TimestampedFormattercreateTimestampedFormatter(java.lang.String pattern, java.util.Locale locale, java.util.TimeZone timeZone, @Nullable Instant creationInstant)voidformatTo(java.lang.StringBuilder buffer, Instant instant)private InstantPatternFormattergetEffectiveFormatter(Instant instant)java.util.LocalegetLocale()java.lang.StringgetPattern()java.time.temporal.ChronoUnitgetPrecision()private static InstantPatternDynamicFormatter.PatternSequencegetStaticPatternSequence(java.lang.String pattern, int startIndex, int endIndex)java.util.TimeZonegetTimeZone()private static booleanisDynamicPatternLetter(char c)private static java.util.List<InstantPatternDynamicFormatter.PatternSequence>mergeFactories(java.util.List<InstantPatternDynamicFormatter.PatternSequence> sequences, java.time.temporal.ChronoUnit precisionThreshold)private static java.time.temporal.ChronoUnitmin(java.time.temporal.ChronoUnit left, java.time.temporal.ChronoUnit right)private static java.util.List<InstantPatternDynamicFormatter.PatternSequence>sequencePattern(java.lang.String pattern)(package private) static java.util.List<InstantPatternDynamicFormatter.PatternSequence>sequencePattern(java.lang.String pattern, java.time.temporal.ChronoUnit precisionThreshold)private static longtoEpochMinutes(Instant instant)private static java.time.temporal.TemporalAccessortoTemporalAccessor(Instant instant)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.logging.log4j.core.util.internal.instant.InstantFormatter
format
-
-
-
-
Field Detail
-
PRECISION_THRESHOLD
static final java.time.temporal.ChronoUnit PRECISION_THRESHOLD
-
timestampedFormatterRef
private final java.util.concurrent.atomic.AtomicReference<InstantPatternDynamicFormatter.TimestampedFormatter> timestampedFormatterRef
-
-
Method Detail
-
getPattern
public java.lang.String getPattern()
- Specified by:
getPatternin interfaceInstantPatternFormatter
-
getLocale
public java.util.Locale getLocale()
- Specified by:
getLocalein interfaceInstantPatternFormatter
-
getTimeZone
public java.util.TimeZone getTimeZone()
- Specified by:
getTimeZonein interfaceInstantPatternFormatter
-
getPrecision
public java.time.temporal.ChronoUnit getPrecision()
- Specified by:
getPrecisionin interfaceInstantFormatter- Returns:
- the time precision of the formatted output
-
formatTo
public void formatTo(java.lang.StringBuilder buffer, Instant instant)- Specified by:
formatToin interfaceInstantFormatter
-
getEffectiveFormatter
private InstantPatternFormatter getEffectiveFormatter(Instant instant)
-
createTimestampedFormatter
private static InstantPatternDynamicFormatter.TimestampedFormatter createTimestampedFormatter(java.lang.String pattern, java.util.Locale locale, java.util.TimeZone timeZone, @Nullable Instant creationInstant)
-
createFormatter
private static InstantPatternFormatter createFormatter(java.lang.String pattern, java.util.Locale locale, java.util.TimeZone timeZone, java.time.temporal.ChronoUnit precisionThreshold, Instant creationInstant)
-
min
private static java.time.temporal.ChronoUnit min(java.time.temporal.ChronoUnit left, java.time.temporal.ChronoUnit right)
-
sequencePattern
static java.util.List<InstantPatternDynamicFormatter.PatternSequence> sequencePattern(java.lang.String pattern, java.time.temporal.ChronoUnit precisionThreshold)
-
sequencePattern
private static java.util.List<InstantPatternDynamicFormatter.PatternSequence> sequencePattern(java.lang.String pattern)
-
getStaticPatternSequence
private static InstantPatternDynamicFormatter.PatternSequence getStaticPatternSequence(java.lang.String pattern, int startIndex, int endIndex)
-
isDynamicPatternLetter
private static boolean isDynamicPatternLetter(char c)
-
mergeFactories
private static java.util.List<InstantPatternDynamicFormatter.PatternSequence> mergeFactories(java.util.List<InstantPatternDynamicFormatter.PatternSequence> sequences, java.time.temporal.ChronoUnit precisionThreshold)
Merges pattern sequences usingInstantPatternDynamicFormatter.PatternSequence.tryMerge(org.apache.logging.log4j.core.util.internal.instant.InstantPatternDynamicFormatter.PatternSequence, java.time.temporal.ChronoUnit).Example
For example, given the
yyyy-MM-dd'T'HH:mm:ss.SSSpattern, a precision threshold ofChronoUnit.MINUTESand the three implementations (InstantPatternDynamicFormatter.DynamicPatternSequence,InstantPatternDynamicFormatter.StaticPatternSequenceandInstantPatternDynamicFormatter.SecondPatternSequence) from this class, this method will combine pattern sequences associated withyyyy-MM-dd'T'HH:mm:into a single sequence, since these are consecutive and effectively constant sequences.[ dateTimeFormatter(pattern="yyyy", precision=YEARS), static(literal="-"), dateTimeFormatter(pattern="MM", precision=MONTHS), static(literal="-"), dateTimeFormatter(pattern="dd", precision=DAYS), static(literal="T"), dateTimeFormatter(pattern="HH", precision=HOURS), static(literal=":"), dateTimeFormatter(pattern="mm", precision=MINUTES), static(literal=":"), second(pattern="ss", precision=SECONDS), static(literal="."), second(pattern="SSS", precision=MILLISECONDS) dateTimeFormatter(pattern="X", precision=HOURS), ]The above sequencing implies creation of 12
InstantPatternDynamicFormatter.AbstractFormatters. This method transforms it to the following:[ dateTimeFormatter(pattern="yyyy-MM-dd'T'HH:mm", precision=MINUTES), second(pattern="ss.SSS", precision=MILLISECONDS), dateTimeFormatter(pattern="X", precision=MINUTES) ]The resultant sequencing effectively translates to 3
InstantPatternDynamicFormatter.AbstractFormatters.- Parameters:
sequences- a list of pattern formatter factoriesprecisionThreshold- a precision threshold to determine effectively constant sequences- Returns:
- transformed sequencing, where sequences that are effectively constant or effectively dynamic are merged.
-
toEpochMinutes
private static long toEpochMinutes(Instant instant)
-
toTemporalAccessor
private static java.time.temporal.TemporalAccessor toTemporalAccessor(Instant instant)
-
-