You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/02/24 02:28:21 UTC

[commons-lang] branch master updated (b03b21b -> f7c5c5c)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git.


    from b03b21b  Bump biz.aQute.bndlib from 5.2.0 to 5.3.0 #715.
     new c631406  Fix some formatting.
     new f7c5c5c  Add some toString() methods handy when debugging.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/commons/lang3/time/FastDateParser.java  | 171 ++++++++++++++-------
 1 file changed, 118 insertions(+), 53 deletions(-)


[commons-lang] 02/02: Add some toString() methods handy when debugging.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit f7c5c5c6db1f6455ee78fa578c3b13f88242a7dd
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Feb 23 21:28:16 2021 -0500

    Add some toString() methods handy when debugging.
---
 .../apache/commons/lang3/time/FastDateParser.java  | 79 +++++++++++++++++++---
 1 file changed, 69 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
index 5638a48..8447bc1 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
@@ -150,7 +150,7 @@ public class FastDateParser implements DateParser, Serializable {
     }
 
     /**
-     * Initialize derived fields from defining fields.
+     * Initializes derived fields from defining fields.
      * This is called from constructor and from readObject (de-serialization)
      *
      * @param definingCalendar the {@link java.util.Calendar} instance used to initialize this FastDateParser
@@ -175,6 +175,7 @@ public class FastDateParser implements DateParser, Serializable {
      * Holds strategy and field width
      */
     private static class StrategyAndWidth {
+
         final Strategy strategy;
         final int width;
 
@@ -191,6 +192,11 @@ public class FastDateParser implements DateParser, Serializable {
             lt.previous();
             return nextStrategy.isNumber() ? width : 0;
         }
+
+        @Override
+        public String toString() {
+            return "StrategyAndWidth [strategy=" + strategy + ", width=" + width + "]";
+        }
     }
 
     /**
@@ -287,7 +293,7 @@ public class FastDateParser implements DateParser, Serializable {
     // Basics
     //-----------------------------------------------------------------------
     /**
-     * <p>Compare another object for equality with this object.</p>
+     * <p>Compares another object for equality with this object.</p>
      *
      * @param obj  the object to compare to
      * @return {@code true}if equal to this instance
@@ -302,7 +308,7 @@ public class FastDateParser implements DateParser, Serializable {
     }
 
     /**
-     * <p>Return a hash code compatible with equals.</p>
+     * <p>Returns a hash code compatible with equals.</p>
      *
      * @return a hash code compatible with equals
      */
@@ -312,7 +318,7 @@ public class FastDateParser implements DateParser, Serializable {
     }
 
     /**
-     * <p>Get a string version of this formatter.</p>
+     * <p>Gets a string version of this formatter.</p>
      *
      * @return a debugging string
      */
