Path: blob/master/test/jdk/sun/util/calendar/zi/RuleDay.java
41153 views
/*1* Copyright (c) 2000, 2018, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;24import java.util.HashMap;25import java.util.List;26import java.util.Map;2728/**29* RuleDay class represents the value of the "ON" field. The day of30* week values start from 1 following the {@link java.util.Calendar}31* convention.32*33* @since 1.434*/35class RuleDay {36private static final Map<String,DayOfWeek> abbreviations = new HashMap<String,DayOfWeek>(7);37static {38for (DayOfWeek day : DayOfWeek.values()) {39abbreviations.put(day.getAbbr(), day);40}41}4243private String dayName = null;44private DayOfWeek dow;45private boolean lastOne = false;46private int soonerOrLater = 0;47private int thanDayOfMonth; // day of month (e.g., 8 for "Sun>=8")4849RuleDay() {50}5152RuleDay(int day) {53thanDayOfMonth = day;54}5556int getDay() {57return thanDayOfMonth;58}5960/**61* @return the day of week value (1-based)62*/63int getDayOfWeekNum() {64return dow.value();65}6667/**68* @return true if this rule day represents the last day of69* week. (e.g., lastSun).70*/71boolean isLast() {72return lastOne;73}7475/**76* @return true if this rule day represents the day of week on or77* later than (after) the {@link #getDay}. (e.g., Sun>=1)78*/79boolean isLater() {80return soonerOrLater > 0;81}8283/**84* @return true if this rule day represents the day of week on or85* earlier than (before) the {@link #getDay}. (e.g., Sun<=15)86*/87boolean isEarlier() {88return soonerOrLater < 0;89}9091/**92* @return true if this rule day represents an exact day.93*/94boolean isExact() {95return soonerOrLater == 0;96}9798/**99* Parses the "ON" field and constructs a RuleDay.100* @param day an "ON" field string (e.g., "Sun>=1")101* @return a RuleDay representing the given "ON" field102*/103static RuleDay parse(String day) {104RuleDay d = new RuleDay();105if (day.startsWith("last")) {106d.lastOne = true;107d.dayName = day.substring(4);108d.dow = getDOW(d.dayName);109} else {110int index;111if ((index = day.indexOf(">=")) != -1) {112d.dayName = day.substring(0, index);113d.dow = getDOW(d.dayName);114d.soonerOrLater = 1; // greater or equal115d.thanDayOfMonth = Integer.parseInt(day.substring(index+2));116} else if ((index = day.indexOf("<=")) != -1) {117d.dayName = day.substring(0, index);118d.dow = getDOW(d.dayName);119d.soonerOrLater = -1; // less or equal120d.thanDayOfMonth = Integer.parseInt(day.substring(index+2));121} else {122// it should be an integer value.123d.thanDayOfMonth = Integer.parseInt(day);124}125}126return d;127}128129/**130* Converts this RuleDay to the SimpleTimeZone day rule.131* @return the converted SimpleTimeZone day rule132*/133int getDayForSimpleTimeZone() {134if (isLast()) {135return -1;136}137return isEarlier() ? -getDay() : getDay();138}139140/**141* Converts this RuleDay to the SimpleTimeZone day-of-week rule.142* @return the SimpleTimeZone day-of-week rule value143*/144int getDayOfWeekForSimpleTimeZoneInt() {145if (isEarlier() || isLater()) {146return -getDayOfWeekNum();147}148return isLast() ? getDayOfWeekNum() : 0;149}150151/**152* @return the string representation of the {@link153* #getDayOfWeekForSimpleTimeZoneInt} value154*/155String getDayOfWeekForSimpleTimeZone() {156int d = getDayOfWeekForSimpleTimeZoneInt();157if (d == 0) {158return "0";159}160String sign = "";161if (d < 0) {162sign = "-";163d = -d;164}165return sign + toString(d);166}167168private static DayOfWeek getDOW(String abbr) {169return abbreviations.get(abbr);170}171172/**173* Converts the specified day of week value to the day-of-week174* name defined in {@link java.util.Calenda}.175* @param dow 1-based day of week value176* @return the Calendar day of week name with "Calendar." prefix.177* @throws IllegalArgumentException if the specified dow value is out of range.178*/179static String toString(int dow) {180if (dow >= DayOfWeek.SUNDAY.value() && dow <= DayOfWeek.SATURDAY.value()) {181return "Calendar." + DayOfWeek.values()[dow - 1];182}183throw new IllegalArgumentException("wrong Day_of_Week number: " + dow);184}185}186187188