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.ObjectStreamException; 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 import java.util.regex.Pattern; 70 71 /** 72 * A geographical region where the same time-zone rules apply. 73 * <p> 74 * Time-zone information is categorized as a set of rules defining when and 75 * how the offset from UTC/Greenwich changes. These rules are accessed using 76 * identifiers based on geographical regions, such as countries or states. 77 * The most common region classification is the Time Zone Database (TZDB), 78 * which defines regions such as 'Europe/Paris' and 'Asia/Tokyo'. 79 * <p> 80 * The region identifier, modeled by this class, is distinct from the 81 * underlying rules, modeled by {@link ZoneRules}. 82 * The rules are defined by governments and change frequently. 83 * By contrast, the region identifier is well-defined and long-lived. 84 * This separation also allows rules to be shared between regions if appropriate. 85 * 86 * <h3>Specification for implementors</h3> 87 * This class is immutable and thread-safe. 88 * 89 * @since 1.8 90 */ 91 final class ZoneRegion extends ZoneId implements Serializable { 92 93 /** 94 * Serialization version. 95 */ 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 * Obtains an instance of {@code ZoneId} wrapping an offset. 158 * <p> 159 * For example, zone IDs like 'UTC', 'GMT', 'UT' and 'UTC+01:30' will be setup here. 160 * 161 * @param zoneId the time-zone ID, not null 162 * @param offset the offset, not null 163 * @return the zone ID, not null 164 */ 165 static ZoneRegion ofPrefixedOffset(String zoneId, ZoneOffset offset) { 166 return new ZoneRegion(zoneId, offset.getRules()); 167 } 168 169 //------------------------------------------------------------------------- 170 /** 171 * Constructor. 172 * 173 * @param id the time-zone ID, not null 174 * @param rules the rules, null for lazy lookup 175 */ 176 ZoneRegion(String id, ZoneRules rules) { 177 this.id = id; 178 this.rules = rules; 179 } 180 181 //----------------------------------------------------------------------- 182 @Override 183 public String getId() { 184 return id; 185 } 186 187 @Override 188 public ZoneRules getRules() { 189 // additional query for group provider when null allows for possibility 190 // that the provider was updated after the ZoneId was created 191 return (rules != null ? rules : ZoneRulesProvider.getRules(id, false)); 192 } 193 194 //----------------------------------------------------------------------- 195 /** 196 * Writes the object using a 197 * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>. 198 * <pre> 199 * out.writeByte(7); // identifies this as a ZoneId (not ZoneOffset) 200 * out.writeUTF(zoneId); 201 * </pre> 202 * 203 * @return the instance of {@code Ser}, not null 204 */ 205 private Object writeReplace() { 206 return new Ser(Ser.ZONE_REGION_TYPE, this); 207 } 208 209 /** 210 * Defend against malicious streams. 211 * @return never 212 * @throws InvalidObjectException always 213 */ 214 private Object readResolve() throws ObjectStreamException { 215 throw new InvalidObjectException("Deserialization via serialization delegate"); 216 } 217 218 @Override 219 void write(DataOutput out) throws IOException { 220 out.writeByte(Ser.ZONE_REGION_TYPE); 221 writeExternal(out); 222 } 223 224 void writeExternal(DataOutput out) throws IOException { 225 out.writeUTF(id); 226 } 227 228 static ZoneId readExternal(DataInput in) throws IOException { 229 String id = in.readUTF(); 230 return ZoneId.of(id, false); 231 } 232 233 }