1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * This file is available under and governed by the GNU General Public
28  * License version 2 only, as published by the Free Software Foundation.
29  * However, the following notice accompanied the original version of this
30  * file:
31  *
32  * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
33  *
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions are met:
38  *
39  *  * Redistributions of source code must retain the above copyright notice,
40  *    this list of conditions and the following disclaimer.
41  *
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.ObjectInputStream;
70 import java.io.ObjectOutput;
71 import java.io.Serializable;
72 import java.time.Instant;
73 import java.time.LocalDateTime;
74 import java.time.ZoneId;
75 import java.time.ZoneOffset;
76 import java.time.temporal.ChronoField;
77 import java.time.temporal.ChronoUnit;
78 import java.time.temporal.Temporal;
79 import java.time.temporal.TemporalField;
80 import java.time.temporal.TemporalUnit;
81 import java.time.zone.ZoneOffsetTransition;
82 import java.time.zone.ZoneRules;
83 import java.util.List;
84 import java.util.Objects;
85 
86 /**
87  * A date-time with a time-zone in the calendar neutral API.
88  * <p>
89  * {@code ZoneChronoDateTime} is an immutable representation of a date-time with a time-zone.
90  * This class stores all date and time fields, to a precision of nanoseconds,
91  * as well as a time-zone and zone offset.
92  * <p>
93  * The purpose of storing the time-zone is to distinguish the ambiguous case where
94  * the local time-line overlaps, typically as a result of the end of daylight time.
95  * Information about the local-time can be obtained using methods on the time-zone.
96  *
97  * @implSpec
98  * This class is immutable and thread-safe.
99  *
100  * @serial Document the delegation of this class in the serialized-form specification.
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>
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 transient ChronoLocalDateTimeImpl<D> dateTime;
116     /**
117      * The zone offset.
118      */
119     private final transient ZoneOffset offset;
120     /**
121      * The zone ID.
122      */
123     private final transient 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      */
ofBest( ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset)134     static <R extends ChronoLocalDate> 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      */
ofInstant(Chronology chrono, Instant instant, ZoneId zone)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     @SuppressWarnings("unchecked")
create(Instant instant, ZoneId zone)187     private ChronoZonedDateTimeImpl<D> create(Instant instant, ZoneId zone) {
188         return (ChronoZonedDateTimeImpl<D>)ofInstant(getChronology(), instant, zone);
189     }
190 
191     /**
192      * Casts the {@code Temporal} to {@code ChronoZonedDateTimeImpl} ensuring it bas the specified chronology.
193      *
194      * @param chrono  the chronology to check for, not null
195      * @param temporal  a date-time to cast, not null
196      * @return the date-time checked and cast to {@code ChronoZonedDateTimeImpl}, not null
197      * @throws ClassCastException if the date-time cannot be cast to ChronoZonedDateTimeImpl
198      *  or the chronology is not equal this Chronology
199      */
ensureValid(Chronology chrono, Temporal temporal)200     static <R extends ChronoLocalDate> ChronoZonedDateTimeImpl<R> ensureValid(Chronology chrono, Temporal temporal) {
201         @SuppressWarnings("unchecked")
202         ChronoZonedDateTimeImpl<R> other = (ChronoZonedDateTimeImpl<R>) temporal;
203         if (chrono.equals(other.getChronology()) == false) {
204             throw new ClassCastException("Chronology mismatch, required: " + chrono.getId()
205                     + ", actual: " + other.getChronology().getId());
206         }
207         return other;
208     }
209 
210     //-----------------------------------------------------------------------
211     /**
212      * Constructor.
213      *
214      * @param dateTime  the date-time, not null
215      * @param offset  the zone offset, not null
216      * @param zone  the zone ID, not null
217      */
ChronoZonedDateTimeImpl(ChronoLocalDateTimeImpl<D> dateTime, ZoneOffset offset, ZoneId zone)218     private ChronoZonedDateTimeImpl(ChronoLocalDateTimeImpl<D> dateTime, ZoneOffset offset, ZoneId zone) {
219         this.dateTime = Objects.requireNonNull(dateTime, "dateTime");
220         this.offset = Objects.requireNonNull(offset, "offset");
221         this.zone = Objects.requireNonNull(zone, "zone");
222     }
223 
224     //-----------------------------------------------------------------------
225     @Override
getOffset()226     public ZoneOffset getOffset() {
227         return offset;
228     }
229 
230     @Override
withEarlierOffsetAtOverlap()231     public ChronoZonedDateTime<D> withEarlierOffsetAtOverlap() {
232         ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
233         if (trans != null && trans.isOverlap()) {
234             ZoneOffset earlierOffset = trans.getOffsetBefore();
235             if (earlierOffset.equals(offset) == false) {
236                 return new ChronoZonedDateTimeImpl<>(dateTime, earlierOffset, zone);
237             }
238         }
239         return this;
240     }
241 
242     @Override
withLaterOffsetAtOverlap()243     public ChronoZonedDateTime<D> withLaterOffsetAtOverlap() {
244         ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
245         if (trans != null) {
246             ZoneOffset offset = trans.getOffsetAfter();
247             if (offset.equals(getOffset()) == false) {
248                 return new ChronoZonedDateTimeImpl<>(dateTime, offset, zone);
249             }
250         }
251         return this;
252     }
253 
254     //-----------------------------------------------------------------------
255     @Override
toLocalDateTime()256     public ChronoLocalDateTime<D> toLocalDateTime() {
257         return dateTime;
258     }
259 
260     @Override
getZone()261     public ZoneId getZone() {
262         return zone;
263     }
264 
265     @Override
withZoneSameLocal(ZoneId zone)266     public ChronoZonedDateTime<D> withZoneSameLocal(ZoneId zone) {
267         return ofBest(dateTime, zone, offset);
268     }
269 
270     @Override
withZoneSameInstant(ZoneId zone)271     public ChronoZonedDateTime<D> withZoneSameInstant(ZoneId zone) {
272         Objects.requireNonNull(zone, "zone");
273         return this.zone.equals(zone) ? this : create(dateTime.toInstant(offset), zone);
274     }
275 
276     //-----------------------------------------------------------------------
277     @Override
isSupported(TemporalField field)278     public boolean isSupported(TemporalField field) {
279         return field instanceof ChronoField || (field != null && field.isSupportedBy(this));
280     }
281 
282     //-----------------------------------------------------------------------
283     @Override
with(TemporalField field, long newValue)284     public ChronoZonedDateTime<D> with(TemporalField field, long newValue) {
285         if (field instanceof ChronoField) {
286             ChronoField f = (ChronoField) field;
287             switch (f) {
288                 case INSTANT_SECONDS: return plus(newValue - toEpochSecond(), SECONDS);
289                 case OFFSET_SECONDS: {
290                     ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue));
291                     return create(dateTime.toInstant(offset), zone);
292                 }
293             }
294             return ofBest(dateTime.with(field, newValue), zone, offset);
295         }
296         return ChronoZonedDateTimeImpl.ensureValid(getChronology(), field.adjustInto(this, newValue));
297     }
298 
299     //-----------------------------------------------------------------------
300     @Override
plus(long amountToAdd, TemporalUnit unit)301     public ChronoZonedDateTime<D> plus(long amountToAdd, TemporalUnit unit) {
302         if (unit instanceof ChronoUnit) {
303             return with(dateTime.plus(amountToAdd, unit));
304         }
305         return ChronoZonedDateTimeImpl.ensureValid(getChronology(), unit.addTo(this, amountToAdd));   /// TODO: Generics replacement Risk!
306     }
307 
308     //-----------------------------------------------------------------------
309     @Override
until(Temporal endExclusive, TemporalUnit unit)310     public long until(Temporal endExclusive, TemporalUnit unit) {
311         Objects.requireNonNull(endExclusive, "endExclusive");
312         @SuppressWarnings("unchecked")
313         ChronoZonedDateTime<D> end = (ChronoZonedDateTime<D>) getChronology().zonedDateTime(endExclusive);
314         if (unit instanceof ChronoUnit) {
315             end = end.withZoneSameInstant(offset);
316             return dateTime.until(end.toLocalDateTime(), unit);
317         }
318         Objects.requireNonNull(unit, "unit");
319         return unit.between(this, end);
320     }
321 
322     //-----------------------------------------------------------------------
323     /**
324      * Writes the ChronoZonedDateTime using a
325      * <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
326      * @serialData
327      * <pre>
328      *  out.writeByte(3);                  // identifies a ChronoZonedDateTime
329      *  out.writeObject(toLocalDateTime());
330      *  out.writeObject(getOffset());
331      *  out.writeObject(getZone());
332      * </pre>
333      *
334      * @return the instance of {@code Ser}, not null
335      */
writeReplace()336     private Object writeReplace() {
337         return new Ser(Ser.CHRONO_ZONE_DATE_TIME_TYPE, this);
338     }
339 
340     /**
341      * Defend against malicious streams.
342      *
343      * @param s the stream to read
344      * @throws InvalidObjectException always
345      */
readObject(ObjectInputStream s)346     private void readObject(ObjectInputStream s) throws InvalidObjectException {
347         throw new InvalidObjectException("Deserialization via serialization delegate");
348     }
349 
writeExternal(ObjectOutput out)350     void writeExternal(ObjectOutput out) throws IOException {
351         out.writeObject(dateTime);
352         out.writeObject(offset);
353         out.writeObject(zone);
354     }
355 
readExternal(ObjectInput in)356     static ChronoZonedDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
357         ChronoLocalDateTime<?> dateTime = (ChronoLocalDateTime<?>) in.readObject();
358         ZoneOffset offset = (ZoneOffset) in.readObject();
359         ZoneId zone = (ZoneId) in.readObject();
360         return dateTime.atZone(offset).withZoneSameLocal(zone);
361         // TODO: ZDT uses ofLenient()
362     }
363 
364     //-------------------------------------------------------------------------
365     @Override
equals(Object obj)366     public boolean equals(Object obj) {
367         if (this == obj) {
368             return true;
369         }
370         if (obj instanceof ChronoZonedDateTime) {
371             return compareTo((ChronoZonedDateTime<?>) obj) == 0;
372         }
373         return false;
374     }
375 
376     @Override
hashCode()377     public int hashCode() {
378         return toLocalDateTime().hashCode() ^ getOffset().hashCode() ^ Integer.rotateLeft(getZone().hashCode(), 3);
379     }
380 
381     @Override
toString()382     public String toString() {
383         String str = toLocalDateTime().toString() + getOffset().toString();
384         if (getOffset() != getZone()) {
385             str += '[' + getZone().toString() + ']';
386         }
387         return str;
388     }
389 
390 
391 }
392