View Javadoc
1   /*
2    * Copyright (c) 2012, 2013, 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      private static final long serialVersionUID = 8386373296231747096L;
96      /**
97       * The time-zone ID, not null.
98       */
99      private final String id;
100     /**
101      * The time-zone rules, null if zone ID was loaded leniently.
102      */
103     private final transient ZoneRules rules;
104 
105     /**
106      * Obtains an instance of {@code ZoneId} from an identifier.
107      *
108      * @param zoneId  the time-zone ID, not null
109      * @param checkAvailable  whether to check if the zone ID is available
110      * @return the zone ID, not null
111      * @throws DateTimeException if the ID format is invalid
112      * @throws ZoneRulesException if checking availability and the ID cannot be found
113      */
114     static ZoneRegion ofId(String zoneId, boolean checkAvailable) {
115         Objects.requireNonNull(zoneId, "zoneId");
116         checkName(zoneId);
117         ZoneRules rules = null;
118         try {
119             // always attempt load for better behavior after deserialization
120             rules = ZoneRulesProvider.getRules(zoneId, true);
121         } catch (ZoneRulesException ex) {
122             if (checkAvailable) {
123                 throw ex;
124             }
125         }
126         return new ZoneRegion(zoneId, rules);
127     }
128 
129     /**
130      * Checks that the given string is a legal ZondId name.
131      *
132      * @param zoneId  the time-zone ID, not null
133      * @throws DateTimeException if the ID format is invalid
134      */
135     private static void checkName(String zoneId) {
136         int n = zoneId.length();
137         if (n < 2) {
138            throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);
139         }
140         for (int i = 0; i < n; i++) {
141             char c = zoneId.charAt(i);
142             if (c >= 'a' && c <= 'z') continue;
143             if (c >= 'A' && c <= 'Z') continue;
144             if (c == '/' && i != 0) continue;
145             if (c >= '0' && c <= '9' && i != 0) continue;
146             if (c == '~' && 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             throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);
152         }
153     }
154 
155     //-------------------------------------------------------------------------
156     /**
157      * Constructor.
158      *
159      * @param id  the time-zone ID, not null
160      * @param rules  the rules, null for lazy lookup
161      */
162     ZoneRegion(String id, ZoneRules rules) {
163         this.id = id;
164         this.rules = rules;
165     }
166 
167     //-----------------------------------------------------------------------
168     @Override
169     public String getId() {
170         return id;
171     }
172 
173     @Override
174     public ZoneRules getRules() {
175         // additional query for group provider when null allows for possibility
176         // that the provider was updated after the ZoneId was created
177         return (rules != null ? rules : ZoneRulesProvider.getRules(id, false));
178     }
179 
180     //-----------------------------------------------------------------------
181     /**
182      * Writes the object using a
183      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
184      * @serialData
185      * <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 null
191      */
192     private Object writeReplace() {
193         return new Ser(Ser.ZONE_REGION_TYPE, this);
194     }
195 
196     /**
197      * Defend against malicious streams.
198      *
199      * @throws InvalidObjectException always
200      */
201     private void readObject(ObjectInputStream s) throws InvalidObjectException {
202         throw new InvalidObjectException("Deserialization via serialization delegate");
203     }
204 
205     @Override
206     void write(DataOutput out) throws IOException {
207         out.writeByte(Ser.ZONE_REGION_TYPE);
208         writeExternal(out);
209     }
210 
211     void writeExternal(DataOutput out) throws IOException {
212         out.writeUTF(id);
213     }
214 
215     static ZoneId readExternal(DataInput in) throws IOException {
216         String id = in.readUTF();
217         return ZoneId.of(id, false);
218     }
219 
220 }