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

Print this page




  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 IDsfor 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 DateTimeException 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 || zoneId.startsWith("UTC") ||
 143                 zoneId.startsWith("GMT") || (PATTERN.matcher(zoneId).matches() == false)) {
 144             throw new DateTimeException("ZoneId format is not a valid region format");


 145         }
 146         ZoneRules rules = null;
 147         try {
 148             // always attempt load for better behavior after deserialization
 149             rules = ZoneRulesProvider.getRules(zoneId);
 150         } catch (ZoneRulesException ex) {
 151             if (checkAvailable) {
 152                 throw ex;
 153             }
 154         }
 155         return new ZoneRegion(zoneId, rules);
 156     }
 157 
 158     //-------------------------------------------------------------------------
 159     /**
 160      * Constructor.
 161      *
 162      * @param id  the time-zone ID, not null
 163      * @param rules  the rules, null for lazy lookup
 164      */
 165     ZoneRegion(String id, ZoneRules rules) {
 166         this.id = id;
 167         this.rules = rules;
 168     }
 169 
 170     //-----------------------------------------------------------------------
 171     @Override
 172     public String getId() {
 173         return id;
 174     }
 175 
 176     @Override
 177     public ZoneRules getRules() {
 178         // additional query for group provider when null allows for possibility
 179         // that the provider was added after the ZoneId was created
 180         return (rules != null ? rules : ZoneRulesProvider.getRules(id));
 181     }
 182 
 183     //-----------------------------------------------------------------------
 184     /**
 185      * Writes the object using a
 186      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
 187      * <pre>
 188      *  out.writeByte(7);  // identifies this as a ZoneId (not ZoneOffset)
 189      *  out.writeUTF(zoneId);
 190      * </pre>
 191      *
 192      * @return the instance of {@code Ser}, not null
 193      */
 194     private Object writeReplace() {
 195         return new Ser(Ser.ZONE_REGION_TYPE, this);
 196     }
 197 
 198     /**
 199      * Defend against malicious streams.
 200      * @return never




  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() {
 180         // additional query for group provider when null allows for possibility
 181         // that the provider was updated after the ZoneId was created
 182         return (rules != null ? rules : ZoneRulesProvider.getRules(id, false));
 183     }
 184 
 185     //-----------------------------------------------------------------------
 186     /**
 187      * Writes the object using a
 188      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
 189      * <pre>
 190      *  out.writeByte(7);  // identifies this as a ZoneId (not ZoneOffset)
 191      *  out.writeUTF(zoneId);
 192      * </pre>
 193      *
 194      * @return the instance of {@code Ser}, not null
 195      */
 196     private Object writeReplace() {
 197         return new Ser(Ser.ZONE_REGION_TYPE, this);
 198     }
 199 
 200     /**
 201      * Defend against malicious streams.
 202      * @return never