rt/emul/compact/src/main/java/java/text/CalendarBuilder.java
branchjdk7-b147
changeset 1334 588d5bf7a560
child 1339 8cc04f85a683
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/text/CalendarBuilder.java	Thu Oct 03 15:40:35 2013 +0200
     1.3 @@ -0,0 +1,170 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.text;
    1.30 +
    1.31 +import java.util.Calendar;
    1.32 +import static java.util.GregorianCalendar.*;
    1.33 +
    1.34 +/**
    1.35 + * {@code CalendarBuilder} keeps field-value pairs for setting
    1.36 + * the calendar fields of the given {@code Calendar}. It has the
    1.37 + * {@link Calendar#FIELD_COUNT FIELD_COUNT}-th field for the week year
    1.38 + * support. Also {@code ISO_DAY_OF_WEEK} is used to specify
    1.39 + * {@code DAY_OF_WEEK} in the ISO day of week numbering.
    1.40 + *
    1.41 + * <p>{@code CalendarBuilder} retains the semantic of the pseudo
    1.42 + * timestamp for fields. {@code CalendarBuilder} uses a single
    1.43 + * int array combining fields[] and stamp[] of {@code Calendar}.
    1.44 + *
    1.45 + * @author Masayoshi Okutsu
    1.46 + */
    1.47 +class CalendarBuilder {
    1.48 +    /*
    1.49 +     * Pseudo time stamp constants used in java.util.Calendar
    1.50 +     */
    1.51 +    private static final int UNSET = 0;
    1.52 +    private static final int COMPUTED = 1;
    1.53 +    private static final int MINIMUM_USER_STAMP = 2;
    1.54 +
    1.55 +    private static final int MAX_FIELD = FIELD_COUNT + 1;
    1.56 +
    1.57 +    public static final int WEEK_YEAR = FIELD_COUNT;
    1.58 +    public static final int ISO_DAY_OF_WEEK = 1000; // pseudo field index
    1.59 +
    1.60 +    // stamp[] (lower half) and field[] (upper half) combined
    1.61 +    private final int[] field;
    1.62 +    private int nextStamp;
    1.63 +    private int maxFieldIndex;
    1.64 +
    1.65 +    CalendarBuilder() {
    1.66 +        field = new int[MAX_FIELD * 2];
    1.67 +        nextStamp = MINIMUM_USER_STAMP;
    1.68 +        maxFieldIndex = -1;
    1.69 +    }
    1.70 +
    1.71 +    CalendarBuilder set(int index, int value) {
    1.72 +        if (index == ISO_DAY_OF_WEEK) {
    1.73 +            index = DAY_OF_WEEK;
    1.74 +            value = toCalendarDayOfWeek(value);
    1.75 +        }
    1.76 +        field[index] = nextStamp++;
    1.77 +        field[MAX_FIELD + index] = value;
    1.78 +        if (index > maxFieldIndex && index < FIELD_COUNT) {
    1.79 +            maxFieldIndex = index;
    1.80 +        }
    1.81 +        return this;
    1.82 +    }
    1.83 +
    1.84 +    CalendarBuilder addYear(int value) {
    1.85 +        field[MAX_FIELD + YEAR] += value;
    1.86 +        field[MAX_FIELD + WEEK_YEAR] += value;
    1.87 +        return this;
    1.88 +    }
    1.89 +
    1.90 +    boolean isSet(int index) {
    1.91 +        if (index == ISO_DAY_OF_WEEK) {
    1.92 +            index = DAY_OF_WEEK;
    1.93 +        }
    1.94 +        return field[index] > UNSET;
    1.95 +    }
    1.96 +
    1.97 +    Calendar establish(Calendar cal) {
    1.98 +        boolean weekDate = isSet(WEEK_YEAR)
    1.99 +                            && field[WEEK_YEAR] > field[YEAR];
   1.100 +        if (weekDate && !cal.isWeekDateSupported()) {
   1.101 +            // Use YEAR instead
   1.102 +            if (!isSet(YEAR)) {
   1.103 +                set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
   1.104 +            }
   1.105 +            weekDate = false;
   1.106 +        }
   1.107 +
   1.108 +        cal.clear();
   1.109 +        // Set the fields from the min stamp to the max stamp so that
   1.110 +        // the field resolution works in the Calendar.
   1.111 +        for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
   1.112 +            for (int index = 0; index <= maxFieldIndex; index++) {
   1.113 +                if (field[index] == stamp) {
   1.114 +                    cal.set(index, field[MAX_FIELD + index]);
   1.115 +                    break;
   1.116 +                }
   1.117 +            }
   1.118 +        }
   1.119 +
   1.120 +        if (weekDate) {
   1.121 +            int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1;
   1.122 +            int dayOfWeek = isSet(DAY_OF_WEEK) ?
   1.123 +                                field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek();
   1.124 +            if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) {
   1.125 +                if (dayOfWeek >= 8) {
   1.126 +                    dayOfWeek--;
   1.127 +                    weekOfYear += dayOfWeek / 7;
   1.128 +                    dayOfWeek = (dayOfWeek % 7) + 1;
   1.129 +                } else {
   1.130 +                    while (dayOfWeek <= 0) {
   1.131 +                        dayOfWeek += 7;
   1.132 +                        weekOfYear--;
   1.133 +                    }
   1.134 +                }
   1.135 +                dayOfWeek = toCalendarDayOfWeek(dayOfWeek);
   1.136 +            }
   1.137 +            cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek);
   1.138 +        }
   1.139 +        return cal;
   1.140 +    }
   1.141 +
   1.142 +    public String toString() {
   1.143 +        StringBuilder sb = new StringBuilder();
   1.144 +        sb.append("CalendarBuilder:[");
   1.145 +        for (int i = 0; i < field.length; i++) {
   1.146 +            if (isSet(i)) {
   1.147 +                sb.append(i).append('=').append(field[MAX_FIELD + i]).append(',');
   1.148 +            }
   1.149 +        }
   1.150 +        int lastIndex = sb.length() - 1;
   1.151 +        if (sb.charAt(lastIndex) == ',') {
   1.152 +            sb.setLength(lastIndex);
   1.153 +        }
   1.154 +        sb.append(']');
   1.155 +        return sb.toString();
   1.156 +    }
   1.157 +
   1.158 +    static int toISODayOfWeek(int calendarDayOfWeek) {
   1.159 +        return calendarDayOfWeek == SUNDAY ? 7 : calendarDayOfWeek - 1;
   1.160 +    }
   1.161 +
   1.162 +    static int toCalendarDayOfWeek(int isoDayOfWeek) {
   1.163 +        if (!isValidDayOfWeek(isoDayOfWeek)) {
   1.164 +            // adjust later for lenient mode
   1.165 +            return isoDayOfWeek;
   1.166 +        }
   1.167 +        return isoDayOfWeek == 7 ? SUNDAY : isoDayOfWeek + 1;
   1.168 +    }
   1.169 +
   1.170 +    static boolean isValidDayOfWeek(int dayOfWeek) {
   1.171 +        return dayOfWeek > 0 && dayOfWeek <= 7;
   1.172 +    }
   1.173 +}