ppkarwasz commented on code in PR #2424:
URL: https://github.com/apache/logging-log4j2/pull/2424#discussion_r1544546233


##########
log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java:
##########
@@ -649,45 +674,57 @@ public static Map<String, Properties> 
partitionOnCommonPrefixes(
      * @return true if system properties tell us we are running on Windows.
      */
     public boolean isOsWindows() {
-        return getStringProperty("os.name", "").startsWith("Windows");
+        return SystemPropertiesPropertySource.getSystemProperty("os.name", 
"").startsWith("Windows");
+    }
+
+    static Duration parseDuration(final String value) {
+        final Matcher matcher = DURATION_PATTERN.matcher(value);
+        if (matcher.matches()) {
+            return Duration.of(TimeUnit.parseAmount(matcher), 
TimeUnit.parseUnit(matcher));
+        }
+        throw new DateTimeParseException("Invalid duration value: should be in 
the format nnn[unit].", value, 0);
     }
 
     private enum TimeUnit {
-        NANOS("ns,nano,nanos,nanosecond,nanoseconds", ChronoUnit.NANOS),
-        MICROS("us,micro,micros,microsecond,microseconds", ChronoUnit.MICROS),
-        MILLIS("ms,milli,millis,millsecond,milliseconds", ChronoUnit.MILLIS),
-        SECONDS("s,second,seconds", ChronoUnit.SECONDS),
-        MINUTES("m,minute,minutes", ChronoUnit.MINUTES),
-        HOURS("h,hour,hours", ChronoUnit.HOURS),
-        DAYS("d,day,days", ChronoUnit.DAYS);
-
-        /*
-         * https://errorprone.info/bugpattern/ImmutableEnumChecker
-         * This field is effectively immutable.
-         */
-        @SuppressWarnings("ImmutableEnumChecker")
-        private final String[] descriptions;
+        NANOS(new String[] {"ns", "nano", "nanos", "nanosecond", 
"nanoseconds"}, ChronoUnit.NANOS),
+        MICROS(new String[] {"us", "micro", "micros", "microsecond", 
"microseconds"}, ChronoUnit.MICROS),
+        MILLIS(new String[] {"ms", "milli", "millis", "millisecond", 
"milliseconds"}, ChronoUnit.MILLIS),
+        SECONDS(new String[] {"s", "second", "seconds"}, ChronoUnit.SECONDS),
+        MINUTES(new String[] {"m", "minute", "minutes"}, ChronoUnit.MINUTES),
+        HOURS(new String[] {"h", "hour", "hours"}, ChronoUnit.HOURS),
+        DAYS(new String[] {"d", "day", "days"}, ChronoUnit.DAYS);
 
-        private final ChronoUnit timeUnit;
+        private final String[] descriptions;
+        private final TemporalUnit timeUnit;
 
-        TimeUnit(final String descriptions, final ChronoUnit timeUnit) {
-            this.descriptions = descriptions.split(",");
+        TimeUnit(final String[] descriptions, final TemporalUnit timeUnit) {
+            this.descriptions = descriptions;
             this.timeUnit = timeUnit;
         }
 
-        static Duration getDuration(final String time) {
-            final String value = time.trim();
-            TemporalUnit temporalUnit = ChronoUnit.MILLIS;
-            long timeVal = 0;
-            for (TimeUnit timeUnit : values()) {
-                for (String suffix : timeUnit.descriptions) {
-                    if (value.endsWith(suffix)) {
-                        temporalUnit = timeUnit.timeUnit;
-                        timeVal = Long.parseLong(value.substring(0, 
value.length() - suffix.length()));
+        private static long parseAmount(final MatchResult matcher) {
+            final String amount = matcher.group(1);
+            try {
+                return Long.parseLong(amount);
+            } catch (final NumberFormatException e) {
+                throw new DateTimeParseException(
+                        "Invalid time amount '" + amount + "'", 
matcher.group(), matcher.start(1), e);
+            }
+        }
+
+        private static TemporalUnit parseUnit(final MatchResult matcher) {

Review Comment:
   `matcher` is used for error handling, but I can remove it.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to