From f7c5c5c6db1f6455ee78fa578c3b13f88242a7dd Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Tue, 23 Feb 2021 21:28:16 -0500 Subject: [PATCH] Add some toString() methods handy when debugging. --- .../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 5638a48f5..8447bc129 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 @@ protected FastDateParser(final String pattern, final TimeZone timeZone, final Lo } /** - * 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 @@ private void init(final Calendar definingCalendar) { * Holds strategy and field width */ private static class StrategyAndWidth { + final Strategy strategy; final int width; @@ -191,6 +192,11 @@ int getMaxWidth(final ListIterator lt) { lt.previous(); return nextStrategy.isNumber() ? width : 0; } + + @Override + public String toString() { + return "StrategyAndWidth [strategy=" + strategy + ", width=" + width + "]"; + } } /** @@ -287,7 +293,7 @@ public Locale getLocale() { // Basics //----------------------------------------------------------------------- /** - *

Compare another object for equality with this object.

+ *

Compares another object for equality with this object.

* * @param obj the object to compare to * @return {@code true}if equal to this instance @@ -302,7 +308,7 @@ public boolean equals(final Object obj) { } /** - *

Return a hash code compatible with equals.

+ *

Returns a hash code compatible with equals.

* * @return a hash code compatible with equals */ @@ -312,7 +318,7 @@ public int hashCode() { } /** - *

Get a string version of this formatter.

+ *

Gets a string version of this formatter.

* * @return a debugging string */ @@ -324,7 +330,7 @@ public String toString() { // 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 Date parse(final String source, final ParsePosition pos) { } /** - * 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 @@ private static Map appendDisplayNames(final Calendar cal, Local } /** - * 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 @@ abstract boolean parse(FastDateParser parser, Calendar calendar, String source, */ private abstract static class PatternStrategy extends Strategy { - private Pattern pattern; + Pattern pattern; void createPattern(final StringBuilder regex) { createPattern(regex.toString()); @@ -543,10 +549,21 @@ boolean parse(final FastDateParser parser, final Calendar calendar, final String } 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 @@ boolean parse(final FastDateParser parser, final Calendar calendar, final String 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 @@ void setCalendar(final FastDateParser parser, final Calendar cal, final String v } 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 @@ void setCalendar(final FastDateParser parser, final Calendar cal, final String v * 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 @@ int modify(final FastDateParser parser, final int iValue) { 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 @@ void setCalendar(final FastDateParser parser, final Calendar cal, final String t 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 {