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}), or {@link Calendar#LONG_STANDALONE}. 178 * 179 * <p>For example, the following call will return {@code "Sunday"}. 180 * <pre> 181 * getDisplayName("gregory", Calendar.DAY_OF_WEEK, Calendar.SUNDAY, 182 * Calendar.LONG_STANDALONE, Locale.ENGLISH); 183 * </pre> 184 * 185 * @param calendarType 186 * the calendar type. (Any calendar type given by {@code locale} 187 * is ignored.) 188 * @param field 189 * the {@code Calendar} field index, 190 * such as {@link Calendar#DAY_OF_WEEK} 191 * @param value 192 * the value of the {@code Calendar field}, 193 * such as {@link Calendar#MONDAY} 194 * @param style 195 * the string representation style: one of {@link 196 * Calendar#SHORT_FORMAT} ({@link Calendar#SHORT SHORT}), 197 * {@link Calendar#SHORT_STANDALONE}, {@link 198 * Calendar#LONG_FORMAT} ({@link Calendar#LONG LONG}), or 199 * {@link Calendar#LONG_STANDALONE} 200 * @param locale 201 * the desired locale 202 * @return the string representation of the {@code field value}, or {@code 203 * null} if the string representation is not applicable or 204 * the given calendar type is unknown 205 * @throws IllegalArgumentException 206 * if {@code field} or {@code style} is invalid 207 * @throws NullPointerException if {@code locale} is {@code null} 208 * @see TimeZoneNameProvider 209 * @see java.util.Calendar#get(int) 210 * @see java.util.Calendar#getDisplayName(int, int, Locale) 211 */ 212 public abstract String getDisplayName(String calendarType, 213 int field, int value, 214 int style, Locale locale); 215 216 /** 217 * Returns a {@code Map} containing all string representations (display 218 * names) of the {@code Calendar} {@code field} in the given {@code style} 219 * and {@code locale} and their corresponding field values. 220 * 221 * <p>{@code field} is a {@code Calendar} field index, such as {@link 222 * Calendar#MONTH}. The time zone fields, {@link Calendar#ZONE_OFFSET} and 223 * {@link Calendar#DST_OFFSET}, are <em>not</em> supported by this 224 * method. {@code null} must be returned if any time zone fields are specified. 225 * 226 * <p>{@code style} gives the style of the string representation. It must be 227 * one of {@link Calendar#ALL_STYLES}, {@link Calendar#SHORT_FORMAT} ({@link 228 * Calendar#SHORT SHORT}), {@link Calendar#SHORT_STANDALONE}, {@link 229 * Calendar#LONG_FORMAT} ({@link Calendar#LONG LONG}), or {@link 230 * Calendar#LONG_STANDALONE}. 231 * 232 * <p>For example, the following call will return a {@code Map} containing 233 * {@code "January"} to {@link Calendar#JANUARY}, {@code "Jan"} to {@link 234 * Calendar#JANUARY}, {@code "February"} to {@link Calendar#FEBRUARY}, 235 * {@code "Feb"} to {@link Calendar#FEBRUARY}, and so on. 236 * <pre> 237 * getDisplayNames("gregory", Calendar.MONTH, Calendar.ALL_STYLES, Locale.ENGLISH); 238 * </pre> 239 * 240 * @param calendarType 241 * the calendar type. (Any calendar type given by {@code locale} 242 * is ignored.) 243 * @param field 244 * the calendar field for which the display names are returned 245 * @param style 246 * the style applied to the display names; one of 247 * {@link Calendar#ALL_STYLES}, {@link Calendar#SHORT_FORMAT} 248 * ({@link Calendar#SHORT SHORT}), {@link 249 * Calendar#SHORT_STANDALONE}, {@link Calendar#LONG_FORMAT} 250 * ({@link Calendar#LONG LONG}), or {@link 251 * Calendar#LONG_STANDALONE}. 252 * @param locale 253 * the desired locale 254 * @return a {@code Map} containing all display names of {@code field} in 255 * {@code style} and {@code locale} and their {@code field} values, 256 * or {@code null} if no display names are defined for {@code field} 257 * @throws NullPointerException 258 * if {@code locale} is {@code null} 259 * @see Calendar#getDisplayNames(int, int, Locale) 260 */ 261 public abstract Map<String, Integer> getDisplayNames(String calendarType, 262 int field, int style, 263 Locale locale); 264 }