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

Print this page




  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 regex pattern for region IDs.
  99      */
 100     private static final Pattern PATTERN = Pattern.compile("[A-Za-z][A-Za-z0-9~/._+-]+");
 101 
 102     /**
 103      * The time-zone ID, not null.
 104      */
 105     private final String id;
 106     /**
 107      * The time-zone rules, null if zone ID was loaded leniently.
 108      */
 109     private final transient ZoneRules rules;
 110 
 111     /**
 112      * Obtains an instance of {@code ZoneRegion} from an identifier without checking
 113      * if the time-zone has available rules.
 114      * <p>
 115      * This method parses the ID and applies any appropriate normalization.
 116      * It does not validate the ID against the known set of IDs for which rules are available.
 117      * <p>
 118      * This method is intended for advanced use cases.
 119      * For example, consider a system that always retrieves time-zone rules from a remote server.
 120      * Using this factory would allow a {@code ZoneRegion}, and thus a {@code ZonedDateTime},
 121      * to be created without loading the rules from the remote server.
 122      *
 123      * @param zoneId  the time-zone ID, not null
 124      * @return the zone ID, not null
 125      * @throws DateTimeException if the ID format is invalid
 126      */
 127     private static ZoneRegion ofLenient(String zoneId) {
 128         return ofId(zoneId, false);
 129     }
 130 
 131     /**
 132      * Obtains an instance of {@code ZoneId} from an identifier.
 133      *
 134      * @param zoneId  the time-zone ID, not null
 135      * @param checkAvailable  whether to check if the zone ID is available
 136      * @return the zone ID, not null
 137      * @throws DateTimeException if the ID format is invalid
 138      * @throws ZoneRulesException if checking availability and the ID cannot be found
 139      */
 140     static ZoneRegion ofId(String zoneId, boolean checkAvailable) {
 141         Objects.requireNonNull(zoneId, "zoneId");
 142         if (zoneId.length() < 2 ||
 143                 zoneId.startsWith("UT") ||  // includes UTC
 144                 zoneId.startsWith("GMT") ||
 145                 (PATTERN.matcher(zoneId).matches() == false)) {
 146             throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId);
 147         }
 148         ZoneRules rules = null;
 149         try {
 150             // always attempt load for better behavior after deserialization
 151             rules = ZoneRulesProvider.getRules(zoneId, true);
 152         } catch (ZoneRulesException ex) {
 153             if (checkAvailable) {
 154                 throw ex;
 155             }
 156         }
 157         return new ZoneRegion(zoneId, rules);
 158     }
 159 







































 160     //-------------------------------------------------------------------------
 161     /**
 162      * Constructor.
 163      *
 164      * @param id  the time-zone ID, not null
 165      * @param rules  the rules, null for lazy lookup
 166      */
 167     ZoneRegion(String id, ZoneRules rules) {
 168         this.id = id;
 169         this.rules = rules;
 170     }
 171 
 172     //-----------------------------------------------------------------------
 173     @Override
 174     public String getId() {
 175         return id;
 176     }
 177 
 178     @Override
 179     public ZoneRules getRules() {


 201      * Defend against malicious streams.
 202      * @return never
 203      * @throws InvalidObjectException always
 204      */
 205     private Object readResolve() throws ObjectStreamException {
 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 ofLenient(id);
 222     }
 223 
 224 }


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


 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 }