2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28 import java.util.Calendar;
29 import static java.util.Calendar.*;
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.
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}.
42 * @author Masayoshi Okutsu
44 class CalendarBuilder {
46 * Pseudo time stamp constants used in java.util.Calendar
48 private static final int UNSET = 0;
49 private static final int COMPUTED = 1;
50 private static final int MINIMUM_USER_STAMP = 2;
52 private static final int MAX_FIELD = FIELD_COUNT + 1;
54 public static final int WEEK_YEAR = FIELD_COUNT;
55 public static final int ISO_DAY_OF_WEEK = 1000; // pseudo field index
57 // stamp[] (lower half) and field[] (upper half) combined
58 private final int[] field;
59 private int nextStamp;
60 private int maxFieldIndex;
63 field = new int[MAX_FIELD * 2];
64 nextStamp = MINIMUM_USER_STAMP;
68 CalendarBuilder set(int index, int value) {
69 if (index == ISO_DAY_OF_WEEK) {
71 value = toCalendarDayOfWeek(value);
73 field[index] = nextStamp++;
74 field[MAX_FIELD + index] = value;
75 if (index > maxFieldIndex && index < FIELD_COUNT) {
76 maxFieldIndex = index;
81 CalendarBuilder addYear(int value) {
82 field[MAX_FIELD + YEAR] += value;
83 field[MAX_FIELD + WEEK_YEAR] += value;
87 boolean isSet(int index) {
88 if (index == ISO_DAY_OF_WEEK) {
91 return field[index] > UNSET;
94 Calendar establish(Calendar cal) {
95 boolean weekDate = isSet(WEEK_YEAR)
96 && field[WEEK_YEAR] > field[YEAR];
97 if (weekDate && !cal.isWeekDateSupported()) {
100 set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
106 // Set the fields from the min stamp to the max stamp so that
107 // the field resolution works in the Calendar.
108 for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
109 for (int index = 0; index <= maxFieldIndex; index++) {
110 if (field[index] == stamp) {
111 cal.set(index, field[MAX_FIELD + index]);
118 int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
119 int dayOfWeek = isSet(DAY_OF_WEEK) ?
120 field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
121 if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
122 if (dayOfWeek >= 8) {
124 weekOfYear += dayOfWeek / 7;
125 dayOfWeek = (dayOfWeek % 7) + 1;
127 while (dayOfWeek <= 0) {
132 dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
134 cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
139 public String toString() {
140 StringBuilder sb = new StringBuilder();
141 sb.append("CalendarBuilder:[");
142 for (int i = 0; i < field.length; i++) {
144 sb.append(i).append('=').append(field[MAX_FIELD + i]).append(',');
147 int lastIndex = sb.length() - 1;
148 if (sb.charAt(lastIndex) == ',') {
149 sb.setLength(lastIndex);
152 return sb.toString();
155 static int toISODayOfWeek(int calendarDayOfWeek) {
156 return calendarDayOfWeek == SUNDAY ? 7 : calendarDayOfWeek - 1;
159 static int toCalendarDayOfWeek(int isoDayOfWeek) {
160 if (!isValidDayOfWeek(isoDayOfWeek)) {
161 // adjust later for lenient mode
164 return isoDayOfWeek == 7 ? SUNDAY : isoDayOfWeek + 1;
167 static boolean isValidDayOfWeek(int dayOfWeek) {
168 return dayOfWeek > 0 && dayOfWeek <= 7;