src/share/classes/java/time/chrono/ChronoZonedDateTimeImpl.java

Print this page




  42  *  * Redistributions in binary form must reproduce the above copyright notice,
  43  *    this list of conditions and the following disclaimer in the documentation
  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  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.
  61  */
  62 package java.time.temporal;
  63 
  64 import static java.time.temporal.ChronoUnit.SECONDS;
  65 
  66 import java.io.IOException;
  67 import java.io.InvalidObjectException;
  68 import java.io.ObjectInput;
  69 import java.io.ObjectOutput;
  70 import java.io.ObjectStreamException;
  71 import java.io.Serializable;
  72 import java.time.DateTimeException;
  73 import java.time.Instant;
  74 import java.time.LocalDateTime;
  75 import java.time.ZoneId;
  76 import java.time.ZoneOffset;





  77 import java.time.zone.ZoneOffsetTransition;
  78 import java.time.zone.ZoneRules;
  79 import java.util.List;
  80 import java.util.Objects;
  81 
  82 /**
  83  * A date-time with a time-zone in the calendar neutral API.
  84  * <p>
  85  * {@code ZoneChronoDateTime} is an immutable representation of a date-time with a time-zone.
  86  * This class stores all date and time fields, to a precision of nanoseconds,
  87  * as well as a time-zone and zone offset.
  88  * <p>
  89  * The purpose of storing the time-zone is to distinguish the ambiguous case where
  90  * the local time-line overlaps, typically as a result of the end of daylight time.
  91  * Information about the local-time can be obtained using methods on the time-zone.
  92  *
  93  * <h3>Specification for implementors</h3>
  94  * This class is immutable and thread-safe.
  95  *
  96  * @param <C> the chronology of this date
  97  * @since 1.8
  98  */
  99 final class ChronoZonedDateTimeImpl<C extends Chrono<C>>
 100         implements ChronoZonedDateTime<C>, Serializable {
 101 
 102     /**
 103      * Serialization version.
 104      */
 105     private static final long serialVersionUID = -5261813987200935591L;
 106 
 107     /**
 108      * The local date-time.
 109      */
 110     private final ChronoLocalDateTimeImpl<C> dateTime;
 111     /**
 112      * The zone offset.
 113      */
 114     private final ZoneOffset offset;
 115     /**
 116      * The zone ID.
 117      */
 118     private final ZoneId zone;
 119 
 120     //-----------------------------------------------------------------------
 121     /**
 122      * Obtains an instance from a local date-time using the preferred offset if possible.
 123      *
 124      * @param localDateTime  the local date-time, not null
 125      * @param zone  the zone identifier, not null
 126      * @param preferredOffset  the zone offset, null if no preference
 127      * @return the zoned date-time, not null
 128      */
 129     static <R extends Chrono<R>> ChronoZonedDateTime<R> ofBest(
 130             ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
 131         Objects.requireNonNull(localDateTime, "localDateTime");
 132         Objects.requireNonNull(zone, "zone");
 133         if (zone instanceof ZoneOffset) {
 134             return new ChronoZonedDateTimeImpl<R>(localDateTime, (ZoneOffset) zone, zone);
 135         }
 136         ZoneRules rules = zone.getRules();
 137         LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
 138         List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
 139         ZoneOffset offset;
 140         if (validOffsets.size() == 1) {
 141             offset = validOffsets.get(0);
 142         } else if (validOffsets.size() == 0) {
 143             ZoneOffsetTransition trans = rules.getTransition(isoLDT);
 144             localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
 145             offset = trans.getOffsetAfter();
 146         } else {
 147             if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
 148                 offset = preferredOffset;
 149             } else {
 150                 offset = validOffsets.get(0);
 151             }
 152         }
 153         Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
 154         return new ChronoZonedDateTimeImpl<R>(localDateTime, offset, zone);
 155     }
 156 
 157     /**
 158      * Obtains an instance from an instant using the specified time-zone.
 159      *
 160      * @param chrono  the chronology, not null
 161      * @param instant  the instant, not null
 162      * @param zone  the zone identifier, not null
 163      * @return the zoned date-time, not null
 164      */
 165     static <R extends Chrono<R>> ChronoZonedDateTimeImpl<R> ofInstant(Chrono<R> chrono, Instant instant, ZoneId zone) {
 166         ZoneRules rules = zone.getRules();
 167         ZoneOffset offset = rules.getOffset(instant);
 168         Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
 169         LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
 170         ChronoLocalDateTimeImpl<R> cldt = (ChronoLocalDateTimeImpl<R>) chrono.localDateTime(ldt);
 171         return new ChronoZonedDateTimeImpl<R>(cldt, offset, zone);
 172     }
 173 
 174     /**
 175      * Obtains an instance from an {@code Instant}.
 176      *
 177      * @param instant  the instant to create the date-time from, not null
 178      * @param zone  the time-zone to use, validated not null
 179      * @return the zoned date-time, validated not null
 180      */
 181     private ChronoZonedDateTimeImpl<C> create(Instant instant, ZoneId zone) {
 182         return ofInstant(getDate().getChrono(), instant, zone);
 183     }
 184 
 185     //-----------------------------------------------------------------------
 186     /**
 187      * Constructor.
 188      *
 189      * @param dateTime  the date-time, not null
 190      * @param offset  the zone offset, not null
 191      * @param zone  the zone ID, not null
 192      */
 193     private ChronoZonedDateTimeImpl(ChronoLocalDateTimeImpl<C> dateTime, ZoneOffset offset, ZoneId zone) {
 194         this.dateTime = Objects.requireNonNull(dateTime, "dateTime");
 195         this.offset = Objects.requireNonNull(offset, "offset");
 196         this.zone = Objects.requireNonNull(zone, "zone");
 197     }
 198 
 199     //-----------------------------------------------------------------------
 200     public ZoneOffset getOffset() {
 201         return offset;
 202     }
 203 
 204     @Override
 205     public ChronoZonedDateTime<C> withEarlierOffsetAtOverlap() {
 206         ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
 207         if (trans != null && trans.isOverlap()) {
 208             ZoneOffset earlierOffset = trans.getOffsetBefore();
 209             if (earlierOffset.equals(offset) == false) {
 210                 return new ChronoZonedDateTimeImpl<C>(dateTime, earlierOffset, zone);
 211             }
 212         }
 213         return this;
 214     }
 215 
 216     @Override
 217     public ChronoZonedDateTime<C> withLaterOffsetAtOverlap() {
 218         ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
 219         if (trans != null) {
 220             ZoneOffset offset = trans.getOffsetAfter();
 221             if (offset.equals(getOffset()) == false) {
 222                 return new ChronoZonedDateTimeImpl<C>(dateTime, offset, zone);
 223             }
 224         }
 225         return this;
 226     }
 227 
 228     //-----------------------------------------------------------------------
 229     @Override
 230     public ChronoLocalDateTime<C> getDateTime() {
 231         return dateTime;
 232     }
 233 
 234     public ZoneId getZone() {
 235         return zone;
 236     }
 237 
 238     public ChronoZonedDateTime<C> withZoneSameLocal(ZoneId zone) {
 239         return ofBest(dateTime, zone, offset);
 240     }
 241 
 242     @Override
 243     public ChronoZonedDateTime<C> withZoneSameInstant(ZoneId zone) {
 244         Objects.requireNonNull(zone, "zone");
 245         return this.zone.equals(zone) ? this : create(dateTime.toInstant(offset), zone);
 246     }
 247 
 248     //-----------------------------------------------------------------------
 249     @Override
 250     public boolean isSupported(TemporalField field) {
 251         return field instanceof ChronoField || (field != null && field.doIsSupported(this));
 252     }
 253 
 254     //-----------------------------------------------------------------------
 255     @Override
 256     public ChronoZonedDateTime<C> with(TemporalField field, long newValue) {
 257         if (field instanceof ChronoField) {
 258             ChronoField f = (ChronoField) field;
 259             switch (f) {
 260                 case INSTANT_SECONDS: return plus(newValue - toEpochSecond(), SECONDS);
 261                 case OFFSET_SECONDS: {
 262                     ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue));
 263                     return create(dateTime.toInstant(offset), zone);
 264                 }
 265             }
 266             return ofBest(dateTime.with(field, newValue), zone, offset);
 267         }
 268         return getDate().getChrono().ensureChronoZonedDateTime(field.doWith(this, newValue));
 269     }
 270 
 271     //-----------------------------------------------------------------------
 272     @Override
 273     public ChronoZonedDateTime<C> plus(long amountToAdd, TemporalUnit unit) {
 274         if (unit instanceof ChronoUnit) {
 275             return with(dateTime.plus(amountToAdd, unit));
 276         }
 277         return getDate().getChrono().ensureChronoZonedDateTime(unit.doPlus(this, amountToAdd));   /// TODO: Generics replacement Risk!
 278     }
 279 
 280     //-----------------------------------------------------------------------
 281     @Override
 282     public long periodUntil(Temporal endDateTime, TemporalUnit unit) {
 283         if (endDateTime instanceof ChronoZonedDateTime == false) {
 284             throw new DateTimeException("Unable to calculate period between objects of two different types");
 285         }
 286         @SuppressWarnings("unchecked")
 287         ChronoZonedDateTime<C> end = (ChronoZonedDateTime<C>) endDateTime;
 288         if (getDate().getChrono().equals(end.getDate().getChrono()) == false) {
 289             throw new DateTimeException("Unable to calculate period between two different chronologies");
 290         }
 291         if (unit instanceof ChronoUnit) {
 292             end = end.withZoneSameInstant(offset);
 293             return dateTime.periodUntil(end.getDateTime(), unit);
 294         }
 295         return unit.between(this, endDateTime).getAmount();
 296     }
 297 
 298     //-----------------------------------------------------------------------
 299     private Object writeReplace() {
 300         return new Ser(Ser.CHRONO_ZONE_DATE_TIME_TYPE, this);
 301     }
 302 
 303     /**
 304      * Defend against malicious streams.
 305      * @return never
 306      * @throws InvalidObjectException always
 307      */
 308     private Object readResolve() throws ObjectStreamException {
 309         throw new InvalidObjectException("Deserialization via serialization delegate");
 310     }
 311 
 312     void writeExternal(ObjectOutput out) throws IOException {
 313         out.writeObject(dateTime);
 314         out.writeObject(offset);
 315         out.writeObject(zone);


 320         ZoneOffset offset = (ZoneOffset) in.readObject();
 321         ZoneId zone = (ZoneId) in.readObject();
 322         return dateTime.atZone(offset).withZoneSameLocal(zone);
 323         // TODO: ZDT uses ofLenient()
 324     }
 325 
 326     //-------------------------------------------------------------------------
 327     @Override
 328     public boolean equals(Object obj) {
 329         if (this == obj) {
 330             return true;
 331         }
 332         if (obj instanceof ChronoZonedDateTime) {
 333             return compareTo((ChronoZonedDateTime<?>) obj) == 0;
 334         }
 335         return false;
 336     }
 337 
 338     @Override
 339     public int hashCode() {
 340         return getDateTime().hashCode() ^ getOffset().hashCode() ^ Integer.rotateLeft(getZone().hashCode(), 3);
 341     }
 342 
 343     @Override
 344     public String toString() {
 345         String str = getDateTime().toString() + getOffset().toString();
 346         if (getOffset() != getZone()) {
 347             str += '[' + getZone().toString() + ']';
 348         }
 349         return str;
 350     }
 351 
 352 
 353 }


  42  *  * Redistributions in binary form must reproduce the above copyright notice,
  43  *    this list of conditions and the following disclaimer in the documentation
  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  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.
  61  */
  62 package java.time.chrono;
  63 
  64 import static java.time.temporal.ChronoUnit.SECONDS;
  65 
  66 import java.io.IOException;
  67 import java.io.InvalidObjectException;
  68 import java.io.ObjectInput;
  69 import java.io.ObjectOutput;
  70 import java.io.ObjectStreamException;
  71 import java.io.Serializable;
  72 import java.time.DateTimeException;
  73 import java.time.Instant;
  74 import java.time.LocalDateTime;
  75 import java.time.ZoneId;
  76 import java.time.ZoneOffset;
  77 import java.time.temporal.ChronoField;
  78 import java.time.temporal.ChronoUnit;
  79 import java.time.temporal.Temporal;
  80 import java.time.temporal.TemporalField;
  81 import java.time.temporal.TemporalUnit;
  82 import java.time.zone.ZoneOffsetTransition;
  83 import java.time.zone.ZoneRules;
  84 import java.util.List;
  85 import java.util.Objects;
  86 
  87 /**
  88  * A date-time with a time-zone in the calendar neutral API.
  89  * <p>
  90  * {@code ZoneChronoDateTime} is an immutable representation of a date-time with a time-zone.
  91  * This class stores all date and time fields, to a precision of nanoseconds,
  92  * as well as a time-zone and zone offset.
  93  * <p>
  94  * The purpose of storing the time-zone is to distinguish the ambiguous case where
  95  * the local time-line overlaps, typically as a result of the end of daylight time.
  96  * Information about the local-time can be obtained using methods on the time-zone.
  97  *
  98  * <h3>Specification for implementors</h3>
  99  * This class is immutable and thread-safe.
 100  *
 101  * @param <D> the concrete type for the date of this date-time
 102  * @since 1.8
 103  */
 104 final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate<D>>
 105         implements ChronoZonedDateTime<D>, Serializable {
 106 
 107     /**
 108      * Serialization version.
 109      */
 110     private static final long serialVersionUID = -5261813987200935591L;
 111 
 112     /**
 113      * The local date-time.
 114      */
 115     private final ChronoLocalDateTimeImpl<D> dateTime;
 116     /**
 117      * The zone offset.
 118      */
 119     private final ZoneOffset offset;
 120     /**
 121      * The zone ID.
 122      */
 123     private final ZoneId zone;
 124 
 125     //-----------------------------------------------------------------------
 126     /**
 127      * Obtains an instance from a local date-time using the preferred offset if possible.
 128      *
 129      * @param localDateTime  the local date-time, not null
 130      * @param zone  the zone identifier, not null
 131      * @param preferredOffset  the zone offset, null if no preference
 132      * @return the zoned date-time, not null
 133      */
 134     static <R extends ChronoLocalDate<R>> ChronoZonedDateTime<R> ofBest(
 135             ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
 136         Objects.requireNonNull(localDateTime, "localDateTime");
 137         Objects.requireNonNull(zone, "zone");
 138         if (zone instanceof ZoneOffset) {
 139             return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
 140         }
 141         ZoneRules rules = zone.getRules();
 142         LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
 143         List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
 144         ZoneOffset offset;
 145         if (validOffsets.size() == 1) {
 146             offset = validOffsets.get(0);
 147         } else if (validOffsets.size() == 0) {
 148             ZoneOffsetTransition trans = rules.getTransition(isoLDT);
 149             localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
 150             offset = trans.getOffsetAfter();
 151         } else {
 152             if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
 153                 offset = preferredOffset;
 154             } else {
 155                 offset = validOffsets.get(0);
 156             }
 157         }
 158         Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
 159         return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
 160     }
 161 
 162     /**
 163      * Obtains an instance from an instant using the specified time-zone.
 164      *
 165      * @param chrono  the chronology, not null
 166      * @param instant  the instant, not null
 167      * @param zone  the zone identifier, not null
 168      * @return the zoned date-time, not null
 169      */
 170     static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
 171         ZoneRules rules = zone.getRules();
 172         ZoneOffset offset = rules.getOffset(instant);
 173         Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
 174         LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
 175         ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>) chrono.localDateTime(ldt);
 176         return new ChronoZonedDateTimeImpl(cldt, offset, zone);
 177     }
 178 
 179     /**
 180      * Obtains an instance from an {@code Instant}.
 181      *
 182      * @param instant  the instant to create the date-time from, not null
 183      * @param zone  the time-zone to use, validated not null
 184      * @return the zoned date-time, validated not null
 185      */
 186     private ChronoZonedDateTimeImpl<D> create(Instant instant, ZoneId zone) {
 187         return (ChronoZonedDateTimeImpl<D>)ofInstant(toLocalDate().getChronology(), instant, zone);
 188     }
 189 
 190     //-----------------------------------------------------------------------
 191     /**
 192      * Constructor.
 193      *
 194      * @param dateTime  the date-time, not null
 195      * @param offset  the zone offset, not null
 196      * @param zone  the zone ID, not null
 197      */
 198     private ChronoZonedDateTimeImpl(ChronoLocalDateTimeImpl<D> dateTime, ZoneOffset offset, ZoneId zone) {
 199         this.dateTime = Objects.requireNonNull(dateTime, "dateTime");
 200         this.offset = Objects.requireNonNull(offset, "offset");
 201         this.zone = Objects.requireNonNull(zone, "zone");
 202     }
 203 
 204     //-----------------------------------------------------------------------
 205     public ZoneOffset getOffset() {
 206         return offset;
 207     }
 208 
 209     @Override
 210     public ChronoZonedDateTime<D> withEarlierOffsetAtOverlap() {
 211         ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
 212         if (trans != null && trans.isOverlap()) {
 213             ZoneOffset earlierOffset = trans.getOffsetBefore();
 214             if (earlierOffset.equals(offset) == false) {
 215                 return new ChronoZonedDateTimeImpl<D>(dateTime, earlierOffset, zone);
 216             }
 217         }
 218         return this;
 219     }
 220 
 221     @Override
 222     public ChronoZonedDateTime<D> withLaterOffsetAtOverlap() {
 223         ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
 224         if (trans != null) {
 225             ZoneOffset offset = trans.getOffsetAfter();
 226             if (offset.equals(getOffset()) == false) {
 227                 return new ChronoZonedDateTimeImpl<D>(dateTime, offset, zone);
 228             }
 229         }
 230         return this;
 231     }
 232 
 233     //-----------------------------------------------------------------------
 234     @Override
 235     public ChronoLocalDateTime<D> toLocalDateTime() {
 236         return dateTime;
 237     }
 238 
 239     public ZoneId getZone() {
 240         return zone;
 241     }
 242 
 243     public ChronoZonedDateTime<D> withZoneSameLocal(ZoneId zone) {
 244         return ofBest(dateTime, zone, offset);
 245     }
 246 
 247     @Override
 248     public ChronoZonedDateTime<D> withZoneSameInstant(ZoneId zone) {
 249         Objects.requireNonNull(zone, "zone");
 250         return this.zone.equals(zone) ? this : create(dateTime.toInstant(offset), zone);
 251     }
 252 
 253     //-----------------------------------------------------------------------
 254     @Override
 255     public boolean isSupported(TemporalField field) {
 256         return field instanceof ChronoField || (field != null && field.isSupportedBy(this));
 257     }
 258 
 259     //-----------------------------------------------------------------------
 260     @Override
 261     public ChronoZonedDateTime<D> with(TemporalField field, long newValue) {
 262         if (field instanceof ChronoField) {
 263             ChronoField f = (ChronoField) field;
 264             switch (f) {
 265                 case INSTANT_SECONDS: return plus(newValue - toEpochSecond(), SECONDS);
 266                 case OFFSET_SECONDS: {
 267                     ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue));
 268                     return create(dateTime.toInstant(offset), zone);
 269                 }
 270             }
 271             return ofBest(dateTime.with(field, newValue), zone, offset);
 272         }
 273         return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(field.adjustInto(this, newValue)));
 274     }
 275 
 276     //-----------------------------------------------------------------------
 277     @Override
 278     public ChronoZonedDateTime<D> plus(long amountToAdd, TemporalUnit unit) {
 279         if (unit instanceof ChronoUnit) {
 280             return with(dateTime.plus(amountToAdd, unit));
 281         }
 282         return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(unit.addTo(this, amountToAdd)));   /// TODO: Generics replacement Risk!
 283     }
 284 
 285     //-----------------------------------------------------------------------
 286     @Override
 287     public long periodUntil(Temporal endDateTime, TemporalUnit unit) {
 288         if (endDateTime instanceof ChronoZonedDateTime == false) {
 289             throw new DateTimeException("Unable to calculate period between objects of two different types");
 290         }
 291         @SuppressWarnings("unchecked")
 292         ChronoZonedDateTime<D> end = (ChronoZonedDateTime<D>) endDateTime;
 293         if (toLocalDate().getChronology().equals(end.toLocalDate().getChronology()) == false) {
 294             throw new DateTimeException("Unable to calculate period between two different chronologies");
 295         }
 296         if (unit instanceof ChronoUnit) {
 297             end = end.withZoneSameInstant(offset);
 298             return dateTime.periodUntil(end.toLocalDateTime(), unit);
 299         }
 300         return unit.between(this, endDateTime);
 301     }
 302 
 303     //-----------------------------------------------------------------------
 304     private Object writeReplace() {
 305         return new Ser(Ser.CHRONO_ZONE_DATE_TIME_TYPE, this);
 306     }
 307 
 308     /**
 309      * Defend against malicious streams.
 310      * @return never
 311      * @throws InvalidObjectException always
 312      */
 313     private Object readResolve() throws ObjectStreamException {
 314         throw new InvalidObjectException("Deserialization via serialization delegate");
 315     }
 316 
 317     void writeExternal(ObjectOutput out) throws IOException {
 318         out.writeObject(dateTime);
 319         out.writeObject(offset);
 320         out.writeObject(zone);


 325         ZoneOffset offset = (ZoneOffset) in.readObject();
 326         ZoneId zone = (ZoneId) in.readObject();
 327         return dateTime.atZone(offset).withZoneSameLocal(zone);
 328         // TODO: ZDT uses ofLenient()
 329     }
 330 
 331     //-------------------------------------------------------------------------
 332     @Override
 333     public boolean equals(Object obj) {
 334         if (this == obj) {
 335             return true;
 336         }
 337         if (obj instanceof ChronoZonedDateTime) {
 338             return compareTo((ChronoZonedDateTime<?>) obj) == 0;
 339         }
 340         return false;
 341     }
 342 
 343     @Override
 344     public int hashCode() {
 345         return toLocalDateTime().hashCode() ^ getOffset().hashCode() ^ Integer.rotateLeft(getZone().hashCode(), 3);
 346     }
 347 
 348     @Override
 349     public String toString() {
 350         String str = toLocalDateTime().toString() + getOffset().toString();
 351         if (getOffset() != getZone()) {
 352             str += '[' + getZone().toString() + ']';
 353         }
 354         return str;
 355     }
 356 
 357 
 358 }