001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.text;
019:
020: import java.io.Serializable;
021: import java.util.Arrays;
022: import java.util.Locale;
023:
024: /**
025: * DateFormatSymbols holds the Strings used in the formating and parsing of
026: * dates and times.
027: */
028: public class DateFormatSymbols implements Serializable, Cloneable {
029:
030: private static final long serialVersionUID = -5987973545549424702L;
031:
032: private String localPatternChars;
033:
034: String[] ampms, eras, months, shortMonths, shortWeekdays, weekdays;
035:
036: String[][] zoneStrings;
037:
038: /**
039: * Constructs a new DateFormatSymbols containing the symbols for the default
040: * Locale.
041: */
042: public DateFormatSymbols() {
043: this (Locale.getDefault());
044: }
045:
046: /**
047: * Constructs a new DateFormatSymbols containing the symbols for the
048: * specified Locale.
049: *
050: * @param locale
051: * the Locale
052: */
053: public DateFormatSymbols(Locale locale) {
054: com.ibm.icu.text.DateFormatSymbols icuSymbols = new com.ibm.icu.text.DateFormatSymbols(
055: locale);
056:
057: localPatternChars = icuSymbols.getLocalPatternChars();
058: ampms = icuSymbols.getAmPmStrings();
059: eras = icuSymbols.getEras();
060: months = icuSymbols.getMonths();
061: shortMonths = icuSymbols.getShortMonths();
062: shortWeekdays = icuSymbols.getShortWeekdays();
063: weekdays = icuSymbols.getWeekdays();
064: zoneStrings = icuSymbols.getZoneStrings();
065: }
066:
067: /**
068: * Answers a new DateFormatSymbols with the same symbols as this
069: * DateFormatSymbols.
070: *
071: * @return a shallow copy of this DateFormatSymbols
072: *
073: * @see java.lang.Cloneable
074: */
075: @Override
076: public Object clone() {
077: try {
078: DateFormatSymbols symbols = (DateFormatSymbols) super
079: .clone();
080: symbols.ampms = ampms.clone();
081: symbols.eras = eras.clone();
082: symbols.months = months.clone();
083: symbols.shortMonths = shortMonths.clone();
084: symbols.shortWeekdays = shortWeekdays.clone();
085: symbols.weekdays = weekdays.clone();
086: symbols.zoneStrings = new String[zoneStrings.length][];
087: for (int i = 0; i < zoneStrings.length; i++) {
088: symbols.zoneStrings[i] = zoneStrings[i].clone();
089: }
090: return symbols;
091: } catch (CloneNotSupportedException e) {
092: return null;
093: }
094: }
095:
096: /**
097: * Compares the specified object to this DateFormatSymbols and answer if
098: * they are equal. The object must be an instance of DateFormatSymbols with
099: * the same symbols.
100: *
101: * @param object
102: * the object to compare with this object
103: * @return true if the specified object is equal to this DateFormatSymbols,
104: * false otherwise
105: *
106: * @see #hashCode
107: */
108: @Override
109: public boolean equals(Object object) {
110: if (this == object) {
111: return true;
112: }
113: if (!(object instanceof DateFormatSymbols)) {
114: return false;
115: }
116: DateFormatSymbols obj = (DateFormatSymbols) object;
117: if (!localPatternChars.equals(obj.localPatternChars)) {
118: return false;
119: }
120: if (!Arrays.equals(ampms, obj.ampms)) {
121: return false;
122: }
123: if (!Arrays.equals(eras, obj.eras)) {
124: return false;
125: }
126: if (!Arrays.equals(months, obj.months)) {
127: return false;
128: }
129: if (!Arrays.equals(shortMonths, obj.shortMonths)) {
130: return false;
131: }
132: if (!Arrays.equals(shortWeekdays, obj.shortWeekdays)) {
133: return false;
134: }
135: if (!Arrays.equals(weekdays, obj.weekdays)) {
136: return false;
137: }
138: if (zoneStrings.length != obj.zoneStrings.length) {
139: return false;
140: }
141: for (String[] element : zoneStrings) {
142: if (element.length != element.length) {
143: return false;
144: }
145: for (int j = 0; j < element.length; j++) {
146: if (element[j] != element[j]
147: && !(element[j].equals(element[j]))) {
148: return false;
149: }
150: }
151: }
152: return true;
153: }
154:
155: /**
156: * Answers the array of Strings which represent AM and PM. Use the Calendar
157: * constants Calendar.AM and Calendar.PM to index into the array.
158: *
159: * @return an array of String
160: */
161: public String[] getAmPmStrings() {
162: return ampms.clone();
163: }
164:
165: /**
166: * Answers the array of Strings which represent BC and AD. Use the Calendar
167: * constants GregorianCalendar.BC and GregorianCalendar.AD to index into the
168: * array.
169: *
170: * @return an array of String
171: */
172: public String[] getEras() {
173: return eras.clone();
174: }
175:
176: /**
177: * Answers the pattern characters used by SimpleDateFormat to specify date
178: * and time fields.
179: *
180: * @return a String containing the pattern characters
181: */
182: public String getLocalPatternChars() {
183: return localPatternChars;
184: }
185:
186: /**
187: * Answers the array of Strings containing the full names of the months. Use
188: * the Calendar constants Calendar.JANUARY, etc. to index into the array.
189: *
190: * @return an array of String
191: */
192: public String[] getMonths() {
193: return months.clone();
194: }
195:
196: /**
197: * Answers the array of Strings containing the abbreviated names of the
198: * months. Use the Calendar constants Calendar.JANUARY, etc. to index into
199: * the array.
200: *
201: * @return an array of String
202: */
203: public String[] getShortMonths() {
204: return shortMonths.clone();
205: }
206:
207: /**
208: * Answers the array of Strings containing the abbreviated names of the days
209: * of the week. Use the Calendar constants Calendar.SUNDAY, etc. to index
210: * into the array.
211: *
212: * @return an array of String
213: */
214: public String[] getShortWeekdays() {
215: return shortWeekdays.clone();
216: }
217:
218: /**
219: * Answers the array of Strings containing the full names of the days of the
220: * week. Use the Calendar constants Calendar.SUNDAY, etc. to index into the
221: * array.
222: *
223: * @return an array of String
224: */
225: public String[] getWeekdays() {
226: return weekdays.clone();
227: }
228:
229: /**
230: * Answers the two-dimensional array of Strings containing the names of the
231: * timezones. Each element in the array is an array of five Strings, the
232: * first is a TimeZone ID, and second and third are the full and abbreviated
233: * timezone names for standard time, and the fourth and fifth are the full
234: * and abbreviated names for daylight time.
235: *
236: * @return a two-dimensional array of String
237: */
238: public String[][] getZoneStrings() {
239: String[][] clone = new String[zoneStrings.length][];
240: for (int i = zoneStrings.length; --i >= 0;) {
241: clone[i] = zoneStrings[i].clone();
242: }
243: return clone;
244: }
245:
246: /**
247: * Answers an integer hash code for the receiver. Objects which are equal
248: * answer the same value for this method.
249: *
250: * @return the receiver's hash
251: *
252: * @see #equals
253: */
254: @Override
255: public int hashCode() {
256: int hashCode;
257: hashCode = localPatternChars.hashCode();
258: for (String element : ampms) {
259: hashCode += element.hashCode();
260: }
261: for (String element : eras) {
262: hashCode += element.hashCode();
263: }
264: for (String element : months) {
265: hashCode += element.hashCode();
266: }
267: for (String element : shortMonths) {
268: hashCode += element.hashCode();
269: }
270: for (String element : shortWeekdays) {
271: hashCode += element.hashCode();
272: }
273: for (String element : weekdays) {
274: hashCode += element.hashCode();
275: }
276: for (String[] element : zoneStrings) {
277: for (int j = 0; j < element.length; j++) {
278: if (element[j] != null) {
279: hashCode += element[j].hashCode();
280: }
281: }
282: }
283: return hashCode;
284: }
285:
286: /**
287: * Sets the array of Strings which represent AM and PM. Use the Calendar
288: * constants Calendar.AM and Calendar.PM to index into the array.
289: *
290: * @param data
291: * the array of Strings
292: */
293: public void setAmPmStrings(String[] data) {
294: ampms = data.clone();
295: }
296:
297: /**
298: * Sets the array of Strings which represent BC and AD. Use the Calendar
299: * constants GregorianCalendar.BC and GregorianCalendar.AD to index into the
300: * array.
301: *
302: * @param data
303: * the array of Strings
304: */
305: public void setEras(String[] data) {
306: eras = data.clone();
307: }
308:
309: /**
310: * Sets the pattern characters used by SimpleDateFormat to specify date and
311: * time fields.
312: *
313: * @param data
314: * the String containing the pattern characters
315: *
316: * @exception NullPointerException
317: * when the data is null
318: */
319: public void setLocalPatternChars(String data) {
320: if (data == null) {
321: throw new NullPointerException();
322: }
323: localPatternChars = data;
324: }
325:
326: /**
327: * Sets the array of Strings containing the full names of the months. Use
328: * the Calendar constants Calendar.JANUARY, etc. to index into the array.
329: *
330: * @param data
331: * the array of Strings
332: */
333: public void setMonths(String[] data) {
334: months = data.clone();
335: }
336:
337: /**
338: * Sets the array of Strings containing the abbreviated names of the months.
339: * Use the Calendar constants Calendar.JANUARY, etc. to index into the
340: * array.
341: *
342: * @param data
343: * the array of Strings
344: */
345: public void setShortMonths(String[] data) {
346: shortMonths = data.clone();
347: }
348:
349: /**
350: * Sets the array of Strings containing the abbreviated names of the days of
351: * the week. Use the Calendar constants Calendar.SUNDAY, etc. to index into
352: * the array.
353: *
354: * @param data
355: * the array of Strings
356: */
357: public void setShortWeekdays(String[] data) {
358: shortWeekdays = data.clone();
359: }
360:
361: /**
362: * Sets the array of Strings containing the full names of the days of the
363: * week. Use the Calendar constants Calendar.SUNDAY, etc. to index into the
364: * array.
365: *
366: * @param data
367: * the array of Strings
368: */
369: public void setWeekdays(String[] data) {
370: weekdays = data.clone();
371: }
372:
373: /**
374: * Sets the two-dimensional array of Strings containing the names of the
375: * timezones. Each element in the array is an array of five Strings, the
376: * first is a TimeZone ID, and second and third are the full and abbreviated
377: * timezone names for standard time, and the fourth and fifth are the full
378: * and abbreviated names for daylight time.
379: *
380: * @param data
381: * the two-dimensional array of Strings
382: */
383: public void setZoneStrings(String[][] data) {
384: zoneStrings = data.clone();
385: }
386: }
|