jdk/src/share/classes/java/time/ZoneOffset.java
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
child 16852 60207b2b4b42
--- a/jdk/src/share/classes/java/time/ZoneOffset.java	Tue Feb 12 16:02:14 2013 +0400
+++ b/jdk/src/share/classes/java/time/ZoneOffset.java	Tue Feb 12 09:25:43 2013 -0800
@@ -61,6 +61,9 @@
  */
 package java.time;
 
+import static java.time.LocalTime.MINUTES_PER_HOUR;
+import static java.time.LocalTime.SECONDS_PER_HOUR;
+import static java.time.LocalTime.SECONDS_PER_MINUTE;
 import static java.time.temporal.ChronoField.OFFSET_SECONDS;
 
 import java.io.DataInput;
@@ -126,18 +129,6 @@
     private static final ConcurrentMap<String, ZoneOffset> ID_CACHE = new ConcurrentHashMap<>(16, 0.75f, 4);
 
     /**
-     * The number of seconds per hour.
-     */
-    private static final int SECONDS_PER_HOUR = 60 * 60;
-    /**
-     * The number of seconds per minute.
-     */
-    private static final int SECONDS_PER_MINUTE = 60;
-    /**
-     * The number of minutes per hour.
-     */
-    private static final int MINUTES_PER_HOUR = 60;
-    /**
      * The abs maximum seconds.
      */
     private static final int MAX_SECONDS = 18 * SECONDS_PER_HOUR;
@@ -239,11 +230,11 @@
                 seconds = parseNumber(offsetId, 7, true);
                 break;
             default:
-                throw new DateTimeException("Zone offset ID '" + offsetId + "' is invalid");
+                throw new DateTimeException("Invalid ID for ZoneOffset, invalid format: " + offsetId);
         }
         char first = offsetId.charAt(0);
         if (first != '+' && first != '-') {
-            throw new DateTimeException("Zone offset ID '" + offsetId + "' is invalid: Plus/minus not found when expected");
+            throw new DateTimeException("Invalid ID for ZoneOffset, plus/minus not found when expected: " + offsetId);
         }
         if (first == '-') {
             return ofHoursMinutesSeconds(-hours, -minutes, -seconds);
@@ -262,12 +253,12 @@
      */
     private static int parseNumber(CharSequence offsetId, int pos, boolean precededByColon) {
         if (precededByColon && offsetId.charAt(pos - 1) != ':') {
-            throw new DateTimeException("Zone offset ID '" + offsetId + "' is invalid: Colon not found when expected");
+            throw new DateTimeException("Invalid ID for ZoneOffset, colon not found when expected: " + offsetId);
         }
         char ch1 = offsetId.charAt(pos);
         char ch2 = offsetId.charAt(pos + 1);
         if (ch1 < '0' || ch1 > '9' || ch2 < '0' || ch2 > '9') {
-            throw new DateTimeException("Zone offset ID '" + offsetId + "' is invalid: Non numeric characters found");
+            throw new DateTimeException("Invalid ID for ZoneOffset, non numeric characters found: " + offsetId);
         }
         return (ch1 - 48) * 10 + (ch2 - 48);
     }
@@ -324,10 +315,15 @@
     /**
      * Obtains an instance of {@code ZoneOffset} from a temporal object.
      * <p>
+     * This obtains an offset based on the specified temporal.
+     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
+     * which this factory converts to an instance of {@code ZoneOffset}.
+     * <p>
      * A {@code TemporalAccessor} represents some form of date and time information.
      * This factory converts the arbitrary temporal object to an instance of {@code ZoneOffset}.
      * <p>
-     * The conversion extracts the {@link ChronoField#OFFSET_SECONDS offset-seconds} field.
+     * The conversion uses the {@link Queries#offset()} query, which relies
+     * on extracting the {@link ChronoField#OFFSET_SECONDS OFFSET_SECONDS} field.
      * <p>
      * This method matches the signature of the functional interface {@link TemporalQuery}
      * allowing it to be used in queries via method reference, {@code ZoneOffset::from}.
@@ -337,14 +333,11 @@
      * @throws DateTimeException if unable to convert to an {@code ZoneOffset}
      */
     public static ZoneOffset from(TemporalAccessor temporal) {
-        if (temporal instanceof ZoneOffset) {
-            return (ZoneOffset) temporal;
+        ZoneOffset offset = temporal.query(Queries.offset());
+        if (offset == null) {
+            throw new DateTimeException("Unable to obtain ZoneOffset from TemporalAccessor: " + temporal.getClass());
         }
-        try {
-            return ofTotalSeconds(temporal.get(OFFSET_SECONDS));
-        } catch (DateTimeException ex) {
-            throw new DateTimeException("Unable to obtain ZoneOffset from TemporalAccessor: " + temporal.getClass(), ex);
-        }
+        return offset;
     }
 
     //-----------------------------------------------------------------------
@@ -515,7 +508,7 @@
      * All other {@code ChronoField} instances will return false.
      * <p>
      * If the field is not a {@code ChronoField}, then the result of this method
-     * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
+     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
      * passing {@code this} as the argument.
      * Whether the field is supported is determined by the field.
      *
@@ -527,7 +520,7 @@
         if (field instanceof ChronoField) {
             return field == OFFSET_SECONDS;
         }
-        return field != null && field.doIsSupported(this);
+        return field != null && field.isSupportedBy(this);
     }
 
     /**
@@ -544,7 +537,7 @@
      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
      * <p>
      * If the field is not a {@code ChronoField}, then the result of this method
-     * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
+     * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
      * passing {@code this} as the argument.
      * Whether the range can be obtained is determined by the field.
      *
@@ -570,7 +563,7 @@
      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
      * <p>
      * If the field is not a {@code ChronoField}, then the result of this method
-     * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
+     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
      * passing {@code this} as the argument. Whether the value can be obtained,
      * and what the value represents, is determined by the field.
      *
@@ -601,7 +594,7 @@
      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
      * <p>
      * If the field is not a {@code ChronoField}, then the result of this method
-     * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
+     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
      * passing {@code this} as the argument. Whether the value can be obtained,
      * and what the value represents, is determined by the field.
      *
@@ -617,7 +610,7 @@
         } else if (field instanceof ChronoField) {
             throw new DateTimeException("Unsupported field: " + field.getName());
         }
-        return field.doGet(this);
+        return field.getFrom(this);
     }
 
     //-----------------------------------------------------------------------