1 /*
   2  * Copyright (c) 2012, 2018, 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  * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
  28  *
  29  * All rights reserved.
  30  *
  31  * Redistribution and use in source and binary forms, with or without
  32  * modification, are permitted provided that the following conditions are met:
  33  *
  34  *  * Redistributions of source code must retain the above copyright notice,
  35  *    this list of conditions and the following disclaimer.
  36  *
  37  *  * Redistributions in binary form must reproduce the above copyright notice,
  38  *    this list of conditions and the following disclaimer in the documentation
  39  *    and/or other materials provided with the distribution.
  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package java.time;
  58 
  59 import java.io.DataInput;
  60 import java.io.DataOutput;
  61 import java.io.IOException;
  62 import java.io.InvalidObjectException;
  63 import java.io.ObjectInputStream;
  64 import java.io.Serializable;
  65 import java.time.zone.ZoneRules;
  66 import java.time.zone.ZoneRulesException;
  67 import java.time.zone.ZoneRulesProvider;
  68 import java.util.Objects;
  69 
  70 /**
  71  * A geographical region where the same time-zone rules apply.
  72  * <p>
  73  * Time-zone information is categorized as a set of rules defining when and
  74  * how the offset from UTC/Greenwich changes. These rules are accessed using
  75  * identifiers based on geographical regions, such as countries or states.
  76  * The most common region classification is the Time Zone Database (TZDB),
  77  * which defines regions such as 'Europe/Paris' and 'Asia/Tokyo'.
  78  * <p>
  79  * The region identifier, modeled by this class, is distinct from the
  80  * underlying rules, modeled by {@link ZoneRules}.
  81  * The rules are defined by governments and change frequently.
  82  * By contrast, the region identifier is well-defined and long-lived.
  83  * This separation also allows rules to be shared between regions if appropriate.
  84  *
  85  * @implSpec
  86  * This class is immutable and thread-safe.
  87  *
  88  * @since 1.8
  89  */
  90 final class ZoneRegion extends ZoneId implements Serializable {
  91 
  92     /**
  93      * Serialization version.
  94      */
  95     @java.io.Serial
  96     private static final long serialVersionUID = 8386373296231747096L;
  97     /**
  98      * The time-zone ID, not null.
  99      */
 100     private final String id;
 101     /**
 102      * The time-zone rules, null if zone ID was loaded leniently.
 103      */
 104     private final transient ZoneRules rules;
 105 
 106     /**
 107      * Obtains an instance of {@code ZoneId} from an identifier.
 108      *
 109      * @param zoneId  the time-zone ID, not null
 110      * @param checkAvailable  whether to check if the zone ID is available
 111      * @return the zone ID, not null
 112      * @throws DateTimeException if the ID format is invalid
 113      * @throws ZoneRulesException if checking availability and the ID cannot be found
 114      */
 115     static ZoneRegion ofId(String zoneId, boolean checkAvailable) {
 116         Objects.requireNonNull(zoneId, "zoneId");
 117         checkName(zoneId);
 118         ZoneRules rules = null;
 119         try {
 120             // always attempt load for better behavior after deserialization
 121             rules = ZoneRulesProvider.getRules(zoneId, true);
 122         } catch (ZoneRulesException ex) {
 123             if (checkAvailable) {
 124                 throw ex;
 125             }
 126         }
 127         return new ZoneRegion(zoneId, rules);
 128     }
 129 
 130     /**
 131      * Checks that the given string is a legal ZondId name.
 132      *
 133      * @param zoneId  the time-zone ID, not null
 134      * @throws DateTimeException if the ID format is invalid
 135      */
 136     private static void checkName(String zoneId) {
 137         int n = zoneId.length();
 138         if (n < 2) {
 139            throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);
 140         }
 141         for (int i = 0; i < n; i++) {
 142             char c = zoneId.charAt(i);
 143             if (c >= 'a' && c <= 'z') continue;
 144             if (c >= 'A' && c <= 'Z') continue;
 145             if (c == '/' && i != 0) continue;
 146             if (c >= '0' && c <= '9' && i != 0) continue;
 147             if (c == '~' && i != 0) continue;
 148             if (c == '.' && i != 0) continue;
 149             if (c == '_' && i != 0) continue;
 150             if (c == '+' && i != 0) continue;
 151             if (c == '-' && i != 0) continue;
 152             throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);
 153         }
 154     }
 155 
 156     //-------------------------------------------------------------------------
 157     /**
 158      * Constructor.
 159      *
 160      * @param id  the time-zone ID, not null
 161      * @param rules  the rules, null for lazy lookup
 162      */
 163     ZoneRegion(String id, ZoneRules rules) {
 164         this.id = id;
 165         this.rules = rules;
 166     }
 167 
 168     //-----------------------------------------------------------------------
 169     @Override
 170     public String getId() {
 171         return id;
 172     }
 173 
 174     @Override
 175     public ZoneRules getRules() {
 176         // additional query for group provider when null allows for possibility
 177         // that the provider was updated after the ZoneId was created
 178         return (rules != null ? rules : ZoneRulesProvider.getRules(id, false));
 179     }
 180 
 181     //-----------------------------------------------------------------------
 182     /**
 183      * Writes the object using a
 184      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
 185      * @serialData
 186      * <pre>
 187      *  out.writeByte(7);  // identifies a ZoneId (not ZoneOffset)
 188      *  out.writeUTF(zoneId);
 189      * </pre>
 190      *
 191      * @return the instance of {@code Ser}, not null
 192      */
 193     @java.io.Serial
 194     private Object writeReplace() {
 195         return new Ser(Ser.ZONE_REGION_TYPE, this);
 196     }
 197 
 198     /**
 199      * Defend against malicious streams.
 200      *
 201      * @param s the stream to read
 202      * @throws InvalidObjectException always
 203      */
 204     @java.io.Serial
 205     private void readObject(ObjectInputStream s) throws InvalidObjectException {
 206         throw new InvalidObjectException("Deserialization via serialization delegate");
 207     }
 208 
 209     @Override
 210     void write(DataOutput out) throws IOException {
 211         out.writeByte(Ser.ZONE_REGION_TYPE);
 212         writeExternal(out);
 213     }
 214 
 215     void writeExternal(DataOutput out) throws IOException {
 216         out.writeUTF(id);
 217     }
 218 
 219     static ZoneId readExternal(DataInput in) throws IOException {
 220         String id = in.readUTF();
 221         return ZoneId.of(id, false);
 222     }
 223 
 224 }