jdk/src/share/classes/java/time/ZoneOffset.java
changeset 16852 60207b2b4b42
parent 15658 55b829ca2334
child 17474 8c100beabcc0
equal deleted inserted replaced
16851:3bbdae468b05 16852:60207b2b4b42
    59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    61  */
    61  */
    62 package java.time;
    62 package java.time;
    63 
    63 
       
    64 import java.time.temporal.UnsupportedTemporalTypeException;
    64 import static java.time.LocalTime.MINUTES_PER_HOUR;
    65 import static java.time.LocalTime.MINUTES_PER_HOUR;
    65 import static java.time.LocalTime.SECONDS_PER_HOUR;
    66 import static java.time.LocalTime.SECONDS_PER_HOUR;
    66 import static java.time.LocalTime.SECONDS_PER_MINUTE;
    67 import static java.time.LocalTime.SECONDS_PER_MINUTE;
    67 import static java.time.temporal.ChronoField.OFFSET_SECONDS;
    68 import static java.time.temporal.ChronoField.OFFSET_SECONDS;
    68 
    69 
    71 import java.io.IOException;
    72 import java.io.IOException;
    72 import java.io.InvalidObjectException;
    73 import java.io.InvalidObjectException;
    73 import java.io.ObjectStreamException;
    74 import java.io.ObjectStreamException;
    74 import java.io.Serializable;
    75 import java.io.Serializable;
    75 import java.time.temporal.ChronoField;
    76 import java.time.temporal.ChronoField;
    76 import java.time.temporal.Queries;
       
    77 import java.time.temporal.Temporal;
    77 import java.time.temporal.Temporal;
    78 import java.time.temporal.TemporalAccessor;
    78 import java.time.temporal.TemporalAccessor;
    79 import java.time.temporal.TemporalAdjuster;
    79 import java.time.temporal.TemporalAdjuster;
    80 import java.time.temporal.TemporalField;
    80 import java.time.temporal.TemporalField;
    81 import java.time.temporal.TemporalQuery;
    81 import java.time.temporal.TemporalQuery;
   320      * which this factory converts to an instance of {@code ZoneOffset}.
   320      * which this factory converts to an instance of {@code ZoneOffset}.
   321      * <p>
   321      * <p>
   322      * A {@code TemporalAccessor} represents some form of date and time information.
   322      * A {@code TemporalAccessor} represents some form of date and time information.
   323      * This factory converts the arbitrary temporal object to an instance of {@code ZoneOffset}.
   323      * This factory converts the arbitrary temporal object to an instance of {@code ZoneOffset}.
   324      * <p>
   324      * <p>
   325      * The conversion uses the {@link Queries#offset()} query, which relies
   325      * The conversion uses the {@link TemporalQuery#offset()} query, which relies
   326      * on extracting the {@link ChronoField#OFFSET_SECONDS OFFSET_SECONDS} field.
   326      * on extracting the {@link ChronoField#OFFSET_SECONDS OFFSET_SECONDS} field.
   327      * <p>
   327      * <p>
   328      * This method matches the signature of the functional interface {@link TemporalQuery}
   328      * This method matches the signature of the functional interface {@link TemporalQuery}
   329      * allowing it to be used in queries via method reference, {@code ZoneOffset::from}.
   329      * allowing it to be used in queries via method reference, {@code ZoneOffset::from}.
   330      *
   330      *
   331      * @param temporal  the temporal object to convert, not null
   331      * @param temporal  the temporal object to convert, not null
   332      * @return the zone-offset, not null
   332      * @return the zone-offset, not null
   333      * @throws DateTimeException if unable to convert to an {@code ZoneOffset}
   333      * @throws DateTimeException if unable to convert to an {@code ZoneOffset}
   334      */
   334      */
   335     public static ZoneOffset from(TemporalAccessor temporal) {
   335     public static ZoneOffset from(TemporalAccessor temporal) {
   336         ZoneOffset offset = temporal.query(Queries.offset());
   336         ZoneOffset offset = temporal.query(TemporalQuery.offset());
   337         if (offset == null) {
   337         if (offset == null) {
   338             throw new DateTimeException("Unable to obtain ZoneOffset from TemporalAccessor: " + temporal.getClass());
   338             throw new DateTimeException("Unable to obtain ZoneOffset from TemporalAccessor: " + temporal.getClass());
   339         }
   339         }
   340         return offset;
   340         return offset;
   341     }
   341     }
   532      * or for some other reason, an exception is thrown.
   532      * or for some other reason, an exception is thrown.
   533      * <p>
   533      * <p>
   534      * If the field is a {@link ChronoField} then the query is implemented here.
   534      * If the field is a {@link ChronoField} then the query is implemented here.
   535      * The {@link #isSupported(TemporalField) supported fields} will return
   535      * The {@link #isSupported(TemporalField) supported fields} will return
   536      * appropriate range instances.
   536      * appropriate range instances.
   537      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   537      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
   538      * <p>
   538      * <p>
   539      * If the field is not a {@code ChronoField}, then the result of this method
   539      * If the field is not a {@code ChronoField}, then the result of this method
   540      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
   540      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
   541      * passing {@code this} as the argument.
   541      * passing {@code this} as the argument.
   542      * Whether the range can be obtained is determined by the field.
   542      * Whether the range can be obtained is determined by the field.
   543      *
   543      *
   544      * @param field  the field to query the range for, not null
   544      * @param field  the field to query the range for, not null
   545      * @return the range of valid values for the field, not null
   545      * @return the range of valid values for the field, not null
   546      * @throws DateTimeException if the range for the field cannot be obtained
   546      * @throws DateTimeException if the range for the field cannot be obtained
       
   547      * @throws UnsupportedTemporalTypeException if the field is not supported
   547      */
   548      */
   548     @Override  // override for Javadoc
   549     @Override  // override for Javadoc
   549     public ValueRange range(TemporalField field) {
   550     public ValueRange range(TemporalField field) {
   550         return TemporalAccessor.super.range(field);
   551         return TemporalAccessor.super.range(field);
   551     }
   552     }
   558      * If it is not possible to return the value, because the field is not supported
   559      * If it is not possible to return the value, because the field is not supported
   559      * or for some other reason, an exception is thrown.
   560      * or for some other reason, an exception is thrown.
   560      * <p>
   561      * <p>
   561      * If the field is a {@link ChronoField} then the query is implemented here.
   562      * If the field is a {@link ChronoField} then the query is implemented here.
   562      * The {@code OFFSET_SECONDS} field returns the value of the offset.
   563      * The {@code OFFSET_SECONDS} field returns the value of the offset.
   563      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   564      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
   564      * <p>
   565      * <p>
   565      * If the field is not a {@code ChronoField}, then the result of this method
   566      * If the field is not a {@code ChronoField}, then the result of this method
   566      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   567      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   567      * passing {@code this} as the argument. Whether the value can be obtained,
   568      * passing {@code this} as the argument. Whether the value can be obtained,
   568      * and what the value represents, is determined by the field.
   569      * and what the value represents, is determined by the field.
   569      *
   570      *
   570      * @param field  the field to get, not null
   571      * @param field  the field to get, not null
   571      * @return the value for the field
   572      * @return the value for the field
   572      * @throws DateTimeException if a value for the field cannot be obtained
   573      * @throws DateTimeException if a value for the field cannot be obtained or
       
   574      *         the value is outside the range of valid values for the field
       
   575      * @throws UnsupportedTemporalTypeException if the field is not supported or
       
   576      *         the range of values exceeds an {@code int}
   573      * @throws ArithmeticException if numeric overflow occurs
   577      * @throws ArithmeticException if numeric overflow occurs
   574      */
   578      */
   575     @Override  // override for Javadoc and performance
   579     @Override  // override for Javadoc and performance
   576     public int get(TemporalField field) {
   580     public int get(TemporalField field) {
   577         if (field == OFFSET_SECONDS) {
   581         if (field == OFFSET_SECONDS) {
   578             return totalSeconds;
   582             return totalSeconds;
   579         } else if (field instanceof ChronoField) {
   583         } else if (field instanceof ChronoField) {
   580             throw new DateTimeException("Unsupported field: " + field.getName());
   584             throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
   581         }
   585         }
   582         return range(field).checkValidIntValue(getLong(field), field);
   586         return range(field).checkValidIntValue(getLong(field), field);
   583     }
   587     }
   584 
   588 
   585     /**
   589     /**
   589      * If it is not possible to return the value, because the field is not supported
   593      * If it is not possible to return the value, because the field is not supported
   590      * or for some other reason, an exception is thrown.
   594      * or for some other reason, an exception is thrown.
   591      * <p>
   595      * <p>
   592      * If the field is a {@link ChronoField} then the query is implemented here.
   596      * If the field is a {@link ChronoField} then the query is implemented here.
   593      * The {@code OFFSET_SECONDS} field returns the value of the offset.
   597      * The {@code OFFSET_SECONDS} field returns the value of the offset.
   594      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   598      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
   595      * <p>
   599      * <p>
   596      * If the field is not a {@code ChronoField}, then the result of this method
   600      * If the field is not a {@code ChronoField}, then the result of this method
   597      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   601      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   598      * passing {@code this} as the argument. Whether the value can be obtained,
   602      * passing {@code this} as the argument. Whether the value can be obtained,
   599      * and what the value represents, is determined by the field.
   603      * and what the value represents, is determined by the field.
   600      *
   604      *
   601      * @param field  the field to get, not null
   605      * @param field  the field to get, not null
   602      * @return the value for the field
   606      * @return the value for the field
   603      * @throws DateTimeException if a value for the field cannot be obtained
   607      * @throws DateTimeException if a value for the field cannot be obtained
       
   608      * @throws UnsupportedTemporalTypeException if the field is not supported
   604      * @throws ArithmeticException if numeric overflow occurs
   609      * @throws ArithmeticException if numeric overflow occurs
   605      */
   610      */
   606     @Override
   611     @Override
   607     public long getLong(TemporalField field) {
   612     public long getLong(TemporalField field) {
   608         if (field == OFFSET_SECONDS) {
   613         if (field == OFFSET_SECONDS) {
   609             return totalSeconds;
   614             return totalSeconds;
   610         } else if (field instanceof ChronoField) {
   615         } else if (field instanceof ChronoField) {
   611             throw new DateTimeException("Unsupported field: " + field.getName());
   616             throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
   612         }
   617         }
   613         return field.getFrom(this);
   618         return field.getFrom(this);
   614     }
   619     }
   615 
   620 
   616     //-----------------------------------------------------------------------
   621     //-----------------------------------------------------------------------
   633      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
   638      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
   634      */
   639      */
   635     @SuppressWarnings("unchecked")
   640     @SuppressWarnings("unchecked")
   636     @Override
   641     @Override
   637     public <R> R query(TemporalQuery<R> query) {
   642     public <R> R query(TemporalQuery<R> query) {
   638         if (query == Queries.offset() || query == Queries.zone()) {
   643         if (query == TemporalQuery.offset() || query == TemporalQuery.zone()) {
   639             return (R) this;
   644             return (R) this;
   640         }
   645         }
   641         return TemporalAccessor.super.query(query);
   646         return TemporalAccessor.super.query(query);
   642     }
   647     }
   643 
   648