Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/calendar/zi/RuleDay.java
38855 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.util.ArrayList;26import java.util.HashMap;27import java.util.List;28import java.util.Map;2930/**31* RuleDay class represents the value of the "ON" field. The day of32* week values start from 1 following the {@link java.util.Calendar}33* convention.34*35* @since 1.436*/37class RuleDay {38private static final Map<String,DayOfWeek> abbreviations = new HashMap<String,DayOfWeek>(7);39static {40for (DayOfWeek day : DayOfWeek.values()) {41abbreviations.put(day.getAbbr(), day);42}43}4445private String dayName = null;46private DayOfWeek dow;47private boolean lastOne = false;48private int soonerOrLater = 0;49private int thanDayOfMonth; // day of month (e.g., 8 for "Sun>=8")5051RuleDay() {52}5354RuleDay(int day) {55thanDayOfMonth = day;56}5758int getDay() {59return thanDayOfMonth;60}6162/**63* @return the day of week value (1-based)64*/65int getDayOfWeekNum() {66return dow.value();67}6869/**70* @return true if this rule day represents the last day of71* week. (e.g., lastSun).72*/73boolean isLast() {74return lastOne;75}7677/**78* @return true if this rule day represents the day of week on or79* later than (after) the {@link #getDay}. (e.g., Sun>=1)80*/81boolean isLater() {82return soonerOrLater > 0;83}8485/**86* @return true if this rule day represents the day of week on or87* earlier than (before) the {@link #getDay}. (e.g., Sun<=15)88*/89boolean isEarlier() {90return soonerOrLater < 0;91}9293/**94* @return true if this rule day represents an exact day.95*/96boolean isExact() {97return soonerOrLater == 0;98}99100/**101* Parses the "ON" field and constructs a RuleDay.102* @param day an "ON" field string (e.g., "Sun>=1")103* @return a RuleDay representing the given "ON" field104*/105static RuleDay parse(String day) {106RuleDay d = new RuleDay();107if (day.startsWith("last")) {108d.lastOne = true;109d.dayName = day.substring(4);110d.dow = getDOW(d.dayName);111} else {112int index;113if ((index = day.indexOf(">=")) != -1) {114d.dayName = day.substring(0, index);115d.dow = getDOW(d.dayName);116d.soonerOrLater = 1; // greater or equal117d.thanDayOfMonth = Integer.parseInt(day.substring(index+2));118} else if ((index = day.indexOf("<=")) != -1) {119d.dayName = day.substring(0, index);120d.dow = getDOW(d.dayName);121d.soonerOrLater = -1; // less or equal122d.thanDayOfMonth = Integer.parseInt(day.substring(index+2));123} else {124// it should be an integer value.125d.thanDayOfMonth = Integer.parseInt(day);126}127}128return d;129}130131/**132* Converts this RuleDay to the SimpleTimeZone day rule.133* @return the converted SimpleTimeZone day rule134*/135int getDayForSimpleTimeZone() {136if (isLast()) {137return -1;138}139return isEarlier() ? -getDay() : getDay();140}141142/**143* Converts this RuleDay to the SimpleTimeZone day-of-week rule.144* @return the SimpleTimeZone day-of-week rule value145*/146int getDayOfWeekForSimpleTimeZoneInt() {147if (isEarlier() || isLater()) {148return -getDayOfWeekNum();149}150return isLast() ? getDayOfWeekNum() : 0;151}152153/**154* @return the string representation of the {@link155* #getDayOfWeekForSimpleTimeZoneInt} value156*/157String getDayOfWeekForSimpleTimeZone() {158int d = getDayOfWeekForSimpleTimeZoneInt();159if (d == 0) {160return "0";161}162String sign = "";163if (d < 0) {164sign = "-";165d = -d;166}167return sign + toString(d);168}169170private static DayOfWeek getDOW(String abbr) {171return abbreviations.get(abbr);172}173174/**175* Converts the specified day of week value to the day-of-week176* name defined in {@link java.util.Calenda}.177* @param dow 1-based day of week value178* @return the Calendar day of week name with "Calendar." prefix.179* @throws IllegalArgumentException if the specified dow value is out of range.180*/181static String toString(int dow) {182if (dow >= DayOfWeek.SUNDAY.value() && dow <= DayOfWeek.SATURDAY.value()) {183return "Calendar." + DayOfWeek.values()[dow - 1];184}185throw new IllegalArgumentException("wrong Day_of_Week number: " + dow);186}187}188189190