Skip to content

Commit a44e3c9

Browse files
committed
Final corrections
1 parent 60e9002 commit a44e3c9

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternDynamicFormatterTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.util.stream.IntStream;
3333
import java.util.stream.Stream;
3434
import org.apache.logging.log4j.core.time.MutableInstant;
35-
import org.apache.logging.log4j.core.util.internal.instant.InstantPatternDynamicFormatter.DateTimeFormatterPatternSequence;
35+
import org.apache.logging.log4j.core.util.internal.instant.InstantPatternDynamicFormatter.DynamicPatternSequence;
3636
import org.apache.logging.log4j.core.util.internal.instant.InstantPatternDynamicFormatter.PatternSequence;
3737
import org.apache.logging.log4j.core.util.internal.instant.InstantPatternDynamicFormatter.SecondPatternSequence;
3838
import org.apache.logging.log4j.core.util.internal.instant.InstantPatternDynamicFormatter.StaticPatternSequence;
@@ -59,56 +59,56 @@ static List<Arguments> sequencingTestCases() {
5959
testCases.add(Arguments.of(":'foo',", ChronoUnit.DAYS, singletonList(new StaticPatternSequence(":foo,"))));
6060

6161
// `SSSX` should be treated constant for daily updates
62-
testCases.add(Arguments.of("SSSX", ChronoUnit.DAYS, asList(pMilliSec(), pDtf("X"))));
62+
testCases.add(Arguments.of("SSSX", ChronoUnit.DAYS, asList(pMilliSec(), pDyn("X"))));
6363

6464
// `yyyyMMddHHmmssSSSX` instant cache updated hourly
6565
testCases.add(Arguments.of(
6666
"yyyyMMddHHmmssSSSX",
6767
ChronoUnit.HOURS,
68-
asList(pDtf("yyyyMMddHH", ChronoUnit.HOURS), pDtf("mm"), pSec("", 3), pDtf("X"))));
68+
asList(pDyn("yyyyMMddHH", ChronoUnit.HOURS), pDyn("mm"), pSec("", 3), pDyn("X"))));
6969

7070
// `yyyyMMddHHmmssSSSX` instant cache updated per minute
7171
testCases.add(Arguments.of(
7272
"yyyyMMddHHmmssSSSX",
7373
ChronoUnit.MINUTES,
74-
asList(pDtf("yyyyMMddHHmm", ChronoUnit.MINUTES), pSec("", 3), pDtf("X"))));
74+
asList(pDyn("yyyyMMddHHmm", ChronoUnit.MINUTES), pSec("", 3), pDyn("X"))));
7575

7676
// ISO9601 instant cache updated daily
7777
final String iso8601InstantPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX";
7878
testCases.add(Arguments.of(
7979
iso8601InstantPattern,
8080
ChronoUnit.DAYS,
8181
asList(
82-
pDtf("yyyy'-'MM'-'dd'T'", ChronoUnit.DAYS),
83-
pDtf("HH':'mm':'", ChronoUnit.MINUTES),
82+
pDyn("yyyy'-'MM'-'dd'T'", ChronoUnit.DAYS),
83+
pDyn("HH':'mm':'", ChronoUnit.MINUTES),
8484
pSec(".", 3),
85-
pDtf("X"))));
85+
pDyn("X"))));
8686

8787
// ISO9601 instant cache updated per minute
8888
testCases.add(Arguments.of(
8989
iso8601InstantPattern,
9090
ChronoUnit.MINUTES,
91-
asList(pDtf("yyyy'-'MM'-'dd'T'HH':'mm':'", ChronoUnit.MINUTES), pSec(".", 3), pDtf("X"))));
91+
asList(pDyn("yyyy'-'MM'-'dd'T'HH':'mm':'", ChronoUnit.MINUTES), pSec(".", 3), pDyn("X"))));
9292

9393
// ISO9601 instant cache updated per second
9494
testCases.add(Arguments.of(
9595
iso8601InstantPattern,
9696
ChronoUnit.SECONDS,
97-
asList(pDtf("yyyy'-'MM'-'dd'T'HH':'mm':'", ChronoUnit.MINUTES), pSec(".", 3), pDtf("X"))));
97+
asList(pDyn("yyyy'-'MM'-'dd'T'HH':'mm':'", ChronoUnit.MINUTES), pSec(".", 3), pDyn("X"))));
9898

9999
// Seconds and micros
100100
testCases.add(Arguments.of(
101-
"HH:mm:ss.SSSSSS", ChronoUnit.MINUTES, asList(pDtf("HH':'mm':'", ChronoUnit.MINUTES), pSec(".", 6))));
101+
"HH:mm:ss.SSSSSS", ChronoUnit.MINUTES, asList(pDyn("HH':'mm':'", ChronoUnit.MINUTES), pSec(".", 6))));
102102

103103
return testCases;
104104
}
105105

