jdk/src/share/classes/java/time/temporal/JulianFields.java
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
child 16852 60207b2b4b42
--- a/jdk/src/share/classes/java/time/temporal/JulianFields.java	Tue Feb 12 16:02:14 2013 +0400
+++ b/jdk/src/share/classes/java/time/temporal/JulianFields.java	Tue Feb 12 09:25:43 2013 -0800
@@ -65,11 +65,9 @@
 import static java.time.temporal.ChronoUnit.DAYS;
 import static java.time.temporal.ChronoUnit.FOREVER;
 
-import java.io.InvalidObjectException;
-import java.io.Serializable;
 import java.time.DateTimeException;
-import java.time.LocalDate;
-import java.time.format.DateTimeBuilder;
+import java.util.Collections;
+import java.util.Map;
 
 /**
  * A set of date fields that provide access to Julian Days.
@@ -77,6 +75,10 @@
  * The Julian Day is a standard way of expressing date and time commonly used in the scientific community.
  * It is expressed as a decimal number of whole days where days start at midday.
  * This class represents variations on Julian Days that count whole days from midnight.
+ * <p>
+ * The fields are implemented relative to {@link ChronoField#EPOCH_DAY EPOCH_DAY}.
+ * The fields are supported, and can be queried and set if {@code EPOCH_DAY} is available.
+ * The fields work with all chronologies.
  *
  * <h3>Specification for implementors</h3>
  * This is an immutable and thread-safe class.
@@ -99,10 +101,10 @@
      * The field  has "JulianDay" as 'name', and 'DAYS' as 'baseUnit'.
      * The field always refers to the local date-time, ignoring the offset or zone.
      * <p>
-     * For date-times, 'JULIAN_DAY.doGet()' assumes the same value from
+     * For date-times, 'JULIAN_DAY.getFrom()' assumes the same value from
      * midnight until just before the next midnight.
-     * When 'JULIAN_DAY.doWith()' is applied to a date-time, the time of day portion remains unaltered.
-     * 'JULIAN_DAY.doWith()' and 'JULIAN_DAY.doGet()' only apply to {@code Temporal} objects that
+     * When 'JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered.
+     * 'JULIAN_DAY.adjustInto()' and 'JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects that
      * can be converted into {@link ChronoField#EPOCH_DAY}.
      * A {@link DateTimeException} is thrown for any other type of object.
      * <p>
@@ -129,7 +131,7 @@
      * implementation always uses the Julian Day number for the local date,
      * regardless of the offset or time-zone.
      */
-    public static final TemporalField JULIAN_DAY = new Field("JulianDay", DAYS, FOREVER, JULIAN_DAY_OFFSET);
+    public static final TemporalField JULIAN_DAY = Field.JULIAN_DAY;
 
     /**
      * Modified Julian Day field.
@@ -140,10 +142,10 @@
      * Each Modified Julian Day runs from midnight to midnight.
      * The field always refers to the local date-time, ignoring the offset or zone.
      * <p>
-     * For date-times, 'MODIFIED_JULIAN_DAY.doGet()' assumes the same value from
+     * For date-times, 'MODIFIED_JULIAN_DAY.getFrom()' assumes the same value from
      * midnight until just before the next midnight.
-     * When 'MODIFIED_JULIAN_DAY.doWith()' is applied to a date-time, the time of day portion remains unaltered.
-     * 'MODIFIED_JULIAN_DAY.doWith()' and 'MODIFIED_JULIAN_DAY.doGet()' only apply to {@code Temporal} objects
+     * When 'MODIFIED_JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered.
+     * 'MODIFIED_JULIAN_DAY.adjustInto()' and 'MODIFIED_JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects
      * that can be converted into {@link ChronoField#EPOCH_DAY}.
      * A {@link DateTimeException} is thrown for any other type of object.
      * <p>
@@ -165,7 +167,7 @@
      * implementation always uses the Modified Julian Day for the local date,
      * regardless of the offset or time-zone.
      */
-    public static final TemporalField MODIFIED_JULIAN_DAY = new Field("ModifiedJulianDay", DAYS, FOREVER, 40587L);
+    public static final TemporalField MODIFIED_JULIAN_DAY = Field.MODIFIED_JULIAN_DAY;
 
     /**
      * Rata Die field.
@@ -173,14 +175,14 @@
      * Rata Die counts whole days continuously starting day 1 at midnight at the beginning of 0001-01-01 (ISO).
      * The field always refers to the local date-time, ignoring the offset or zone.
      * <p>
-     * For date-times, 'RATA_DIE.doGet()' assumes the same value from
+     * For date-times, 'RATA_DIE.getFrom()' assumes the same value from
      * midnight until just before the next midnight.
-     * When 'RATA_DIE.doWith()' is applied to a date-time, the time of day portion remains unaltered.
-     * 'MODIFIED_JULIAN_DAY.doWith()' and 'RATA_DIE.doGet()' only apply to {@code Temporal} objects
+     * When 'RATA_DIE.adjustInto()' is applied to a date-time, the time of day portion remains unaltered.
+     * 'RATA_DIE.adjustInto()' and 'RATA_DIE.getFrom()' only apply to {@code Temporal} objects
      * that can be converted into {@link ChronoField#EPOCH_DAY}.
      * A {@link DateTimeException} is thrown for any other type of object.
      */
