schedule.java

来自「数据仓库展示程序」· Java 代码 · 共 834 行 · 第 1/3 页

JAVA
834
字号
/*
// $Id: //open/mondrian/src/main/mondrian/util/Schedule.java#5 $
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2002-2005 Julian Hyde
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/

package mondrian.util;

import java.sql.Time;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * A <code>Schedule</code> generates a series of time events.
 *
 * <p> Create a schedule using one of the factory methods:<ul>
 * <li>{@link #createOnce},</li>
 * <li>{@link #createDaily},</li>
 * <li>{@link #createWeekly},</li>
 * <li>{@link #createMonthlyByDay},</li>
 * <li>{@link #createMonthlyByWeek}.</li></ul>
 *
 * <p> Then use the {@link #nextOccurrence} method to find the next occurrence
 * after a particular point in time.
 *
 * <p> The <code>begin</code> and <code>end</code> parameters represent the
 * points in time between which the schedule is active. Both are optional.
 * However, if a schedule type supports a <code>period</code> parameter, and
 * you supply a value greater than 1, <code>begin</code> is used to determine
 * the start of the cycle. If <code>begin</code> is not specified, the cycle
 * starts at the epoch (January 1st, 1970).
 *
 * <p> The {@link Date} parameters in this API -- <code>begin</code> and
 * <code>end</code>, the <code>time</code> parameter to {@link #createOnce},
 * and the <code>earliestDate</code> parameter and value returned from {@link
 * #nextOccurrence} -- always represent a point in time (GMT), not a local
 * time.  If a schedule is to start at 12 noon Tokyo time, April 1st, 2002, it
 * is the application's reponsibility to convert this into a UTC {@link Date}
 * value.
 *
 * @author jhyde
 * @since May 7, 2002
 * @version $Id: //open/mondrian/src/main/mondrian/util/Schedule.java#5 $
 **/
public class Schedule {

    // members

    private DateSchedule dateSchedule;
    private TimeSchedule timeSchedule;
    private TimeZone tz;
    private Date begin;
    private Date end;

    // constants

    /**
     * Indicates that a schedule should fire on the last day of the month.
     * @see #createMonthlyByDay
     */
    public static final int LAST_DAY_OF_MONTH = 0;
    /**
     * Indicates that a schedule should fire on the last week of the month.
     * @see #createMonthlyByWeek
     */
    public static final int LAST_WEEK_OF_MONTH = 0;

    static final TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");

    static final int allDaysOfWeekBitmap =
            (1 << Calendar.MONDAY) |
            (1 << Calendar.TUESDAY) |
            (1 << Calendar.WEDNESDAY) |
            (1 << Calendar.THURSDAY) |
            (1 << Calendar.FRIDAY) |
            (1 << Calendar.SATURDAY) |
            (1 << Calendar.SUNDAY);
    static final int allDaysOfMonthBitmap = 0xefffFffe | // bits 1..31
            (1 << LAST_DAY_OF_MONTH);
    static final int allWeeksOfMonthBitmap = 0x0000003e | // bits 1..5
            (1 << LAST_WEEK_OF_MONTH);

    // constructor(s) and factory methods

    /**
     * Please use the factory methods {@link #createDaily} etc. to create a
     * Schedule.
     */
    private Schedule(
            DateSchedule dateSchedule,
            TimeSchedule timeSchedule,
            TimeZone tz,
            Date begin,
            Date end) {
        this.dateSchedule = dateSchedule;
        this.timeSchedule = timeSchedule;
        this.tz = tz;
        this.begin = begin;
        this.end = end;
    }

