jdk/src/java.base/share/classes/java/text/CalendarBuilder.java
author igerasim
Wed, 27 Aug 2014 22:08:19 +0400
changeset 26219 1a19360ff122
parent 25859 3317bb8137f4
permissions -rw-r--r--
8054714: Use StringJoiner where it makes the code cleaner Reviewed-by: psandoz, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     1
/*
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
     2
 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     4
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    10
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    15
 * accompanied this code).
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    16
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    20
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    23
 * questions.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    24
 */
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    25
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    26
package java.text;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    27
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    28
import java.util.Calendar;
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
    29
import java.util.StringJoiner;
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    30
import static java.util.GregorianCalendar.*;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    31
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    32
/**
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    33
 * {@code CalendarBuilder} keeps field-value pairs for setting
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    34
 * the calendar fields of the given {@code Calendar}. It has the
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    35
 * {@link Calendar#FIELD_COUNT FIELD_COUNT}-th field for the week year
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    36
 * support. Also {@code ISO_DAY_OF_WEEK} is used to specify
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    37
 * {@code DAY_OF_WEEK} in the ISO day of week numbering.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    38
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    39
 * <p>{@code CalendarBuilder} retains the semantic of the pseudo
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    40
 * timestamp for fields. {@code CalendarBuilder} uses a single
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    41
 * int array combining fields[] and stamp[] of {@code Calendar}.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    42
 *
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    43
 * @author Masayoshi Okutsu
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    44
 */
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    45
class CalendarBuilder {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    46
    /*
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    47
     * Pseudo time stamp constants used in java.util.Calendar
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    48
     */
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    49
    private static final int UNSET = 0;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    50
    private static final int COMPUTED = 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    51
    private static final int MINIMUM_USER_STAMP = 2;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    52
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    53
    private static final int MAX_FIELD = FIELD_COUNT + 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    54
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    55
    public static final int WEEK_YEAR = FIELD_COUNT;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    56
    public static final int ISO_DAY_OF_WEEK = 1000; // pseudo field index
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    57
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    58
    // stamp[] (lower half) and field[] (upper half) combined
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    59
    private final int[] field;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    60
    private int nextStamp;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    61
    private int maxFieldIndex;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    62
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    63
    CalendarBuilder() {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    64
        field = new int[MAX_FIELD * 2];
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    65
        nextStamp = MINIMUM_USER_STAMP;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    66
        maxFieldIndex = -1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    67
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    68
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    69
    CalendarBuilder set(int index, int value) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    70
        if (index == ISO_DAY_OF_WEEK) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    71
            index = DAY_OF_WEEK;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    72
            value = toCalendarDayOfWeek(value);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    73
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    74
        field[index] = nextStamp++;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    75
        field[MAX_FIELD + index] = value;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    76
        if (index > maxFieldIndex && index < FIELD_COUNT) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    77
            maxFieldIndex = index;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    78
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    79
        return this;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    80
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    81
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    82
    CalendarBuilder addYear(int value) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    83
        field[MAX_FIELD + YEAR] += value;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    84
        field[MAX_FIELD + WEEK_YEAR] += value;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    85
        return this;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    86
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    87
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    88
    boolean isSet(int index) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    89
        if (index == ISO_DAY_OF_WEEK) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    90
            index = DAY_OF_WEEK;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    91
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    92
        return field[index] > UNSET;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    93
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
    94
17178
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    95
    CalendarBuilder clear(int index) {
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    96
        if (index == ISO_DAY_OF_WEEK) {
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    97
            index = DAY_OF_WEEK;
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    98
        }
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
    99
        field[index] = UNSET;
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
   100
        field[MAX_FIELD + index] = 0;
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
   101
        return this;
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
   102
    }
bf3ea41ef154 8000529: Regression: SimpleDateFormat incorrectly parses dates formatted with Z and z pattern letters
coffeys
parents: 6491
diff changeset
   103
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   104
    Calendar establish(Calendar cal) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   105
        boolean weekDate = isSet(WEEK_YEAR)
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   106
                            && field[WEEK_YEAR] > field[YEAR];
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   107
        if (weekDate && !cal.isWeekDateSupported()) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   108
            // Use YEAR instead
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   109
            if (!isSet(YEAR)) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   110
                set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   111
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   112
            weekDate = false;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   113
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   114
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   115
        cal.clear();
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   116
        // Set the fields from the min stamp to the max stamp so that
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   117
        // the field resolution works in the Calendar.
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   118
        for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   119
            for (int index = 0; index <= maxFieldIndex; index++) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   120
                if (field[index] == stamp) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   121
                    cal.set(index, field[MAX_FIELD + index]);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   122
                    break;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   123
                }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   124
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   125
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   126
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   127
        if (weekDate) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   128
            int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   129
            int dayOfWeek = isSet(DAY_OF_WEEK) ?
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   130
                                field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   131
            if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   132
                if (dayOfWeek >= 8) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   133
                    dayOfWeek--;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   134
                    weekOfYear += dayOfWeek / 7;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   135
                    dayOfWeek = (dayOfWeek % 7) + 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   136
                } else {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   137
                    while (dayOfWeek <= 0) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   138
                        dayOfWeek += 7;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   139
                        weekOfYear--;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   140
                    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   141
                }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   142
                dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   143
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   144
            cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   145
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   146
        return cal;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   147
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   148
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   149
    public String toString() {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   150
        StringJoiner sj = new StringJoiner(",", "CalendarBuilder:[", "]");
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   151
        for (int i = 0; i < field.length; i++) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   152
            if (isSet(i)) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   153
                sj.add(i + "=" + field[MAX_FIELD + i]);
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   154
            }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   155
        }
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   156
        return sj.toString();
6491
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   157
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   158
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   159
    static int toISODayOfWeek(int calendarDayOfWeek) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   160
        return calendarDayOfWeek == SUNDAY ? 7 : calendarDayOfWeek - 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   161
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   162
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   163
    static int toCalendarDayOfWeek(int isoDayOfWeek) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   164
        if (!isValidDayOfWeek(isoDayOfWeek)) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   165
            // adjust later for lenient mode
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   166
            return isoDayOfWeek;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   167
        }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   168
        return isoDayOfWeek == 7 ? SUNDAY : isoDayOfWeek + 1;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   169
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   170
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   171
    static boolean isValidDayOfWeek(int dayOfWeek) {
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   172
        return dayOfWeek > 0 && dayOfWeek <= 7;
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   173
    }
cd1bcc5057f4 4267450: (cal) API: Need public API to calculate, format and parse "year of week"
okutsu
parents:
diff changeset
   174
}