-    public static final TemporalField RATA_DIE = new Field("RataDie", DAYS, FOREVER, 719163L);
+    public static final TemporalField RATA_DIE = Field.RATA_DIE;
 
     /**
      * Restricted constructor.
@@ -190,13 +192,16 @@
     }
 
     /**
-     * implementation of JulianFields.  Each instance is a singleton.
+     * Implementation of JulianFields.  Each instance is a singleton.
      */
-    private static class Field implements TemporalField, Serializable {
+    private static enum Field implements TemporalField {
+        JULIAN_DAY("JulianDay", DAYS, FOREVER, JULIAN_DAY_OFFSET),
+        MODIFIED_JULIAN_DAY("ModifiedJulianDay", DAYS, FOREVER, 40587L),
+        RATA_DIE("RataDie", DAYS, FOREVER, 719163L);
 
         private static final long serialVersionUID = -7501623920830201812L;
 
-        private final String name;
+        private final transient String name;
         private final transient TemporalUnit baseUnit;
         private final transient TemporalUnit rangeUnit;
         private final transient ValueRange range;
@@ -210,25 +215,6 @@
             this.offset = offset;
         }
 
-
-        /**
-         * Resolve the object from the stream to the appropriate singleton.
-         * @return one of the singleton objects {@link #JULIAN_DAY},
-         *     {@link #MODIFIED_JULIAN_DAY}, or {@link #RATA_DIE}.
-         * @throws InvalidObjectException if the object in the stream is not one of the singletons.
-         */
-        private Object readResolve() throws InvalidObjectException {
-            if (JULIAN_DAY.getName().equals(name)) {
-                return JULIAN_DAY;
-            } else if (MODIFIED_JULIAN_DAY.getName().equals(name)) {
-                return MODIFIED_JULIAN_DAY;
-            } else if (RATA_DIE.getName().equals(name)) {
-                return RATA_DIE;
-            } else {
-                throw new InvalidObjectException("Not one of the singletons");
-            }
-        }
-
         //-----------------------------------------------------------------------
         @Override
         public String getName() {
@@ -252,25 +238,26 @@
 
         //-----------------------------------------------------------------------
         @Override
-        public boolean doIsSupported(TemporalAccessor temporal) {
+        public boolean isSupportedBy(TemporalAccessor temporal) {
             return temporal.isSupported(EPOCH_DAY);
         }
 
         @Override
-        public ValueRange doRange(TemporalAccessor temporal) {
-            if (doIsSupported(temporal) == false) {
+        public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
+            if (isSupportedBy(temporal) == false) {
                 throw new DateTimeException("Unsupported field: " + this);
             }
             return range();
         }
 
         @Override
-        public long doGet(TemporalAccessor temporal) {
+        public long getFrom(TemporalAccessor temporal) {
             return temporal.getLong(EPOCH_DAY) + offset;
         }
 
+        @SuppressWarnings("unchecked")
         @Override
-        public <R extends Temporal> R doWith(R temporal, long newValue) {
+        public <R extends Temporal> R adjustInto(R temporal, long newValue) {
             if (range().isValidValue(newValue) == false) {
                 throw new DateTimeException("Invalid value: " + name + " " + newValue);
             }
@@ -279,27 +266,14 @@
 
         //-----------------------------------------------------------------------
         @Override
-        public boolean resolve(DateTimeBuilder builder, long value) {
-            boolean changed = false;
-            changed = resolve0(JULIAN_DAY, builder, changed);
-            changed = resolve0(MODIFIED_JULIAN_DAY, builder, changed);
-            changed = resolve0(RATA_DIE, builder, changed);
-            return changed;
-        }
-
-        private boolean resolve0(TemporalField field, DateTimeBuilder builder, boolean changed) {
-            if (builder.containsFieldValue(field)) {
-                builder.addCalendrical(LocalDate.ofEpochDay(Math.subtractExact(builder.getFieldValue(JULIAN_DAY), JULIAN_DAY_OFFSET)));
-                builder.removeFieldValue(JULIAN_DAY);
-                changed = true;
-            }
-            return changed;
+        public Map<TemporalField, Long> resolve(TemporalAccessor temporal, long value) {
+            return Collections.<TemporalField, Long>singletonMap(EPOCH_DAY, Math.subtractExact(value, offset));
         }
 
         //-----------------------------------------------------------------------
         @Override
         public String toString() {
-            return getName();
+            return name;
         }
     }
 }