You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2018/01/29 16:57:58 UTC
[3/6] logging-log4j2 git commit: LOG4J2-2224 Renamed package
core.util.datetime to core.time.internal.format to clarify these classes are
to be considered private.
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/14d0150f/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
deleted file mode 100644
index 4f33b6b..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FastDatePrinter.java
+++ /dev/null
@@ -1,1549 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.util.datetime;
-
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.Serializable;
-import java.text.DateFormat;
-import java.text.DateFormatSymbols;
-import java.text.FieldPosition;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Date;
-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;
-
-/**
- * <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 StringBuffer conversion.
- //
- // The following produces a padded 2 digit number:
- // buffer.append((char)(value / 10 + '0'));
- // buffer.append((char)(value % 10 + '0'));
- //
- // 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;
- /**
- * LONG locale dependent date or time style.
- */
- public static final int LONG = DateFormat.LONG;
- /**
- * MEDIUM locale dependent date or time style.
- */
- public static final int MEDIUM = DateFormat.MEDIUM;
- /**
- * SHORT locale dependent date or time style.
- */
- public static final int SHORT = DateFormat.SHORT;
-
- /**
- * The pattern.
- */
- private final String mPattern;
- /**
- * The time zone.
- */
- private final TimeZone mTimeZone;
- /**
- * The locale.
- */
- private final Locale mLocale;
- /**
- * The parsed rules.
- */
- private transient Rule[] mRules;
- /**
- * The estimated maximum length.
- */
- 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.
- *
- * @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) {
- mPattern = pattern;
- mTimeZone = timeZone;
- mLocale = locale;
-
- init();
- }
-
- /**
- * <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; ) {
- len += mRules[i].estimateLength();
- }
-
- mMaxLengthEstimate = len;
- }
-
- // Parse the pattern
- //-----------------------------------------------------------------------
- /**
- * <p>Returns a list of Rules given a pattern.</p>
- *
- * @return a {@code List} of Rule objects
- * @throws IllegalArgumentException if pattern is invalid
- */
- protected List<Rule> parsePattern() {
- final DateFormatSymbols symbols = new DateFormatSymbols(mLocale);
- final List<Rule> rules = new ArrayList<>();
-
- final String[] ERAs = symbols.getEras();
- final String[] months = symbols.getMonths();
- final String[] shortMonths = symbols.getShortMonths();
- final String[] weekdays = symbols.getWeekdays();
- final String[] shortWeekdays = symbols.getShortWeekdays();
- final String[] AmPmStrings = symbols.getAmPmStrings();
-
- final int length = mPattern.length();
- final int[] indexRef = new int[1];
-
- for (int i = 0; i < length; i++) {
- indexRef[0] = i;
- final String token = parseToken(mPattern, indexRef);
- i = indexRef[0];
-
- final int tokenLen = token.length();
- if (tokenLen == 0) {
- break;
- }
-
- Rule rule;
- final char c = token.charAt(0);
-
- switch (c) {
- case 'G': // era designator (text)
- 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) {
- rule = new TextField(Calendar.MONTH, months);
- } else if (tokenLen == 3) {
- rule = new TextField(Calendar.MONTH, shortMonths);
- } else if (tokenLen == 2) {
- rule = TwoDigitMonthField.INSTANCE;
- } else {
- rule = UnpaddedMonthField.INSTANCE;
- }
- break;
- case 'd': // day in month (number)
- rule = selectNumberRule(Calendar.DAY_OF_MONTH, tokenLen);
- break;
- case 'h': // hour in am/pm (number, 1..12)
- rule = new TwelveHourField(selectNumberRule(Calendar.HOUR, tokenLen));
- break;
- case 'H': // hour in day (number, 0..23)
- rule = selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen);
- break;
- case 'm': // minute in hour (number)
- rule = selectNumberRule(Calendar.MINUTE, tokenLen);
- break;
- case 's': // second in minute (number)
- rule = selectNumberRule(Calendar.SECOND, tokenLen);
- break;
- case 'S': // millisecond (number)
- rule = selectNumberRule(Calendar.MILLISECOND, tokenLen);
- break;
- 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;
- case 'F': // day of week in month (number)
- rule = selectNumberRule(Calendar.DAY_OF_WEEK_IN_MONTH, tokenLen);
- break;
- case 'w': // week in year (number)
- rule = selectNumberRule(Calendar.WEEK_OF_YEAR, tokenLen);
- break;
- case 'W': // week in month (number)
- rule = selectNumberRule(Calendar.WEEK_OF_MONTH, tokenLen);
- break;
- case 'a': // am/pm marker (text)
- rule = new TextField(Calendar.AM_PM, AmPmStrings);
- break;
- case 'k': // hour in day (1..24)
- rule = new TwentyFourHourField(selectNumberRule(Calendar.HOUR_OF_DAY, tokenLen));
- break;
- case 'K': // hour in am/pm (0..11)
- rule = selectNumberRule(Calendar.HOUR, tokenLen);
- break;
- case 'X': // ISO 8601
- rule = Iso8601_Rule.getRule(tokenLen);
- break;
- case 'z': // time zone (text)
- if (tokenLen >= 4) {
- rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.LONG);
- } else {
- rule = new TimeZoneNameRule(mTimeZone, mLocale, TimeZone.SHORT);
- }
- break;
- case 'Z': // time zone (value)
- if (tokenLen == 1) {
- rule = TimeZoneNumberRule.INSTANCE_NO_COLON;
- } else if (tokenLen == 2) {
- rule = Iso8601_Rule.ISO8601_HOURS_COLON_MINUTES;
- } else {
- rule = TimeZoneNumberRule.INSTANCE_COLON;
- }
- break;
- case '\'': // literal text
- final String sub = token.substring(1);
- if (sub.length() == 1) {
- rule = new CharacterLiteral(sub.charAt(0));
- } else {
- rule = new StringLiteral(sub);
- }
- break;
- default:
- throw new IllegalArgumentException("Illegal pattern component: " + token);
- }
-
- rules.add(rule);
- }
-
- return rules;
- }
-
- /**
- * <p>Performs the parsing of tokens.</p>
- *
- * @param pattern the pattern
- * @param indexRef index references
- * @return parsed token
- */
- protected String parseToken(final String pattern, final int[] indexRef) {
- final StringBuilder buf = new StringBuilder();
-
- int i = indexRef[0];
- final int length = pattern.length();
-
- char c = pattern.charAt(i);
- if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
- // Scan a run of the same character, which indicates a time
- // pattern.
- buf.append(c);
-
- while (i + 1 < length) {
- final char peek = pattern.charAt(i + 1);
- if (peek == c) {
- buf.append(c);
- i++;
- } else {
- break;
- }
- }
- } else {
- // This will identify token as text.
- buf.append('\'');
-
- boolean inLiteral = false;
-
- for (; i < length; i++) {
- c = pattern.charAt(i);
-
- if (c == '\'') {
- if (i + 1 < length && pattern.charAt(i + 1) == '\'') {
- // '' is treated as escaped '
- i++;
- buf.append(c);
- } else {
- inLiteral = !inLiteral;
- }
- } else if (!inLiteral &&
- (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')) {
- i--;
- break;
- } else {
- buf.append(c);
- }
- }
- }
-
- indexRef[0] = i;
- return buf.toString();
- }
-
- /**
- * <p>Gets an appropriate rule for the padding required.</p>
- *
- * @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) {
- switch (padding) {
- case 1:
- return new UnpaddedNumberField(field);
- case 2:
- return new TwoDigitNumberField(field);
- default:
- return new PaddedNumberField(field, padding);
- }
- }
-
- // Format methods
- //-----------------------------------------------------------------------
- /**
- * <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) {
- return format((Date) obj, toAppendTo);
- } else if (obj instanceof Calendar) {
- return format((Calendar) obj, toAppendTo);
- } else if (obj instanceof Long) {
- return format(((Long) obj).longValue(), toAppendTo);
- } else {
- throw new IllegalArgumentException("Unknown class: " +
- (obj == null ? "<null>" : obj.getClass().getName()));
- }
- }
-
- /**
- * <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();
- 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.
- */
- private String applyRulesToString(final Calendar c) {
- return applyRules(c, new StringBuilder(mMaxLengthEstimate)).toString();
- }
-
- /**
- * Creation method for new calender instances.
- * @return a new Calendar instance.
- */
- private Calendar newCalendar() {
- return Calendar.getInstance(mTimeZone, mLocale);
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.lang3.time.DatePrinter#format(java.util.Date)
- */
- @Override
- public String format(final Date date) {
- final Calendar c = newCalendar();
- c.setTime(date);
- return applyRulesToString(c);
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.lang3.time.DatePrinter#format(java.util.Calendar)
- */
- @Override
- public String format(final Calendar calendar) {
- return format(calendar, new StringBuilder(mMaxLengthEstimate)).toString();
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.lang3.time.DatePrinter#format(long, java.lang.Appendable)
- */
- @Override
- 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.Appendable)
- */
- @Override
- 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.Appendable)
- */
- @Override
- 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
- if(!calendar.getTimeZone().equals(mTimeZone)) {
- calendar = (Calendar)calendar.clone();
- calendar.setTimeZone(mTimeZone);
- }
- return applyRules(calendar, buf);
- }
-
- /**
- * 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
- */
- 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)
- * @see org.apache.commons.lang3.time.DatePrinter#getPattern()
- */
- @Override
- public String getPattern() {
- return mPattern;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.lang3.time.DatePrinter#getTimeZone()
- */
- @Override
- public TimeZone getTimeZone() {
- return mTimeZone;
- }
-
- /* (non-Javadoc)
- * @see org.apache.commons.lang3.time.DatePrinter#getLocale()
- */
- @Override
- public Locale getLocale() {
- return mLocale;
- }
-
- /**
- * <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>
- *
- * @return the maximum formatted length
- */
- public int getMaxLengthEstimate() {
- return mMaxLengthEstimate;
- }
-
- // Basics
- //-----------------------------------------------------------------------
- /**
- * <p>Compares two objects for equality.</p>
- *
- * @param obj the object to compare to
- * @return {@code true} if equal
- */
- @Override
- public boolean equals(final Object obj) {
- if (obj instanceof FastDatePrinter == false) {
- return false;
- }
- final FastDatePrinter other = (FastDatePrinter) obj;
- return mPattern.equals(other.mPattern)
- && mTimeZone.equals(other.mTimeZone)
- && mLocale.equals(other.mLocale);
- }
-
- /**
- * <p>Returns a hash code compatible with equals.</p>
- *
- * @return a hash code compatible with equals
- */
- @Override
- public int hashCode() {
- return mPattern.hashCode() + 13 * (mTimeZone.hashCode() + 13 * mLocale.hashCode());
- }
-
- /**
- * <p>Gets a debugging string version of this formatter.</p>
- *
- * @return a debugging string
- */
- @Override
- public String toString() {
- return "FastDatePrinter[" + mPattern + "," + mLocale + "," + mTimeZone.getID() + "]";
- }
-
- // Serializing
- //-----------------------------------------------------------------------
- /**
- * 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.
- * @throws ClassNotFoundException if a class cannot be found.
- */
- private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
- in.defaultReadObject();
- init();
- }
-
- /**
- * 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 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>
- */
- private interface Rule {
- /**
- * Returns the estimated length of the result.
- *
- * @return the estimated length
- */
- int estimateLength();
-
- /**
- * Appends the value of the specified calendar to the output buffer based on the rule implementation.
- *
- * @param buf the output buffer
- * @param calendar calendar to be appended
- * @throws IOException if an I/O error occurs
- */
- void appendTo(Appendable buf, Calendar calendar) throws IOException;
- }
-
- /**
- * <p>Inner class defining a numeric rule.</p>
- */
- private interface NumberRule extends Rule {
- /**
- * Appends the specified value to the output buffer based on the rule implementation.
- *
- * @param buffer the output buffer
- * @param value the value to be appended
- * @throws IOException if an I/O error occurs
- */
- void appendTo(Appendable buffer, int value) throws IOException;
- }
-
- /**
- * <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.
- *
- * @param value the character literal
- */
- CharacterLiteral(final char value) {
- mValue = value;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return 1;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException {
- buffer.append(mValue);
- }
- }
-
- /**
- * <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.
- *
- * @param value the string literal
- */
- StringLiteral(final String value) {
- mValue = value;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return mValue.length();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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>
- */
- 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.
- *
- * @param field the field
- * @param values the field values
- */
- TextField(final int field, final String[] values) {
- mField = field;
- mValues = values;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- int max = 0;
- for (int i=mValues.length; --i >= 0; ) {
- final int len = mValues[i].length();
- if (len > max) {
- max = len;
- }
- }
- return max;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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>
- */
- private static class UnpaddedNumberField implements NumberRule {
- private final int mField;
-
- /**
- * Constructs an instance of {@code UnpadedNumberField} with the specified field.
- *
- * @param field the field
- */
- UnpaddedNumberField(final int field) {
- mField = field;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return 4;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException {
- appendTo(buffer, calendar.get(mField));
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public final void appendTo(final Appendable buffer, final int value) throws IOException {
- if (value < 10) {
- buffer.append((char)(value + '0'));
- } else if (value < 100) {
- appendDigits(buffer, value);
- } else {
- appendFullDigits(buffer, value, 1);
- }
- }
- }
-
- /**
- * <p>Inner class to output an unpadded month.</p>
- */
- private static class UnpaddedMonthField implements NumberRule {
- static final UnpaddedMonthField INSTANCE = new UnpaddedMonthField();
-
- /**
- * Constructs an instance of {@code UnpaddedMonthField}.
- *
- */
- UnpaddedMonthField() {
- super();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return 2;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException {
- appendTo(buffer, calendar.get(Calendar.MONTH) + 1);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public final void appendTo(final Appendable buffer, final int value) throws IOException {
- if (value < 10) {
- buffer.append((char)(value + '0'));
- } else {
- appendDigits(buffer, value);
- }
- }
- }
-
- /**
- * <p>Inner class to output a padded number.</p>
- */
- private static class PaddedNumberField implements NumberRule {
- private final int mField;
- private final int mSize;
-
- /**
- * Constructs an instance of {@code PaddedNumberField}.
- *
- * @param field the field
- * @param size size of the output field
- */
- PaddedNumberField(final int field, final int size) {
- if (size < 3) {
- // Should use UnpaddedNumberField or TwoDigitNumberField.
- throw new IllegalArgumentException();
- }
- mField = field;
- mSize = size;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return mSize;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException {
- appendTo(buffer, calendar.get(mField));
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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>
- */
- private static class TwoDigitNumberField implements NumberRule {
- private final int mField;
-
- /**
- * Constructs an instance of {@code TwoDigitNumberField} with the specified field.
- *
- * @param field the field
- */
- TwoDigitNumberField(final int field) {
- mField = field;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return 2;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException {
- appendTo(buffer, calendar.get(mField));
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public final void appendTo(final Appendable buffer, final int value) throws IOException {
- if (value < 100) {
- appendDigits(buffer, value);
- } else {
- appendFullDigits(buffer, value, 2);
- }
- }
- }
-
- /**
- * <p>Inner class to output a two digit year.</p>
- */
- private static class TwoDigitYearField implements NumberRule {
- static final TwoDigitYearField INSTANCE = new TwoDigitYearField();
-
- /**
- * Constructs an instance of {@code TwoDigitYearField}.
- */
- TwoDigitYearField() {
- super();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return 2;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException {
- appendTo(buffer, calendar.get(Calendar.YEAR) % 100);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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>
- */
- private static class TwoDigitMonthField implements NumberRule {
- static final TwoDigitMonthField INSTANCE = new TwoDigitMonthField();
-
- /**
- * Constructs an instance of {@code TwoDigitMonthField}.
- */
- TwoDigitMonthField() {
- super();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return 2;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException {
- appendTo(buffer, calendar.get(Calendar.MONTH) + 1);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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>
- */
- private static class TwelveHourField implements NumberRule {
- private final NumberRule mRule;
-
- /**
- * Constructs an instance of {@code TwelveHourField} with the specified
- * {@code NumberRule}.
- *
- * @param rule the rule
- */
- TwelveHourField(final NumberRule rule) {
- mRule = rule;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return mRule.estimateLength();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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;
- }
- mRule.appendTo(buffer, value);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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>
- */
- private static class TwentyFourHourField implements NumberRule {
- private final NumberRule mRule;
-
- /**
- * Constructs an instance of {@code TwentyFourHourField} with the specified
- * {@code NumberRule}.
- *
- * @param rule the rule
- */
- TwentyFourHourField(final NumberRule rule) {
- mRule = rule;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return mRule.estimateLength();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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;
- }
- mRule.appendTo(buffer, value);
- }
-
- /**
- * {@inheritDoc}
- */
- @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 DayInWeekField implements NumberRule {
- private final NumberRule mRule;
-
- DayInWeekField(final NumberRule rule) {
- mRule = rule;
- }
-
- @Override
- public int estimateLength() {
- return mRule.estimateLength();
- }
-
- @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>
- *
- * @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) {
- final TimeZoneDisplayKey key = new TimeZoneDisplayKey(tz, daylight, style, locale);
- String value = cTimeZoneDisplayCache.get(key);
- if (value == null) {
- // This is a very slow call, so cache the results.
- value = tz.getDisplayName(daylight, style, locale);
- final String prior = cTimeZoneDisplayCache.putIfAbsent(key, value);
- if (prior != null) {
- value= prior;
- }
- }
- return value;
- }
-
- /**
- * <p>Inner class to output a time zone name.</p>
- */
- private static class TimeZoneNameRule implements Rule {
- private final Locale mLocale;
- private final int mStyle;
- private final String mStandard;
- private final String mDaylight;
-
- /**
- * Constructs an instance of {@code TimeZoneNameRule} with the specified properties.
- *
- * @param timeZone the time zone
- * @param locale the locale
- * @param style the style
- */
- 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);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- // We have no access to the Calendar object that will be passed to
- // appendTo so base estimate on the TimeZone passed to the
- // constructor
- return Math.max(mStandard.length(), mDaylight.length());
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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));
- } else {
- buffer.append(getTimeZoneDisplay(zone, false, mStyle, mLocale));
- }
- }
- }
-
- /**
- * <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;
-
- /**
- * Constructs an instance of {@code TimeZoneNumberRule} with the specified properties.
- *
- * @param colon add colon between HH and MM in the output if {@code true}
- */
- TimeZoneNumberRule(final boolean colon) {
- mColon = colon;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return 5;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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('-');
- offset = -offset;
- } else {
- buffer.append('+');
- }
-
- final int hours = offset / (60 * 60 * 1000);
- appendDigits(buffer, hours);
-
- if (mColon) {
- buffer.append(':');
- }
-
- final int minutes = offset / (60 * 1000) - 60 * hours;
- appendDigits(buffer, minutes);
- }
- }
-
- /**
- * <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);
- // Sign TwoDigitHours Minutes or Z
- static final Iso8601_Rule ISO8601_HOURS_MINUTES = new Iso8601_Rule(5);
- // Sign TwoDigitHours : Minutes or Z
- static final Iso8601_Rule ISO8601_HOURS_COLON_MINUTES = new Iso8601_Rule(6);
-
- /**
- * 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.
- */
- static Iso8601_Rule getRule(final int tokenLen) {
- switch(tokenLen) {
- case 1:
- return Iso8601_Rule.ISO8601_HOURS;
- case 2:
- return Iso8601_Rule.ISO8601_HOURS_MINUTES;
- case 3:
- return Iso8601_Rule.ISO8601_HOURS_COLON_MINUTES;
- default:
- throw new IllegalArgumentException("invalid number of X");
- }
- }
-
- final int length;
-
- /**
- * Constructs an instance of {@code Iso8601_Rule} with the specified properties.
- *
- * @param length The number of characters in output (unless Z is output)
- */
- Iso8601_Rule(final int length) {
- this.length = length;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int estimateLength() {
- return length;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- 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;
- } else {
- buffer.append('+');
- }
-
- final int hours = offset / (60 * 60 * 1000);
- appendDigits(buffer, hours);
-
- if (length<5) {
- return;
- }
-
- if (length==6) {
- buffer.append(':');
- }
-
- final int minutes = offset / (60 * 1000) - 60 * hours;
- appendDigits(buffer, minutes);
- }
- }
-
- // ----------------------------------------------------------------------
- /**
- * <p>Inner class that acts as a compound key for time zone names.</p>
- */
- private static class TimeZoneDisplayKey {
- private final TimeZone mTimeZone;
- private final int mStyle;
- private final Locale mLocale;
-
- /**
- * Constructs an instance of {@code TimeZoneDisplayKey} with the specified properties.
- *
- * @param timeZone the time zone
- * @param daylight adjust the style for daylight saving time if {@code true}
- * @param style the timezone style
- * @param locale the timezone locale
- */
- TimeZoneDisplayKey(final TimeZone timeZone,
- final boolean daylight, final int style, final Locale locale) {
- mTimeZone = timeZone;
- if (daylight) {
- mStyle = style | 0x80000000;
- } else {
- mStyle = style;
- }
- mLocale = locale;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode() {
- return (mStyle * 31 + mLocale.hashCode() ) * 31 + mTimeZone.hashCode();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj instanceof TimeZoneDisplayKey) {
- 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/14d0150f/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormat.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormat.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormat.java
deleted file mode 100644
index f814b62..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FixedDateFormat.java
+++ /dev/null
@@ -1,556 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.util.datetime;
-
-import org.apache.logging.log4j.core.time.Instant;
-
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.Objects;
-import java.util.TimeZone;
-import java.util.concurrent.TimeUnit;
-
-/**
- * Custom time formatter that trades flexibility for performance. This formatter only supports the date patterns defined
- * in {@link FixedFormat}. For any other date patterns use {@link FastDateFormat}.
- * <p>
- * Related benchmarks: /log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TimeFormatBenchmark.java and
- * /log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/ThreadsafeDateFormatBenchmark.java
- */
-public class FixedDateFormat {
-
- /**
- * Enumeration over the supported date/time format patterns.
- * <p>
- * Package protected for unit tests.
- */
- public enum FixedFormat {
- /**
- * ABSOLUTE time format: {@code "HH:mm:ss,SSS"}.
- */
- ABSOLUTE("HH:mm:ss,SSS", null, 0, ':', 1, ',', 1, 3),
- /**
- * ABSOLUTE time format with microsecond precision: {@code "HH:mm:ss,nnnnnn"}.
- */
- ABSOLUTE_MICROS("HH:mm:ss,nnnnnn", null, 0, ':', 1, ',', 1, 6),
- /**
- * ABSOLUTE time format with nanosecond precision: {@code "HH:mm:ss,nnnnnnnnn"}.
- */
- ABSOLUTE_NANOS("HH:mm:ss,nnnnnnnnn", null, 0, ':', 1, ',', 1, 9),
-
- /**
- * ABSOLUTE time format variation with period separator: {@code "HH:mm:ss.SSS"}.
- */
- ABSOLUTE_PERIOD("HH:mm:ss.SSS", null, 0, ':', 1, '.', 1, 3),
-
- /**
- * COMPACT time format: {@code "yyyyMMddHHmmssSSS"}.
- */
- COMPACT("yyyyMMddHHmmssSSS", "yyyyMMdd", 0, ' ', 0, ' ', 0, 3),
-
- /**
- * DATE_AND_TIME time format: {@code "dd MMM yyyy HH:mm:ss,SSS"}.
- */
- DATE("dd MMM yyyy HH:mm:ss,SSS", "dd MMM yyyy ", 0, ':', 1, ',', 1, 3),
-
- /**
- * DATE_AND_TIME time format variation with period separator: {@code "dd MMM yyyy HH:mm:ss.SSS"}.
- */
- DATE_PERIOD("dd MMM yyyy HH:mm:ss.SSS", "dd MMM yyyy ", 0, ':', 1, '.', 1, 3),
-
- /**
- * DEFAULT time format: {@code "yyyy-MM-dd HH:mm:ss,SSS"}.
- */
- DEFAULT("yyyy-MM-dd HH:mm:ss,SSS", "yyyy-MM-dd ", 0, ':', 1, ',', 1, 3),
- /**
- * DEFAULT time format with microsecond precision: {@code "yyyy-MM-dd HH:mm:ss,nnnnnn"}.
- */
- DEFAULT_MICROS("yyyy-MM-dd HH:mm:ss,nnnnnn", "yyyy-MM-dd ", 0, ':', 1, ',', 1, 6),
- /**
- * DEFAULT time format with nanosecond precision: {@code "yyyy-MM-dd HH:mm:ss,nnnnnnnnn"}.
- */
- DEFAULT_NANOS("yyyy-MM-dd HH:mm:ss,nnnnnnnnn", "yyyy-MM-dd ", 0, ':', 1, ',', 1, 9),
-
- /**
- * DEFAULT time format variation with period separator: {@code "yyyy-MM-dd HH:mm:ss.SSS"}.
- */
- DEFAULT_PERIOD("yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd ", 0, ':', 1, '.', 1, 3),
-
- /**
- * ISO8601_BASIC time format: {@code "yyyyMMdd'T'HHmmss,SSS"}.
- */
- ISO8601_BASIC("yyyyMMdd'T'HHmmss,SSS", "yyyyMMdd'T'", 2, ' ', 0, ',', 1, 3),
-
- /**
- * ISO8601_BASIC time format: {@code "yyyyMMdd'T'HHmmss.SSS"}.
- */
- ISO8601_BASIC_PERIOD("yyyyMMdd'T'HHmmss.SSS", "yyyyMMdd'T'", 2, ' ', 0, '.', 1, 3),
-
- /**
- * ISO8601 time format: {@code "yyyy-MM-dd'T'HH:mm:ss,SSS"}.
- */
- ISO8601("yyyy-MM-dd'T'HH:mm:ss,SSS", "yyyy-MM-dd'T'", 2, ':', 1, ',', 1, 3),
-
- /**
- * ISO8601 time format: {@code "yyyy-MM-dd'T'HH:mm:ss.SSS"}.
- */
- ISO8601_PERIOD("yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'", 2, ':', 1, '.', 1, 3);
-
- private static final String DEFAULT_SECOND_FRACTION_PATTERN = "SSS";
- private static final int MILLI_FRACTION_DIGITS = DEFAULT_SECOND_FRACTION_PATTERN.length();
- private static final char SECOND_FRACTION_PATTERN = 'n';
-
- private final String pattern;
- private final String datePattern;
- private final int escapeCount;
- private final char timeSeparatorChar;
- private final int timeSeparatorLength;
- private final char millisSeparatorChar;
- private final int millisSeparatorLength;
- private final int secondFractionDigits;
-
- FixedFormat(final String pattern, final String datePattern, final int escapeCount, final char timeSeparator,
- final int timeSepLength, final char millisSeparator, final int millisSepLength,
- final int secondFractionDigits) {
- this.timeSeparatorChar = timeSeparator;
- this.timeSeparatorLength = timeSepLength;
- this.millisSeparatorChar = millisSeparator;
- this.millisSeparatorLength = millisSepLength;
- this.pattern = Objects.requireNonNull(pattern);
- this.datePattern = datePattern; // may be null
- this.escapeCount = escapeCount;
- this.secondFractionDigits = secondFractionDigits;
- }
-
- /**
- * Returns the full pattern.
- *
- * @return the full pattern
- */
- public String getPattern() {
- return pattern;
- }
-
- /**
- * Returns the date part of the pattern.
- *
- * @return the date part of the pattern
- */
- public String getDatePattern() {
- return datePattern;
- }
-
- /**
- * Returns the FixedFormat with the name or pattern matching the specified string or {@code null} if not found.
- *
- * @param nameOrPattern the name or pattern to find a FixedFormat for
- * @return the FixedFormat with the name or pattern matching the specified string
- */
- public static FixedFormat lookup(final String nameOrPattern) {
- for (final FixedFormat type : FixedFormat.values()) {
- if (type.name().equals(nameOrPattern) || type.getPattern().equals(nameOrPattern)) {
- return type;
- }
- }
- return null;
- }
-
- static FixedFormat lookupIgnoringNanos(final String pattern) {
- final int nanoStart = nanoStart(pattern);
- if (nanoStart > 0) {
- final String subPattern = pattern.substring(0, nanoStart) + DEFAULT_SECOND_FRACTION_PATTERN;
- for (final FixedFormat type : FixedFormat.values()) {
- if (type.getPattern().equals(subPattern)) {
- return type;
- }
- }
- }
- return null;
- }
-
- private static int nanoStart(final String pattern) {
- final int index = pattern.indexOf(SECOND_FRACTION_PATTERN);
- if (index >= 0) {
- for (int i = index + 1; i < pattern.length(); i++) {
- if (pattern.charAt(i) != SECOND_FRACTION_PATTERN) {
- return -1;
- }
- }
- }
- return index;
- }
-
- /**
- * Returns the length of the resulting formatted date and time strings.
- *
- * @return the length of the resulting formatted date and time strings
- */
- public int getLength() {
- return pattern.length() - escapeCount;
- }
-
- /**
- * Returns the length of the date part of the resulting formatted string.
- *
- * @return the length of the date part of the resulting formatted string
- */
- public int getDatePatternLength() {
- return getDatePattern() == null ? 0 : getDatePattern().length() - escapeCount;
- }
-
- /**
- * Returns the {@code FastDateFormat} object for formatting the date part of the pattern or {@code null} if the
- * pattern does not have a date part.
- *
- * @return the {@code FastDateFormat} object for formatting the date part of the pattern or {@code null}
- */
- public FastDateFormat getFastDateFormat() {
- return getFastDateFormat(null);
- }
-
- /**
- * Returns the {@code FastDateFormat} object for formatting the date part of the pattern or {@code null} if the
- * pattern does not have a date part.
- *
- * @param tz the time zone to use
- * @return the {@code FastDateFormat} object for formatting the date part of the pattern or {@code null}
- */
- public FastDateFormat getFastDateFormat(final TimeZone tz) {
- return getDatePattern() == null ? null : FastDateFormat.getInstance(getDatePattern(), tz);
- }
-
- /**
- * Returns the number of digits specifying the fraction of the second to show
- * @return 3 for millisecond precision, 6 for microsecond precision or 9 for nanosecond precision
- */
- public int getSecondFractionDigits() {
- return secondFractionDigits;
- }
- }
-
- private final FixedFormat fixedFormat;
- private final TimeZone timeZone;
- private final int length;
- private final int secondFractionDigits;
- private final FastDateFormat fastDateFormat; // may be null
- private final char timeSeparatorChar;
- private final char millisSeparatorChar;
- private final int timeSeparatorLength;
- private final int millisSeparatorLength;
-
- private volatile long midnightToday = 0;
- private volatile long midnightTomorrow = 0;
- private final int[] dstOffsets = new int[25];
-
- // cachedDate does not need to be volatile because
- // there is a write to a volatile field *after* cachedDate is modified,
- // and there is a read from a volatile field *before* cachedDate is read.
- // The Java memory model guarantees that because of the above,
- // changes to cachedDate in one thread are visible to other threads.
- // See http://g.oswego.edu/dl/jmm/cookbook.html
- private char[] cachedDate; // may be null
- private int dateLength;
-
- /**
- * Constructs a FixedDateFormat for the specified fixed format.
- * <p>
- * Package protected for unit tests.
- *
- * @param fixedFormat the fixed format
- * @param tz time zone
- */
- FixedDateFormat(final FixedFormat fixedFormat, final TimeZone tz) {
- this(fixedFormat, tz, fixedFormat.getSecondFractionDigits());
- }
-
- /**
- * Constructs a FixedDateFormat for the specified fixed format.
- * <p>
- * Package protected for unit tests.
- *
- * @param fixedFormat the fixed format
- * @param tz time zone
- * @param secondFractionDigits the number of digits specifying the fraction of the second to show
- */
- FixedDateFormat(final FixedFormat fixedFormat, final TimeZone tz, final int secondFractionDigits) {
- this.fixedFormat = Objects.requireNonNull(fixedFormat);
- this.timeZone = Objects.requireNonNull(tz);
- this.timeSeparatorChar = fixedFormat.timeSeparatorChar;
- this.timeSeparatorLength = fixedFormat.timeSeparatorLength;
- this.millisSeparatorChar = fixedFormat.millisSeparatorChar;
- this.millisSeparatorLength = fixedFormat.millisSeparatorLength;
- this.length = fixedFormat.getLength();
- this.secondFractionDigits = Math.max(1, Math.min(9, secondFractionDigits));
- this.fastDateFormat = fixedFormat.getFastDateFormat(tz);
- }
-
- public static FixedDateFormat createIfSupported(final String... options) {
- if (options == null || options.length == 0 || options[0] == null) {
- return new FixedDateFormat(FixedFormat.DEFAULT, TimeZone.getDefault());
- }
- final TimeZone tz;
- if (options.length > 1) {
- if (options[1] != null) {
- tz = TimeZone.getTimeZone(options[1]);
- } else {
- tz = TimeZone.getDefault();
- }
- } else {
- tz = TimeZone.getDefault();
- }
-
- final FixedFormat withNanos = FixedFormat.lookupIgnoringNanos(options[0]);
- if (withNanos != null) {
- final int secondFractionDigits = options[0].length() - FixedFormat.nanoStart(options[0]);
- return new FixedDateFormat(withNanos, tz, secondFractionDigits);
- }
- final FixedFormat type = FixedFormat.lookup(options[0]);
- return type == null ? null : new FixedDateFormat(type, tz);
- }
-
- /**
- * Returns a new {@code FixedDateFormat} object for the specified {@code FixedFormat} and a {@code TimeZone.getDefault()} TimeZone.
- *
- * @param format the format to use
- * @return a new {@code FixedDateFormat} object
- */
- public static FixedDateFormat create(final FixedFormat format) {
- return new FixedDateFormat(format, TimeZone.getDefault());
- }
-
- /**
- * Returns a new {@code FixedDateFormat} object for the specified {@code FixedFormat} and TimeZone.
- *
- * @param format the format to use
- * @param tz the time zone to use
- * @return a new {@code FixedDateFormat} object
- */
- public static FixedDateFormat create(final FixedFormat format, final TimeZone tz) {
- return new FixedDateFormat(format, tz != null ? tz : TimeZone.getDefault());
- }
-
- /**
- * Returns the full pattern of the selected fixed format.
- *
- * @return the full date-time pattern
- */
- public String getFormat() {
- return fixedFormat.getPattern();
- }
-
- /**
- * Returns the time zone.
- *
- * @return the time zone
- */
-
- public TimeZone getTimeZone() {
- return timeZone;
- }
-
- /**
- * <p>Returns the number of milliseconds since midnight in the time zone that this {@code FixedDateFormat}
- * was constructed with for the specified currentTime.</p>
- * <p>As a side effect, this method updates the cached formatted date and the cached date demarcation timestamps
- * when the specified current time is outside the previously set demarcation timestamps for the start or end
- * of the current day.</p>
- * @param currentTime the current time in millis since the epoch
- * @return the number of milliseconds since midnight for the specified time
- */
- // Profiling showed this method is important to log4j performance. Modify with care!
- // 30 bytes (allows immediate JVM inlining: <= -XX:MaxInlineSize=35 bytes)
- public long millisSinceMidnight(final long currentTime) {
- if (currentTime >= midnightTomorrow || currentTime < midnightToday) {
- updateMidnightMillis(currentTime);
- }
- return currentTime - midnightToday;
- }
-
- private void updateMidnightMillis(final long now) {
- if (now >= midnightTomorrow || now < midnightToday) {
- synchronized (this) {
- updateCachedDate(now);
- midnightToday = calcMidnightMillis(now, 0);
- midnightTomorrow = calcMidnightMillis(now, 1);
-
- updateDaylightSavingTime();
- }
- }
- }
-
- private long calcMidnightMillis(final long time, final int addDays) {
- final Calendar cal = Calendar.getInstance(timeZone);
- cal.setTimeInMillis(time);
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.SECOND, 0);
- cal.set(Calendar.MILLISECOND, 0);
- cal.add(Calendar.DATE, addDays);
- return cal.getTimeInMillis();
- }
-
- private void updateDaylightSavingTime() {
- Arrays.fill(dstOffsets, 0);
- final int ONE_HOUR = (int) TimeUnit.HOURS.toMillis(1);
- if (timeZone.getOffset(midnightToday) != timeZone.getOffset(midnightToday + 23 * ONE_HOUR)) {
- for (int i = 0; i < dstOffsets.length; i++) {
- final long time = midnightToday + i * ONE_HOUR;
- dstOffsets[i] = timeZone.getOffset(time) - timeZone.getRawOffset();
- }
- if (dstOffsets[0] > dstOffsets[23]) { // clock is moved backwards.
- // we obtain midnightTonight with Calendar.getInstance(TimeZone), so it already includes raw offset
- for (int i = dstOffsets.length - 1; i >= 0; i--) {
- dstOffsets[i] -= dstOffsets[0]; //
- }
- }
- }
- }
-
- private void updateCachedDate(final long now) {
- if (fastDateFormat != null) {
- final StringBuilder result = fastDateFormat.format(now, new StringBuilder());
- cachedDate = result.toString().toCharArray();
- dateLength = result.length();
- }
- }
-
- public String formatInstant(final Instant instant) {
- final char[] result = new char[length << 1]; // double size for locales with lengthy DateFormatSymbols
- final int written = formatInstant(instant, result, 0);
- return new String(result, 0, written);
- }
-
- public int formatInstant(final Instant instant, final char[] buffer, final int startPos) {
- int result = format(instant.getEpochMillisecond(), buffer, startPos);
- result -= digitsLessThanThree();
- formatNanoOfMillisecond(instant.getNanoOfMillisecond(), buffer, startPos + result);
- return result + digitsMorePreciseThanMillis();
- }
-
- private int digitsLessThanThree() { // in case user specified only 1 or 2 'n' format characters
- return Math.max(0, FixedFormat.MILLI_FRACTION_DIGITS - secondFractionDigits);
- }
-
- private int digitsMorePreciseThanMillis() {
- return Math.max(0, secondFractionDigits - FixedFormat.MILLI_FRACTION_DIGITS);
- }
-
- // Profiling showed this method is important to log4j performance. Modify with care!
- // 28 bytes (allows immediate JVM inlining: <= -XX:MaxInlineSize=35 bytes)
- public String format(final long epochMillis) {
- final char[] result = new char[length << 1]; // double size for locales with lengthy DateFormatSymbols
- final int written = format(epochMillis, result, 0);
- return new String(result, 0, written);
- }
-
- // Profiling showed this method is important to log4j performance. Modify with care!
- // 31 bytes (allows immediate JVM inlining: <= -XX:MaxInlineSize=35 bytes)
- public int format(final long epochMillis, final char[] buffer, final int startPos) {
- // Calculate values by getting the ms values first and do then
- // calculate the hour minute and second values divisions.
-
- // Get daytime in ms: this does fit into an int
- // int ms = (int) (time % 86400000);
- final int ms = (int) (millisSinceMidnight(epochMillis));
- writeDate(buffer, startPos);
- return writeTime(ms, buffer, startPos + dateLength) - startPos;
- }
-
- // Profiling showed this method is important to log4j performance. Modify with care!
- // 22 bytes (allows immediate JVM inlining: <= -XX:MaxInlineSize=35 bytes)
- private void writeDate(final char[] buffer, final int startPos) {
- if (cachedDate != null) {
- System.arraycopy(cachedDate, 0, buffer, startPos, dateLength);
- }
- }
-
- // Profiling showed this method is important to log4j performance. Modify with care!
- // 262 bytes (will be inlined when hot enough: <= -XX:FreqInlineSize=325 bytes on Linux)
- private int writeTime(int ms, final char[] buffer, int pos) {
- final int hourOfDay = ms / 3600000;
- final int hours = hourOfDay + daylightSavingTime(hourOfDay) / 3600000;
- ms -= 3600000 * hourOfDay;
-
- final int minutes = ms / 60000;
- ms -= 60000 * minutes;
-
- final int seconds = ms / 1000;
- ms -= 1000 * seconds;
-
- // Hour
- int temp = hours / 10;
- buffer[pos++] = ((char) (temp + '0'));
-
- // Do subtract to get remainder instead of doing % 10
- buffer[pos++] = ((char) (hours - 10 * temp + '0'));
- buffer[pos] = timeSeparatorChar;
- pos += timeSeparatorLength;
-
- // Minute
- temp = minutes / 10;
- buffer[pos++] = ((char) (temp + '0'));
-
- // Do subtract to get remainder instead of doing % 10
- buffer[pos++] = ((char) (minutes - 10 * temp + '0'));
- buffer[pos] = timeSeparatorChar;
- pos += timeSeparatorLength;
-
- // Second
- temp = seconds / 10;
- buffer[pos++] = ((char) (temp + '0'));
- buffer[pos++] = ((char) (seconds - 10 * temp + '0'));
- buffer[pos] = millisSeparatorChar;
- pos += millisSeparatorLength;
-
- // Millisecond
- temp = ms / 100;
- buffer[pos++] = ((char) (temp + '0'));
-
- ms -= 100 * temp;
- temp = ms / 10;
- buffer[pos++] = ((char) (temp + '0'));
-
- ms -= 10 * temp;
- buffer[pos++] = ((char) (ms + '0'));
- return pos;
- }
-
- static int[] TABLE = {
- 100000, // 0
- 10000, // 1
- 1000, // 2
- 100, // 3
- 10, // 4
- 1, // 5
- };
-
- private void formatNanoOfMillisecond(int nanoOfMillisecond, final char[] buffer, int pos) {
- int temp;
- int remain = nanoOfMillisecond;
- for (int i = 0; i < secondFractionDigits - FixedFormat.MILLI_FRACTION_DIGITS; i++) {
- int divisor = TABLE[i];
- temp = remain / divisor;
- buffer[pos++] = ((char) (temp + '0'));
- remain -= divisor * temp; // equivalent of remain % 10
- }
- }
-
- private int daylightSavingTime(final int hourOfDay) {
- return hourOfDay > 23 ? dstOffsets[23] : dstOffsets[hourOfDay];
- }
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/14d0150f/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/Format.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/Format.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/Format.java
deleted file mode 100644
index a4a2f69..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/Format.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-package org.apache.logging.log4j.core.util.datetime;
-
-import java.text.FieldPosition;
-import java.text.ParseException;
-import java.text.ParsePosition;
-
-/**
- * The basic methods for performing date formatting.
- */
-public abstract class Format {
-
- public final String format (final Object obj) {
- return format(obj, new StringBuilder(), new FieldPosition(0)).toString();
- }
-
- public abstract StringBuilder format(Object obj, StringBuilder toAppendTo, FieldPosition pos);
-
- public abstract Object parseObject (String source, ParsePosition pos);
-
- public Object parseObject(final String source) throws ParseException {
- final ParsePosition pos = new ParsePosition(0);
- final Object result = parseObject(source, pos);
- if (pos.getIndex() == 0) {
- throw new ParseException("Format.parseObject(String) failed", pos.getErrorIndex());
- }
- return result;
- }
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/14d0150f/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
deleted file mode 100644
index e146a1d..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/datetime/FormatCache.java
+++ /dev/null
@@ -1,270 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache license, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the license for the specific language governing permissions and
- * limitations under the license.
- */
-
-package org.apache.logging.log4j.core.util.datetime;
-
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Arrays;
-import java.util.Locale;
-import java.util.TimeZone;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-
-/**
- * <p>FormatCache is a cache and factory for {@link Format}s.</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.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
- */
- 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>
- *
- * @return a date/time formatter
- */
- public F getInstance() {
- return getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, TimeZone.getDefault(), Locale.getDefault());
- }
-
- /**
- * <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>
- */
- public F getInstance(final String pattern, TimeZone timeZone, Locale locale) {
- if (pattern == null) {
- throw new NullPointerException("pattern must not be null");
- }
- if (timeZone == null) {
- timeZone = TimeZone.getDefault();
- }
- if (locale == null) {
- locale = Locale.getDefault();
- }
- final MultipartKey key = new MultipartKey(pattern, timeZone, locale);
- F format = cInstanceCache.get(key);
- if (format == null) {
- format = createInstance(pattern, timeZone, locale);
- 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;
- }
- }
- 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.
- * @return a pattern based date/time formatter
- * @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
- * @return a localized standard date/time formatter
- * @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) {
- if (locale == null) {
- locale = Locale.getDefault();
- }
- final String pattern = getPatternForStyle(dateStyle, timeStyle, locale);
- return getInstance(pattern, timeZone, 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
- */
- // 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) {
- return getDateTimeInstance(Integer.valueOf(dateStyle), Integer.valueOf(timeStyle), timeZone, 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
- */
- // package protected, for access from FastDateFormat; do not make public or protected
- F getDateInstance(final int dateStyle, final TimeZone timeZone, final Locale locale) {
- return getDateTimeInstance(Integer.valueOf(dateStyle), null, timeZone, 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
- */
- // package protected, for access from FastDateFormat; do not make public or protected
- F getTimeInstance(final int timeStyle, final TimeZone timeZone, final Locale locale) {
- return getDateTimeInstance(null, Integer.valueOf(timeStyle), timeZone, locale);
- }
-
- /**
- * <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
- */
- // package protected, for access from test code; do not make public or protected
- static String getPatternForStyle(final Integer dateStyle, final Integer timeStyle, final Locale locale) {
- final MultipartKey key = new MultipartKey(dateStyle, timeStyle, locale);
-
- 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.getDateTimeInstance(dateStyle.intValue(), timeStyle.intValue(), locale);
- }
- 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;
- }
- } catch (final ClassCastException ex) {
- throw new IllegalArgumentException("No date time pattern for locale: " + locale);
- }
- }
- return pattern;
- }
-
- // ----------------------------------------------------------------------
- /**
- * <p>Helper class to hold multi-part Map keys</p>
- */
- private static class MultipartKey {
- private final Object[] keys;
- private int hashCode;
-
- /**
- * 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.
- */
- public MultipartKey(final Object... keys) {
- this.keys = keys;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean equals(final Object obj) {
- // 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 Arrays.equals(keys, ((MultipartKey)obj).keys);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode() {
- if(hashCode==0) {
- int rc= 0;
- for(final Object key : keys) {
- if(key!=null) {
- rc= rc*7 + key.hashCode();
- }
- }
- hashCode= rc;
- }
- return hashCode;
- }
- }
-
-}