summaryrefslogtreecommitdiff
path: root/icu4j/main/localespi/src/main/java/com/ibm/icu/impl/jdkadapter/CalendarICU.java
blob: c4ce868a3e14c889fca9fe68aff4b01e122199e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
 *******************************************************************************
 * Copyright (C) 2008, International Business Machines Corporation and         *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
package com.ibm.icu.impl.jdkadapter;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import com.ibm.icu.impl.icuadapter.TimeZoneJDK;
import com.ibm.icu.text.DateFormatSymbols;
import com.ibm.icu.util.Calendar;

/**
 * CalendarICU is an adapter class which wraps ICU4J Calendar and
 * implements java.util.Calendar APIs.
 */
public class CalendarICU extends java.util.Calendar {

    private static final long serialVersionUID = -8641226371713600671L;

    private Calendar fIcuCal;

    private CalendarICU(Calendar icuCal) {
        fIcuCal = icuCal;
        init();
    }

    public static java.util.Calendar wrap(Calendar icuCal) {
        return new CalendarICU(icuCal);
    }

    public Calendar unwrap() {
        sync();
        return fIcuCal;
    }

    @Override
    public void add(int field, int amount) {
        sync();
        fIcuCal.add(field, amount);
    }

    // Note:    We do not need to override followings.  These methods
    //          call int compareTo(Calendar anotherCalendar) and we
    //          override the method.
    //public boolean after(Object when)
    //public boolean before(Object when)

    // Note:    Jeez!  These methods are final and we cannot override them.
    //          We do not want to rewrite ICU Calendar implementation classes
    //          as subclasses of java.util.Calendar.  This adapter class
    //          wraps an ICU Calendar instance and the calendar calculation
    //          is actually done independently from java.util.Calendar
    //          implementation.  Thus, we need to monitor the status of
    //          superclass fields in some methods and call ICU Calendar's
    //          clear if superclass clear update the status of superclass's
    //          calendar fields.  See private void sync().
    //public void clear()
    //public void clear(int field)

    @Override
    public Object clone() {
        sync();
        CalendarICU other = (CalendarICU)super.clone();
        other.fIcuCal = (Calendar)fIcuCal.clone();
        return other;
    }

    public int compareTo(Calendar anotherCalendar)  {
        sync();
        long thisMillis = getTimeInMillis();
        long otherMillis = anotherCalendar.getTimeInMillis();
        return thisMillis > otherMillis ? 1 : (thisMillis == otherMillis ? 0 : -1);
    }

