1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.spi;
  27 
  28 import java.util.Calendar;
  29 import java.util.Locale;
  30 import java.util.Map;
  31 
  32 /**
  33  * An abstract class for service providers that provide localized string
  34  * representations (display names) of {@code Calendar} field values.
  35  *
  36  * <p><a name="calendartypes"><b>Calendar Types</b></a>
  37  *
  38  * <p>Calendar types are used to specify calendar systems for which the {@link
  39  * #getDisplayName(String, int, int, int, Locale) getDisplayName} and {@link
  40  * #getDisplayNames(String, int, int, Locale) getDisplayNames} methods provide
  41  * calendar field value names. See {@link Calendar#getCalendarType()} for details.
  42  *
  43  * <p><b>Calendar Fields</b>
  44  *
  45  * <p>Calendar fields are specified with the constants defined in {@link
  46  * Calendar}. The following are calendar-common fields and their values to be
  47  * supported for each calendar system.
  48  *
  49  * <table style="border-bottom:1px solid" border="1" cellpadding="3" cellspacing="0" summary="Field values">
  50  *   <tr>
  51  *     <th>Field</th>
  52  *     <th>Value</th>
  53  *     <th>Description</th>
  54  *   </tr>
  55  *   <tr>
  56  *     <td valign="top">{@link Calendar#MONTH}</td>
  57  *     <td valign="top">{@link Calendar#JANUARY} to {@link Calendar#UNDECIMBER}</td>
  58  *     <td>Month numbering is 0-based (e.g., 0 - January, ..., 11 -
  59  *         December). Some calendar systems have 13 months. Month
  60  *         names need to be supported in both the formatting and
  61  *         stand-alone forms if required by the supported locales. If there's
  62  *         no distinction in the two forms, the same names should be returned
  63  *         in both of the forms.</td>
  64  *   </tr>
  65  *   <tr>
  66  *     <td valign="top">{@link Calendar#DAY_OF_WEEK}</td>
  67  *     <td valign="top">{@link Calendar#SUNDAY} to {@link Calendar#SATURDAY}</td>
  68  *     <td>Day-of-week numbering is 1-based starting from Sunday (i.e., 1 - Sunday,
  69  *         ..., 7 - Saturday).</td>
  70  *   </tr>
  71  *   <tr>
  72  *     <td valign="top">{@link Calendar#AM_PM}</td>
  73  *     <td valign="top">{@link Calendar#AM} to {@link Calendar#PM}</td>
  74  *     <td>0 - AM, 1 - PM</td>
  75  *   </tr>
  76  * </table>
  77  *
  78  * <p style="margin-top:20px">The following are calendar-specific fields and their values to be supported.
  79  *
  80  * <table style="border-bottom:1px solid" border="1" cellpadding="3" cellspacing="0" summary="Calendar type and field values">
  81  *   <tr>
  82  *     <th>Calendar Type</th>
  83  *     <th>Field</th>
  84  *     <th>Value</th>
  85  *     <th>Description</th>
  86  *   </tr>
  87  *   <tr>
  88  *     <td rowspan="2" valign="top">{@code "gregory"}</td>
  89  *     <td rowspan="2" valign="top">{@link Calendar#ERA}</td>
  90  *     <td>0</td>
  91  *     <td>{@link java.util.GregorianCalendar#BC} (BCE)</td>
  92  *   </tr>
  93  *   <tr>
  94  *     <td>1</td>
  95  *     <td>{@link java.util.GregorianCalendar#AD} (CE)</td>
  96  *   </tr>
  97  *   <tr>
  98  *     <td rowspan="2" valign="top">{@code "buddhist"}</td>
  99  *     <td rowspan="2" valign="top">{@link Calendar#ERA}</td>
 100  *     <td>0</td>
 101  *     <td>BC (BCE)</td>
 102  *   </tr>
 103  *   <tr>
 104  *     <td>1</td>
 105  *     <td>B.E. (Buddhist Era)</td>
 106  *   </tr>
 107  *   <tr>
 108  *     <td rowspan="6" valign="top">{@code "japanese"}</td>
 109  *     <td rowspan="5" valign="top">{@link Calendar#ERA}</td>
 110  *     <td>0</td>
 111  *     <td>Seireki (Before Meiji)</td>
 112  *   </tr>
 113  *   <tr>
 114  *     <td>1</td>
 115  *     <td>Meiji</td>
 116  *   </tr>
 117  *   <tr>
 118  *     <td>2</td>
 119  *     <td>Taisho</td>
 120  *   </tr>
 121  *   <tr>
 122  *     <td>3</td>
 123  *     <td>Showa</td>
 124  *   </tr>
 125  *   <tr>
 126  *     <td>4</td>
 127  *     <td >Heisei</td>
 128  *   </tr>
 129  *   <tr>
 130  *     <td>{@link Calendar#YEAR}</td>
 131  *     <td>1</td>
 132  *     <td>the first year in each era. It should be returned when a long
 133  *     style ({@link Calendar#LONG_FORMAT} or {@link Calendar#LONG_STANDALONE}) is
 134  *     specified. See also the <a href="../../text/SimpleDateFormat.html#year">
 135  *     Year representation in {@code SimpleDateFormat}</a>.</td>
 136  *   </tr>
 137  * </table>
 138  *
 139  * <p>Calendar field value names for {@code "gregory"} must be consistent with
 140  * the date-time symbols provided by {@link java.text.spi.DateFormatSymbolsProvider}.
 141  *
 142  * <p>Time zone names are supported by {@link TimeZoneNameProvider}.
 143  *
 144  * @author Masayoshi Okutsu
 145  * @since 1.8
 146  * @see CalendarDataProvider
 147  * @see Locale#getUnicodeLocaleType(String)
 148  */
 149 public abstract class CalendarNameProvider extends LocaleServiceProvider {
 150     /**
 151      * Sole constructor. (For invocation by subclass constructors, typically
 152      * implicit.)
 153      */
 154     protected CalendarNameProvider() {
 155     }
 156 
 157     /**
 158      * Returns the string representation (display name) of the calendar
 159      * <code>field value</code> in the given <code>style</code> and
 160      * <code>locale</code>.  If no string representation is
 161      * applicable, <code>null</code> is returned.
 162      *
 163      * <p>{@code field} is a {@code Calendar} field index, such as {@link
 164      * Calendar#MONTH}. The time zone fields, {@link Calendar#ZONE_OFFSET} and
 165      * {@link Calendar#DST_OFFSET}, are <em>not</em> supported by this
 166      * method. {@code null} must be returned if any time zone fields are
 167      * specified.
 168      *
 169      * <p>{@code value} is the numeric representation of the {@code field} value.
 170      * For example, if {@code field} is {@link Calendar#DAY_OF_WEEK}, the valid
 171      * values are {@link Calendar#SUNDAY} to {@link Calendar#SATURDAY}
 172      * (inclusive).
 173      *
 174      * <p>{@code style} gives the style of the string representation. It is one
 175      * of {@link Calendar#SHORT_FORMAT} ({@link Calendar#SHORT SHORT}),
 176      * {@link Calendar#SHORT_STANDALONE}, {@link Calendar#LONG_FORMAT}
 177      * ({@link Calendar#LONG LONG}), {@link Calendar#LONG_STANDALONE},
 178      * {@link Calendar#NARROW_FORMAT}, or {@link Calendar#NARROW_STANDALONE}.
 179      *
 180      * <p>For example, the following call will return {@code "Sunday"}.
 181      * <pre>
 182      * getDisplayName("gregory", Calendar.DAY_OF_WEEK, Calendar.SUNDAY,
 183      *                Calendar.LONG_STANDALONE, Locale.ENGLISH);
 184      * </pre>
 185      *
 186      * @param calendarType
 187      *              the calendar type. (Any calendar type given by {@code locale}
 188      *              is ignored.)
 189      * @param field
 190      *              the {@code Calendar} field index,
 191      *              such as {@link Calendar#DAY_OF_WEEK}
 192      * @param value
 193      *              the value of the {@code Calendar field},
 194      *              such as {@link Calendar#MONDAY}
 195      * @param style
 196      *              the string representation style: one of {@link
 197      *              Calendar#SHORT_FORMAT} ({@link Calendar#SHORT SHORT}),
 198      *              {@link Calendar#SHORT_STANDALONE}, {@link
 199      *              Calendar#LONG_FORMAT} ({@link Calendar#LONG LONG}),
 200      *              {@link Calendar#LONG_STANDALONE},
 201      *              {@link Calendar#NARROW_FORMAT},
 202      *              or {@link Calendar#NARROW_STANDALONE}
 203      * @param locale
 204      *              the desired locale
 205      * @return the string representation of the {@code field value}, or {@code
 206      *         null} if the string representation is not applicable or
 207      *         the given calendar type is unknown
 208      * @throws IllegalArgumentException
 209      *         if {@code field} or {@code style} is invalid
 210      * @throws NullPointerException if {@code locale} is {@code null}
 211      * @see TimeZoneNameProvider
 212      * @see java.util.Calendar#get(int)
 213      * @see java.util.Calendar#getDisplayName(int, int, Locale)
 214      */
 215     public abstract String getDisplayName(String calendarType,
 216                                           int field, int value,
 217                                           int style, Locale locale);
 218 
 219     /**
 220      * Returns a {@code Map} containing all string representations (display
 221      * names) of the {@code Calendar} {@code field} in the given {@code style}
 222      * and {@code locale} and their corresponding field values.
 223      *
 224      * <p>{@code field} is a {@code Calendar} field index, such as {@link
 225      * Calendar#MONTH}. The time zone fields, {@link Calendar#ZONE_OFFSET} and
 226      * {@link Calendar#DST_OFFSET}, are <em>not</em> supported by this
 227      * method. {@code null} must be returned if any time zone fields are specified.
 228      *
 229      * <p>{@code style} gives the style of the string representation. It must be
 230      * one of {@link Calendar#ALL_STYLES}, {@link Calendar#SHORT_FORMAT} ({@link
 231      * Calendar#SHORT SHORT}), {@link Calendar#SHORT_STANDALONE}, {@link
 232      * Calendar#LONG_FORMAT} ({@link Calendar#LONG LONG}), {@link
 233      * Calendar#LONG_STANDALONE}, {@link Calendar#NARROW_FORMAT}, or
 234      * {@link Calendar#NARROW_STANDALONE}. Note that narrow names may
 235      * not be unique due to use of single characters, such as "S" for Sunday
 236      * and Saturday, and that no narrow names are included in that case.
 237      *
 238      * <p>For example, the following call will return a {@code Map} containing
 239      * {@code "January"} to {@link Calendar#JANUARY}, {@code "Jan"} to {@link
 240      * Calendar#JANUARY}, {@code "February"} to {@link Calendar#FEBRUARY},
 241      * {@code "Feb"} to {@link Calendar#FEBRUARY}, and so on.
 242      * <pre>
 243      * getDisplayNames("gregory", Calendar.MONTH, Calendar.ALL_STYLES, Locale.ENGLISH);
 244      * </pre>
 245      *
 246      * @param calendarType
 247      *              the calendar type. (Any calendar type given by {@code locale}
 248      *              is ignored.)
 249      * @param field
 250      *              the calendar field for which the display names are returned
 251      * @param style
 252      *              the style applied to the display names; one of
 253      *              {@link Calendar#ALL_STYLES}, {@link Calendar#SHORT_FORMAT}
 254      *              ({@link Calendar#SHORT SHORT}), {@link
 255      *              Calendar#SHORT_STANDALONE}, {@link Calendar#LONG_FORMAT}
 256      *              ({@link Calendar#LONG LONG}), {@link Calendar#LONG_STANDALONE},
 257      *              {@link Calendar#NARROW_FORMAT},
 258      *              or {@link Calendar#NARROW_STANDALONE}
 259      * @param locale
 260      *              the desired locale
 261      * @return a {@code Map} containing all display names of {@code field} in
 262      *         {@code style} and {@code locale} and their {@code field} values,
 263      *         or {@code null} if no display names are defined for {@code field}
 264      * @throws NullPointerException
 265      *         if {@code locale} is {@code null}
 266      * @see Calendar#getDisplayNames(int, int, Locale)
 267      */
 268     public abstract Map<String, Integer> getDisplayNames(String calendarType,
 269                                                          int field, int style,
 270                                                          Locale locale);
 271 }