1/*
2 * Copyright (c) 2010, 2013, 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
26package java.text;
27
28import java.util.Calendar;
29import static java.util.GregorianCalendar.*;
30
31/**
32 * {@code CalendarBuilder} keeps field-value pairs for setting
33 * the calendar fields of the given {@code Calendar}. It has the
34 * {@link Calendar#FIELD_COUNT FIELD_COUNT}-th field for the week year
35 * support. Also {@code ISO_DAY_OF_WEEK} is used to specify
36 * {@code DAY_OF_WEEK} in the ISO day of week numbering.
37 *
38 * <p>{@code CalendarBuilder} retains the semantic of the pseudo
39 * timestamp for fields. {@code CalendarBuilder} uses a single
40 * int array combining fields[] and stamp[] of {@code Calendar}.
41 *
42 * @author Masayoshi Okutsu
43 */
44class CalendarBuilder {
45    /*
46     * Pseudo time stamp constants used in java.util.Calendar
47     */
48    private static final int UNSET = 0;
49    private static final int COMPUTED = 1;
50    private static final int MINIMUM_USER_STAMP = 2;
51
52    private static final int MAX_FIELD = FIELD_COUNT + 1;
53
54    public static final int WEEK_YEAR = FIELD_COUNT;
55    public static final int ISO_DAY_OF_WEEK = 1000; // pseudo field index
56
57    // stamp[] (lower half) and field[] (upper half) combined
58    private final int[] field;
59    private int nextStamp;
60    private int maxFieldIndex;
61
62    CalendarBuilder() {
63        field = new int[MAX_FIELD * 2];
64        nextStamp = MINIMUM_USER_STAMP;
65        maxFieldIndex = -1;
66    }
67
68    CalendarBuilder set(int index, int value) {
69        if (index == ISO_DAY_OF_WEEK) {
70            index = DAY_OF_WEEK;
71            value = toCalendarDayOfWeek(value);
72        }
73        field[index] = nextStamp++;
74        field[MAX_FIELD + index] = value;
75        if (index > maxFieldIndex && index < FIELD_COUNT) {
76            maxFieldIndex = index;
77        }
78        return this;
79    }
80
81    CalendarBuilder addYear(int value) {
82        field[MAX_FIELD + YEAR] += value;
83        field[MAX_FIELD + WEEK_YEAR] += value;
84        return this;
85    }
86
87    boolean isSet(int index) {
88        if (index == ISO_DAY_OF_WEEK) {
89            index = DAY_OF_WEEK;
90        }
91        return field[index] > UNSET;
92    }
93
94    CalendarBuilder clear(int index) {
95        if (index == ISO_DAY_OF_WEEK) {
96            index = DAY_OF_WEEK;
97        }
98        field[index] = UNSET;
99        field[MAX_FIELD + index] = 0;
100        return this;
101    }
102
103    Calendar establish(Calendar cal) {
104        boolean weekDate = isSet(WEEK_YEAR)
105                            && field[WEEK_YEAR] > field[YEAR];
106        if (weekDate && !cal.isWeekDateSupported()) {
107            // Use YEAR instead
108            if (!isSet(YEAR)) {
109                set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
110            }
111            weekDate = false;
112        }
113
114        cal.clear();
115        // Set the fields from the min stamp to the max stamp so that
116        // the field resolution works in the Calendar.
117        for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
118            for (int index = 0; index <= maxFieldIndex; index++) {
119                if (field[index] == stamp) {
120                    cal.set(index, field[MAX_FIELD + index]);
121                    break;
122                }
123            }
124        }
125
126        if (weekDate) {
127            int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
128            int dayOfWeek = isSet(DAY_OF_WEEK) ?
129                                field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
130            if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
131                if (dayOfWeek >= 8) {
132                    dayOfWeek--;
133                    weekOfYear += dayOfWeek / 7;
134                    dayOfWeek = (dayOfWeek % 7) + 1;
135                } else {
136                    while (dayOfWeek <= 0) {
137                        dayOfWeek += 7;
138                        weekOfYear--;
139                    }
140                }
141                dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
142            }
143            cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
144        }
145        return cal;
146    }
147
148    public String toString() {
149        StringBuilder sb = new StringBuilder();
150        sb.append("CalendarBuilder:[");
151        for (int i = 0; i < field.length; i++) {
152            if (isSet(i)) {
153                sb.append(i).append('=').append(field[MAX_FIELD + i]).append(',');
154            }
155        }
156        int lastIndex = sb.length() - 1;
157        if (sb.charAt(lastIndex) == ',') {
158            sb.setLength(lastIndex);
159        }
160        sb.append(']');
161        return sb.toString();
162    }
163
164    static int toISODayOfWeek(int calendarDayOfWeek) {
165        return calendarDayOfWeek == SUNDAY ? 7 : calendarDayOfWeek - 1;
166    }
167
168    static int toCalendarDayOfWeek(int isoDayOfWeek) {
169        if (!isValidDayOfWeek(isoDayOfWeek)) {
170            // adjust later for lenient mode
171            return isoDayOfWeek;
172        }
173        return isoDayOfWeek == 7 ? SUNDAY : isoDayOfWeek + 1;
174    }
175
176    static boolean isValidDayOfWeek(int dayOfWeek) {
177        return dayOfWeek > 0 && dayOfWeek <= 7;
178    }
179}
180