@@ -324,7 +330,7 @@ public class FastDateParser implements DateParser, Serializable {
     // Serializing
     //-----------------------------------------------------------------------
     /**
-     * Create the object after serialization. This implementation reinitializes the
+     * Creates the object after serialization. This implementation reinitializes the
      * transient properties.
      *
      * @param in ObjectInputStream from which the object is being deserialized.
@@ -394,7 +400,7 @@ public class FastDateParser implements DateParser, Serializable {
     }
 
     /**
-     * Parse a formatted date string according to the format.  Updates the Calendar with parsed fields.
+     * Parses a formatted date string according to the format.  Updates the Calendar with parsed fields.
      * Upon success, the ParsePosition index is updated to indicate how much of the source text was consumed.
      * Not all source text needs to be consumed.  Upon parse failure, ParsePosition error index is updated to
      * the offset of the source text which does not match the supplied format.
@@ -477,7 +483,7 @@ public class FastDateParser implements DateParser, Serializable {
     }
 
     /**
-     * Adjust dates to be within appropriate century
+     * Adjusts dates to be within appropriate century
      * @param twoDigitYear The year to adjust
      * @return A value between centuryStart(inclusive) to centuryStart+100(exclusive)
      */
@@ -509,7 +515,7 @@ public class FastDateParser implements DateParser, Serializable {
      */
     private abstract static class PatternStrategy extends Strategy {
 
-        private Pattern pattern;
+        Pattern pattern;
 
         void createPattern(final StringBuilder regex) {
             createPattern(regex.toString());
@@ -543,10 +549,21 @@ public class FastDateParser implements DateParser, Serializable {
         }
 
         abstract void setCalendar(FastDateParser parser, Calendar cal, String value);
-    }
+
+        /**
+         * Converts this instance to a handy debug string.
+         *
+         * @since 3.12.0
+         */
+        @Override
+        public String toString() {
+            return getClass().getSimpleName() + " [pattern=" + pattern + "]";
+        }
+
+}
 
     /**
-     * Obtain a Strategy given a field from a SimpleDateFormat pattern
+     * Gets a Strategy given a field from a SimpleDateFormat pattern
      * @param f A sub-sequence of the SimpleDateFormat pattern
      * @param definingCalendar The calendar to obtain the short and long values
      * @return The Strategy that will handle parsing for the field
@@ -682,6 +699,16 @@ public class FastDateParser implements DateParser, Serializable {
             pos.setIndex(formatField.length() + pos.getIndex());
             return true;
         }
+
+        /**
+         * Converts this instance to a handy debug string.
+         *
+         * @since 3.12.0
+         */
+        @Override
+        public String toString() {
+            return "CopyQuotedStrategy [formatField=" + formatField + "]";
+        }
     }
 
     /**
@@ -724,6 +751,17 @@ public class FastDateParser implements DateParser, Serializable {
             }
             cal.set(field, iVal.intValue());
         }
+
+        /**
+         * Converts this instance to a handy debug string.
+         *
+         * @since 3.12.0
+         */
+        @Override
+        public String toString() {
+            return "CaseInsensitiveTextStrategy [field=" + field + ", locale=" + locale + ", lKeyValues=" + lKeyValues
+                + ", pattern=" + pattern + "]";
+        }
     }
 
 
