Repository: logging-log4j2 Updated Branches: refs/heads/master 73eba12f7 -> 81b5806eb
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81b5806e/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDatePrinter.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDatePrinter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDatePrinter.java index 1a439be..4f33b6b 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDatePrinter.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDatePrinter.java @@ -25,30 +25,83 @@ import java.text.FieldPosition; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; -import java.util.GregorianCalendar; import java.util.List; import java.util.Locale; import java.util.TimeZone; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import org.apache.logging.log4j.core.util.Throwables; + /** - * Copied from Commons Lang 3. + * <p>FastDatePrinter is a fast and thread-safe version of + * {@link java.text.SimpleDateFormat}.</p> + * + * <p>To obtain a FastDatePrinter, use {@link FastDateFormat#getInstance(String, TimeZone, Locale)} + * or another variation of the factory methods of {@link FastDateFormat}.</p> + * + * <p>Since FastDatePrinter is thread safe, you can use a static member instance:</p> + * <code> + * private static final DatePrinter DATE_PRINTER = FastDateFormat.getInstance("yyyy-MM-dd"); + * </code> + * + * <p>This class can be used as a direct replacement to + * {@code SimpleDateFormat} in most formatting situations. + * This class is especially useful in multi-threaded server environments. + * {@code SimpleDateFormat} is not thread-safe in any JDK version, + * nor will it be as Sun have closed the bug/RFE. + * </p> + * + * <p>Only formatting is supported by this class, but all patterns are compatible with + * SimpleDateFormat (except time zones and some year patterns - see below).</p> + * + * <p>Java 1.4 introduced a new pattern letter, {@code 'Z'}, to represent + * time zones in RFC822 format (eg. {@code +0800} or {@code -1100}). + * This pattern letter can be used here (on all JDK versions).</p> + * + * <p>In addition, the pattern {@code 'ZZ'} has been made to represent + * ISO 8601 extended format time zones (eg. {@code +08:00} or {@code -11:00}). + * This introduces a minor incompatibility with Java 1.4, but at a gain of + * useful functionality.</p> + * + * <p>Starting with JDK7, ISO 8601 support was added using the pattern {@code 'X'}. + * To maintain compatibility, {@code 'ZZ'} will continue to be supported, but using + * one of the {@code 'X'} formats is recommended. + * + * <p>Javadoc cites for the year pattern: <i>For formatting, if the number of + * pattern letters is 2, the year is truncated to 2 digits; otherwise it is + * interpreted as a number.</i> Starting with Java 1.7 a pattern of 'Y' or + * 'YYY' will be formatted as '2003', while it was '03' in former Java + * versions. FastDatePrinter implements the behavior of Java 7.</p> + * + * <p> + * Copied and modified from <a href="https://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a>. + * </p> + * + * @since Apache Commons Lang 3.2 + * @see FastDateParser */ public class FastDatePrinter implements DatePrinter, Serializable { // A lot of the speed in this class comes from caching, but some comes - // from the special int to StringBuilder conversion. + // from the special int to StringBuffer conversion. // // The following produces a padded 2 digit number: - // buffer.append((char)(value / 10 + '0')); - // buffer.append((char)(value % 10 + '0')); + // buffer.append((char)(value / 10 + '0')); + // buffer.append((char)(value % 10 + '0')); // - // Note that the fastest append to StringBuilder is a single char (used here). + // Note that the fastest append to StringBuffer is a single char (used here). // Note that Integer.toString() is not called, the conversion is simply // taking the value and adding (mathematically) the ASCII value for '0'. // So, don't change this code! It works and is very fast. /** + * Required for serialization support. + * + * @see java.io.Serializable + */ + private static final long serialVersionUID = 1L; + + /** * FULL locale dependent date or time style. */ public static final int FULL = DateFormat.FULL; @@ -66,13 +119,6 @@ public class FastDatePrinter implements DatePrinter, Serializable { public static final int SHORT = DateFormat.SHORT; /** - * Required for serialization support. - * - * @see java.io.Serializable - */ - private static final long serialVersionUID = 1L; - - /** * The pattern. */ private final String mPattern; @@ -94,17 +140,15 @@ public class FastDatePrinter implements DatePrinter, Serializable { private transient int mMaxLengthEstimate; // Constructor - // ----------------------------------------------------------------------- + //----------------------------------------------------------------------- /** - * <p> - * Constructs a new FastDatePrinter. - * </p> - * Use {@link FastDateFormat#getInstance(String, TimeZone, Locale)} or another variation of the factory methods of - * {@link FastDateFormat} to get a cached FastDatePrinter instance. + * <p>Constructs a new FastDatePrinter.</p> + * Use {@link FastDateFormat#getInstance(String, TimeZone, Locale)} or another variation of the + * factory methods of {@link FastDateFormat} to get a cached FastDatePrinter instance. * - * @param pattern {@link java.text.SimpleDateFormat} compatible pattern - * @param timeZone non-null time zone to use - * @param locale non-null locale to use + * @param pattern {@link java.text.SimpleDateFormat} compatible pattern + * @param timeZone non-null time zone to use + * @param locale non-null locale to use * @throws NullPointerException if pattern, timeZone, or locale is null. */ protected FastDatePrinter(final String pattern, final TimeZone timeZone, final Locale locale) { @@ -116,16 +160,14 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * <p> - * Initializes the instance for first use. - * </p> + * <p>Initializes the instance for first use.</p> */ private void init() { final List<Rule> rulesList = parsePattern(); mRules = rulesList.toArray(new Rule[rulesList.size()]); int len = 0; - for (int i = mRules.length; --i >= 0;) { + for (int i=mRules.length; --i >= 0; ) { len += mRules[i].estimateLength(); } @@ -133,11 +175,9 @@ public class FastDatePrinter implements DatePrinter, Serializable { } // Parse the pattern - // ----------------------------------------------------------------------- + //----------------------------------------------------------------------- /** - * <p> - * Returns a list of Rules given a pattern. - * </p> + * <p>Returns a list of Rules given a pattern.</p> * * @return a {@code List} of Rule objects * @throws IllegalArgumentException if pattern is invalid @@ -174,11 +214,15 @@ public class FastDatePrinter implements DatePrinter, Serializable { rule = new TextField(Calendar.ERA, ERAs); break; case 'y': // year (number) + case 'Y': // week year if (tokenLen == 2) { rule = TwoDigitYearField.INSTANCE; } else { rule = selectNumberRule(Calendar.YEAR, tokenLen < 4 ? 4 : tokenLen); } + if (c == 'Y') { + rule = new WeekYear((NumberRule) rule); + } break; case 'M': // month in year (text and number) if (tokenLen >= 4) { @@ -212,6 +256,9 @@ public class FastDatePrinter implements DatePrinter, Serializable { case 'E': // day in week (text) rule = new TextField(Calendar.DAY_OF_WEEK, tokenLen < 4 ? shortWeekdays : weekdays); break; + case 'u': // day in week (number) + rule = new DayInWeekField(selectNumberRule(Calendar.DAY_OF_WEEK, tokenLen)); + break; case 'D': // day in year (number) rule = selectNumberRule(Calendar.DAY_OF_YEAR, tokenLen); break; @@ -233,12 +280,9 @@ public class FastDatePrinter implements DatePrinter, Serializable { case 'K': // hour in am/pm (0..11) rule = selectNumberRule(Calendar.HOUR, tokenLen); break; - case 'u': // day of week (1..7) - rule = selectNumberRule(Calendar.DAY_OF_WEEK, tokenLen); - break; - case 'X': // ISO 8601 + case 'X': // ISO 8601 rule = Iso8601_Rule.getRule(tokenLen); - break; + break; case 'z': // time zone (text) if (tokenLen >= 4) { rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG); @@ -274,12 +318,10 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * <p> - * Performs the parsing of tokens. - * </p> + * <p>Performs the parsing of tokens.</p> * - * @param pattern the pattern - * @param indexRef index references + * @param pattern the pattern + * @param indexRef index references * @return parsed token */ protected String parseToken(final String pattern, final int[] indexRef) { @@ -320,7 +362,8 @@ public class FastDatePrinter implements DatePrinter, Serializable { } else { inLiteral = !inLiteral; } - } else if (!inLiteral && (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) { + } else if (!inLiteral && + (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) { i--; break; } else { @@ -334,12 +377,10 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * <p> - * Gets an appropriate rule for the padding required. - * </p> + * <p>Gets an appropriate rule for the padding required.</p> * - * @param field the field to get a rule for - * @param padding the padding required + * @param field the field to get a rule for + * @param padding the padding required * @return a new rule with the correct padding */ protected NumberRule selectNumberRule(final int field, final int padding) { @@ -354,17 +395,17 @@ public class FastDatePrinter implements DatePrinter, Serializable { } // Format methods - // ----------------------------------------------------------------------- + //----------------------------------------------------------------------- /** - * <p> - * Formats a {@code Date}, {@code Calendar} or {@code Long} (milliseconds) object. - * </p> - * - * @param obj the object to format - * @param toAppendTo the buffer to append to - * @param pos the position - ignored + * <p>Formats a {@code Date}, {@code Calendar} or + * {@code Long} (milliseconds) object.</p> + * @deprecated Use {{@link #format(Date)}, {{@link #format(Calendar)}, {{@link #format(long)}, or {{@link #format(Object)} + * @param obj the object to format + * @param toAppendTo the buffer to append to + * @param pos the position - ignored * @return the buffer passed in */ + @Deprecated @Override public StringBuilder format(final Object obj, final StringBuilder toAppendTo, final FieldPosition pos) { if (obj instanceof Date) { @@ -374,25 +415,43 @@ public class FastDatePrinter implements DatePrinter, Serializable { } else if (obj instanceof Long) { return format(((Long) obj).longValue(), toAppendTo); } else { - throw new IllegalArgumentException("Unknown class: " + (obj == null ? "<null>" : obj.getClass().getName())); + throw new IllegalArgumentException("Unknown class: " + + (obj == null ? "<null>" : obj.getClass().getName())); } } - /* - * (non-Javadoc) - * + /** + * <p>Formats a {@code Date}, {@code Calendar} or + * {@code Long} (milliseconds) object.</p> + * @since 3.5 + * @param obj the object to format + * @return The formatted value. + */ + String format(final Object obj) { + if (obj instanceof Date) { + return format((Date) obj); + } else if (obj instanceof Calendar) { + return format((Calendar) obj); + } else if (obj instanceof Long) { + return format(((Long) obj).longValue()); + } else { + throw new IllegalArgumentException("Unknown class: " + + (obj == null ? "<null>" : obj.getClass().getName())); + } + } + + /* (non-Javadoc) * @see org.apache.commons.lang3.time.DatePrinter#format(long) */ @Override public String format(final long millis) { - final Calendar c = newCalendar(); // hard code GregorianCalendar + final Calendar c = newCalendar(); c.setTimeInMillis(millis); return applyRulesToString(c); } /** * Creates a String representation of the given Calendar by applying the rules of this printer to it. - * * @param c the Calender to apply the rules to. * @return a String representation of the given Calendar. */ @@ -401,30 +460,24 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * Creation method for ne calender instances. - * + * Creation method for new calender instances. * @return a new Calendar instance. */ - private GregorianCalendar newCalendar() { - // hard code GregorianCalendar - return new GregorianCalendar(mTimeZone, mLocale); + private Calendar newCalendar() { + return Calendar.getInstance(mTimeZone, mLocale); } - /* - * (non-Javadoc) - * + /* (non-Javadoc) * @see org.apache.commons.lang3.time.DatePrinter#format(java.util.Date) */ @Override public String format(final Date date) { - final Calendar c = newCalendar(); // hard code GregorianCalendar + final Calendar c = newCalendar(); c.setTime(date); return applyRulesToString(c); } - /* - * (non-Javadoc) - * + /* (non-Javadoc) * @see org.apache.commons.lang3.time.DatePrinter#format(java.util.Calendar) */ @Override @@ -432,60 +485,77 @@ public class FastDatePrinter implements DatePrinter, Serializable { return format(calendar, new StringBuilder(mMaxLengthEstimate)).toString(); } - /* - * (non-Javadoc) - * - * @see org.apache.commons.lang3.time.DatePrinter#format(long, java.lang.StringBuilder) + /* (non-Javadoc) + * @see org.apache.commons.lang3.time.DatePrinter#format(long, java.lang.Appendable) */ @Override - public StringBuilder format(final long millis, final StringBuilder buf) { - return format(new Date(millis), buf); + public <B extends Appendable> B format(final long millis, final B buf) { + final Calendar c = newCalendar(); + c.setTimeInMillis(millis); + return applyRules(c, buf); } - /* - * (non-Javadoc) - * - * @see org.apache.commons.lang3.time.DatePrinter#format(java.util.Date, java.lang.StringBuilder) + /* (non-Javadoc) + * @see org.apache.commons.lang3.time.DatePrinter#format(java.util.Date, java.lang.Appendable) */ @Override - public StringBuilder format(final Date date, final StringBuilder buf) { - final Calendar c = newCalendar(); // hard code GregorianCalendar + public <B extends Appendable> B format(final Date date, final B buf) { + final Calendar c = newCalendar(); c.setTime(date); return applyRules(c, buf); } - /* - * (non-Javadoc) - * - * @see org.apache.commons.lang3.time.DatePrinter#format(java.util.Calendar, java.lang.StringBuilder) + /* (non-Javadoc) + * @see org.apache.commons.lang3.time.DatePrinter#format(java.util.Calendar, java.lang.Appendable) */ @Override - public StringBuilder format(final Calendar calendar, final StringBuilder buf) { + public <B extends Appendable> B format(Calendar calendar, final B buf) { // do not pass in calendar directly, this will cause TimeZone of FastDatePrinter to be ignored - return format(calendar.getTime(), buf); + if(!calendar.getTimeZone().equals(mTimeZone)) { + calendar = (Calendar)calendar.clone(); + calendar.setTimeZone(mTimeZone); + } + return applyRules(calendar, buf); } /** - * <p> - * Performs the formatting by applying the rules to the specified calendar. - * </p> + * Performs the formatting by applying the rules to the + * specified calendar. * * @param calendar the calendar to format * @param buf the buffer to format into * @return the specified string buffer + * + * @deprecated use {@link #format(Calendar)} or {@link #format(Calendar, Appendable)} + */ + @Deprecated + protected StringBuffer applyRules(final Calendar calendar, final StringBuffer buf) { + return (StringBuffer) applyRules(calendar, (Appendable)buf); + } + + /** + * <p>Performs the formatting by applying the rules to the + * specified calendar.</p> + * + * @param calendar the calendar to format + * @param buf the buffer to format into + * @param <B> the Appendable class type, usually StringBuilder or StringBuffer. + * @return the specified string buffer */ - protected StringBuilder applyRules(final Calendar calendar, final StringBuilder buf) { - for (final Rule rule : mRules) { - rule.appendTo(buf, calendar); + private <B extends Appendable> B applyRules(final Calendar calendar, final B buf) { + try { + for (final Rule rule : mRules) { + rule.appendTo(buf, calendar); + } + } catch (final IOException ioe) { + Throwables.rethrow(ioe); } return buf; } // Accessors - // ----------------------------------------------------------------------- - /* - * (non-Javadoc) - * + //----------------------------------------------------------------------- + /* (non-Javadoc) * @see org.apache.commons.lang3.time.DatePrinter#getPattern() */ @Override @@ -493,9 +563,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { return mPattern; } - /* - * (non-Javadoc) - * + /* (non-Javadoc) * @see org.apache.commons.lang3.time.DatePrinter#getTimeZone() */ @Override @@ -503,9 +571,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { return mTimeZone; } - /* - * (non-Javadoc) - * + /* (non-Javadoc) * @see org.apache.commons.lang3.time.DatePrinter#getLocale() */ @Override @@ -514,13 +580,11 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * <p> - * Gets an estimate for the maximum string length that the formatter will produce. - * </p> + * <p>Gets an estimate for the maximum string length that the + * formatter will produce.</p> * - * <p> - * The actual formatted length will almost always be less than or equal to this amount. - * </p> + * <p>The actual formatted length will almost always be less than or + * equal to this amount.</p> * * @return the maximum formatted length */ @@ -529,13 +593,11 @@ public class FastDatePrinter implements DatePrinter, Serializable { } // Basics - // ----------------------------------------------------------------------- + //----------------------------------------------------------------------- /** - * <p> - * Compares two objects for equality. - * </p> + * <p>Compares two objects for equality.</p> * - * @param obj the object to compare to + * @param obj the object to compare to * @return {@code true} if equal */ @Override @@ -544,13 +606,13 @@ public class FastDatePrinter implements DatePrinter, Serializable { return false; } final FastDatePrinter other = (FastDatePrinter) obj; - return mPattern.equals(other.mPattern) && mTimeZone.equals(other.mTimeZone) && mLocale.equals(other.mLocale); + return mPattern.equals(other.mPattern) + && mTimeZone.equals(other.mTimeZone) + && mLocale.equals(other.mLocale); } /** - * <p> - * Returns a hash code compatible with equals. - * </p> + * <p>Returns a hash code compatible with equals.</p> * * @return a hash code compatible with equals */ @@ -560,9 +622,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * <p> - * Gets a debugging string version of this formatter. - * </p> + * <p>Gets a debugging string version of this formatter.</p> * * @return a debugging string */ @@ -572,9 +632,10 @@ public class FastDatePrinter implements DatePrinter, Serializable { } // Serializing - // ----------------------------------------------------------------------- + //----------------------------------------------------------------------- /** - * Create the object after serialization. This implementation reinitializes the transient properties. + * Create the object after serialization. This implementation reinitializes the + * transient properties. * * @param in ObjectInputStream from which the object is being deserialized. * @throws IOException if there is an IO issue. @@ -586,22 +647,94 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * Appends digits to the given buffer. - * + * Appends two digits to the given buffer. + * + * @param buffer the buffer to append to. + * @param value the value to append digits from. + */ + private static void appendDigits(final Appendable buffer, final int value) throws IOException { + buffer.append((char)(value / 10 + '0')); + buffer.append((char)(value % 10 + '0')); + } + + private static final int MAX_DIGITS = 10; // log10(Integer.MAX_VALUE) ~= 9.3 + + /** + * Appends all digits to the given buffer. + * * @param buffer the buffer to append to. * @param value the value to append digits from. */ - private static void appendDigits(final StringBuilder buffer, final int value) { - buffer.append((char) (value / 10 + '0')); - buffer.append((char) (value % 10 + '0')); + private static void appendFullDigits(final Appendable buffer, int value, int minFieldWidth) throws IOException { + // specialized paths for 1 to 4 digits -> avoid the memory allocation from the temporary work array + // see LANG-1248 + if (value < 10000) { + // less memory allocation path works for four digits or less + + int nDigits = 4; + if (value < 1000) { + --nDigits; + if (value < 100) { + --nDigits; + if (value < 10) { + --nDigits; + } + } + } + // left zero pad + for (int i = minFieldWidth - nDigits; i > 0; --i) { + buffer.append('0'); + } + + switch (nDigits) { + case 4: + buffer.append((char) (value / 1000 + '0')); + value %= 1000; + case 3: + if (value >= 100) { + buffer.append((char) (value / 100 + '0')); + value %= 100; + } else { + buffer.append('0'); + } + case 2: + if (value >= 10) { + buffer.append((char) (value / 10 + '0')); + value %= 10; + } else { + buffer.append('0'); + } + case 1: + buffer.append((char) (value + '0')); + } + } else { + // more memory allocation path works for any digits + + // build up decimal representation in reverse + final char[] work = new char[MAX_DIGITS]; + int digit = 0; + while (value != 0) { + work[digit++] = (char) (value % 10 + '0'); + value = value / 10; + } + + // pad with zeros + while (digit < minFieldWidth) { + buffer.append('0'); + --minFieldWidth; + } + + // reverse + while (--digit >= 0) { + buffer.append(work[digit]); + } + } } // Rules - // ----------------------------------------------------------------------- + //----------------------------------------------------------------------- /** - * <p> - * Inner class defining a rule. - * </p> + * <p>Inner class defining a rule.</p> */ private interface Rule { /** @@ -614,16 +747,15 @@ public class FastDatePrinter implements DatePrinter, Serializable { /** * Appends the value of the specified calendar to the output buffer based on the rule implementation. * - * @param buffer the output buffer + * @param buf the output buffer * @param calendar calendar to be appended + * @throws IOException if an I/O error occurs */ - void appendTo(StringBuilder buffer, Calendar calendar); + void appendTo(Appendable buf, Calendar calendar) throws IOException; } /** - * <p> - * Inner class defining a numeric rule. - * </p> + * <p>Inner class defining a numeric rule.</p> */ private interface NumberRule extends Rule { /** @@ -631,20 +763,20 @@ public class FastDatePrinter implements DatePrinter, Serializable { * * @param buffer the output buffer * @param value the value to be appended + * @throws IOException if an I/O error occurs */ - void appendTo(StringBuilder buffer, int value); + void appendTo(Appendable buffer, int value) throws IOException; } /** - * <p> - * Inner class to output a constant single character. - * </p> + * <p>Inner class to output a constant single character.</p> */ private static class CharacterLiteral implements Rule { private final char mValue; /** - * Constructs a new instance of {@code CharacterLiteral} to hold the specified value. + * Constructs a new instance of {@code CharacterLiteral} + * to hold the specified value. * * @param value the character literal */ @@ -664,21 +796,20 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { buffer.append(mValue); } } /** - * <p> - * Inner class to output a constant string. - * </p> + * <p>Inner class to output a constant string.</p> */ private static class StringLiteral implements Rule { private final String mValue; /** - * Constructs a new instance of {@code StringLiteral} to hold the specified value. + * Constructs a new instance of {@code StringLiteral} + * to hold the specified value. * * @param value the string literal */ @@ -698,22 +829,21 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { buffer.append(mValue); } } /** - * <p> - * Inner class to output one of a set of values. - * </p> + * <p>Inner class to output one of a set of values.</p> */ private static class TextField implements Rule { private final int mField; private final String[] mValues; /** - * Constructs an instance of {@code TextField} with the specified field and values. + * Constructs an instance of {@code TextField} + * with the specified field and values. * * @param field the field * @param values the field values @@ -729,7 +859,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { @Override public int estimateLength() { int max = 0; - for (int i = mValues.length; --i >= 0;) { + for (int i=mValues.length; --i >= 0; ) { final int len = mValues[i].length(); if (len > max) { max = len; @@ -742,15 +872,13 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { buffer.append(mValues[calendar.get(mField)]); } } /** - * <p> - * Inner class to output an unpadded number. - * </p> + * <p>Inner class to output an unpadded number.</p> */ private static class UnpaddedNumberField implements NumberRule { private final int mField; @@ -776,7 +904,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { appendTo(buffer, calendar.get(mField)); } @@ -784,21 +912,19 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public final void appendTo(final StringBuilder buffer, final int value) { + public final void appendTo(final Appendable buffer, final int value) throws IOException { if (value < 10) { - buffer.append((char) (value + '0')); + buffer.append((char)(value + '0')); } else if (value < 100) { appendDigits(buffer, value); } else { - buffer.append(value); + appendFullDigits(buffer, value, 1); } } } /** - * <p> - * Inner class to output an unpadded month. - * </p> + * <p>Inner class to output an unpadded month.</p> */ private static class UnpaddedMonthField implements NumberRule { static final UnpaddedMonthField INSTANCE = new UnpaddedMonthField(); @@ -823,7 +949,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { appendTo(buffer, calendar.get(Calendar.MONTH) + 1); } @@ -831,9 +957,9 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public final void appendTo(final StringBuilder buffer, final int value) { + public final void appendTo(final Appendable buffer, final int value) throws IOException { if (value < 10) { - buffer.append((char) (value + '0')); + buffer.append((char)(value + '0')); } else { appendDigits(buffer, value); } @@ -841,9 +967,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * <p> - * Inner class to output a padded number. - * </p> + * <p>Inner class to output a padded number.</p> */ private static class PaddedNumberField implements NumberRule { private final int mField; @@ -876,7 +1000,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { appendTo(buffer, calendar.get(mField)); } @@ -884,23 +1008,13 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public final void appendTo(final StringBuilder buffer, int value) { - // pad the buffer with adequate zeros - for (int digit = 0; digit < mSize; ++digit) { - buffer.append('0'); - } - // backfill the buffer with non-zero digits - int index = buffer.length(); - for (; value > 0; value /= 10) { - buffer.setCharAt(--index, (char) ('0' + value % 10)); - } + public final void appendTo(final Appendable buffer, final int value) throws IOException { + appendFullDigits(buffer, value, mSize); } } /** - * <p> - * Inner class to output a two digit number. - * </p> + * <p>Inner class to output a two digit number.</p> */ private static class TwoDigitNumberField implements NumberRule { private final int mField; @@ -926,7 +1040,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { appendTo(buffer, calendar.get(mField)); } @@ -934,19 +1048,17 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public final void appendTo(final StringBuilder buffer, final int value) { + public final void appendTo(final Appendable buffer, final int value) throws IOException { if (value < 100) { appendDigits(buffer, value); } else { - buffer.append(value); + appendFullDigits(buffer, value, 2); } } } /** - * <p> - * Inner class to output a two digit year. - * </p> + * <p>Inner class to output a two digit year.</p> */ private static class TwoDigitYearField implements NumberRule { static final TwoDigitYearField INSTANCE = new TwoDigitYearField(); @@ -970,7 +1082,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { appendTo(buffer, calendar.get(Calendar.YEAR) % 100); } @@ -978,15 +1090,13 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public final void appendTo(final StringBuilder buffer, final int value) { + public final void appendTo(final Appendable buffer, final int value) throws IOException { appendDigits(buffer, value); } } /** - * <p> - * Inner class to output a two digit month. - * </p> + * <p>Inner class to output a two digit month.</p> */ private static class TwoDigitMonthField implements NumberRule { static final TwoDigitMonthField INSTANCE = new TwoDigitMonthField(); @@ -1010,7 +1120,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { appendTo(buffer, calendar.get(Calendar.MONTH) + 1); } @@ -1018,21 +1128,20 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public final void appendTo(final StringBuilder buffer, final int value) { + public final void appendTo(final Appendable buffer, final int value) throws IOException { appendDigits(buffer, value); } } /** - * <p> - * Inner class to output the twelve hour field. - * </p> + * <p>Inner class to output the twelve hour field.</p> */ private static class TwelveHourField implements NumberRule { private final NumberRule mRule; /** - * Constructs an instance of {@code TwelveHourField} with the specified {@code NumberRule}. + * Constructs an instance of {@code TwelveHourField} with the specified + * {@code NumberRule}. * * @param rule the rule */ @@ -1052,7 +1161,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { int value = calendar.get(Calendar.HOUR); if (value == 0) { value = calendar.getLeastMaximum(Calendar.HOUR) + 1; @@ -1064,21 +1173,20 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final int value) { + public void appendTo(final Appendable buffer, final int value) throws IOException { mRule.appendTo(buffer, value); } } /** - * <p> - * Inner class to output the twenty four hour field. - * </p> + * <p>Inner class to output the twenty four hour field.</p> */ private static class TwentyFourHourField implements NumberRule { private final NumberRule mRule; /** - * Constructs an instance of {@code TwentyFourHourField} with the specified {@code NumberRule}. + * Constructs an instance of {@code TwentyFourHourField} with the specified + * {@code NumberRule}. * * @param rule the rule */ @@ -1098,7 +1206,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { int value = calendar.get(Calendar.HOUR_OF_DAY); if (value == 0) { value = calendar.getMaximum(Calendar.HOUR_OF_DAY) + 1; @@ -1110,25 +1218,75 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final int value) { + public void appendTo(final Appendable buffer, final int value) throws IOException { mRule.appendTo(buffer, value); } } - // ----------------------------------------------------------------------- + /** + * <p>Inner class to output the numeric day in week.</p> + */ + private static class DayInWeekField implements NumberRule { + private final NumberRule mRule; + + DayInWeekField(final NumberRule rule) { + mRule = rule; + } + + @Override + public int estimateLength() { + return mRule.estimateLength(); + } - private static final ConcurrentMap<TimeZoneDisplayKey, String> cTimeZoneDisplayCache = new ConcurrentHashMap<>( - 7); + @Override + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { + final int value = calendar.get(Calendar.DAY_OF_WEEK); + mRule.appendTo(buffer, value != Calendar.SUNDAY ? value - 1 : 7); + } + @Override + public void appendTo(final Appendable buffer, final int value) throws IOException { + mRule.appendTo(buffer, value); + } + } + + /** + * <p>Inner class to output the numeric day in week.</p> + */ + private static class WeekYear implements NumberRule { + private final NumberRule mRule; + + WeekYear(final NumberRule rule) { + mRule = rule; + } + + @Override + public int estimateLength() { + return mRule.estimateLength(); + } + + @Override + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { + mRule.appendTo(buffer, calendar.getWeekYear()); + } + + @Override + public void appendTo(final Appendable buffer, final int value) throws IOException { + mRule.appendTo(buffer, value); + } + } + + //----------------------------------------------------------------------- + + private static final ConcurrentMap<TimeZoneDisplayKey, String> cTimeZoneDisplayCache = + new ConcurrentHashMap<>(7); /** - * <p> - * Gets the time zone display name, using a cache for performance. - * </p> + * <p>Gets the time zone display name, using a cache for performance.</p> * - * @param tz the zone to query - * @param daylight true if daylight savings - * @param style the style to use {@code TimeZone.LONG} or {@code TimeZone.SHORT} - * @param locale the locale to use + * @param tz the zone to query + * @param daylight true if daylight savings + * @param style the style to use {@code TimeZone.LONG} or {@code TimeZone.SHORT} + * @param locale the locale to use * @return the textual name of the time zone */ static String getTimeZoneDisplay(final TimeZone tz, final boolean daylight, final int style, final Locale locale) { @@ -1139,16 +1297,14 @@ public class FastDatePrinter implements DatePrinter, Serializable { value = tz.getDisplayName(daylight, style, locale); final String prior = cTimeZoneDisplayCache.putIfAbsent(key, value); if (prior != null) { - value = prior; + value= prior; } } return value; } /** - * <p> - * Inner class to output a time zone name. - * </p> + * <p>Inner class to output a time zone name.</p> */ private static class TimeZoneNameRule implements Rule { private final Locale mLocale; @@ -1166,7 +1322,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { TimeZoneNameRule(final TimeZone timeZone, final Locale locale, final int style) { mLocale = locale; mStyle = style; - + mStandard = getTimeZoneDisplay(timeZone, false, style, locale); mDaylight = getTimeZoneDisplay(timeZone, true, style, locale); } @@ -1186,7 +1342,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { final TimeZone zone = calendar.getTimeZone(); if (calendar.get(Calendar.DST_OFFSET) != 0) { buffer.append(getTimeZoneDisplay(zone, true, mStyle, mLocale)); @@ -1197,14 +1353,13 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * <p> - * Inner class to output a time zone as a number {@code +/-HHMM} or {@code +/-HH:MM}. - * </p> + * <p>Inner class to output a time zone as a number {@code +/-HHMM} + * or {@code +/-HH:MM}.</p> */ private static class TimeZoneNumberRule implements Rule { static final TimeZoneNumberRule INSTANCE_COLON = new TimeZoneNumberRule(true); static final TimeZoneNumberRule INSTANCE_NO_COLON = new TimeZoneNumberRule(false); - + final boolean mColon; /** @@ -1228,8 +1383,8 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { - + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { + int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET); if (offset < 0) { @@ -1252,14 +1407,13 @@ public class FastDatePrinter implements DatePrinter, Serializable { } /** - * <p> - * Inner class to output a time zone as a number {@code +/-HHMM} or {@code +/-HH:MM}. - * </p> + * <p>Inner class to output a time zone as a number {@code +/-HHMM} + * or {@code +/-HH:MM}.</p> */ private static class Iso8601_Rule implements Rule { - + // Sign TwoDigitHours or Z - static final Iso8601_Rule ISO8601_HOURS = new Iso8601_Rule(3); + static final Iso8601_Rule ISO8601_HOURS = new Iso8601_Rule(3); // Sign TwoDigitHours Minutes or Z static final Iso8601_Rule ISO8601_HOURS_MINUTES = new Iso8601_Rule(5); // Sign TwoDigitHours : Minutes or Z @@ -1269,11 +1423,11 @@ public class FastDatePrinter implements DatePrinter, Serializable { * Factory method for Iso8601_Rules. * * @param tokenLen a token indicating the length of the TimeZone String to be formatted. - * @return a Iso8601_Rule that can format TimeZone String of length {@code tokenLen}. If no such rule exists, an - * IllegalArgumentException will be thrown. + * @return a Iso8601_Rule that can format TimeZone String of length {@code tokenLen}. If no such + * rule exists, an IllegalArgumentException will be thrown. */ static Iso8601_Rule getRule(final int tokenLen) { - switch (tokenLen) { + switch(tokenLen) { case 1: return Iso8601_Rule.ISO8601_HOURS; case 2: @@ -1281,10 +1435,10 @@ public class FastDatePrinter implements DatePrinter, Serializable { case 3: return Iso8601_Rule.ISO8601_HOURS_COLON_MINUTES; default: - throw new IllegalArgumentException("invalid number of X"); + throw new IllegalArgumentException("invalid number of X"); } - } - + } + final int length; /** @@ -1308,13 +1462,13 @@ public class FastDatePrinter implements DatePrinter, Serializable { * {@inheritDoc} */ @Override - public void appendTo(final StringBuilder buffer, final Calendar calendar) { + public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException { int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET); if (offset == 0) { buffer.append("Z"); return; } - + if (offset < 0) { buffer.append('-'); offset = -offset; @@ -1325,11 +1479,11 @@ public class FastDatePrinter implements DatePrinter, Serializable { final int hours = offset / (60 * 60 * 1000); appendDigits(buffer, hours); - if (length < 5) { + if (length<5) { return; } - - if (length == 6) { + + if (length==6) { buffer.append(':'); } @@ -1340,9 +1494,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { // ---------------------------------------------------------------------- /** - * <p> - * Inner class that acts as a compound key for time zone names. - * </p> + * <p>Inner class that acts as a compound key for time zone names.</p> */ private static class TimeZoneDisplayKey { private final TimeZone mTimeZone; @@ -1357,7 +1509,8 @@ public class FastDatePrinter implements DatePrinter, Serializable { * @param style the timezone style * @param locale the timezone locale */ - TimeZoneDisplayKey(final TimeZone timeZone, final boolean daylight, final int style, final Locale locale) { + TimeZoneDisplayKey(final TimeZone timeZone, + final boolean daylight, final int style, final Locale locale) { mTimeZone = timeZone; if (daylight) { mStyle = style | 0x80000000; @@ -1372,7 +1525,7 @@ public class FastDatePrinter implements DatePrinter, Serializable { */ @Override public int hashCode() { - return (mStyle * 31 + mLocale.hashCode()) * 31 + mTimeZone.hashCode(); + return (mStyle * 31 + mLocale.hashCode() ) * 31 + mTimeZone.hashCode(); } /** @@ -1384,8 +1537,11 @@ public class FastDatePrinter implements DatePrinter, Serializable { return true; } if (obj instanceof TimeZoneDisplayKey) { - final TimeZoneDisplayKey other = (TimeZoneDisplayKey) obj; - return mTimeZone.equals(other.mTimeZone) && mStyle == other.mStyle && mLocale.equals(other.mLocale); + final TimeZoneDisplayKey other = (TimeZoneDisplayKey)obj; + return + mTimeZone.equals(other.mTimeZone) && + mStyle == other.mStyle && + mLocale.equals(other.mLocale); } return false; } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/81b5806e/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FormatCache.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FormatCache.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FormatCache.java index 98d494a..e146a1d 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FormatCache.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FormatCache.java @@ -26,29 +26,32 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** + * <p>FormatCache is a cache and factory for {@link Format}s.</p> + * * <p> - * FormatCache is a cache and factory for {@link Format}s. + * Copied and modified from <a href="https://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a>. * </p> - * - * @since 3.0 + * + * @since Apache Commons Lang 3.0 */ // TODO: Before making public move from getDateTimeInstance(Integer,...) to int; or some other approach. abstract class FormatCache<F extends Format> { + /** - * No date or no time. Used in same parameters as DateFormat.SHORT or DateFormat.LONG + * No date or no time. Used in same parameters as DateFormat.SHORT or DateFormat.LONG */ - static final int NONE = -1; - - private static final ConcurrentMap<MultipartKey, String> DATETIME_INSTANCE_CACHE = - new ConcurrentHashMap<>(7); - - private final ConcurrentMap<MultipartKey, F> cInstanceCache = new ConcurrentHashMap<>(7); + static final int NONE= -1; + + private final ConcurrentMap<MultipartKey, F> cInstanceCache + = new ConcurrentHashMap<>(7); + + private static final ConcurrentMap<MultipartKey, String> cDateTimeInstanceCache + = new ConcurrentHashMap<>(7); /** - * <p> - * Gets a formatter instance using the default pattern in the default timezone and locale. - * </p> - * + * <p>Gets a formatter instance using the default pattern in the + * default timezone and locale.</p> + * * @return a date/time formatter */ public F getInstance() { @@ -56,15 +59,16 @@ abstract class FormatCache<F extends Format> { } /** - * <p> - * Gets a formatter instance using the specified pattern, time zone and locale. - * </p> - * - * @param pattern {@link java.text.SimpleDateFormat} compatible pattern, non-null - * @param timeZone the time zone, null means use the default TimeZone - * @param locale the locale, null means use the default Locale + * <p>Gets a formatter instance using the specified pattern, time zone + * and locale.</p> + * + * @param pattern {@link java.text.SimpleDateFormat} compatible + * pattern, non-null + * @param timeZone the time zone, null means use the default TimeZone + * @param locale the locale, null means use the default Locale * @return a pattern based date/time formatter - * @throws IllegalArgumentException if pattern is invalid or <code>null</code> + * @throws IllegalArgumentException if pattern is invalid + * or <code>null</code> */ public F getInstance(final String pattern, TimeZone timeZone, Locale locale) { if (pattern == null) { @@ -78,46 +82,46 @@ abstract class FormatCache<F extends Format> { } final MultipartKey key = new MultipartKey(pattern, timeZone, locale); F format = cInstanceCache.get(key); - if (format == null) { + if (format == null) { format = createInstance(pattern, timeZone, locale); - final F previousValue = cInstanceCache.putIfAbsent(key, format); + final F previousValue= cInstanceCache.putIfAbsent(key, format); if (previousValue != null) { // another thread snuck in and did the same work // we should return the instance that is in ConcurrentMap - format = previousValue; + format= previousValue; } } return format; } - + /** - * <p> - * Create a format instance using the specified pattern, time zone and locale. - * </p> - * - * @param pattern {@link java.text.SimpleDateFormat} compatible pattern, this will not be null. - * @param timeZone time zone, this will not be null. - * @param locale locale, this will not be null. + * <p>Create a format instance using the specified pattern, time zone + * and locale.</p> + * + * @param pattern {@link java.text.SimpleDateFormat} compatible pattern, this will not be null. + * @param timeZone time zone, this will not be null. + * @param locale locale, this will not be null. * @return a pattern based date/time formatter - * @throws IllegalArgumentException if pattern is invalid or <code>null</code> + * @throws IllegalArgumentException if pattern is invalid + * or <code>null</code> */ abstract protected F createInstance(String pattern, TimeZone timeZone, Locale locale); - + /** - * <p> - * Gets a date/time formatter instance using the specified style, time zone and locale. - * </p> - * - * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT, null indicates no date in format - * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT, null indicates no time in format - * @param timeZone optional time zone, overrides time zone of formatted date, null means use default Locale - * @param locale optional locale, overrides system locale + * <p>Gets a date/time formatter instance using the specified style, + * time zone and locale.</p> + * + * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT, null indicates no date in format + * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT, null indicates no time in format + * @param timeZone optional time zone, overrides time zone of + * formatted date, null means use default Locale + * @param locale optional locale, overrides system locale * @return a localized standard date/time formatter - * @throws IllegalArgumentException if the Locale has no date/time pattern defined + * @throws IllegalArgumentException if the Locale has no date/time + * pattern defined */ - // This must remain private, see LANG-884 - private F getDateTimeInstance(final Integer dateStyle, final Integer timeStyle, final TimeZone timeZone, - Locale locale) { + // This must remain private, see LANG-884 + private F getDateTimeInstance(final Integer dateStyle, final Integer timeStyle, final TimeZone timeZone, Locale locale) { if (locale == null) { locale = Locale.getDefault(); } @@ -126,16 +130,17 @@ abstract class FormatCache<F extends Format> { } /** - * <p> - * Gets a date/time formatter instance using the specified style, time zone and locale. - * </p> - * - * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT - * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT - * @param timeZone optional time zone, overrides time zone of formatted date, null means use default Locale - * @param locale optional locale, overrides system locale + * <p>Gets a date/time formatter instance using the specified style, + * time zone and locale.</p> + * + * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT + * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT + * @param timeZone optional time zone, overrides time zone of + * formatted date, null means use default Locale + * @param locale optional locale, overrides system locale * @return a localized standard date/time formatter - * @throws IllegalArgumentException if the Locale has no date/time pattern defined + * @throws IllegalArgumentException if the Locale has no date/time + * pattern defined */ // package protected, for access from FastDateFormat; do not make public or protected F getDateTimeInstance(final int dateStyle, final int timeStyle, final TimeZone timeZone, final Locale locale) { @@ -143,15 +148,16 @@ abstract class FormatCache<F extends Format> { } /** - * <p> - * Gets a date formatter instance using the specified style, time zone and locale. - * </p> - * - * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT - * @param timeZone optional time zone, overrides time zone of formatted date, null means use default Locale - * @param locale optional locale, overrides system locale + * <p>Gets a date formatter instance using the specified style, + * time zone and locale.</p> + * + * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT + * @param timeZone optional time zone, overrides time zone of + * formatted date, null means use default Locale + * @param locale optional locale, overrides system locale * @return a localized standard date/time formatter - * @throws IllegalArgumentException if the Locale has no date/time pattern defined + * @throws IllegalArgumentException if the Locale has no date/time + * pattern defined */ // package protected, for access from FastDateFormat; do not make public or protected F getDateInstance(final int dateStyle, final TimeZone timeZone, final Locale locale) { @@ -159,15 +165,16 @@ abstract class FormatCache<F extends Format> { } /** - * <p> - * Gets a time formatter instance using the specified style, time zone and locale. - * </p> - * - * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT - * @param timeZone optional time zone, overrides time zone of formatted date, null means use default Locale - * @param locale optional locale, overrides system locale + * <p>Gets a time formatter instance using the specified style, + * time zone and locale.</p> + * + * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT + * @param timeZone optional time zone, overrides time zone of + * formatted date, null means use default Locale + * @param locale optional locale, overrides system locale * @return a localized standard date/time formatter - * @throws IllegalArgumentException if the Locale has no date/time pattern defined + * @throws IllegalArgumentException if the Locale has no date/time + * pattern defined */ // package protected, for access from FastDateFormat; do not make public or protected F getTimeInstance(final int timeStyle, final TimeZone timeZone, final Locale locale) { @@ -175,13 +182,11 @@ abstract class FormatCache<F extends Format> { } /** - * <p> - * Gets a date/time format for the specified styles and locale. - * </p> - * - * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT, null indicates no date in format - * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT, null indicates no time in format - * @param locale The non-null locale of the desired format + * <p>Gets a date/time format for the specified styles and locale.</p> + * + * @param dateStyle date style: FULL, LONG, MEDIUM, or SHORT, null indicates no date in format + * @param timeStyle time style: FULL, LONG, MEDIUM, or SHORT, null indicates no time in format + * @param locale The non-null locale of the desired format * @return a localized standard date/time format * @throws IllegalArgumentException if the Locale has no date/time pattern defined */ @@ -189,24 +194,26 @@ abstract class FormatCache<F extends Format> { static String getPatternForStyle(final Integer dateStyle, final Integer timeStyle, final Locale locale) { final MultipartKey key = new MultipartKey(dateStyle, timeStyle, locale); - String pattern = DATETIME_INSTANCE_CACHE.get(key); + String pattern = cDateTimeInstanceCache.get(key); if (pattern == null) { try { DateFormat formatter; if (dateStyle == null) { - formatter = DateFormat.getTimeInstance(timeStyle.intValue(), locale); - } else if (timeStyle == null) { - formatter = DateFormat.getDateInstance(dateStyle.intValue(), locale); - } else { + formatter = DateFormat.getTimeInstance(timeStyle.intValue(), locale); + } + else if (timeStyle == null) { + formatter = DateFormat.getDateInstance(dateStyle.intValue(), locale); + } + else { formatter = DateFormat.getDateTimeInstance(dateStyle.intValue(), timeStyle.intValue(), locale); } - pattern = ((SimpleDateFormat) formatter).toPattern(); - final String previous = DATETIME_INSTANCE_CACHE.putIfAbsent(key, pattern); + pattern = ((SimpleDateFormat)formatter).toPattern(); + final String previous = cDateTimeInstanceCache.putIfAbsent(key, pattern); if (previous != null) { // even though it doesn't matter if another thread put the pattern // it's still good practice to return the String instance that is // actually in the ConcurrentMap - pattern = previous; + pattern= previous; } } catch (final ClassCastException ex) { throw new IllegalArgumentException("No date time pattern for locale: " + locale); @@ -217,9 +224,7 @@ abstract class FormatCache<F extends Format> { // ---------------------------------------------------------------------- /** - * <p> - * Helper class to hold multi-part Map keys - * </p> + * <p>Helper class to hold multi-part Map keys</p> */ private static class MultipartKey { private final Object[] keys; @@ -227,8 +232,7 @@ abstract class FormatCache<F extends Format> { /** * Constructs an instance of <code>MultipartKey</code> to hold the specified objects. - * - * @param keys the set of objects that make up the key. Each key may be null. + * @param keys the set of objects that make up the key. Each key may be null. */ public MultipartKey(final Object... keys) { this.keys = keys; @@ -242,7 +246,7 @@ abstract class FormatCache<F extends Format> { // Eliminate the usual boilerplate because // this inner static class is only used in a generic ConcurrentHashMap // which will not compare against other Object types - return obj != null && Arrays.equals(keys, ((MultipartKey) obj).keys); + return Arrays.equals(keys, ((MultipartKey)obj).keys); } /** @@ -250,14 +254,14 @@ abstract class FormatCache<F extends Format> { */ @Override public int hashCode() { - if (hashCode == 0) { - int rc = 0; - for (final Object key : keys) { - if (key != null) { - rc = rc * 7 + key.hashCode(); + if(hashCode==0) { + int rc= 0; + for(final Object key : keys) { + if(key!=null) { + rc= rc*7 + key.hashCode(); } } - hashCode = rc; + hashCode= rc; } return hashCode; }