1/*********************************************************************
2 * Copyright (C) 2000-2014, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 *********************************************************************
5 */
6package com.ibm.icu.text;
7
8import java.io.InvalidObjectException;
9import java.text.FieldPosition;
10import java.util.Locale;
11
12import com.ibm.icu.util.Calendar;
13import com.ibm.icu.util.ChineseCalendar;
14import com.ibm.icu.util.TimeZone;
15import com.ibm.icu.util.ULocale;
16
17/**
18 * A concrete {@link DateFormat} for {@link com.ibm.icu.util.ChineseCalendar}.
19 * This class handles a <code>ChineseCalendar</code>-specific field,
20 * <code>ChineseCalendar.IS_LEAP_MONTH</code>.  It also redefines the
21 * handling of two fields, <code>ERA</code> and <code>YEAR</code>.  The
22 * former is displayed numerically, instead of symbolically, since it is
23 * the numeric cycle number in <code>ChineseCalendar</code>.  The latter is
24 * numeric, as before, but has no special 2-digit Y2K behavior.
25 *
26 * <p>With regard to <code>ChineseCalendar.IS_LEAP_MONTH</code>, this
27 * class handles parsing specially.  If no string symbol is found at all,
28 * this is taken as equivalent to an <code>IS_LEAP_MONTH</code> value of
29 * zero.  This allows formats to display a special string (e.g., "*") for
30 * leap months, but no string for normal months.
31 *
32 * <p>Summary of field changes vs. {@link SimpleDateFormat}:<pre>
33 * Symbol   Meaning                 Presentation        Example
34 * ------   -------                 ------------        -------
35 * G        cycle                   (Number)            78
36 * y        year of cycle (1..60)   (Number)            17
37 * l        is leap month           (Text)              4637
38 * </pre>
39 *
40 * @see com.ibm.icu.util.ChineseCalendar
41 * @see ChineseDateFormatSymbols
42 * @author Alan Liu
43 * @deprecated ICU 50 Use SimpleDateFormat instead.
44 */
45@Deprecated
46public class ChineseDateFormat extends SimpleDateFormat {
47    // Generated by serialver from JDK 1.4.1_01
48    static final long serialVersionUID = -4610300753104099899L;
49
50    // TODO Finish the constructors
51
52    /**
53     * Construct a ChineseDateFormat from a date format pattern and locale
54     * @param pattern the pattern
55     * @param locale the locale
56     * @deprecated ICU 50
57     */
58    @Deprecated
59   public ChineseDateFormat(String pattern, Locale locale) {
60       this(pattern, ULocale.forLocale(locale));
61    }
62
63    /**
64     * Construct a ChineseDateFormat from a date format pattern and locale
65     * @param pattern the pattern
66     * @param locale the locale
67     * @deprecated ICU 50
68     */
69    @Deprecated
70   public ChineseDateFormat(String pattern, ULocale locale) {
71       this(pattern, null, locale);
72    }
73
74    /**
75     * Construct a ChineseDateFormat from a date format pattern, numbering system override and locale
76     * @param pattern the pattern
77     * @param override The override string.  A numbering system override string can take one of the following forms:
78     *     1). If just a numbering system name is specified, it applies to all numeric fields in the date format pattern.
79     *     2). To specify an alternate numbering system on a field by field basis, use the field letters from the pattern
80     *         followed by an = sign, followed by the numbering system name.  For example, to specify that just the year
81     *         be formatted using Hebrew digits, use the override "y=hebr".  Multiple overrides can be specified in a single
82     *         string by separating them with a semi-colon. For example, the override string "m=thai;y=deva" would format using
83     *         Thai digits for the month and Devanagari digits for the year.
84     * @param locale the locale
85     * @deprecated ICU 50
86     */
87    @Deprecated
88    public ChineseDateFormat(String pattern, String override, ULocale locale) {
89       super(pattern, new ChineseDateFormatSymbols(locale),
90               new ChineseCalendar(TimeZone.getDefault(), locale), locale, true, override);
91    }
92
93// NOTE: This API still exists; we just inherit it from SimpleDateFormat
94// as of ICU 3.0
95//  /**
96//   * @stable ICU 2.0
97//   */
98//  protected String subFormat(char ch, int count, int beginOffset,
99//                             FieldPosition pos, DateFormatSymbols formatData,
100//                             Calendar cal)  {
101//      switch (ch) {
102//      case 'G': // 'G' - ERA
103//          return zeroPaddingNumber(cal.get(Calendar.ERA), 1, 9);
104//      case 'l': // 'l' - IS_LEAP_MONTH
105//          {
106//              ChineseDateFormatSymbols symbols =
107//                  (ChineseDateFormatSymbols) formatData;
108//              return symbols.getLeapMonth(cal.get(
109//                             ChineseCalendar.IS_LEAP_MONTH));
110//          }
111//      default:
112//          return super.subFormat(ch, count, beginOffset, pos, formatData, cal);
113//      }
114//  }
115
116    /**
117     * {@inheritDoc}
118     * @internal
119     * @deprecated This API is ICU internal only.
120     */
121    @Deprecated
122    protected void subFormat(StringBuffer buf,
123                             char ch, int count, int beginOffset,
124                             int fieldNum, DisplayContext capitalizationContext,
125                             FieldPosition pos,
126                             Calendar cal) {
127
128        // Logic to handle 'G' for chinese calendar is moved into SimpleDateFormat,
129        // and obsolete pattern char 'l' is now ignored in SimpleDateFormat, so we
130        // just use its implementation
131        super.subFormat(buf, ch, count, beginOffset, fieldNum, capitalizationContext, pos, cal);
132
133        // The following is no longer an issue for this subclass...
134        // TODO: add code to set FieldPosition for 'G' and 'l' fields. This
135        // is a DESIGN FLAW -- subclasses shouldn't have to duplicate the
136        // code that handles this at the end of SimpleDateFormat.subFormat.
137        // The logic should be moved up into SimpleDateFormat.format.
138    }
139
140    /**
141     * {@inheritDoc}
142     *
143     * @deprecated ICU 50
144     */
145    @Deprecated
146    protected int subParse(String text, int start, char ch, int count, boolean obeyCount, boolean allowNegative,
147            boolean[] ambiguousYear, Calendar cal) {
148        // Logic to handle numeric 'G' eras for chinese calendar, and to skip special 2-digit year
149        // handling for chinese calendar, is moved into SimpleDateFormat, so delete here.
150        // Obsolete pattern char 'l' is now ignored for parsing in SimpleDateFormat, no handling
151        // needed here.
152        // So just use SimpleDateFormat implementation for this.
153        // just use its implementation
154        return super.subParse(text, start, ch, count, obeyCount, allowNegative, ambiguousYear, cal);
155    }
156
157    /**
158     * {@inheritDoc}
159     *
160     * @deprecated ICU 50
161     */
162    @Deprecated
163    protected DateFormat.Field patternCharToDateFormatField(char ch) {
164        // no longer any field corresponding to pattern char 'l'
165        return super.patternCharToDateFormatField(ch);
166    }
167
168    /**
169     * The instances of this inner class are used as attribute keys and values
170     * in AttributedCharacterIterator that
171     * ChineseDateFormat.formatToCharacterIterator() method returns.
172     * <p>
173     * There is no public constructor to this class, the only instances are the
174     * constants defined here.
175     * <p>
176     * @deprecated ICU 50
177     */
178    @Deprecated
179    public static class Field extends DateFormat.Field {
180
181        private static final long serialVersionUID = -5102130532751400330L;
182
183        /**
184         * Constant identifying the leap month marker.
185         * @deprecated ICU 50 This field is only used by the deprecated ChineseDateFormat class.
186         */
187        @Deprecated
188        public static final Field IS_LEAP_MONTH = new Field("is leap month", ChineseCalendar.IS_LEAP_MONTH);
189
190        /**
191         * Constructs a <code>ChineseDateFormat.Field</code> with the given name and
192         * the <code>ChineseCalendar</code> field which this attribute represents.
193         * Use -1 for <code>calendarField</code> if this field does not have a
194         * corresponding <code>ChineseCalendar</code> field.
195         *
196         * @param name          Name of the attribute
197         * @param calendarField <code>Calendar</code> field constant
198         *
199         * @deprecated ICU 50
200         */
201        @Deprecated
202        protected Field(String name, int calendarField) {
203            super(name, calendarField);
204        }
205
206        /**
207         * Returns the <code>Field</code> constant that corresponds to the <code>
208         * ChineseCalendar</code> field <code>calendarField</code>.  If there is no
209         * corresponding <code>Field</code> is available, null is returned.
210         *
211         * @param calendarField <code>ChineseCalendar</code> field constant
212         * @return <code>Field</code> associated with the <code>calendarField</code>,
213         * or null if no associated <code>Field</code> is available.
214         * @throws IllegalArgumentException if <code>calendarField</code> is not
215         * a valid <code>Calendar</code> field constant.
216         *
217         * @deprecated ICU 50
218         */
219        @Deprecated
220        public static DateFormat.Field ofCalendarField(int calendarField) {
221            // Should we remove the following, since there is no longer a specific
222            // date format field for leap month (since 'l' pattern char is obsolete)?
223            if (calendarField == ChineseCalendar.IS_LEAP_MONTH) {
224                return IS_LEAP_MONTH;
225            }
226            return DateFormat.Field.ofCalendarField(calendarField);
227        }
228
229        /**
230         * {@inheritDoc}
231         *
232         * @deprecated ICU 50
233         */
234        @Deprecated
235        ///CLOVER:OFF
236        protected Object readResolve() throws InvalidObjectException {
237            if (this.getClass() != ChineseDateFormat.Field.class) {
238                throw new InvalidObjectException("A subclass of ChineseDateFormat.Field must implement readResolve.");
239            }
240            if (this.getName().equals(IS_LEAP_MONTH.getName())) {
241                return IS_LEAP_MONTH;
242            } else {
243                throw new InvalidObjectException("Unknown attribute name.");
244            }
245        }
246        ///CLOVER:ON
247    }
248}
249