vy commented on code in PR #3338:
URL: https://github.com/apache/logging-log4j2/pull/3338#discussion_r1899184613


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternDynamicFormatter.java:
##########
@@ -711,23 +488,114 @@ public void formatTo(final StringBuilder buffer, final 
Instant instant) {
                 }
             };
         }
+
+        @Override
+        @Nullable
+        PatternFormatterFactory tryMerge(PatternFormatterFactory other, 
ChronoUnit thresholdPrecision) {
+            // We always merge consecutive static pattern factories
+            if (other instanceof StaticPatternFormatterFactory) {
+                final StaticPatternFormatterFactory otherStatic = 
(StaticPatternFormatterFactory) other;
+                return new StaticPatternFormatterFactory(this.literal + 
otherStatic.literal);
+            }
+            // We also merge a static pattern factory with a DTF factory
+            if (other instanceof DateTimeFormatterPatternFormatterFactory) {
+                final DateTimeFormatterPatternFormatterFactory otherDtf =
+                        (DateTimeFormatterPatternFormatterFactory) other;
+                return new DateTimeFormatterPatternFormatterFactory(
+                        this.pattern + otherDtf.pattern, otherDtf.precision);
+            }
+            return super.tryMerge(other, thresholdPrecision);
+        }
+
+        static String escapeLiteral(String literal) {
+            StringBuilder sb = new StringBuilder(literal.length() + 2);
+            boolean inSingleQuotes = false;
+            for (int i = 0; i < literal.length(); i++) {
+                char c = literal.charAt(i);
+                if (c == '\'') {
+                    if (inSingleQuotes) {
+                        sb.append("'");
+                    }
+                    inSingleQuotes = false;
+                    sb.append("''");
+                } else {
+                    if (!inSingleQuotes) {
+                        sb.append("'");
+                    }
+                    inSingleQuotes = true;
+                    sb.append(c);
+                }
+            }
+            if (inSingleQuotes) {
+                sb.append("'");
+            }
+            return sb.toString();
+        }
     }
 
-    static final class DynamicPatternSequence extends PatternSequence {
+    /**
+     * Formats the pattern using {@link DateTimeFormatter}.
+     */
+    static final class DateTimeFormatterPatternFormatterFactory extends 
PatternFormatterFactory {
 
-        DynamicPatternSequence(final String content) {
-            super(content, contentPrecision(content));
+        /**
+         * @param simplePattern A {@link DateTimeFormatter} pattern containing 
a single letter.

Review Comment:
   Could you also fix the `simplePattern` usage in `patternPrecision()` too, 
please?



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/instant/InstantPatternDynamicFormatter.java:
##########
@@ -711,23 +488,114 @@ public void formatTo(final StringBuilder buffer, final 
Instant instant) {
                 }
             };
         }
+
+        @Override
+        @Nullable
+        PatternFormatterFactory tryMerge(PatternFormatterFactory other, 
ChronoUnit thresholdPrecision) {
+            // We always merge consecutive static pattern factories
+            if (other instanceof StaticPatternFormatterFactory) {
+                final StaticPatternFormatterFactory otherStatic = 
(StaticPatternFormatterFactory) other;
+                return new StaticPatternFormatterFactory(this.literal + 
otherStatic.literal);
+            }
+            // We also merge a static pattern factory with a DTF factory
+            if (other instanceof DateTimeFormatterPatternFormatterFactory) {
+                final DateTimeFormatterPatternFormatterFactory otherDtf =
+                        (DateTimeFormatterPatternFormatterFactory) other;
+                return new DateTimeFormatterPatternFormatterFactory(
+                        this.pattern + otherDtf.pattern, otherDtf.precision);
+            }
+            return super.tryMerge(other, thresholdPrecision);
+        }
+
+        static String escapeLiteral(String literal) {
+            StringBuilder sb = new StringBuilder(literal.length() + 2);
+            boolean inSingleQuotes = false;
+            for (int i = 0; i < literal.length(); i++) {
+                char c = literal.charAt(i);
+                if (c == '\'') {
+                    if (inSingleQuotes) {
+                        sb.append("'");
+                    }
+                    inSingleQuotes = false;
+                    sb.append("''");
+                } else {
+                    if (!inSingleQuotes) {
+                        sb.append("'");
+                    }
+                    inSingleQuotes = true;
+                    sb.append(c);
+                }
+            }
+            if (inSingleQuotes) {
+                sb.append("'");
+            }
+            return sb.toString();
+        }
     }
 
-    static final class DynamicPatternSequence extends PatternSequence {
+    /**
+     * Formats the pattern using {@link DateTimeFormatter}.
+     */
+    static final class DateTimeFormatterPatternFormatterFactory extends 
PatternFormatterFactory {
 
-        DynamicPatternSequence(final String content) {
-            super(content, contentPrecision(content));
+        /**
+         * @param simplePattern A {@link DateTimeFormatter} pattern containing 
a single letter.
+         */
+        DateTimeFormatterPatternFormatterFactory(final String simplePattern) {
+            this(simplePattern, patternPrecision(simplePattern));
         }
 
         /**
-         * @param content a single-letter directive content complying (e.g., 
{@code H}, {@code HH}, or {@code pHH})
-         * @return the time precision of the directive
+         * @param pattern Any {@link DateTimeFormatter} pattern.
+         * @param precision The maximum interval of time over which this 
pattern is constant.
          */
+        DateTimeFormatterPatternFormatterFactory(final String pattern, final 
ChronoUnit precision) {
+            super(pattern, precision);
+        }
+
+        InstantPatternFormatter createFormatter(final Locale locale, final 
TimeZone timeZone) {
+            final DateTimeFormatter dateTimeFormatter =
+                    DateTimeFormatter.ofPattern(pattern, 
locale).withZone(timeZone.toZoneId());
+            return new AbstractFormatter(pattern, locale, timeZone, precision) 
{
+                @Override
+                public void formatTo(final StringBuilder buffer, final Instant 
instant) {
+                    final TemporalAccessor instantAccessor = 
toTemporalAccessor(instant);
+                    dateTimeFormatter.formatTo(instantAccessor, buffer);
+                }
+            };
+        }
+
+        @Override
         @Nullable
-        private static ChronoUnit contentPrecision(final String content) {
+        PatternFormatterFactory tryMerge(PatternFormatterFactory other, 
ChronoUnit thresholdPrecision) {

Review Comment:
   I was expecting documentation on what this particular `tryMerge()` is doing. 
But I leave it to your discretion.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to