    // Note:    These methods are supposed to be implemented by java.util.Calendar
    //          subclasses.  But we actually use a instance of ICU Calendar
    //          for all calendar calculation, we do nothing here.
    @Override
    protected void complete() {}
    @Override
    protected void computeFields() {}
    @Override
    protected void computeTime() {}

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof CalendarICU) {
            sync();
            return ((CalendarICU)obj).fIcuCal.equals(fIcuCal);
        }
        return false;
    }

    @Override
    public int get(int field) {
        sync();
        return fIcuCal.get(field);
    }

    @Override
    public int getActualMaximum(int field) {
        return fIcuCal.getActualMaximum(field);
    }

    @Override
    public int getActualMinimum(int field) {
        return fIcuCal.getActualMinimum(field);
    }

    @Override
    public String getDisplayName(int field, int style, Locale locale) {
        if (field < 0 || field >= FIELD_COUNT || (style != SHORT && style != LONG && style != ALL_STYLES)) {
            throw new IllegalArgumentException("Bad field or style.");
        }
        DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
        String[] array = getFieldStrings(field, style, dfs);
        if (array != null) {
            int fieldVal = get(field);
            if (fieldVal < array.length) {
                return array[fieldVal];
            }
        }
        return null;
    }

    @Override
    public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
        if (field < 0 || field >= FIELD_COUNT || (style != SHORT && style != LONG && style != ALL_STYLES)) {
            throw new IllegalArgumentException("Bad field or style.");
        }
        DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
        if (style != ALL_STYLES) {
            return getFieldStringsMap(field, style, dfs);
        }

        Map<String,Integer> result = getFieldStringsMap(field, SHORT, dfs);
        if (result == null) {
            return null;
        }
        if (field == MONTH || field == DAY_OF_WEEK) {
            Map<String,Integer> longMap = getFieldStringsMap(field, LONG, dfs);
            if (longMap != null) {
                result.putAll(longMap);
            }
        }
        return result;
    }

    @Override
    public int getFirstDayOfWeek() {
        return fIcuCal.getFirstDayOfWeek();
    }

    @Override
    public int getGreatestMinimum(int field) {
        return fIcuCal.getGreatestMinimum(field);
    }

    @Override
    public int getLeastMaximum(int field) {
        return fIcuCal.getLeastMaximum(field);
    }

    @Override
    public int getMaximum(int field) {
        return fIcuCal.getMaximum(field);
    }

    @Override
    public int getMinimalDaysInFirstWeek() {
        return fIcuCal.getMinimalDaysInFirstWeek();
    }

    @Override
    public int getMinimum(int field) {
        return fIcuCal.getMinimum(field);
    }

    // Note:    getTime() calls getTimeInMillis()
    //public Date getTime()

    @Override
    public long getTimeInMillis() {
        sync();
        return fIcuCal.getTimeInMillis();
    }

    @Override
    public TimeZone getTimeZone() {
        return TimeZoneICU.wrap(fIcuCal.getTimeZone());
    }

    @Override
    public int hashCode() {
        sync();
        return fIcuCal.hashCode();
    }

    //protected int internalGet(int field)

    @Override
    public boolean isLenient() {
        return fIcuCal.isLenient();
    }

    //public boolean isSet(int field)

    @Override
    public void roll(int field, boolean up) {
        sync();
        fIcuCal.roll(field, up);
    }

    @Override
    public void roll(int field, int amount) {
        sync();
        fIcuCal.roll(field, amount);
    }

    @Override
    public void set(int field, int value) {
        sync();
        fIcuCal.set(field, value);
    }

    // Note:    These set methods call set(int field, int value) for each field.
    //          These are final, so we cannot override them, but we override
    //          set(int field, int value), so the superclass implementations
    //          still work as we want.
    //public void set(int year, int month, int date)
    //public void set(int year, int month, int date, int hourOfDay, int minute)
    //public void set(int year, int month, int date, int hourOfDay, int minute, int second)

    @Override
    public void setFirstDayOfWeek(int value) {
        fIcuCal.setFirstDayOfWeek(value);
    }

    @Override
    public void setLenient(boolean lenient) {
        fIcuCal.setLenient(lenient);
    }

    @Override
    public void setMinimalDaysInFirstWeek(int value) {
        fIcuCal.setMinimalDaysInFirstWeek(value);
    }

    // Note:    This method calls setTimeInMillis(long millis).
    //          This method is final, so we cannot override it, but we
    //          override setTimeInMillis(long millis), so the superclass
    //          implementation still works as we want.
    //public void setTime(Date date)

    @Override
    public void setTimeInMillis(long millis) {
        fIcuCal.setTimeInMillis(millis);
    }

    @Override
    public void setTimeZone(TimeZone value) {
        fIcuCal.setTimeZone(TimeZoneJDK.wrap(value));
    }

    @Override
    public String toString() {
        sync();
        return "CalendarICU: " + fIcuCal.toString();
    }

    private void sync() {
        // Check if clear is called for each JDK Calendar field.
        // If it was, then call clear for the field in the wrapped
        // ICU Calendar.
        for (int i = 0; i < isSet.length; i++) {
            if (!isSet[i]) {
                isSet[i] = true;
                try {
                    fIcuCal.clear(i);
                } catch (ArrayIndexOutOfBoundsException e) {
                    // More fields in JDK calendar, which is unlikely
                }
            }
        }
    }

    private void init() {
        // Mark "set" for all fields, so we can detect the invocation of
        // clear() later.
        for (int i = 0; i < isSet.length; i++) {
            isSet[i] = true;
        }
    }

    private static String[] getFieldStrings(int field, int style, DateFormatSymbols dfs) {
        String[] result = null;
        switch (field) {
        case AM_PM:
            result = dfs.getAmPmStrings();
            break;
        case DAY_OF_WEEK:
            result = (style == LONG) ? dfs.getWeekdays() : dfs.getShortWeekdays();
            break;
        case ERA:
            //result = (style == LONG) ? dfs.getEraNames() : dfs.getEras();
            result = dfs.getEras();
            break;
        case MONTH:
            result = (style == LONG) ? dfs.getMonths() : dfs.getShortMonths();
            break;
        }
        return result;
    }

    private static Map<String,Integer> getFieldStringsMap(int field, int style, DateFormatSymbols dfs) {
        String[] strings = getFieldStrings(field, style, dfs);
        if (strings == null) {
            return null;
        }
        Map<String,Integer> res = new HashMap<String,Integer>();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].length() != 0) {
                res.put(strings[i], Integer.valueOf(i));
            }
        }
        return res;
    }
}