@@ -731,6 +769,7 @@ public class FastDateParser implements DateParser, Serializable {
      * A strategy that handles a number field in the parsing pattern
      */
     private static class NumberStrategy extends Strategy {
+
         private final int field;
 
         /**
@@ -802,6 +841,15 @@ public class FastDateParser implements DateParser, Serializable {
             return iValue;
         }
 
+        /**
+         * Converts this instance to a handy debug string.
+         *
+         * @since 3.12.0
+         */
+        @Override
+        public String toString() {
+            return "NumberStrategy [field=" + field + "]";
+        }
     }
 
     private static final Strategy ABBREVIATED_YEAR_STRATEGY = new NumberStrategy(Calendar.YEAR) {
@@ -914,6 +962,17 @@ public class FastDateParser implements DateParser, Serializable {
                 cal.set(Calendar.ZONE_OFFSET, tzInfo.zone.getRawOffset());
             }
         }
+
+        /**
+         * Converts this instance to a handy debug string.
+         *
+         * @since 3.12.0
+         */
+        @Override
+        public String toString() {
+            return "TimeZoneStrategy [locale=" + locale + ", tzNames=" + tzNames + ", pattern=" + pattern + "]";
+        }
+
     }
 
     private static class ISO8601TimeZoneStrategy extends PatternStrategy {


[commons-lang] 01/02: Fix some formatting.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit c6314067f071d125d1d6e951a4f8a25d52f5e70a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Feb 23 21:10:58 2021 -0500

    Fix some formatting.
---
 .../apache/commons/lang3/time/FastDateParser.java  | 92 ++++++++++++----------
 1 file changed, 49 insertions(+), 43 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
index d34a42c..5638a48 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
@@ -124,7 +124,8 @@ public class FastDateParser implements DateParser, Serializable {
      *
      * @since 3.5
      */
-    protected FastDateParser(final String pattern, final TimeZone timeZone, final Locale locale, final Date centuryStart) {
+    protected FastDateParser(final String pattern, final TimeZone timeZone, final Locale locale,
+        final Date centuryStart) {
         this.pattern = pattern;
         this.timeZone = timeZone;
         this.locale = LocaleUtils.toLocale(locale);
@@ -132,7 +133,7 @@ public class FastDateParser implements DateParser, Serializable {
         final Calendar definingCalendar = Calendar.getInstance(timeZone, this.locale);
 
         final int centuryStartYear;
-        if (centuryStart!=null) {
+        if (centuryStart != null) {
             definingCalendar.setTime(centuryStart);
             centuryStartYear = definingCalendar.get(Calendar.YEAR);
         } else if (this.locale.equals(JAPANESE_IMPERIAL)) {
@@ -140,7 +141,7 @@ public class FastDateParser implements DateParser, Serializable {
         } else {
             // from 80 years ago to 20 years from now
             definingCalendar.setTime(new Date());
-            centuryStartYear = definingCalendar.get(Calendar.YEAR)-80;
+            centuryStartYear = definingCalendar.get(Calendar.YEAR) - 80;
         }
         century = centuryStartYear / 100 * 100;
         startYear = centuryStartYear - century;
@@ -160,7 +161,7 @@ public class FastDateParser implements DateParser, Serializable {
         final StrategyParser fm = new StrategyParser(definingCalendar);
         for (;;) {
             final StrategyAndWidth field = fm.getNextStrategy();
-            if (field==null) {
+            if (field == null) {
                 break;
             }
             patterns.add(field);
@@ -188,8 +189,8 @@ public class FastDateParser implements DateParser, Serializable {
             }
             final Strategy nextStrategy = lt.next().strategy;
             lt.previous();
-            return nextStrategy.isNumber() ?width :0;
-       }
+            return nextStrategy.isNumber() ? width : 0;
+        }
     }
 
     /**
@@ -297,9 +298,7 @@ public class FastDateParser implements DateParser, Serializable {
             return false;
         }
         final FastDateParser other = (FastDateParser) obj;
-        return pattern.equals(other.pattern)
-            && timeZone.equals(other.timeZone)
-            && locale.equals(other.locale);
+        return pattern.equals(other.pattern) && timeZone.equals(other.timeZone) && locale.equals(other.locale);
     }
 
     /**
@@ -357,11 +356,10 @@ public class FastDateParser implements DateParser, Serializable {
         if (date == null) {
             // Add a note re supported date range
             if (locale.equals(JAPANESE_IMPERIAL)) {
-                throw new ParseException(
-                        "(The " +locale + " locale does not support dates before 1868 AD)\n" +
-                                "Unparseable date: \""+source, pp.getErrorIndex());
+                throw new ParseException("(The " + locale + " locale does not support dates before 1868 AD)\n"
+                    + "Unparseable date: \"" + source, pp.getErrorIndex());
             }
-            throw new ParseException("Unparseable date: "+source, pp.getErrorIndex());
+            throw new ParseException("Unparseable date: " + source, pp.getErrorIndex());
         }
         return date;
     }
@@ -460,7 +458,8 @@ public class FastDateParser implements DateParser, Serializable {
      * @param regex The regular expression to build
      * @return The map of string display names to field values
      */
-    private static Map<String, Integer> appendDisplayNames(final Calendar cal, Locale locale, final int field, final StringBuilder regex) {
+    private static Map<String, Integer> appendDisplayNames(final Calendar cal, Locale locale, final int field,
+        final StringBuilder regex) {
         final Map<String, Integer> values = new HashMap<>();
         locale = LocaleUtils.toLocale(locale);
         final Map<String, Integer> displayNames = cal.getDisplayNames(field, Calendar.ALL_STYLES, locale);
@@ -491,9 +490,9 @@ public class FastDateParser implements DateParser, Serializable {
      * A strategy to parse a single field from the parsing pattern
      */
     private abstract static class Strategy {
+
         /**
-         * Is this field a number?
-         * The default implementation returns false.
+         * Is this field a number? The default implementation returns false.
          *
          * @return true, if field is a number
          */
@@ -501,7 +500,8 @@ public class FastDateParser implements DateParser, Serializable {
             return false;
         }
 
-        abstract boolean parse(FastDateParser parser, Calendar calendar, String source, ParsePosition pos, int maxWidth);
+        abstract boolean parse(FastDateParser parser, Calendar calendar, String source, ParsePosition pos,
+            int maxWidth);
     }
 
     /**
@@ -520,8 +520,7 @@ public class FastDateParser implements DateParser, Serializable {
         }
 
         /**
-         * Is this field a number?
-         * The default implementation returns false.
+         * Is this field a number? The default implementation returns false.
          *
          * @return true, if field is a number
          */
@@ -531,7 +530,8 @@ public class FastDateParser implements DateParser, Serializable {
         }
 
         @Override
-        boolean parse(final FastDateParser parser, final Calendar calendar, final String source, final ParsePosition pos, final int maxWidth) {
+        boolean parse(final FastDateParser parser, final Calendar calendar, final String source,
+            final ParsePosition pos, final int maxWidth) {
             final Matcher matcher = pattern.matcher(source.substring(pos.getIndex()));
             if (!matcher.lookingAt()) {
                 pos.setErrorIndex(pos.getIndex());
@@ -552,9 +552,9 @@ public class FastDateParser implements DateParser, Serializable {
      * @return The Strategy that will handle parsing for the field
      */
     private Strategy getStrategy(final char f, final int width, final Calendar definingCalendar) {
-        switch(f) {
+        switch (f) {
         default:
-            throw new IllegalArgumentException("Format '"+f+"' not supported");
+            throw new IllegalArgumentException("Format '" + f + "' not supported");
         case 'D':
             return DAY_OF_YEAR_STRATEGY;
         case 'E':
@@ -563,12 +563,12 @@ public class FastDateParser implements DateParser, Serializable {
             return DAY_OF_WEEK_IN_MONTH_STRATEGY;
         case 'G':
             return getLocaleSpecificStrategy(Calendar.ERA, definingCalendar);
-        case 'H':  // Hour in day (0-23)
+        case 'H': // Hour in day (0-23)
             return HOUR_OF_DAY_STRATEGY;
-        case 'K':  // Hour in am/pm (0-11)
+        case 'K': // Hour in am/pm (0-11)
             return HOUR_STRATEGY;
         case 'M':
-            return width>=3 ?getLocaleSpecificStrategy(Calendar.MONTH, definingCalendar) :NUMBER_MONTH_STRATEGY;
+            return width >= 3 ? getLocaleSpecificStrategy(Calendar.MONTH, definingCalendar) : NUMBER_MONTH_STRATEGY;
         case 'S':
             return MILLISECOND_STRATEGY;
         case 'W':
@@ -577,9 +577,9 @@ public class FastDateParser implements DateParser, Serializable {
             return getLocaleSpecificStrategy(Calendar.AM_PM, definingCalendar);
         case 'd':
             return DAY_OF_MONTH_STRATEGY;
-        case 'h':  // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
+        case 'h': // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
             return HOUR12_STRATEGY;
-        case 'k':  // Hour in day (1-24), i.e. midnight is 24, not 0
+        case 'k': // Hour in day (1-24), i.e. midnight is 24, not 0
             return HOUR24_OF_DAY_STRATEGY;
         case 'm':
             return MINUTE_STRATEGY;
@@ -591,11 +591,11 @@ public class FastDateParser implements DateParser, Serializable {
             return WEEK_OF_YEAR_STRATEGY;
         case 'y':
         case 'Y':
-            return width>2 ?LITERAL_YEAR_STRATEGY :ABBREVIATED_YEAR_STRATEGY;
+            return width > 2 ? LITERAL_YEAR_STRATEGY : ABBREVIATED_YEAR_STRATEGY;
         case 'X':
             return ISO8601TimeZoneStrategy.getStrategy(width);
         case 'Z':
-            if (width==2) {
+            if (width == 2) {
                 return ISO8601TimeZoneStrategy.ISO_8601_3_STRATEGY;
             }
             //$FALL-THROUGH$
@@ -631,9 +631,8 @@ public class FastDateParser implements DateParser, Serializable {
         final ConcurrentMap<Locale, Strategy> cache = getCache(field);
         Strategy strategy = cache.get(locale);
         if (strategy == null) {
-            strategy = field == Calendar.ZONE_OFFSET
-                    ? new TimeZoneStrategy(locale)
-                    : new CaseInsensitiveTextStrategy(field, definingCalendar, locale);
+            strategy = field == Calendar.ZONE_OFFSET ? new TimeZoneStrategy(locale)
+                : new CaseInsensitiveTextStrategy(field, definingCalendar, locale);
             final Strategy inCache = cache.putIfAbsent(locale, strategy);
             if (inCache != null) {
                 return inCache;
@@ -651,6 +650,7 @@ public class FastDateParser implements DateParser, Serializable {
 
         /**
          * Constructs a Strategy that ensures the formatField has literal text
+         *
          * @param formatField The literal text to match
          */
         CopyQuotedStrategy(final String formatField) {
@@ -666,7 +666,8 @@ public class FastDateParser implements DateParser, Serializable {
         }
 
         @Override
-        boolean parse(final FastDateParser parser, final Calendar calendar, final String source, final ParsePosition pos, final int maxWidth) {
+        boolean parse(final FastDateParser parser, final Calendar calendar, final String source,
+            final ParsePosition pos, final int maxWidth) {
             for (int idx = 0; idx < formatField.length(); ++idx) {
                 final int sIdx = idx + pos.getIndex();
                 if (sIdx == source.length()) {
@@ -686,16 +687,17 @@ public class FastDateParser implements DateParser, Serializable {
     /**
      * A strategy that handles a text field in the parsing pattern
      */
-     private static class CaseInsensitiveTextStrategy extends PatternStrategy {
+    private static class CaseInsensitiveTextStrategy extends PatternStrategy {
         private final int field;
         final Locale locale;
         private final Map<String, Integer> lKeyValues;
 
         /**
          * Constructs a Strategy that parses a Text field
-         * @param field  The Calendar field
-         * @param definingCalendar  The Calendar to use
-         * @param locale  The Locale to use
+         *
+         * @param field The Calendar field
+         * @param definingCalendar The Calendar to use
+         * @param locale The Locale to use
          */
         CaseInsensitiveTextStrategy(final int field, final Calendar definingCalendar, final Locale locale) {
             this.field = field;
@@ -704,7 +706,7 @@ public class FastDateParser implements DateParser, Serializable {
             final StringBuilder regex = new StringBuilder();
             regex.append("((?iu)");
             lKeyValues = appendDisplayNames(definingCalendar, locale, field, regex);
-            regex.setLength(regex.length()-1);
+            regex.setLength(regex.length() - 1);
             regex.append(")");
             createPattern(regex);
         }
@@ -733,10 +735,11 @@ public class FastDateParser implements DateParser, Serializable {
 
         /**
          * Constructs a Strategy that parses a Number field
+         *
          * @param field The Calendar field
          */
         NumberStrategy(final int field) {
-             this.field= field;
+            this.field = field;
         }
 
         /**
@@ -748,7 +751,8 @@ public class FastDateParser implements DateParser, Serializable {
         }
 
         @Override
-        boolean parse(final FastDateParser parser, final Calendar calendar, final String source, final ParsePosition pos, final int maxWidth) {
+        boolean parse(final FastDateParser parser, final Calendar calendar, final String source,
+            final ParsePosition pos, final int maxWidth) {
             int idx = pos.getIndex();
             int last = source.length();
 
@@ -789,6 +793,7 @@ public class FastDateParser implements DateParser, Serializable {
 
         /**
          * Make any modifications to parsed integer
+         *
          * @param parser The parser
          * @param iValue The parsed integer
          * @return The modified value
@@ -817,7 +822,7 @@ public class FastDateParser implements DateParser, Serializable {
         private static final String GMT_OPTION = TimeZones.GMT_ID + "[+-]\\d{1,2}:\\d{2}";
 
         private final Locale locale;
-        private final Map<String, TzInfo> tzNames= new HashMap<>();
+        private final Map<String, TzInfo> tzNames = new HashMap<>();
 
         private static class TzInfo {
             final TimeZone zone;
@@ -825,7 +830,7 @@ public class FastDateParser implements DateParser, Serializable {
 
             TzInfo(final TimeZone tz, final boolean useDst) {
                 zone = tz;
-                dstOffset = useDst ?tz.getDSTSavings() :0;
+                dstOffset = useDst ? tz.getDSTSavings() : 0;
             }
         }
 
@@ -836,13 +841,14 @@ public class FastDateParser implements DateParser, Serializable {
 
         /**
          * Constructs a Strategy that parses a TimeZone
+         *
          * @param locale The Locale
          */
         TimeZoneStrategy(final Locale locale) {
             this.locale = LocaleUtils.toLocale(locale);
 
             final StringBuilder sb = new StringBuilder();
-            sb.append("((?iu)" + RFC_822_TIME_ZONE + "|" + GMT_OPTION );
+            sb.append("((?iu)" + RFC_822_TIME_ZONE + "|" + GMT_OPTION);
 
             final Set<String> sorted = new TreeSet<>(LONGER_FIRST_LOWERCASE);