Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/tzdb/LocalTime.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) 2007-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.ChronoField.HOUR_OF_DAY;64import static build.tools.tzdb.ChronoField.MINUTE_OF_HOUR;65import static build.tools.tzdb.ChronoField.SECOND_OF_MINUTE;66import static build.tools.tzdb.ChronoField.SECOND_OF_DAY;6768import java.util.Objects;6970/**71* A time without time-zone in the ISO-8601 calendar system,72* such as {@code 10:15:30}.73*74*/75final class LocalTime {7677/**78* The minimum supported {@code LocalTime}, '00:00'.79* This is the time of midnight at the start of the day.80*/81public static final LocalTime MIN;82/**83* The minimum supported {@code LocalTime}, '23:59:59.999999999'.84* This is the time just before midnight at the end of the day.85*/86public static final LocalTime MAX;87/**88* The time of midnight at the start of the day, '00:00'.89*/90public static final LocalTime MIDNIGHT;91/**92* The time of noon in the middle of the day, '12:00'.93*/94public static final LocalTime NOON;95/**96* Constants for the local time of each hour.97*/98private static final LocalTime[] HOURS = new LocalTime[24];99static {100for (int i = 0; i < HOURS.length; i++) {101HOURS[i] = new LocalTime(i, 0, 0);102}103MIDNIGHT = HOURS[0];104NOON = HOURS[12];105MIN = HOURS[0];106MAX = new LocalTime(23, 59, 59);107}108109/**110* Hours per day.111*/112static final int HOURS_PER_DAY = 24;113/**114* Minutes per hour.115*/116static final int MINUTES_PER_HOUR = 60;117/**118* Minutes per day.119*/120static final int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;121/**122* Seconds per minute.123*/124static final int SECONDS_PER_MINUTE = 60;125/**126* Seconds per hour.127*/128static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;129/**130* Seconds per day.131*/132static final int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;133/**134* Milliseconds per day.135*/136static final long MILLIS_PER_DAY = SECONDS_PER_DAY * 1000L;137/**138* Microseconds per day.139*/140static final long MICROS_PER_DAY = SECONDS_PER_DAY * 1000_000L;141142/**143* The hour.144*/145private final byte hour;146/**147* The minute.148*/149private final byte minute;150/**151* The second.152*/153private final byte second;154155/**156* Obtains an instance of {@code LocalTime} from an hour and minute.157* <p>158* The second and nanosecond fields will be set to zero by this factory method.159* <p>160* This factory may return a cached value, but applications must not rely on this.161*162* @param hour the hour-of-day to represent, from 0 to 23163* @param minute the minute-of-hour to represent, from 0 to 59164* @return the local time, not null165* @throws DateTimeException if the value of any field is out of range166*/167public static LocalTime of(int hour, int minute) {168HOUR_OF_DAY.checkValidValue(hour);169if (minute == 0) {170return HOURS[hour]; // for performance171}172MINUTE_OF_HOUR.checkValidValue(minute);173return new LocalTime(hour, minute, 0);174}175176/**177* Obtains an instance of {@code LocalTime} from an hour, minute and second.178* <p>179* The nanosecond field will be set to zero by this factory method.180* <p>181* This factory may return a cached value, but applications must not rely on this.182*183* @param hour the hour-of-day to represent, from 0 to 23184* @param minute the minute-of-hour to represent, from 0 to 59185* @param second the second-of-minute to represent, from 0 to 59186* @return the local time, not null187* @throws DateTimeException if the value of any field is out of range188*/189public static LocalTime of(int hour, int minute, int second) {190HOUR_OF_DAY.checkValidValue(hour);191if ((minute | second) == 0) {192return HOURS[hour]; // for performance193}194MINUTE_OF_HOUR.checkValidValue(minute);195SECOND_OF_MINUTE.checkValidValue(second);196return new LocalTime(hour, minute, second);197}198199/**200* Obtains an instance of {@code LocalTime} from a second-of-day value.201* <p>202* This factory may return a cached value, but applications must not rely on this.203*204* @param secondOfDay the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1}205* @return the local time, not null206* @throws DateTimeException if the second-of-day value is invalid207*/208public static LocalTime ofSecondOfDay(int secondOfDay) {209SECOND_OF_DAY.checkValidValue(secondOfDay);210int hours = secondOfDay / SECONDS_PER_HOUR;211secondOfDay -= hours * SECONDS_PER_HOUR;212int minutes = secondOfDay / SECONDS_PER_MINUTE;213secondOfDay -= minutes * SECONDS_PER_MINUTE;214return create(hours, minutes, secondOfDay);215}216217218/**219* Creates a local time from the hour, minute, second and nanosecond fields.220* <p>221* This factory may return a cached value, but applications must not rely on this.222*223* @param hour the hour-of-day to represent, validated from 0 to 23224* @param minute the minute-of-hour to represent, validated from 0 to 59225* @param second the second-of-minute to represent, validated from 0 to 59226* @return the local time, not null227*/228private static LocalTime create(int hour, int minute, int second) {229if ((minute | second) == 0) {230return HOURS[hour];231}232return new LocalTime(hour, minute, second);233}234235/**236* Constructor, previously validated.237*238* @param hour the hour-of-day to represent, validated from 0 to 23239* @param minute the minute-of-hour to represent, validated from 0 to 59240* @param second the second-of-minute to represent, validated from 0 to 59241*/242private LocalTime(int hour, int minute, int second) {243this.hour = (byte) hour;244this.minute = (byte) minute;245this.second = (byte) second;246}247248/**249* Gets the hour-of-day field.250*251* @return the hour-of-day, from 0 to 23252*/253public int getHour() {254return hour;255}256257/**258* Gets the minute-of-hour field.259*260* @return the minute-of-hour, from 0 to 59261*/262public int getMinute() {263return minute;264}265266/**267* Gets the second-of-minute field.268*269* @return the second-of-minute, from 0 to 59270*/271public int getSecond() {272return second;273}274275/**276* Returns a copy of this {@code LocalTime} with the specified period in seconds added.277* <p>278* This adds the specified number of seconds to this time, returning a new time.279* The calculation wraps around midnight.280* <p>281* This instance is immutable and unaffected by this method call.282*283* @param secondstoAdd the seconds to add, may be negative284* @return a {@code LocalTime} based on this time with the seconds added, not null285*/286public LocalTime plusSeconds(long secondstoAdd) {287if (secondstoAdd == 0) {288return this;289}290int sofd = hour * SECONDS_PER_HOUR +291minute * SECONDS_PER_MINUTE + second;292int newSofd = ((int) (secondstoAdd % SECONDS_PER_DAY) + sofd + SECONDS_PER_DAY) % SECONDS_PER_DAY;293if (sofd == newSofd) {294return this;295}296int newHour = newSofd / SECONDS_PER_HOUR;297int newMinute = (newSofd / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR;298int newSecond = newSofd % SECONDS_PER_MINUTE;299return create(newHour, newMinute, newSecond);300}301302/**303* Returns a copy of this {@code LocalTime} with the specified period in seconds subtracted.304* <p>305* This subtracts the specified number of seconds from this time, returning a new time.306* The calculation wraps around midnight.307* <p>308* This instance is immutable and unaffected by this method call.309*310* @param secondsToSubtract the seconds to subtract, may be negative311* @return a {@code LocalTime} based on this time with the seconds subtracted, not null312*/313public LocalTime minusSeconds(long secondsToSubtract) {314return plusSeconds(-(secondsToSubtract % SECONDS_PER_DAY));315}316317/**318* Extracts the time as seconds of day,319* from {@code 0} to {@code 24 * 60 * 60 - 1}.320*321* @return the second-of-day equivalent to this time322*/323public int toSecondOfDay() {324int total = hour * SECONDS_PER_HOUR;325total += minute * SECONDS_PER_MINUTE;326total += second;327return total;328}329330/**331* Compares this {@code LocalTime} to another time.332* <p>333* The comparison is based on the time-line position of the local times within a day.334* It is "consistent with equals", as defined by {@link Comparable}.335*336* @param other the other time to compare to, not null337* @return the comparator value, negative if less, positive if greater338* @throws NullPointerException if {@code other} is null339*/340public int compareTo(LocalTime other) {341int cmp = Integer.compare(hour, other.hour);342if (cmp == 0) {343cmp = Integer.compare(minute, other.minute);344if (cmp == 0) {345cmp = Integer.compare(second, other.second);346}347}348return cmp;349}350351/**352* Checks if this time is equal to another time.353* <p>354* The comparison is based on the time-line position of the time within a day.355* <p>356* Only objects of type {@code LocalTime} are compared, other types return false.357* To compare the date of two {@code TemporalAccessor} instances, use358* {@link ChronoField#NANO_OF_DAY} as a comparator.359*360* @param obj the object to check, null returns false361* @return true if this is equal to the other time362*/363@Override364public boolean equals(Object obj) {365if (this == obj) {366return true;367}368if (obj instanceof LocalTime) {369LocalTime other = (LocalTime) obj;370return hour == other.hour && minute == other.minute &&371second == other.second;372}373return false;374}375376/**377* A hash code for this time.378*379* @return a suitable hash code380*/381@Override382public int hashCode() {383long sod = toSecondOfDay();384return (int) (sod ^ (sod >>> 32));385}386387}388389390