author | rriggs |
Wed, 11 Dec 2013 16:52:41 -0500 | |
changeset 22108 | 99859c0e9a33 |
parent 22082 | 31eb413d0153 |
child 22566 | 4ebe53dd7814 |
permissions | -rw-r--r-- |
15658 | 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 |
* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos |
|
28 |
* |
|
29 |
* All rights reserved. |
|
30 |
* |
|
31 |
* Redistribution and use in source and binary forms, with or without |
|
32 |
* modification, are permitted provided that the following conditions are met: |
|
33 |
* |
|
34 |
* * Redistributions of source code must retain the above copyright notice, |
|
35 |
* this list of conditions and the following disclaimer. |
|
36 |
* |
|
37 |
* * Redistributions in binary form must reproduce the above copyright notice, |
|
38 |
* this list of conditions and the following disclaimer in the documentation |
|
39 |
* and/or other materials provided with the distribution. |
|
40 |
* |
|
41 |
* * Neither the name of JSR-310 nor the names of its contributors |
|
42 |
* may be used to endorse or promote products derived from this software |
|
43 |
* without specific prior written permission. |
|
44 |
* |
|
45 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
46 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
47 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
48 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
49 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
50 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
51 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
52 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
53 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
54 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
55 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
56 |
*/ |
|
57 |
package java.time.chrono; |
|
58 |
||
59 |
import static java.time.chrono.MinguoChronology.YEARS_DIFFERENCE; |
|
60 |
import static java.time.temporal.ChronoField.DAY_OF_MONTH; |
|
61 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR; |
|
62 |
import static java.time.temporal.ChronoField.YEAR; |
|
63 |
||
64 |
import java.io.DataInput; |
|
65 |
import java.io.DataOutput; |
|
66 |
import java.io.IOException; |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
67 |
import java.io.InvalidObjectException; |
22081 | 68 |
import java.io.ObjectInputStream; |
15658 | 69 |
import java.io.Serializable; |
70 |
import java.time.Clock; |
|
71 |
import java.time.DateTimeException; |
|
72 |
import java.time.LocalDate; |
|
73 |
import java.time.LocalTime; |
|
74 |
import java.time.Period; |
|
75 |
import java.time.ZoneId; |
|
76 |
import java.time.temporal.ChronoField; |
|
77 |
import java.time.temporal.TemporalAccessor; |
|
78 |
import java.time.temporal.TemporalAdjuster; |
|
79 |
import java.time.temporal.TemporalAmount; |
|
80 |
import java.time.temporal.TemporalField; |
|
16852 | 81 |
import java.time.temporal.TemporalQuery; |
15658 | 82 |
import java.time.temporal.TemporalUnit; |
16852 | 83 |
import java.time.temporal.UnsupportedTemporalTypeException; |
15658 | 84 |
import java.time.temporal.ValueRange; |
85 |
import java.util.Objects; |
|
86 |
||
87 |
/** |
|
88 |
* A date in the Minguo calendar system. |
|
89 |
* <p> |
|
90 |
* This date operates using the {@linkplain MinguoChronology Minguo calendar}. |
|
91 |
* This calendar system is primarily used in the Republic of China, often known as Taiwan. |
|
92 |
* Dates are aligned such that {@code 0001-01-01 (Minguo)} is {@code 1912-01-01 (ISO)}. |
|
93 |
* |
|
22108
99859c0e9a33
8029551: Add value-type notice to java.time classes
rriggs
parents:
22082
diff
changeset
|
94 |
* <p> |
99859c0e9a33
8029551: Add value-type notice to java.time classes
rriggs
parents:
22082
diff
changeset
|
95 |
* This is a <a href="{@docRoot}/java/lang/doc-files/ValueBased.html">value-based</a> |
99859c0e9a33
8029551: Add value-type notice to java.time classes
rriggs
parents:
22082
diff
changeset
|
96 |
* class; use of identity-sensitive operations (including reference equality |
99859c0e9a33
8029551: Add value-type notice to java.time classes
rriggs
parents:
22082
diff
changeset
|
97 |
* ({@code ==}), identity hash code, or synchronization) on instances of |
99859c0e9a33
8029551: Add value-type notice to java.time classes
rriggs
parents:
22082
diff
changeset
|
98 |
* {@code MinguoDate} may have unpredictable results and should be avoided. |
99859c0e9a33
8029551: Add value-type notice to java.time classes
rriggs
parents:
22082
diff
changeset
|
99 |
* The {@code equals} method should be used for comparisons. |
99859c0e9a33
8029551: Add value-type notice to java.time classes
rriggs
parents:
22082
diff
changeset
|
100 |
* |
17474 | 101 |
* @implSpec |
15658 | 102 |
* This class is immutable and thread-safe. |
103 |
* |
|
104 |
* @since 1.8 |
|
105 |
*/ |
|
106 |
public final class MinguoDate |
|
20518 | 107 |
extends ChronoLocalDateImpl<MinguoDate> |
19030 | 108 |
implements ChronoLocalDate, Serializable { |
15658 | 109 |
|
110 |
/** |
|
111 |
* Serialization version. |
|
112 |
*/ |
|
113 |
private static final long serialVersionUID = 1300372329181994526L; |
|
114 |
||
115 |
/** |
|
116 |
* The underlying date. |
|
117 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
118 |
private final transient LocalDate isoDate; |
15658 | 119 |
|
120 |
//----------------------------------------------------------------------- |
|
121 |
/** |
|
122 |
* Obtains the current {@code MinguoDate} from the system clock in the default time-zone. |
|
123 |
* <p> |
|
124 |
* This will query the {@link Clock#systemDefaultZone() system clock} in the default |
|
125 |
* time-zone to obtain the current date. |
|
126 |
* <p> |
|
127 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
128 |
* because the clock is hard-coded. |
|
129 |
* |
|
130 |
* @return the current date using the system clock and default time-zone, not null |
|
131 |
*/ |
|
132 |
public static MinguoDate now() { |
|
133 |
return now(Clock.systemDefaultZone()); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Obtains the current {@code MinguoDate} from the system clock in the specified time-zone. |
|
138 |
* <p> |
|
139 |
* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. |
|
140 |
* Specifying the time-zone avoids dependence on the default time-zone. |
|
141 |
* <p> |
|
142 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
143 |
* because the clock is hard-coded. |
|
144 |
* |
|
145 |
* @param zone the zone ID to use, not null |
|
146 |
* @return the current date using the system clock, not null |
|
147 |
*/ |
|
148 |
public static MinguoDate now(ZoneId zone) { |
|
149 |
return now(Clock.system(zone)); |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Obtains the current {@code MinguoDate} from the specified clock. |
|
154 |
* <p> |
|
155 |
* This will query the specified clock to obtain the current date - today. |
|
156 |
* Using this method allows the use of an alternate clock for testing. |
|
157 |
* The alternate clock may be introduced using {@linkplain Clock dependency injection}. |
|
158 |
* |
|
159 |
* @param clock the clock to use, not null |
|
160 |
* @return the current date, not null |
|
161 |
* @throws DateTimeException if the current date cannot be obtained |
|
162 |
*/ |
|
163 |
public static MinguoDate now(Clock clock) { |
|
19030 | 164 |
return new MinguoDate(LocalDate.now(clock)); |
15658 | 165 |
} |
166 |
||
167 |
/** |
|
168 |
* Obtains a {@code MinguoDate} representing a date in the Minguo calendar |
|
169 |
* system from the proleptic-year, month-of-year and day-of-month fields. |
|
170 |
* <p> |
|
171 |
* This returns a {@code MinguoDate} with the specified fields. |
|
172 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
|
173 |
* |
|
174 |
* @param prolepticYear the Minguo proleptic-year |
|
175 |
* @param month the Minguo month-of-year, from 1 to 12 |
|
176 |
* @param dayOfMonth the Minguo day-of-month, from 1 to 31 |
|
177 |
* @return the date in Minguo calendar system, not null |
|
178 |
* @throws DateTimeException if the value of any field is out of range, |
|
179 |
* or if the day-of-month is invalid for the month-year |
|
180 |
*/ |
|
181 |
public static MinguoDate of(int prolepticYear, int month, int dayOfMonth) { |
|
182 |
return new MinguoDate(LocalDate.of(prolepticYear + YEARS_DIFFERENCE, month, dayOfMonth)); |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Obtains a {@code MinguoDate} from a temporal object. |
|
187 |
* <p> |
|
188 |
* This obtains a date in the Minguo calendar system based on the specified temporal. |
|
189 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
190 |
* which this factory converts to an instance of {@code MinguoDate}. |
|
191 |
* <p> |
|
192 |
* The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY} |
|
193 |
* field, which is standardized across calendar systems. |
|
194 |
* <p> |
|
195 |
* This method matches the signature of the functional interface {@link TemporalQuery} |
|
196 |
* allowing it to be used as a query via method reference, {@code MinguoDate::from}. |
|
197 |
* |
|
198 |
* @param temporal the temporal object to convert, not null |
|
199 |
* @return the date in Minguo calendar system, not null |
|
200 |
* @throws DateTimeException if unable to convert to a {@code MinguoDate} |
|
201 |
*/ |
|
202 |
public static MinguoDate from(TemporalAccessor temporal) { |
|
203 |
return MinguoChronology.INSTANCE.date(temporal); |
|
204 |
} |
|
205 |
||
206 |
//----------------------------------------------------------------------- |
|
207 |
/** |
|
208 |
* Creates an instance from an ISO date. |
|
209 |
* |
|
210 |
* @param isoDate the standard local date, validated not null |
|
211 |
*/ |
|
212 |
MinguoDate(LocalDate isoDate) { |
|
213 |
Objects.requireNonNull(isoDate, "isoDate"); |
|
214 |
this.isoDate = isoDate; |
|
215 |
} |
|
216 |
||
217 |
//----------------------------------------------------------------------- |
|
16852 | 218 |
/** |
219 |
* Gets the chronology of this date, which is the Minguo calendar system. |
|
220 |
* <p> |
|
221 |
* The {@code Chronology} represents the calendar system in use. |
|
222 |
* The era and other fields in {@link ChronoField} are defined by the chronology. |
|
223 |
* |
|
224 |
* @return the Minguo chronology, not null |
|
225 |
*/ |
|
15658 | 226 |
@Override |
227 |
public MinguoChronology getChronology() { |
|
228 |
return MinguoChronology.INSTANCE; |
|
229 |
} |
|
230 |
||
16852 | 231 |
/** |
232 |
* Gets the era applicable at this date. |
|
233 |
* <p> |
|
234 |
* The Minguo calendar system has two eras, 'ROC' and 'BEFORE_ROC', |
|
235 |
* defined by {@link MinguoEra}. |
|
236 |
* |
|
237 |
* @return the era applicable at this date, not null |
|
238 |
*/ |
|
239 |
@Override |
|
240 |
public MinguoEra getEra() { |
|
241 |
return (getProlepticYear() >= 1 ? MinguoEra.ROC : MinguoEra.BEFORE_ROC); |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* Returns the length of the month represented by this date. |
|
246 |
* <p> |
|
247 |
* This returns the length of the month in days. |
|
248 |
* Month lengths match those of the ISO calendar system. |
|
249 |
* |
|
250 |
* @return the length of the month in days |
|
251 |
*/ |
|
15658 | 252 |
@Override |
253 |
public int lengthOfMonth() { |
|
254 |
return isoDate.lengthOfMonth(); |
|
255 |
} |
|
256 |
||
16852 | 257 |
//----------------------------------------------------------------------- |
15658 | 258 |
@Override |
259 |
public ValueRange range(TemporalField field) { |
|
260 |
if (field instanceof ChronoField) { |
|
261 |
if (isSupported(field)) { |
|
262 |
ChronoField f = (ChronoField) field; |
|
263 |
switch (f) { |
|
264 |
case DAY_OF_MONTH: |
|
265 |
case DAY_OF_YEAR: |
|
266 |
case ALIGNED_WEEK_OF_MONTH: |
|
267 |
return isoDate.range(field); |
|
268 |
case YEAR_OF_ERA: { |
|
269 |
ValueRange range = YEAR.range(); |
|
270 |
long max = (getProlepticYear() <= 0 ? -range.getMinimum() + 1 + YEARS_DIFFERENCE : range.getMaximum() - YEARS_DIFFERENCE); |
|
271 |
return ValueRange.of(1, max); |
|
272 |
} |
|
273 |
} |
|
274 |
return getChronology().range(f); |
|
275 |
} |
|
19030 | 276 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15658 | 277 |
} |
278 |
return field.rangeRefinedBy(this); |
|
279 |
} |
|
280 |
||
281 |
@Override |
|
282 |
public long getLong(TemporalField field) { |
|
283 |
if (field instanceof ChronoField) { |
|
284 |
switch ((ChronoField) field) { |
|
16852 | 285 |
case PROLEPTIC_MONTH: |
286 |
return getProlepticMonth(); |
|
15658 | 287 |
case YEAR_OF_ERA: { |
288 |
int prolepticYear = getProlepticYear(); |
|
289 |
return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear); |
|
290 |
} |
|
291 |
case YEAR: |
|
292 |
return getProlepticYear(); |
|
293 |
case ERA: |
|
294 |
return (getProlepticYear() >= 1 ? 1 : 0); |
|
295 |
} |
|
296 |
return isoDate.getLong(field); |
|
297 |
} |
|
298 |
return field.getFrom(this); |
|
299 |
} |
|
300 |
||
16852 | 301 |
private long getProlepticMonth() { |
302 |
return getProlepticYear() * 12L + isoDate.getMonthValue() - 1; |
|
303 |
} |
|
304 |
||
15658 | 305 |
private int getProlepticYear() { |
306 |
return isoDate.getYear() - YEARS_DIFFERENCE; |
|
307 |
} |
|
308 |
||
309 |
//----------------------------------------------------------------------- |
|
310 |
@Override |
|
311 |
public MinguoDate with(TemporalField field, long newValue) { |
|
312 |
if (field instanceof ChronoField) { |
|
313 |
ChronoField f = (ChronoField) field; |
|
314 |
if (getLong(f) == newValue) { |
|
315 |
return this; |
|
316 |
} |
|
317 |
switch (f) { |
|
16852 | 318 |
case PROLEPTIC_MONTH: |
319 |
getChronology().range(f).checkValidValue(newValue, f); |
|
320 |
return plusMonths(newValue - getProlepticMonth()); |
|
15658 | 321 |
case YEAR_OF_ERA: |
322 |
case YEAR: |
|
323 |
case ERA: { |
|
16852 | 324 |
int nvalue = getChronology().range(f).checkValidIntValue(newValue, f); |
15658 | 325 |
switch (f) { |
326 |
case YEAR_OF_ERA: |
|
327 |
return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue) + YEARS_DIFFERENCE)); |
|
328 |
case YEAR: |
|
329 |
return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE)); |
|
330 |
case ERA: |
|
331 |
return with(isoDate.withYear((1 - getProlepticYear()) + YEARS_DIFFERENCE)); |
|
332 |
} |
|
333 |
} |
|
334 |
} |
|
335 |
return with(isoDate.with(field, newValue)); |
|
336 |
} |
|
19030 | 337 |
return super.with(field, newValue); |
15658 | 338 |
} |
339 |
||
340 |
/** |
|
341 |
* {@inheritDoc} |
|
342 |
* @throws DateTimeException {@inheritDoc} |
|
343 |
* @throws ArithmeticException {@inheritDoc} |
|
344 |
*/ |
|
345 |
@Override |
|
346 |
public MinguoDate with(TemporalAdjuster adjuster) { |
|
16852 | 347 |
return super.with(adjuster); |
15658 | 348 |
} |
349 |
||
350 |
/** |
|
351 |
* {@inheritDoc} |
|
352 |
* @throws DateTimeException {@inheritDoc} |
|
353 |
* @throws ArithmeticException {@inheritDoc} |
|
354 |
*/ |
|
355 |
@Override |
|
356 |
public MinguoDate plus(TemporalAmount amount) { |
|
16852 | 357 |
return super.plus(amount); |
15658 | 358 |
} |
359 |
||
360 |
/** |
|
361 |
* {@inheritDoc} |
|
362 |
* @throws DateTimeException {@inheritDoc} |
|
363 |
* @throws ArithmeticException {@inheritDoc} |
|
364 |
*/ |
|
365 |
@Override |
|
366 |
public MinguoDate minus(TemporalAmount amount) { |
|
16852 | 367 |
return super.minus(amount); |
15658 | 368 |
} |
369 |
||
370 |
//----------------------------------------------------------------------- |
|
371 |
@Override |
|
372 |
MinguoDate plusYears(long years) { |
|
373 |
return with(isoDate.plusYears(years)); |
|
374 |
} |
|
375 |
||
376 |
@Override |
|
377 |
MinguoDate plusMonths(long months) { |
|
378 |
return with(isoDate.plusMonths(months)); |
|
379 |
} |
|
380 |
||
381 |
@Override |
|
19030 | 382 |
MinguoDate plusWeeks(long weeksToAdd) { |
383 |
return super.plusWeeks(weeksToAdd); |
|
384 |
} |
|
385 |
||
386 |
@Override |
|
15658 | 387 |
MinguoDate plusDays(long days) { |
388 |
return with(isoDate.plusDays(days)); |
|
389 |
} |
|
390 |
||
391 |
@Override |
|
392 |
public MinguoDate plus(long amountToAdd, TemporalUnit unit) { |
|
16852 | 393 |
return super.plus(amountToAdd, unit); |
15658 | 394 |
} |
395 |
||
396 |
@Override |
|
397 |
public MinguoDate minus(long amountToAdd, TemporalUnit unit) { |
|
16852 | 398 |
return super.minus(amountToAdd, unit); |
15658 | 399 |
} |
400 |
||
401 |
@Override |
|
402 |
MinguoDate minusYears(long yearsToSubtract) { |
|
16852 | 403 |
return super.minusYears(yearsToSubtract); |
15658 | 404 |
} |
405 |
||
406 |
@Override |
|
407 |
MinguoDate minusMonths(long monthsToSubtract) { |
|
16852 | 408 |
return super.minusMonths(monthsToSubtract); |
15658 | 409 |
} |
410 |
||
411 |
@Override |
|
412 |
MinguoDate minusWeeks(long weeksToSubtract) { |
|
16852 | 413 |
return super.minusWeeks(weeksToSubtract); |
15658 | 414 |
} |
415 |
||
416 |
@Override |
|
417 |
MinguoDate minusDays(long daysToSubtract) { |
|
16852 | 418 |
return super.minusDays(daysToSubtract); |
15658 | 419 |
} |
420 |
||
421 |
private MinguoDate with(LocalDate newDate) { |
|
422 |
return (newDate.equals(isoDate) ? this : new MinguoDate(newDate)); |
|
423 |
} |
|
424 |
||
425 |
@Override // for javadoc and covariant return type |
|
19030 | 426 |
@SuppressWarnings("unchecked") |
15658 | 427 |
public final ChronoLocalDateTime<MinguoDate> atTime(LocalTime localTime) { |
19030 | 428 |
return (ChronoLocalDateTime<MinguoDate>)super.atTime(localTime); |
15658 | 429 |
} |
430 |
||
431 |
@Override |
|
20519
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
20518
diff
changeset
|
432 |
public ChronoPeriod until(ChronoLocalDate endDate) { |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
20518
diff
changeset
|
433 |
Period period = isoDate.until(endDate); |
eee7a92074fd
8023762: Add ChronoPeriod interface and bind period to Chronology
rriggs
parents:
20518
diff
changeset
|
434 |
return getChronology().period(period.getYears(), period.getMonths(), period.getDays()); |
15658 | 435 |
} |
436 |
||
437 |
@Override // override for performance |
|
438 |
public long toEpochDay() { |
|
439 |
return isoDate.toEpochDay(); |
|
440 |
} |
|
441 |
||
442 |
//------------------------------------------------------------------------- |
|
22082
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
443 |
/** |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
444 |
* Compares this date to another date, including the chronology. |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
445 |
* <p> |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
446 |
* Compares this {@code MinguoDate} with another ensuring that the date is the same. |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
447 |
* <p> |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
448 |
* Only objects of type {@code MinguoDate} are compared, other types return false. |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
449 |
* To compare the dates of two {@code TemporalAccessor} instances, including dates |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
450 |
* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator. |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
451 |
* |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
452 |
* @param obj the object to check, null returns false |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
453 |
* @return true if this is equal to the other date |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
454 |
*/ |
15658 | 455 |
@Override // override for performance |
456 |
public boolean equals(Object obj) { |
|
457 |
if (this == obj) { |
|
458 |
return true; |
|
459 |
} |
|
460 |
if (obj instanceof MinguoDate) { |
|
461 |
MinguoDate otherDate = (MinguoDate) obj; |
|
462 |
return this.isoDate.equals(otherDate.isoDate); |
|
463 |
} |
|
464 |
return false; |
|
465 |
} |
|
466 |
||
22082
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
467 |
/** |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
468 |
* A hash code for this date. |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
469 |
* |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
470 |
* @return a suitable hash code based only on the Chronology and the date |
31eb413d0153
8029909: Clarify equals/hashcode behavior for java.time types
rriggs
parents:
22081
diff
changeset
|
471 |
*/ |
15658 | 472 |
@Override // override for performance |
473 |
public int hashCode() { |
|
474 |
return getChronology().getId().hashCode() ^ isoDate.hashCode(); |
|
475 |
} |
|
476 |
||
477 |
//----------------------------------------------------------------------- |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
478 |
/** |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
479 |
* Defend against malicious streams. |
22081 | 480 |
* |
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
481 |
* @throws InvalidObjectException always |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
482 |
*/ |
22081 | 483 |
private void readObject(ObjectInputStream s) throws InvalidObjectException { |
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
484 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
485 |
} |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
486 |
|
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
487 |
/** |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
488 |
* Writes the object using a |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
489 |
* <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
490 |
* @serialData |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
491 |
* <pre> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
492 |
* out.writeByte(8); // identifies a MinguoDate |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
493 |
* out.writeInt(get(YEAR)); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
494 |
* out.writeByte(get(MONTH_OF_YEAR)); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
495 |
* out.writeByte(get(DAY_OF_MONTH)); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
496 |
* </pre> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
497 |
* |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
498 |
* @return the instance of {@code Ser}, not null |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
499 |
*/ |
15658 | 500 |
private Object writeReplace() { |
501 |
return new Ser(Ser.MINGUO_DATE_TYPE, this); |
|
502 |
} |
|
503 |
||
504 |
void writeExternal(DataOutput out) throws IOException { |
|
505 |
// MinguoChronology is implicit in the MINGUO_DATE_TYPE |
|
506 |
out.writeInt(get(YEAR)); |
|
507 |
out.writeByte(get(MONTH_OF_YEAR)); |
|
508 |
out.writeByte(get(DAY_OF_MONTH)); |
|
509 |
} |
|
510 |
||
19030 | 511 |
static MinguoDate readExternal(DataInput in) throws IOException { |
15658 | 512 |
int year = in.readInt(); |
513 |
int month = in.readByte(); |
|
514 |
int dayOfMonth = in.readByte(); |
|
515 |
return MinguoChronology.INSTANCE.date(year, month, dayOfMonth); |
|
516 |
} |
|
517 |
||
518 |
} |