106-
private static DateTimeFormatterPatternSequence pDtf(final String singlePattern) {
107-
return new DateTimeFormatterPatternSequence(singlePattern);
106+
private static DynamicPatternSequence pDyn(final String singlePattern) {
107+
return new DynamicPatternSequence(singlePattern);
108108
}
109109

110-
private static DateTimeFormatterPatternSequence pDtf(final String pattern, final ChronoUnit precision) {
111-
return new DateTimeFormatterPatternSequence(pattern, precision);
110+
private static DynamicPatternSequence pDyn(final String pattern, final ChronoUnit precision) {
111+
return new DynamicPatternSequence(pattern, precision);
112112
}
113113

114114
private static SecondPatternSequence pSec(String separator, int fractionalDigits) {

log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternDynamicFormatter.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private static List<PatternSequence> sequencePattern(final String pattern) {
227227
sequence = new SecondPatternSequence(false, "", sequenceContent.length());
228228
break;
229229
default:
230-
sequence = new DateTimeFormatterPatternSequence(sequenceContent);
230+
sequence = new DynamicPatternSequence(sequenceContent);
231231
}
232232
sequences.add(sequence);
233233
startIndex = endIndex;
@@ -273,7 +273,7 @@ private static boolean isDynamicPatternLetter(final char c) {
273273
*
274274
* <p>
275275
* For example, given the {@code yyyy-MM-dd'T'HH:mm:ss.SSS} pattern, a precision threshold of {@link ChronoUnit#MINUTES}
276-
* and the three implementations ({@link DateTimeFormatterPatternSequence}, {@link StaticPatternSequence} and
276+
* and the three implementations ({@link DynamicPatternSequence}, {@link StaticPatternSequence} and
277277
* {@link SecondPatternSequence}) from this class,
278278
* this method will combine pattern sequences associated with {@code yyyy-MM-dd'T'HH:mm:} into a single sequence,
279279
* since these are consecutive and effectively constant sequences.
@@ -473,9 +473,9 @@ PatternSequence tryMerge(PatternSequence other, ChronoUnit thresholdPrecision) {
473473
return new StaticPatternSequence(this.literal + otherStatic.literal);
474474
}
475475
// We also merge a static pattern factory with a DTF factory
476-
if (other instanceof DateTimeFormatterPatternSequence) {
477-
final DateTimeFormatterPatternSequence otherDtf = (DateTimeFormatterPatternSequence) other;
478-
return new DateTimeFormatterPatternSequence(this.pattern + otherDtf.pattern, otherDtf.precision);
476+
if (other instanceof DynamicPatternSequence) {
477+
final DynamicPatternSequence otherDtf = (DynamicPatternSequence) other;
478+
return new DynamicPatternSequence(this.pattern + otherDtf.pattern, otherDtf.precision);
479479
}
480480
return null;
481481
}
@@ -509,20 +509,20 @@ static String escapeLiteral(String literal) {
509509
/**
510510
* Creates formatters that use {@link DateTimeFormatter}.
511511
*/
512-
static final class DateTimeFormatterPatternSequence extends PatternSequence {
512+
static final class DynamicPatternSequence extends PatternSequence {
513513

514514
/**
515515
* @param singlePattern A {@link DateTimeFormatter} pattern containing a single letter.
516516
*/
517-
DateTimeFormatterPatternSequence(final String singlePattern) {
517+
DynamicPatternSequence(final String singlePattern) {
518518
this(singlePattern, patternPrecision(singlePattern));
519519
}
520520

521521
/**
522522
* @param pattern Any {@link DateTimeFormatter} pattern.
523523
* @param precision The maximum interval of time over which this pattern is constant.
524524
*/
525-
DateTimeFormatterPatternSequence(final String pattern, final ChronoUnit precision) {
525+
DynamicPatternSequence(final String pattern, final ChronoUnit precision) {
526526
super(pattern, precision);
527527
}
528528

@@ -542,20 +542,20 @@ public void formatTo(final StringBuilder buffer, final Instant instant) {
542542
@Nullable
543543
PatternSequence tryMerge(PatternSequence other, ChronoUnit thresholdPrecision) {
544544
// We merge two DTF factories if they are both above or below the threshold
545-
if (other instanceof DateTimeFormatterPatternSequence) {
546-
final DateTimeFormatterPatternSequence otherDtf = (DateTimeFormatterPatternSequence) other;
545+
if (other instanceof DynamicPatternSequence) {
546+
final DynamicPatternSequence otherDtf = (DynamicPatternSequence) other;
547547
if (isConstantForDurationOf(thresholdPrecision)
548548
== otherDtf.isConstantForDurationOf(thresholdPrecision)) {
549549
ChronoUnit precision = this.precision.getDuration().compareTo(otherDtf.precision.getDuration()) < 0
550550
? this.precision
551551
: otherDtf.precision;
552-
return new DateTimeFormatterPatternSequence(this.pattern + otherDtf.pattern, precision);
552+
return new DynamicPatternSequence(this.pattern + otherDtf.pattern, precision);
553553
}
554554
}
555555
// We merge a static pattern factory
556556
if (other instanceof StaticPatternSequence) {
557557
final StaticPatternSequence otherStatic = (StaticPatternSequence) other;
558-
return new DateTimeFormatterPatternSequence(this.pattern + otherStatic.pattern, this.precision);
558+
return new DynamicPatternSequence(this.pattern + otherStatic.pattern, this.precision);
559559
}
560560
return null;
561561
}
@@ -674,11 +674,10 @@ private static String createPattern(boolean printSeconds, String separator, int
674674
}
675675

676676
private static ChronoUnit determinePrecision(boolean printSeconds, int digits) {
677-
return digits > 6
678-
? ChronoUnit.NANOS
679-
: digits > 3
680-
? ChronoUnit.MICROS
681-
: digits > 0 ? ChronoUnit.MILLIS : printSeconds ? ChronoUnit.SECONDS : ChronoUnit.FOREVER;
677+
if (digits > 6) return ChronoUnit.NANOS;
678+
if (digits > 3) return ChronoUnit.MICROS;
679+
if (digits > 0) return ChronoUnit.MILLIS;
680+
return printSeconds ? ChronoUnit.SECONDS : ChronoUnit.FOREVER;
682681
}
683682

684683
private static void formatSeconds(StringBuilder buffer, Instant instant) {

0 commit comments

Comments
 (0)