Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/tzdb/Utils.java
32287 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* Copyright (c) 2009-2012, Stephen Colebourne & Michael Nascimento Santos26*27* All rights reserved.28*29* Redistribution and use in source and binary forms, with or without30* modification, are permitted provided that the following conditions are met:31*32* * Redistributions of source code must retain the above copyright notice,33* this list of conditions and the following disclaimer.34*35* * Redistributions in binary form must reproduce the above copyright notice,36* this list of conditions and the following disclaimer in the documentation37* and/or other materials provided with the distribution.38*39* * Neither the name of JSR-310 nor the names of its contributors40* may be used to endorse or promote products derived from this software41* without specific prior written permission.42*43* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS44* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT45* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR46* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR47* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,48* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,49* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR50* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF51* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING52* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS53* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.54*/55package build.tools.tzdb;5657import java.util.Objects;5859class Utils {6061// Returns the largest (closest to positive infinity)62public static long floorDiv(long x, long y) {63long r = x / y;64// if the signs are different and modulo not zero, round down65if ((x ^ y) < 0 && (r * y != x)) {66r--;67}68return r;69}7071// Returns the floor modulus of the {@code long} arguments.72public static long floorMod(long x, long y) {73return x - floorDiv(x, y) * y;74}7576// Returns the sum of its arguments,77public static long addExact(long x, long y) {78long r = x + y;79// HD 2-12 Overflow iff both arguments have the opposite sign of the result80if (((x ^ r) & (y ^ r)) < 0) {81throw new ArithmeticException("long overflow");82}83return r;84}8586// Year8788// Returns true if the specified year is a leap year.89public static boolean isLeapYear(int year) {90return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0);91}9293// The minimum supported year, '-999,999,999'.94public static final int YEAR_MIN_VALUE = -999_999_999;9596// The maximum supported year, '+999,999,999'.97public static final int YEAR_MAX_VALUE = 999_999_999;9899100// Gets the length of the specified month in days.101public static int lengthOfMonth(int month, boolean leapYear) {102switch (month) {103case 2: //FEBRUARY:104return (leapYear ? 29 : 28);105case 4: //APRIL:106case 6: //JUNE:107case 9: //SEPTEMBER:108case 11: //NOVEMBER:109return 30;110default:111return 31;112}113}114115// Gets the maximum length of the specified month in days.116public static int maxLengthOfMonth(int month) {117switch (month) {118case 2: //FEBRUARY:119return 29;120case 4: //APRIL:121case 6: //JUNE:122case 9: //SEPTEMBER:123case 11: //NOVEMBER:124return 30;125default:126return 31;127}128}129130// DayOfWeek131132// Returns the day-of-week that is the specified number of days after133// this one, from 1 to 7 for Monday to Sunday.134public static int plusDayOfWeek(int dow, long days) {135int amount = (int) (days % 7);136return (dow - 1 + (amount + 7)) % 7 + 1;137}138139// Returns the day-of-week that is the specified number of days before140// this one, from 1 to 7 for Monday to Sunday.141public static int minusDayOfWeek(int dow, long days) {142return plusDayOfWeek(dow, -(days % 7));143}144145// Adjusts the date to the first occurrence of the specified day-of-week146// before the date being adjusted unless it is already on that day in147// which case the same object is returned.148public static LocalDate previousOrSame(LocalDate date, int dayOfWeek) {149return adjust(date, dayOfWeek, 1);150}151152// Adjusts the date to the first occurrence of the specified day-of-week153// after the date being adjusted unless it is already on that day in154// which case the same object is returned.155public static LocalDate nextOrSame(LocalDate date, int dayOfWeek) {156return adjust(date, dayOfWeek, 0);157}158159// Implementation of next, previous or current day-of-week.160// @param relative whether the current date is a valid answer161private static final LocalDate adjust(LocalDate date, int dow, int relative) {162int calDow = date.getDayOfWeek();163if (relative < 2 && calDow == dow) {164return date;165}166if ((relative & 1) == 0) {167int daysDiff = calDow - dow;168return date.plusDays(daysDiff >= 0 ? 7 - daysDiff : -daysDiff);169} else {170int daysDiff = dow - calDow;171return date.minusDays(daysDiff >= 0 ? 7 - daysDiff : -daysDiff);172}173}174175}176177178