Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/tzdb/ZoneOffsetTransitionRule.java
32287 views
/*1* Copyright (c) 2012, 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*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package build.tools.tzdb;6263import static build.tools.tzdb.Utils.*;64import java.util.Objects;6566/**67* A rule expressing how to create a transition.68* <p>69* This class allows rules for identifying future transitions to be expressed.70* A rule might be written in many forms:71* <p><ul>72* <li>the 16th March73* <li>the Sunday on or after the 16th March74* <li>the Sunday on or before the 16th March75* <li>the last Sunday in February76* </ul><p>77* These different rule types can be expressed and queried.78*79* <h3>Specification for implementors</h3>80* This class is immutable and thread-safe.81*82* @since 1.883*/84final class ZoneOffsetTransitionRule {8586/**87* The month of the month-day of the first day of the cutover week.88* The actual date will be adjusted by the dowChange field.89*/90final int month;91/**92* The day-of-month of the month-day of the cutover week.93* If positive, it is the start of the week where the cutover can occur.94* If negative, it represents the end of the week where cutover can occur.95* The value is the number of days from the end of the month, such that96* {@code -1} is the last day of the month, {@code -2} is the second97* to last day, and so on.98*/99final byte dom;100/**101* The cutover day-of-week, -1 to retain the day-of-month.102*/103final int dow;104/**105* The cutover time in the 'before' offset.106*/107final LocalTime time;108/**109* Whether the cutover time is midnight at the end of day.110*/111final boolean timeEndOfDay;112/**113* The definition of how the local time should be interpreted.114*/115final TimeDefinition timeDefinition;116/**117* The standard offset at the cutover.118*/119final ZoneOffset standardOffset;120/**121* The offset before the cutover.122*/123final ZoneOffset offsetBefore;124/**125* The offset after the cutover.126*/127final ZoneOffset offsetAfter;128129/**130* Creates an instance defining the yearly rule to create transitions between two offsets.131*132* @param month the month of the month-day of the first day of the cutover week, from 1 to 12133* @param dayOfMonthIndicator the day of the month-day of the cutover week, positive if the week is that134* day or later, negative if the week is that day or earlier, counting from the last day of the month,135* from -28 to 31 excluding 0136* @param dayOfWeek the required day-of-week, -1 if the month-day should not be changed137* @param time the cutover time in the 'before' offset, not null138* @param timeEndOfDay whether the time is midnight at the end of day139* @param timeDefnition how to interpret the cutover140* @param standardOffset the standard offset in force at the cutover, not null141* @param offsetBefore the offset before the cutover, not null142* @param offsetAfter the offset after the cutover, not null143* @throws IllegalArgumentException if the day of month indicator is invalid144* @throws IllegalArgumentException if the end of day flag is true when the time is not midnight145*/146ZoneOffsetTransitionRule(147int month,148int dayOfMonthIndicator,149int dayOfWeek,150LocalTime time,151boolean timeEndOfDay,152TimeDefinition timeDefnition,153ZoneOffset standardOffset,154ZoneOffset offsetBefore,155ZoneOffset offsetAfter) {156Objects.requireNonNull(time, "time");157Objects.requireNonNull(timeDefnition, "timeDefnition");158Objects.requireNonNull(standardOffset, "standardOffset");159Objects.requireNonNull(offsetBefore, "offsetBefore");160Objects.requireNonNull(offsetAfter, "offsetAfter");161if (month < 1 || month > 12) {162throw new IllegalArgumentException("month must be between 1 and 12");163}164if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) {165throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");166}167if (timeEndOfDay && time.equals(LocalTime.MIDNIGHT) == false) {168throw new IllegalArgumentException("Time must be midnight when end of day flag is true");169}170this.month = month;171this.dom = (byte) dayOfMonthIndicator;172this.dow = dayOfWeek;173this.time = time;174this.timeEndOfDay = timeEndOfDay;175this.timeDefinition = timeDefnition;176this.standardOffset = standardOffset;177this.offsetBefore = offsetBefore;178this.offsetAfter = offsetAfter;179}180181//-----------------------------------------------------------------------182/**183* Checks if this object equals another.184* <p>185* The entire state of the object is compared.186*187* @param otherRule the other object to compare to, null returns false188* @return true if equal189*/190@Override191public boolean equals(Object otherRule) {192if (otherRule == this) {193return true;194}195if (otherRule instanceof ZoneOffsetTransitionRule) {196ZoneOffsetTransitionRule other = (ZoneOffsetTransitionRule) otherRule;197return month == other.month && dom == other.dom && dow == other.dow &&198timeDefinition == other.timeDefinition &&199time.equals(other.time) &&200timeEndOfDay == other.timeEndOfDay &&201standardOffset.equals(other.standardOffset) &&202offsetBefore.equals(other.offsetBefore) &&203offsetAfter.equals(other.offsetAfter);204}205return false;206}207208/**209* Returns a suitable hash code.210*211* @return the hash code212*/213@Override214public int hashCode() {215int hash = ((time.toSecondOfDay() + (timeEndOfDay ? 1 : 0)) << 15) +216(month << 11) + ((dom + 32) << 5) +217((dow == -1 ? 8 : dow) << 2) + (timeDefinition.ordinal());218return hash ^ standardOffset.hashCode() ^219offsetBefore.hashCode() ^ offsetAfter.hashCode();220}221222}223224225