1 /*
2  * Copyright (c) 2007, 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.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 /*
24  *
25  */
26 
27 package com.foo;
28 
29 import java.text.*;
30 import java.text.spi.*;
31 import java.util.*;
32 
33 import com.foobar.Utils;
34 
35 public class DateFormatSymbolsProviderImpl extends DateFormatSymbolsProvider {
36 
37     static Locale[] avail = {
38         new Locale("ja", "JP", "osaka"),
39         new Locale("ja", "JP", "kyoto"),
40         Locale.JAPAN,
41         new Locale("yy", "ZZ")
42     };
43     static List<Locale> availList = Arrays.asList(avail);
44 
45     static String[] dialect = {
46         "\u3084\u3002",
47         "\u3069\u3059\u3002",
48         "\u3067\u3059\u3002",
49         "-yy-ZZ"
50     };
51 
52     static Map<Locale, FooDateFormatSymbols> symbols = new HashMap<Locale, FooDateFormatSymbols>(4);
53 
getAvailableLocales()54     public Locale[] getAvailableLocales() {
55         return avail;
56     }
57 
getInstance(Locale locale)58     public DateFormatSymbols getInstance(Locale locale) {
59         if (!Utils.supportsLocale(availList, locale)) {
60             throw new IllegalArgumentException("locale is not supported: "+locale);
61         }
62 
63         FooDateFormatSymbols fdfs = symbols.get(locale);
64         if (fdfs == null) {
65             for (int index = 0; index < avail.length; index ++) {
66                 if (Utils.supportsLocale(avail[index], locale)) {
67                     fdfs = new FooDateFormatSymbols(index);
68                     symbols.put(locale, fdfs);
69                     break;
70                 }
71             }
72         }
73         return fdfs;
74     }
75 
76     class FooDateFormatSymbols extends DateFormatSymbols {
77         String dialect = "";
78 
79         String[] eras = null;
80         String[] months = null;
81         String[] shortMonths = null;
82         String[] weekdays = null;
83         String[] shortWeekdays = null;
84         String[] ampms = null;
85 
FooDateFormatSymbols(int index)86         public FooDateFormatSymbols(int index) {
87             super(DateFormatSymbolsProviderImpl.this.avail[index]);
88             dialect = DateFormatSymbolsProviderImpl.this.dialect[index];
89         }
90 
getEras()91         public String[] getEras() {
92             if (eras == null) {
93                 eras = super.getEras();
94                 for (int i = 0; i < eras.length; i++) {
95                     eras[i] = eras[i]+dialect;
96                 }
97             }
98             return eras;
99         }
100 
101         /**
102          * Sets era strings. For example: "AD" and "BC".
103          * @param newEras the new era strings.
104          */
setEras(String[] newEras)105         public void setEras(String[] newEras) {
106             eras = newEras;
107         }
108 
109         /**
110          * Gets month strings. For example: "January", "February", etc.
111          * @return the month strings.
112          */
getMonths()113         public String[] getMonths() {
114             if (months == null) {
115                 months = super.getMonths();
116                 for (int i = 0; i < months.length; i++) {
117                     months[i] = months[i]+dialect;
118                 }
119             }
120             return months;
121         }
122 
123         /**
124          * Sets month strings. For example: "January", "February", etc.
125          * @param newMonths the new month strings.
126          */
setMonths(String[] newMonths)127         public void setMonths(String[] newMonths) {
128             months = newMonths;
129         }
130 
131         /**
132          * Gets short month strings. For example: "Jan", "Feb", etc.
133          * @return the short month strings.
134          */
getShortMonths()135         public String[] getShortMonths() {
136             if (shortMonths == null) {
137                 shortMonths = super.getShortMonths();
138                 for (int i = 0; i < shortMonths.length; i++) {
139                     shortMonths[i] = shortMonths[i]+dialect;
140                 }
141             }
142             return shortMonths;
143         }
144 
145         /**
146          * Sets short month strings. For example: "Jan", "Feb", etc.
147          * @param newShortMonths the new short month strings.
148          */
setShortMonths(String[] newShortMonths)149         public void setShortMonths(String[] newShortMonths) {
150             shortMonths = newShortMonths;
151         }
152 
153         /**
154          * Gets weekday strings. For example: "Sunday", "Monday", etc.
155          * @return the weekday strings. Use <code>Calendar.SUNDAY</code>,
156          * <code>Calendar.MONDAY</code>, etc. to index the result array.
157          */
getWeekdays()158         public String[] getWeekdays() {
159             if (weekdays == null) {
160                 weekdays = super.getWeekdays();
161                 for (int i = 0; i < weekdays.length; i++) {
162                     weekdays[i] = weekdays[i]+dialect;
163                 }
164             }
165             return weekdays;
166         }
167 
168         /**
169          * Sets weekday strings. For example: "Sunday", "Monday", etc.
170          * @param newWeekdays the new weekday strings. The array should
171          * be indexed by <code>Calendar.SUNDAY</code>,
172          * <code>Calendar.MONDAY</code>, etc.
173          */
setWeekdays(String[] newWeekdays)174         public void setWeekdays(String[] newWeekdays) {
175             weekdays = newWeekdays;
176         }
177 
178         /**
179          * Gets short weekday strings. For example: "Sun", "Mon", etc.
180          * @return the short weekday strings. Use <code>Calendar.SUNDAY</code>,
181          * <code>Calendar.MONDAY</code>, etc. to index the result array.
182          */
getShortWeekdays()183         public String[] getShortWeekdays() {
184             if (shortWeekdays == null) {
185                 shortWeekdays = super.getShortWeekdays();
186                 for (int i = 0; i < shortWeekdays.length; i++) {
187                     shortWeekdays[i] = shortWeekdays[i]+dialect;
188                 }
189             }
190             return shortWeekdays;
191         }
192 
193         /**
194          * Sets short weekday strings. For example: "Sun", "Mon", etc.
195          * @param newShortWeekdays the new short weekday strings. The array should
196          * be indexed by <code>Calendar.SUNDAY</code>,
197          * <code>Calendar.MONDAY</code>, etc.
198          */
setShortWeekdays(String[] newShortWeekdays)199         public void setShortWeekdays(String[] newShortWeekdays) {
200             shortWeekdays = newShortWeekdays;
201         }
202 
203         /**
204          * Gets ampm strings. For example: "AM" and "PM".
205          * @return the ampm strings.
206          */
getAmPmStrings()207         public String[] getAmPmStrings() {
208             if (ampms == null) {
209                 ampms = super.getAmPmStrings();
210                 for (int i = 0; i < ampms.length; i++) {
211                     ampms[i] = ampms[i]+dialect;
212                 }
213             }
214             return ampms;
215         }
216 
217         /**
218          * Sets ampm strings. For example: "AM" and "PM".
219          * @param newAmpms the new ampm strings.
220          */
setAmPmStrings(String[] newAmpms)221         public void setAmPmStrings(String[] newAmpms) {
222             ampms = newAmpms;
223         }
224 
225         @Override
getZoneStrings()226         public String[][] getZoneStrings() {
227             return new String[0][0];
228         }
229     }
230 }
231