Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/ZoneRegion.java
38829 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* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos27*28* All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions are met:32*33* * Redistributions of source code must retain the above copyright notice,34* this list of conditions and the following disclaimer.35*36* * Redistributions in binary form must reproduce the above copyright notice,37* this list of conditions and the following disclaimer in the documentation38* and/or other materials provided with the distribution.39*40* * Neither the name of JSR-310 nor the names of its contributors41* may be used to endorse or promote products derived from this software42* without specific prior written permission.43*44* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS45* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT46* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR47* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR48* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,49* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,50* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR51* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF52* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING53* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS54* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.55*/56package java.time;5758import java.io.DataInput;59import java.io.DataOutput;60import java.io.IOException;61import java.io.InvalidObjectException;62import java.io.ObjectInputStream;63import java.io.Serializable;64import java.time.zone.ZoneRules;65import java.time.zone.ZoneRulesException;66import java.time.zone.ZoneRulesProvider;67import java.util.Objects;6869/**70* A geographical region where the same time-zone rules apply.71* <p>72* Time-zone information is categorized as a set of rules defining when and73* how the offset from UTC/Greenwich changes. These rules are accessed using74* identifiers based on geographical regions, such as countries or states.75* The most common region classification is the Time Zone Database (TZDB),76* which defines regions such as 'Europe/Paris' and 'Asia/Tokyo'.77* <p>78* The region identifier, modeled by this class, is distinct from the79* underlying rules, modeled by {@link ZoneRules}.80* The rules are defined by governments and change frequently.81* By contrast, the region identifier is well-defined and long-lived.82* This separation also allows rules to be shared between regions if appropriate.83*84* @implSpec85* This class is immutable and thread-safe.86*87* @since 1.888*/89final class ZoneRegion extends ZoneId implements Serializable {9091/**92* Serialization version.93*/94private static final long serialVersionUID = 8386373296231747096L;95/**96* The time-zone ID, not null.97*/98private final String id;99/**100* The time-zone rules, null if zone ID was loaded leniently.101*/102private final transient ZoneRules rules;103104/**105* Obtains an instance of {@code ZoneId} from an identifier.106*107* @param zoneId the time-zone ID, not null108* @param checkAvailable whether to check if the zone ID is available109* @return the zone ID, not null110* @throws DateTimeException if the ID format is invalid111* @throws ZoneRulesException if checking availability and the ID cannot be found112*/113static ZoneRegion ofId(String zoneId, boolean checkAvailable) {114Objects.requireNonNull(zoneId, "zoneId");115checkName(zoneId);116ZoneRules rules = null;117try {118// always attempt load for better behavior after deserialization119rules = ZoneRulesProvider.getRules(zoneId, true);120} catch (ZoneRulesException ex) {121if (checkAvailable) {122throw ex;123}124}125return new ZoneRegion(zoneId, rules);126}127128/**129* Checks that the given string is a legal ZondId name.130*131* @param zoneId the time-zone ID, not null132* @throws DateTimeException if the ID format is invalid133*/134private static void checkName(String zoneId) {135int n = zoneId.length();136if (n < 2) {137throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);138}139for (int i = 0; i < n; i++) {140char c = zoneId.charAt(i);141if (c >= 'a' && c <= 'z') continue;142if (c >= 'A' && c <= 'Z') continue;143if (c == '/' && i != 0) continue;144if (c >= '0' && c <= '9' && i != 0) continue;145if (c == '~' && i != 0) continue;146if (c == '.' && i != 0) continue;147if (c == '_' && i != 0) continue;148if (c == '+' && i != 0) continue;149if (c == '-' && i != 0) continue;150throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);151}152}153154//-------------------------------------------------------------------------155/**156* Constructor.157*158* @param id the time-zone ID, not null159* @param rules the rules, null for lazy lookup160*/161ZoneRegion(String id, ZoneRules rules) {162this.id = id;163this.rules = rules;164}165166//-----------------------------------------------------------------------167@Override168public String getId() {169return id;170}171172@Override173public ZoneRules getRules() {174// additional query for group provider when null allows for possibility175// that the provider was updated after the ZoneId was created176return (rules != null ? rules : ZoneRulesProvider.getRules(id, false));177}178179//-----------------------------------------------------------------------180/**181* Writes the object using a182* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.183* @serialData184* <pre>185* out.writeByte(7); // identifies a ZoneId (not ZoneOffset)186* out.writeUTF(zoneId);187* </pre>188*189* @return the instance of {@code Ser}, not null190*/191private Object writeReplace() {192return new Ser(Ser.ZONE_REGION_TYPE, this);193}194195/**196* Defend against malicious streams.197*198* @param s the stream to read199* @throws InvalidObjectException always200*/201private void readObject(ObjectInputStream s) throws InvalidObjectException {202throw new InvalidObjectException("Deserialization via serialization delegate");203}204205@Override206void write(DataOutput out) throws IOException {207out.writeByte(Ser.ZONE_REGION_TYPE);208writeExternal(out);209}210211void writeExternal(DataOutput out) throws IOException {212out.writeUTF(id);213}214215static ZoneId readExternal(DataInput in) throws IOException {216String id = in.readUTF();217return ZoneId.of(id, false);218}219220}221222223