    /**
     * Creates a calendar which fires only once.
     *
     * @param date date and time to fire, must be UTC
     * @param tz timezone
     *
     * @pre tz != null
     * @pre date != null
     * @post return != null
     */
    public static Schedule createOnce(Date date, TimeZone tz) {
        Calendar calendar = ScheduleUtil.createCalendar(date);
        Time timeOfDay = ScheduleUtil.createTime(
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND));
        calendar.add(Calendar.SECOND, 1);
        Date datePlusDelta = calendar.getTime();
        return createDaily(date, datePlusDelta, tz, timeOfDay, 1);
    }

    /**
     * Creates a calendar which fires every day.
     *
     * @param begin open lower bound, may be null, must be UTC
     * @param end closed upper bound, may be null, must be UTC
     * @param tz timezone
     * @param timeOfDay time at which to fire
     * @param period causes the schedule to fire every <code>period</code>
     *     days. If <code>period</code> is greater than 1, the cycle starts
     *     at the begin point of the schedule, or at the epoch (1 January,
     *     1970) if <code>begin</code> is not specified.
     *
     * @pre tz != null
     * @pre period > 0
     * @post return != null
     */
    public static Schedule createDaily(
            Date begin, Date end, TimeZone tz, Time timeOfDay, int period) {
        DateSchedule dateSchedule = new DailyDateSchedule(
                begin == null ? null : ScheduleUtil.createCalendar(begin),
                period);
        return new Schedule(
                dateSchedule,
                new OnceTimeSchedule(ScheduleUtil.createTimeCalendar(timeOfDay)),
                tz,
                begin,
                end);
    }

    /**
     * Creates a calendar which fires on particular days each week.
     *
     * @param tz timezone
     * @param daysOfMonthBitmap a bitmap of day values, for example
     *     <code>(1 << {@link Calendar#TUESDAY}) |
     *           (1 << {@link Calendar#THURSDAY})</code> to fire on Tuesdays
     *     and Thursdays
     * @param timeOfDay time at which to fire
     * @param begin open lower bound, may be null
     * @param end closed upper bound, may be null
     * @param period causes the schedule to be active every <code>period</code>
     *     weeks. If <code>period</code> is greater than 1, the cycle starts
     *     at the begin point of the schedule, or at the epoch (1 January,
     *     1970) if <code>begin</code> is not specified.
     *
     * @pre tz != null
     * @pre period > 0
     * @post return != null
     */
    public static Schedule createWeekly(
            Date begin, Date end, TimeZone tz,
            Time timeOfDay, int period, int daysOfWeekBitmap) {
        DateSchedule dateSchedule = new WeeklyDateSchedule(
                begin == null ? null : ScheduleUtil.createCalendar(begin),
                period,
                daysOfWeekBitmap);
        return new Schedule(
                dateSchedule,
                new OnceTimeSchedule(ScheduleUtil.createTimeCalendar(timeOfDay)),
                tz,
                begin,
                end);
    }

    /**
     * Creates a calendar which fires on particular days of each month.
     * For example,<blockquote>
     *
     * <pre>createMonthlyByDay(
     *     null, null, TimeZone.getTimeZone("PST"), 1,
     *     (1 << 12) | (1 << 14) | (1 << {@link #LAST_DAY_OF_MONTH}))</pre>
     *
     * </blockquote> creates a schedule which fires on the 12th, 14th and last
     * day of the month.
     *
     * @param begin open lower bound, may be null
     * @param end closed upper bound, may be null
     * @param tz timezone
     * @param daysOfMonthBitmap a bitmap of day values, may include
     *     {@link #LAST_DAY_OF_MONTH}
     * @param timeOfDay time at which to fire
     * @param period causes the schedule to be active every <code>period</code>
     *     months. If <code>period</code> is greater than 1, the cycle starts
     *     at the begin point of the schedule, or at the epoch (1 January,
     *     1970) if <code>begin</code> is not specified.
     *
     * @pre tz != null
     * @pre period > 0
     * @post return != null
     */
    public static Schedule createMonthlyByDay(
            Date begin, Date end, TimeZone tz, Time timeOfDay, int period,
            int daysOfMonthBitmap) {
        DateSchedule dateSchedule = new MonthlyByDayDateSchedule(
                begin == null ? null : ScheduleUtil.createCalendar(begin),
                period, daysOfMonthBitmap);
        return new Schedule(
                dateSchedule,
                new OnceTimeSchedule(ScheduleUtil.createTimeCalendar(timeOfDay)),
                tz,
                begin,
                end);
    }

    /**
     * Creates a calendar which fires on particular days of particular weeks of
     * a month. For example,<blockquote>
     *
     * <pre>createMonthlyByWeek(
     *     null, null, TimeZone.getTimeZone("PST"),
     *     (1 << Calendar.TUESDAY) | (1 << Calendar.THURSDAY),
     *     (1 << 2) | (1 << {@link #LAST_WEEK_OF_MONTH})</pre>
     *
     * </blockquote> creates a schedule which fires on the 2nd and last Tuesday
     * and Thursday of the month.
     *
     * @param begin open lower bound, may be null
     * @param end closed upper bound, may be null
     * @param tz timezone
     * @param daysOfMonthBitmap a bitmap of day values, for example
     *     <code>(1 << Calendar.TUESDAY) | (1 << Calendar.THURSDAY)</code>
     * @param weeksOfMonthBitmap a bitmap of week values (may include
     *     {@link #LAST_WEEK_OF_MONTH}
     * @param timeOfDay time at which to fire
     * @param period causes the schedule be active every <code>period</code>
     *     months. If <code>period</code> is greater than 1, the cycle starts
     *     at the begin point of the schedule, or at the epoch (1 January,
     *     1970) if <code>begin</code> is not specified.
     *
     * @pre tz != null
     * @pre period > 0
     * @post return != null
     */
    public static Schedule createMonthlyByWeek(
            Date begin, Date end, TimeZone tz,
            Time timeOfDay, int period, int daysOfWeekBitmap,
            int weeksOfMonthBitmap) {
        DateSchedule dateSchedule = new MonthlyByWeekDateSchedule(
                begin == null ? null : ScheduleUtil.createCalendar(begin),
                period,
                daysOfWeekBitmap,
                weeksOfMonthBitmap);
        return new Schedule(
                dateSchedule,
                new OnceTimeSchedule(ScheduleUtil.createTimeCalendar(timeOfDay)),
                tz,
                begin,
                end);
    }

    /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?