< prev index next >

src/java.base/share/classes/java/time/ZoneRegion.java

Print this page




  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) {


 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="{@docRoot}/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      * @param s the stream to read
 200      * @throws InvalidObjectException always
 201      */

 202     private void readObject(ObjectInputStream s) throws InvalidObjectException {
 203         throw new InvalidObjectException("Deserialization via serialization delegate");
 204     }
 205 
 206     @Override
 207     void write(DataOutput out) throws IOException {
 208         out.writeByte(Ser.ZONE_REGION_TYPE);
 209         writeExternal(out);
 210     }
 211 
 212     void writeExternal(DataOutput out) throws IOException {
 213         out.writeUTF(id);
 214     }
 215 
 216     static ZoneId readExternal(DataInput in) throws IOException {
 217         String id = in.readUTF();
 218         return ZoneId.of(id, false);
 219     }
 220 
 221 }


  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) {


 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 }
< prev index next >