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