Path: blob/master/src/java.base/share/classes/java/time/ZoneRegion.java
41152 views
/*1* Copyright (c) 2012, 2019, 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*/94@java.io.Serial95private static final long serialVersionUID = 8386373296231747096L;96/**97* The time-zone ID, not null.98*/99private final String id;100/**101* The time-zone rules, null if zone ID was loaded leniently.102*/103private final transient ZoneRules rules;104105/**106* Obtains an instance of {@code ZoneId} from an identifier.107*108* @param zoneId the time-zone ID, not null109* @param checkAvailable whether to check if the zone ID is available110* @return the zone ID, not null111* @throws DateTimeException if the ID format is invalid112* @throws ZoneRulesException if checking availability and the ID cannot be found113*/114static ZoneRegion ofId(String zoneId, boolean checkAvailable) {115Objects.requireNonNull(zoneId, "zoneId");116checkName(zoneId);117ZoneRules rules = null;118try {119// always attempt load for better behavior after deserialization120rules = ZoneRulesProvider.getRules(zoneId, true);121} catch (ZoneRulesException ex) {122if (checkAvailable) {123throw ex;124}125}126return new ZoneRegion(zoneId, rules);127}128129/**130* Checks that the given string is a legal ZondId name.131*132* @param zoneId the time-zone ID, not null133* @throws DateTimeException if the ID format is invalid134*/135private static void checkName(String zoneId) {136int n = zoneId.length();137if (n < 2) {138throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);139}140for (int i = 0; i < n; i++) {141char c = zoneId.charAt(i);142if (c >= 'a' && c <= 'z') continue;143if (c >= 'A' && c <= 'Z') continue;144if (c == '/' && i != 0) continue;145if (c >= '0' && c <= '9' && i != 0) continue;146if (c == '~' && i != 0) continue;147if (c == '.' && i != 0) continue;148if (c == '_' && i != 0) continue;149if (c == '+' && i != 0) continue;150if (c == '-' && i != 0) continue;151throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);152}153}154155//-------------------------------------------------------------------------156/**157* Constructor.158*159* @param id the time-zone ID, not null160* @param rules the rules, null for lazy lookup161*/162ZoneRegion(String id, ZoneRules rules) {163this.id = id;164this.rules = rules;165}166167//-----------------------------------------------------------------------168@Override169public String getId() {170return id;171}172173@Override174public ZoneRules getRules() {175// additional query for group provider when null allows for possibility176// that the provider was updated after the ZoneId was created177return (rules != null ? rules : ZoneRulesProvider.getRules(id, false));178}179180//-----------------------------------------------------------------------181/**182* Writes the object using a183* <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.184* @serialData185* <pre>186* out.writeByte(7); // identifies a ZoneId (not ZoneOffset)187* out.writeUTF(zoneId);188* </pre>189*190* @return the instance of {@code Ser}, not null191*/192@java.io.Serial193private Object writeReplace() {194return new Ser(Ser.ZONE_REGION_TYPE, this);195}196197/**198* Defend against malicious streams.199*200* @param s the stream to read201* @throws InvalidObjectException always202*/203@java.io.Serial204private void readObject(ObjectInputStream s) throws InvalidObjectException {205throw new InvalidObjectException("Deserialization via serialization delegate");206}207208@Override209void write(DataOutput out) throws IOException {210out.writeByte(Ser.ZONE_REGION_TYPE);211writeExternal(out);212}213214void writeExternal(DataOutput out) throws IOException {215out.writeUTF(id);216}217218static ZoneId readExternal(DataInput in) throws IOException {219String id = in.readUTF();220return ZoneId.of(id, false);221}222223}224225226