KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > chrono > GJLocaleSymbols


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time.chrono;
17
18 import java.lang.ref.WeakReference JavaDoc;
19 import java.text.DateFormatSymbols JavaDoc;
20 import java.util.WeakHashMap JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.TreeMap JavaDoc;
23
24 import org.joda.time.DateTimeFieldType;
25 import org.joda.time.IllegalFieldValueException;
26
27 /**
28  * Utility class used by a few of the GJDateTimeFields.
29  *
30  * @author Brian S O'Neill
31  * @since 1.0
32  */

33 class GJLocaleSymbols {
34     private static final int FAST_CACHE_SIZE = 64;
35
36     private static final GJLocaleSymbols[] cFastCache = new GJLocaleSymbols[FAST_CACHE_SIZE];
37
38     private static WeakHashMap JavaDoc cCache = new WeakHashMap JavaDoc();
39
40     public static GJLocaleSymbols forLocale(Locale JavaDoc locale) {
41         int index = System.identityHashCode(locale) & (FAST_CACHE_SIZE - 1);
42         GJLocaleSymbols symbols = cFastCache[index];
43         if (symbols != null && symbols.iLocale.get() == locale) {
44             return symbols;
45         }
46         synchronized (cCache) {
47             symbols = (GJLocaleSymbols) cCache.get(locale);
48             if (symbols == null) {
49                 symbols = new GJLocaleSymbols(locale);
50                 cCache.put(locale, symbols);
51             }
52         }
53         cFastCache[index] = symbols;
54         return symbols;
55     }
56
57     private static String JavaDoc[] realignMonths(String JavaDoc[] months) {
58         String JavaDoc[] a = new String JavaDoc[13];
59         for (int i=1; i<13; i++) {
60             a[i] = months[i - 1];
61         }
62         return a;
63     }
64
65     private static String JavaDoc[] realignDaysOfWeek(String JavaDoc[] daysOfWeek) {
66         String JavaDoc[] a = new String JavaDoc[8];
67         for (int i=1; i<8; i++) {
68             a[i] = daysOfWeek[(i < 7) ? i + 1 : 1];
69         }
70         return a;
71     }
72
73     private static void addSymbols(TreeMap JavaDoc map, String JavaDoc[] symbols, Integer JavaDoc[] integers) {
74         for (int i=symbols.length; --i>=0; ) {
75             String JavaDoc symbol = symbols[i];
76             if (symbol != null) {
77                 map.put(symbol, integers[i]);
78             }
79         }
80     }
81
82     private static void addNumerals(TreeMap JavaDoc map, int start, int end, Integer JavaDoc[] integers) {
83         for (int i=start; i<=end; i++) {
84             map.put(String.valueOf(i).intern(), integers[i]);
85         }
86     }
87
88     private static int maxLength(String JavaDoc[] a) {
89         int max = 0;
90         for (int i=a.length; --i>=0; ) {
91             String JavaDoc s = a[i];
92             if (s != null) {
93                 int len = s.length();
94                 if (len > max) {
95                     max = len;
96                 }
97             }
98         }
99         return max;
100     }
101
102     private final WeakReference JavaDoc iLocale;
103
104     private final String JavaDoc[] iEras;
105     private final String JavaDoc[] iDaysOfWeek;
106     private final String JavaDoc[] iShortDaysOfWeek;
107     private final String JavaDoc[] iMonths;
108     private final String JavaDoc[] iShortMonths;
109     private final String JavaDoc[] iHalfday;
110
111     // These map Strings to Integers.
112
private final TreeMap JavaDoc iParseEras;
113     private final TreeMap JavaDoc iParseDaysOfWeek;
114     private final TreeMap JavaDoc iParseMonths;
115
116     private final int iMaxEraLength;
117     private final int iMaxDayOfWeekLength;
118     private final int iMaxShortDayOfWeekLength;
119     private final int iMaxMonthLength;
120     private final int iMaxShortMonthLength;
121     private final int iMaxHalfdayLength;
122
123     private GJLocaleSymbols(Locale JavaDoc locale) {
124         if (locale == null) {
125             locale = Locale.getDefault();
126         }
127
128         iLocale = new WeakReference JavaDoc(locale);
129
130         DateFormatSymbols JavaDoc dfs = new DateFormatSymbols JavaDoc(locale);
131
132         iEras = dfs.getEras();
133         iDaysOfWeek = realignDaysOfWeek(dfs.getWeekdays());
134         iShortDaysOfWeek = realignDaysOfWeek(dfs.getShortWeekdays());
135         iMonths = realignMonths(dfs.getMonths());
136         iShortMonths = realignMonths(dfs.getShortMonths());
137         iHalfday = dfs.getAmPmStrings();
138
139         Integer JavaDoc[] integers = new Integer JavaDoc[13];
140         for (int i=0; i<13; i++) {
141             integers[i] = new Integer JavaDoc(i);
142         }
143
144         iParseEras = new TreeMap JavaDoc(String.CASE_INSENSITIVE_ORDER);
145         addSymbols(iParseEras, iEras, integers);
146         if ("en".equals(locale.getLanguage())) {
147             // Include support for parsing "BCE" and "CE" if the language is
148
// English. At some point Joda-Time will need an independent set of
149
// localized symbols and not depend on java.text.DateFormatSymbols.
150
iParseEras.put("BCE", integers[0]);
151             iParseEras.put("CE", integers[1]);
152         }
153
154         iParseDaysOfWeek = new TreeMap JavaDoc(String.CASE_INSENSITIVE_ORDER);
155         addSymbols(iParseDaysOfWeek, iDaysOfWeek, integers);
156         addSymbols(iParseDaysOfWeek, iShortDaysOfWeek, integers);
157         addNumerals(iParseDaysOfWeek, 1, 7, integers);
158
159         iParseMonths = new TreeMap JavaDoc(String.CASE_INSENSITIVE_ORDER);
160         addSymbols(iParseMonths, iMonths, integers);
161         addSymbols(iParseMonths, iShortMonths, integers);
162         addNumerals(iParseMonths, 1, 12, integers);
163
164         iMaxEraLength = maxLength(iEras);
165         iMaxDayOfWeekLength = maxLength(iDaysOfWeek);
166         iMaxShortDayOfWeekLength = maxLength(iShortDaysOfWeek);
167         iMaxMonthLength = maxLength(iMonths);
168         iMaxShortMonthLength = maxLength(iShortMonths);
169         iMaxHalfdayLength = maxLength(iHalfday);
170     }
171
172     public String JavaDoc eraValueToText(int value) {
173         return iEras[value];
174     }
175
176     public int eraTextToValue(String JavaDoc text) {
177         Integer JavaDoc era = (Integer JavaDoc) iParseEras.get(text);
178         if (era != null) {
179             return era.intValue();
180         }
181         throw new IllegalFieldValueException(DateTimeFieldType.era(), text);
182     }
183
184     public int getEraMaxTextLength() {
185         return iMaxEraLength;
186     }
187
188     public String JavaDoc monthOfYearValueToText(int value) {
189         return iMonths[value];
190     }
191
192     public String JavaDoc monthOfYearValueToShortText(int value) {
193         return iShortMonths[value];
194     }
195
196     public int monthOfYearTextToValue(String JavaDoc text) {
197         Integer JavaDoc month = (Integer JavaDoc) iParseMonths.get(text);
198         if (month != null) {
199             return month.intValue();
200         }
201         throw new IllegalFieldValueException(DateTimeFieldType.monthOfYear(), text);
202     }
203
204     public int getMonthMaxTextLength() {
205         return iMaxMonthLength;
206     }
207
208     public int getMonthMaxShortTextLength() {
209         return iMaxShortMonthLength;
210     }
211
212     public String JavaDoc dayOfWeekValueToText(int value) {
213         return iDaysOfWeek[value];
214     }
215
216     public String JavaDoc dayOfWeekValueToShortText(int value) {
217         return iShortDaysOfWeek[value];
218     }
219
220     public int dayOfWeekTextToValue(String JavaDoc text) {
221         Integer JavaDoc day = (Integer JavaDoc) iParseDaysOfWeek.get(text);
222         if (day != null) {
223             return day.intValue();
224         }
225         throw new IllegalFieldValueException(DateTimeFieldType.dayOfWeek(), text);
226     }
227
228     public int getDayOfWeekMaxTextLength() {
229         return iMaxDayOfWeekLength;
230     }
231
232     public int getDayOfWeekMaxShortTextLength() {
233         return iMaxShortDayOfWeekLength;
234     }
235
236     public String JavaDoc halfdayValueToText(int value) {
237         return iHalfday[value];
238     }
239
240     public int halfdayTextToValue(String JavaDoc text) {
241         String JavaDoc[] halfday = iHalfday;
242         for (int i = halfday.length; --i>=0; ) {
243             if (halfday[i].equalsIgnoreCase(text)) {
244                 return i;
245             }
246         }
247         throw new IllegalFieldValueException(DateTimeFieldType.halfdayOfDay(), text);
248     }
249
250     public int getHalfdayMaxTextLength() {
251         return iMaxHalfdayLength;
252     }
253 }
254
Popular Tags