1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * This file is available under and governed by the GNU General Public 28 * License version 2 only, as published by the Free Software Foundation. 29 * However, the following notice accompanied the original version of this 30 * file: 31 * 32 * Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos 33 * 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions are met: 38 * 39 * * Redistributions of source code must retain the above copyright notice, 40 * this list of conditions and the following disclaimer. 41 * 42 * * Redistributions in binary form must reproduce the above copyright notice, 43 * this list of conditions and the following disclaimer in the documentation 44 * and/or other materials provided with the distribution. 45 * 46 * * Neither the name of JSR-310 nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 package build.tools.tzdb; 63 64 import static build.tools.tzdb.Utils.*; 65 import java.util.Objects; 66 67 /** 68 * A rule expressing how to create a transition. 69 * <p> 70 * This class allows rules for identifying future transitions to be expressed. 71 * A rule might be written in many forms: 72 * <p><ul> 73 * <li>the 16th March 74 * <li>the Sunday on or after the 16th March 75 * <li>the Sunday on or before the 16th March 76 * <li>the last Sunday in February 77 * </ul><p> 78 * These different rule types can be expressed and queried. 79 * 80 * <h3>Specification for implementors</h3> 81 * This class is immutable and thread-safe. 82 * 83 * @since 1.8 84 */ 85 final class ZoneOffsetTransitionRule { 86 87 /** 88 * The month of the month-day of the first day of the cutover week. 89 * The actual date will be adjusted by the dowChange field. 90 */ 91 final int month; 92 /** 93 * The day-of-month of the month-day of the cutover week. 94 * If positive, it is the start of the week where the cutover can occur. 95 * If negative, it represents the end of the week where cutover can occur. 96 * The value is the number of days from the end of the month, such that 97 * {@code -1} is the last day of the month, {@code -2} is the second 98 * to last day, and so on. 99 */ 100 final byte dom; 101 /** 102 * The cutover day-of-week, -1 to retain the day-of-month. 103 */ 104 final int dow; 105 /** 106 * The cutover time in the 'before' offset. 107 */ 108 final LocalTime time; 109 /** 110 * Whether the cutover time is midnight at the end of day. 111 */ 112 final boolean timeEndOfDay; 113 /** 114 * The definition of how the local time should be interpreted. 115 */ 116 final TimeDefinition timeDefinition; 117 /** 118 * The standard offset at the cutover. 119 */ 120 final ZoneOffset standardOffset; 121 /** 122 * The offset before the cutover. 123 */ 124 final ZoneOffset offsetBefore; 125 /** 126 * The offset after the cutover. 127 */ 128 final ZoneOffset offsetAfter; 129 130 /** 131 * Creates an instance defining the yearly rule to create transitions between two offsets. 132 * 133 * @param month the month of the month-day of the first day of the cutover week, from 1 to 12 134 * @param dayOfMonthIndicator the day of the month-day of the cutover week, positive if the week is that 135 * day or later, negative if the week is that day or earlier, counting from the last day of the month, 136 * from -28 to 31 excluding 0 137 * @param dayOfWeek the required day-of-week, -1 if the month-day should not be changed 138 * @param time the cutover time in the 'before' offset, not null 139 * @param timeEndOfDay whether the time is midnight at the end of day 140 * @param timeDefnition how to interpret the cutover 141 * @param standardOffset the standard offset in force at the cutover, not null 142 * @param offsetBefore the offset before the cutover, not null 143 * @param offsetAfter the offset after the cutover, not null 144 * @throws IllegalArgumentException if the day of month indicator is invalid 145 * @throws IllegalArgumentException if the end of day flag is true when the time is not midnight 146 */ 147 ZoneOffsetTransitionRule( 148 int month, 149 int dayOfMonthIndicator, 150 int dayOfWeek, 151 LocalTime time, 152 boolean timeEndOfDay, 153 TimeDefinition timeDefnition, 154 ZoneOffset standardOffset, 155 ZoneOffset offsetBefore, 156 ZoneOffset offsetAfter) { 157 Objects.requireNonNull(time, "time"); 158 Objects.requireNonNull(timeDefnition, "timeDefnition"); 159 Objects.requireNonNull(standardOffset, "standardOffset"); 160 Objects.requireNonNull(offsetBefore, "offsetBefore"); 161 Objects.requireNonNull(offsetAfter, "offsetAfter"); 162 if (month < 1 || month > 12) { 163 throw new IllegalArgumentException("month must be between 1 and 12"); 164 } 165 if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) { 166 throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero"); 167 } 168 if (timeEndOfDay && time.equals(LocalTime.MIDNIGHT) == false) { 169 throw new IllegalArgumentException("Time must be midnight when end of day flag is true"); 170 } 171 this.month = month; 172 this.dom = (byte) dayOfMonthIndicator; 173 this.dow = dayOfWeek; 174 this.time = time; 175 this.timeEndOfDay = timeEndOfDay; 176 this.timeDefinition = timeDefnition; 177 this.standardOffset = standardOffset; 178 this.offsetBefore = offsetBefore; 179 this.offsetAfter = offsetAfter; 180 } 181 182 //----------------------------------------------------------------------- 183 /** 184 * Checks if this object equals another. 185 * <p> 186 * The entire state of the object is compared. 187 * 188 * @param otherRule the other object to compare to, null returns false 189 * @return true if equal 190 */ 191 @Override 192 public boolean equals(Object otherRule) { 193 if (otherRule == this) { 194 return true; 195 } 196 if (otherRule instanceof ZoneOffsetTransitionRule) { 197 ZoneOffsetTransitionRule other = (ZoneOffsetTransitionRule) otherRule; 198 return month == other.month && dom == other.dom && dow == other.dow && 199 timeDefinition == other.timeDefinition && 200 time.equals(other.time) && 201 timeEndOfDay == other.timeEndOfDay && 202 standardOffset.equals(other.standardOffset) && 203 offsetBefore.equals(other.offsetBefore) && 204 offsetAfter.equals(other.offsetAfter); 205 } 206 return false; 207 } 208 209 /** 210 * Returns a suitable hash code. 211 * 212 * @return the hash code 213 */ 214 @Override 215 public int hashCode() { 216 int hash = ((time.toSecondOfDay() + (timeEndOfDay ? 1 : 0)) << 15) + 217 (month << 11) + ((dom + 32) << 5) + 218 ((dow == -1 ? 8 : dow) << 2) + (timeDefinition.ordinal()); 219 return hash ^ standardOffset.hashCode() ^ 220 offsetBefore.hashCode() ^ offsetAfter.hashCode(); 221 } 222 223 }