author | rriggs |
Wed, 11 Sep 2013 10:16:21 -0400 | |
changeset 19841 | 15c8e97d6a14 |
parent 19030 | 32f129cb6351 |
child 20518 | dde564773845 |
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.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; |
|
60 |
import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; |
|
61 |
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; |
|
62 |
import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; |
|
63 |
import static java.time.temporal.ChronoField.DAY_OF_MONTH; |
|
64 |
import static java.time.temporal.ChronoField.MONTH_OF_YEAR; |
|
65 |
import static java.time.temporal.ChronoField.YEAR; |
|
66 |
||
67 |
import java.io.IOException; |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
68 |
import java.io.InvalidObjectException; |
15658 | 69 |
import java.io.ObjectInput; |
70 |
import java.io.ObjectOutput; |
|
71 |
import java.io.Serializable; |
|
72 |
import java.time.Clock; |
|
73 |
import java.time.DateTimeException; |
|
74 |
import java.time.LocalDate; |
|
75 |
import java.time.LocalTime; |
|
76 |
import java.time.Period; |
|
77 |
import java.time.ZoneId; |
|
78 |
import java.time.temporal.ChronoField; |
|
79 |
import java.time.temporal.TemporalAccessor; |
|
80 |
import java.time.temporal.TemporalAdjuster; |
|
81 |
import java.time.temporal.TemporalAmount; |
|
82 |
import java.time.temporal.TemporalField; |
|
16852 | 83 |
import java.time.temporal.TemporalQuery; |
15658 | 84 |
import java.time.temporal.TemporalUnit; |
16852 | 85 |
import java.time.temporal.UnsupportedTemporalTypeException; |
15658 | 86 |
import java.time.temporal.ValueRange; |
87 |
||
88 |
/** |
|
89 |
* A date in the Hijrah calendar system. |
|
90 |
* <p> |
|
16852 | 91 |
* This date operates using one of several variants of the |
92 |
* {@linkplain HijrahChronology Hijrah calendar}. |
|
15658 | 93 |
* <p> |
94 |
* The Hijrah calendar has a different total of days in a year than |
|
16852 | 95 |
* Gregorian calendar, and the length of each month is based on the period |
96 |
* of a complete revolution of the moon around the earth |
|
97 |
* (as between successive new moons). |
|
98 |
* Refer to the {@link HijrahChronology} for details of supported variants. |
|
15658 | 99 |
* <p> |
16852 | 100 |
* Each HijrahDate is created bound to a particular HijrahChronology, |
101 |
* The same chronology is propagated to each HijrahDate computed from the date. |
|
102 |
* To use a different Hijrah variant, its HijrahChronology can be used |
|
103 |
* to create new HijrahDate instances. |
|
104 |
* Alternatively, the {@link #withVariant} method can be used to convert |
|
105 |
* to a new HijrahChronology. |
|
17474 | 106 |
* @implSpec |
15658 | 107 |
* This class is immutable and thread-safe. |
108 |
* |
|
109 |
* @since 1.8 |
|
110 |
*/ |
|
111 |
public final class HijrahDate |
|
112 |
extends ChronoDateImpl<HijrahDate> |
|
19030 | 113 |
implements ChronoLocalDate, Serializable { |
15658 | 114 |
|
115 |
/** |
|
116 |
* Serialization version. |
|
117 |
*/ |
|
118 |
private static final long serialVersionUID = -5207853542612002020L; |
|
119 |
/** |
|
120 |
* The Chronology of this HijrahDate. |
|
121 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
122 |
private final transient HijrahChronology chrono; |
15658 | 123 |
/** |
16852 | 124 |
* The proleptic year. |
15658 | 125 |
*/ |
16852 | 126 |
private final transient int prolepticYear; |
15658 | 127 |
/** |
128 |
* The month-of-year. |
|
129 |
*/ |
|
130 |
private final transient int monthOfYear; |
|
131 |
/** |
|
132 |
* The day-of-month. |
|
133 |
*/ |
|
134 |
private final transient int dayOfMonth; |
|
135 |
||
136 |
//------------------------------------------------------------------------- |
|
137 |
/** |
|
16852 | 138 |
* Obtains an instance of {@code HijrahDate} from the Hijrah proleptic year, |
139 |
* month-of-year and day-of-month. |
|
15658 | 140 |
* |
16852 | 141 |
* @param prolepticYear the proleptic year to represent in the Hijrah calendar |
15658 | 142 |
* @param monthOfYear the month-of-year to represent, from 1 to 12 |
143 |
* @param dayOfMonth the day-of-month to represent, from 1 to 30 |
|
144 |
* @return the Hijrah date, never null |
|
145 |
* @throws DateTimeException if the value of any field is out of range |
|
146 |
*/ |
|
147 |
static HijrahDate of(HijrahChronology chrono, int prolepticYear, int monthOfYear, int dayOfMonth) { |
|
16852 | 148 |
return new HijrahDate(chrono, prolepticYear, monthOfYear, dayOfMonth); |
15658 | 149 |
} |
150 |
||
151 |
/** |
|
16852 | 152 |
* Returns a HijrahDate for the chronology and epochDay. |
153 |
* @param chrono The Hijrah chronology |
|
154 |
* @param epochDay the epoch day |
|
155 |
* @return a HijrahDate for the epoch day; non-null |
|
15658 | 156 |
*/ |
157 |
static HijrahDate ofEpochDay(HijrahChronology chrono, long epochDay) { |
|
158 |
return new HijrahDate(chrono, epochDay); |
|
159 |
} |
|
160 |
||
161 |
//----------------------------------------------------------------------- |
|
162 |
/** |
|
16852 | 163 |
* Obtains the current {@code HijrahDate} of the Islamic Umm Al-Qura calendar |
164 |
* in the default time-zone. |
|
15658 | 165 |
* <p> |
166 |
* This will query the {@link Clock#systemDefaultZone() system clock} in the default |
|
167 |
* time-zone to obtain the current date. |
|
168 |
* <p> |
|
169 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
170 |
* because the clock is hard-coded. |
|
171 |
* |
|
172 |
* @return the current date using the system clock and default time-zone, not null |
|
173 |
*/ |
|
174 |
public static HijrahDate now() { |
|
175 |
return now(Clock.systemDefaultZone()); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
16852 | 179 |
* Obtains the current {@code HijrahDate} of the Islamic Umm Al-Qura calendar |
180 |
* in the specified time-zone. |
|
15658 | 181 |
* <p> |
182 |
* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. |
|
183 |
* Specifying the time-zone avoids dependence on the default time-zone. |
|
184 |
* <p> |
|
185 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
186 |
* because the clock is hard-coded. |
|
187 |
* |
|
188 |
* @param zone the zone ID to use, not null |
|
189 |
* @return the current date using the system clock, not null |
|
190 |
*/ |
|
191 |
public static HijrahDate now(ZoneId zone) { |
|
192 |
return now(Clock.system(zone)); |
|
193 |
} |
|
194 |
||
195 |
/** |
|
16852 | 196 |
* Obtains the current {@code HijrahDate} of the Islamic Umm Al-Qura calendar |
197 |
* from the specified clock. |
|
15658 | 198 |
* <p> |
199 |
* This will query the specified clock to obtain the current date - today. |
|
200 |
* Using this method allows the use of an alternate clock for testing. |
|
201 |
* The alternate clock may be introduced using {@linkplain Clock dependency injection}. |
|
202 |
* |
|
203 |
* @param clock the clock to use, not null |
|
204 |
* @return the current date, not null |
|
205 |
* @throws DateTimeException if the current date cannot be obtained |
|
206 |
*/ |
|
207 |
public static HijrahDate now(Clock clock) { |
|
19030 | 208 |
return HijrahDate.ofEpochDay(HijrahChronology.INSTANCE, LocalDate.now(clock).toEpochDay()); |
15658 | 209 |
} |
210 |
||
211 |
/** |
|
16852 | 212 |
* Obtains a {@code HijrahDate} of the Islamic Umm Al-Qura calendar |
213 |
* from the proleptic-year, month-of-year and day-of-month fields. |
|
15658 | 214 |
* <p> |
215 |
* This returns a {@code HijrahDate} with the specified fields. |
|
216 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
|
217 |
* |
|
218 |
* @param prolepticYear the Hijrah proleptic-year |
|
219 |
* @param month the Hijrah month-of-year, from 1 to 12 |
|
220 |
* @param dayOfMonth the Hijrah day-of-month, from 1 to 30 |
|
221 |
* @return the date in Hijrah calendar system, not null |
|
222 |
* @throws DateTimeException if the value of any field is out of range, |
|
223 |
* or if the day-of-month is invalid for the month-year |
|
224 |
*/ |
|
225 |
public static HijrahDate of(int prolepticYear, int month, int dayOfMonth) { |
|
226 |
return HijrahChronology.INSTANCE.date(prolepticYear, month, dayOfMonth); |
|
227 |
} |
|
228 |
||
229 |
/** |
|
16852 | 230 |
* Obtains a {@code HijrahDate} of the Islamic Umm Al-Qura calendar from a temporal object. |
15658 | 231 |
* <p> |
232 |
* This obtains a date in the Hijrah calendar system based on the specified temporal. |
|
233 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
234 |
* which this factory converts to an instance of {@code HijrahDate}. |
|
235 |
* <p> |
|
236 |
* The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY} |
|
237 |
* field, which is standardized across calendar systems. |
|
238 |
* <p> |
|
239 |
* This method matches the signature of the functional interface {@link TemporalQuery} |
|
240 |
* allowing it to be used as a query via method reference, {@code HijrahDate::from}. |
|
241 |
* |
|
242 |
* @param temporal the temporal object to convert, not null |
|
243 |
* @return the date in Hijrah calendar system, not null |
|
244 |
* @throws DateTimeException if unable to convert to a {@code HijrahDate} |
|
245 |
*/ |
|
246 |
public static HijrahDate from(TemporalAccessor temporal) { |
|
247 |
return HijrahChronology.INSTANCE.date(temporal); |
|
248 |
} |
|
249 |
||
250 |
//----------------------------------------------------------------------- |
|
251 |
/** |
|
16852 | 252 |
* Constructs an {@code HijrahDate} with the proleptic-year, month-of-year and |
253 |
* day-of-month fields. |
|
15658 | 254 |
* |
16852 | 255 |
* @param chrono The chronology to create the date with |
256 |
* @param prolepticYear the proleptic year |
|
257 |
* @param monthOfYear the month of year |
|
258 |
* @param dayOfMonth the day of month |
|
15658 | 259 |
*/ |
16852 | 260 |
private HijrahDate(HijrahChronology chrono, int prolepticYear, int monthOfYear, int dayOfMonth) { |
261 |
// Computing the Gregorian day checks the valid ranges |
|
262 |
chrono.getEpochDay(prolepticYear, monthOfYear, dayOfMonth); |
|
15658 | 263 |
|
16852 | 264 |
this.chrono = chrono; |
265 |
this.prolepticYear = prolepticYear; |
|
266 |
this.monthOfYear = monthOfYear; |
|
267 |
this.dayOfMonth = dayOfMonth; |
|
268 |
} |
|
15658 | 269 |
|
16852 | 270 |
/** |
271 |
* Constructs an instance with the Epoch Day. |
|
272 |
* |
|
273 |
* @param epochDay the epochDay |
|
274 |
*/ |
|
275 |
private HijrahDate(HijrahChronology chrono, long epochDay) { |
|
276 |
int[] dateInfo = chrono.getHijrahDateInfo((int)epochDay); |
|
277 |
||
278 |
this.chrono = chrono; |
|
279 |
this.prolepticYear = dateInfo[0]; |
|
280 |
this.monthOfYear = dateInfo[1]; |
|
281 |
this.dayOfMonth = dateInfo[2]; |
|
15658 | 282 |
} |
283 |
||
284 |
//----------------------------------------------------------------------- |
|
16852 | 285 |
/** |
286 |
* Gets the chronology of this date, which is the Hijrah calendar system. |
|
287 |
* <p> |
|
288 |
* The {@code Chronology} represents the calendar system in use. |
|
289 |
* The era and other fields in {@link ChronoField} are defined by the chronology. |
|
290 |
* |
|
291 |
* @return the Hijrah chronology, not null |
|
292 |
*/ |
|
15658 | 293 |
@Override |
294 |
public HijrahChronology getChronology() { |
|
295 |
return chrono; |
|
296 |
} |
|
297 |
||
16852 | 298 |
/** |
299 |
* Gets the era applicable at this date. |
|
300 |
* <p> |
|
301 |
* The Hijrah calendar system has one era, 'AH', |
|
302 |
* defined by {@link HijrahEra}. |
|
303 |
* |
|
304 |
* @return the era applicable at this date, not null |
|
305 |
*/ |
|
306 |
@Override |
|
307 |
public HijrahEra getEra() { |
|
308 |
return HijrahEra.AH; |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* Returns the length of the month represented by this date. |
|
313 |
* <p> |
|
314 |
* This returns the length of the month in days. |
|
315 |
* Month lengths in the Hijrah calendar system vary between 29 and 30 days. |
|
316 |
* |
|
317 |
* @return the length of the month in days |
|
318 |
*/ |
|
319 |
@Override |
|
320 |
public int lengthOfMonth() { |
|
321 |
return chrono.getMonthLength(prolepticYear, monthOfYear); |
|
322 |
} |
|
323 |
||
324 |
/** |
|
325 |
* Returns the length of the year represented by this date. |
|
326 |
* <p> |
|
327 |
* This returns the length of the year in days. |
|
328 |
* A Hijrah calendar system year is typically shorter than |
|
329 |
* that of the ISO calendar system. |
|
330 |
* |
|
331 |
* @return the length of the year in days |
|
332 |
*/ |
|
333 |
@Override |
|
334 |
public int lengthOfYear() { |
|
335 |
return chrono.getYearLength(prolepticYear); |
|
336 |
} |
|
337 |
||
338 |
//----------------------------------------------------------------------- |
|
15658 | 339 |
@Override |
340 |
public ValueRange range(TemporalField field) { |
|
341 |
if (field instanceof ChronoField) { |
|
342 |
if (isSupported(field)) { |
|
343 |
ChronoField f = (ChronoField) field; |
|
344 |
switch (f) { |
|
345 |
case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); |
|
346 |
case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); |
|
347 |
case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); // TODO |
|
16852 | 348 |
// TODO does the limited range of valid years cause years to |
349 |
// start/end part way through? that would affect range |
|
15658 | 350 |
} |
351 |
return getChronology().range(f); |
|
352 |
} |
|
19030 | 353 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15658 | 354 |
} |
355 |
return field.rangeRefinedBy(this); |
|
356 |
} |
|
357 |
||
358 |
@Override |
|
359 |
public long getLong(TemporalField field) { |
|
360 |
if (field instanceof ChronoField) { |
|
361 |
switch ((ChronoField) field) { |
|
16852 | 362 |
case DAY_OF_WEEK: return getDayOfWeek(); |
363 |
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((getDayOfWeek() - 1) % 7) + 1; |
|
364 |
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1; |
|
15658 | 365 |
case DAY_OF_MONTH: return this.dayOfMonth; |
16852 | 366 |
case DAY_OF_YEAR: return this.getDayOfYear(); |
15658 | 367 |
case EPOCH_DAY: return toEpochDay(); |
368 |
case ALIGNED_WEEK_OF_MONTH: return ((dayOfMonth - 1) / 7) + 1; |
|
16852 | 369 |
case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1; |
15658 | 370 |
case MONTH_OF_YEAR: return monthOfYear; |
16852 | 371 |
case PROLEPTIC_MONTH: return getProlepticMonth(); |
372 |
case YEAR_OF_ERA: return prolepticYear; |
|
373 |
case YEAR: return prolepticYear; |
|
374 |
case ERA: return getEraValue(); |
|
15658 | 375 |
} |
19030 | 376 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15658 | 377 |
} |
378 |
return field.getFrom(this); |
|
379 |
} |
|
380 |
||
16852 | 381 |
private long getProlepticMonth() { |
382 |
return prolepticYear * 12L + monthOfYear - 1; |
|
383 |
} |
|
384 |
||
15658 | 385 |
@Override |
386 |
public HijrahDate with(TemporalField field, long newValue) { |
|
387 |
if (field instanceof ChronoField) { |
|
388 |
ChronoField f = (ChronoField) field; |
|
16852 | 389 |
// not using checkValidIntValue so EPOCH_DAY and PROLEPTIC_MONTH work |
390 |
chrono.range(f).checkValidValue(newValue, f); // TODO: validate value |
|
15658 | 391 |
int nvalue = (int) newValue; |
392 |
switch (f) { |
|
16852 | 393 |
case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek()); |
15658 | 394 |
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH)); |
395 |
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR)); |
|
16852 | 396 |
case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, monthOfYear, nvalue); |
19030 | 397 |
case DAY_OF_YEAR: return plusDays(Math.min(nvalue, lengthOfYear()) - getDayOfYear()); |
16852 | 398 |
case EPOCH_DAY: return new HijrahDate(chrono, newValue); |
15658 | 399 |
case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7); |
400 |
case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7); |
|
16852 | 401 |
case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, dayOfMonth); |
402 |
case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth()); |
|
403 |
case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth); |
|
15658 | 404 |
case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth); |
16852 | 405 |
case ERA: return resolvePreviousValid(1 - prolepticYear, monthOfYear, dayOfMonth); |
15658 | 406 |
} |
19030 | 407 |
throw new UnsupportedTemporalTypeException("Unsupported field: " + field); |
15658 | 408 |
} |
19030 | 409 |
return super.with(field, newValue); |
15658 | 410 |
} |
411 |
||
16852 | 412 |
private HijrahDate resolvePreviousValid(int prolepticYear, int month, int day) { |
413 |
int monthDays = chrono.getMonthLength(prolepticYear, month); |
|
15658 | 414 |
if (day > monthDays) { |
415 |
day = monthDays; |
|
416 |
} |
|
16852 | 417 |
return HijrahDate.of(chrono, prolepticYear, month, day); |
15658 | 418 |
} |
419 |
||
420 |
/** |
|
421 |
* {@inheritDoc} |
|
16852 | 422 |
* @throws DateTimeException if unable to make the adjustment. |
423 |
* For example, if the adjuster requires an ISO chronology |
|
15658 | 424 |
* @throws ArithmeticException {@inheritDoc} |
425 |
*/ |
|
426 |
@Override |
|
427 |
public HijrahDate with(TemporalAdjuster adjuster) { |
|
16852 | 428 |
return super.with(adjuster); |
429 |
} |
|
430 |
||
431 |
/** |
|
432 |
* Returns a {@code HijrahDate} with the Chronology requested. |
|
433 |
* <p> |
|
434 |
* The year, month, and day are checked against the new requested |
|
435 |
* HijrahChronology. If the chronology has a shorter month length |
|
436 |
* for the month, the day is reduced to be the last day of the month. |
|
437 |
* |
|
438 |
* @param chronology the new HijrahChonology, non-null |
|
439 |
* @return a HijrahDate with the requested HijrahChronology, non-null |
|
440 |
*/ |
|
441 |
public HijrahDate withVariant(HijrahChronology chronology) { |
|
442 |
if (chrono == chronology) { |
|
443 |
return this; |
|
444 |
} |
|
445 |
// Like resolvePreviousValid the day is constrained to stay in the same month |
|
446 |
int monthDays = chronology.getDayOfYear(prolepticYear, monthOfYear); |
|
447 |
return HijrahDate.of(chronology, prolepticYear, monthOfYear,(dayOfMonth > monthDays) ? monthDays : dayOfMonth ); |
|
15658 | 448 |
} |
449 |
||
450 |
/** |
|
451 |
* {@inheritDoc} |
|
452 |
* @throws DateTimeException {@inheritDoc} |
|
453 |
* @throws ArithmeticException {@inheritDoc} |
|
454 |
*/ |
|
455 |
@Override |
|
456 |
public HijrahDate plus(TemporalAmount amount) { |
|
16852 | 457 |
return super.plus(amount); |
15658 | 458 |
} |
459 |
||
460 |
/** |
|
461 |
* {@inheritDoc} |
|
462 |
* @throws DateTimeException {@inheritDoc} |
|
463 |
* @throws ArithmeticException {@inheritDoc} |
|
464 |
*/ |
|
465 |
@Override |
|
466 |
public HijrahDate minus(TemporalAmount amount) { |
|
16852 | 467 |
return super.minus(amount); |
15658 | 468 |
} |
469 |
||
470 |
@Override |
|
471 |
public long toEpochDay() { |
|
16852 | 472 |
return chrono.getEpochDay(prolepticYear, monthOfYear, dayOfMonth); |
473 |
} |
|
474 |
||
475 |
/** |
|
476 |
* Gets the day-of-year field. |
|
477 |
* <p> |
|
478 |
* This method returns the primitive {@code int} value for the day-of-year. |
|
479 |
* |
|
480 |
* @return the day-of-year |
|
481 |
*/ |
|
482 |
private int getDayOfYear() { |
|
19030 | 483 |
return chrono.getDayOfYear(prolepticYear, monthOfYear) + dayOfMonth; |
16852 | 484 |
} |
485 |
||
486 |
/** |
|
487 |
* Gets the day-of-week value. |
|
488 |
* |
|
489 |
* @return the day-of-week; computed from the epochday |
|
490 |
*/ |
|
491 |
private int getDayOfWeek() { |
|
492 |
int dow0 = (int)Math.floorMod(toEpochDay() + 3, 7); |
|
493 |
return dow0 + 1; |
|
494 |
} |
|
495 |
||
496 |
/** |
|
497 |
* Gets the Era of this date. |
|
498 |
* |
|
499 |
* @return the Era of this date; computed from epochDay |
|
500 |
*/ |
|
501 |
private int getEraValue() { |
|
502 |
return (prolepticYear > 1 ? 1 : 0); |
|
15658 | 503 |
} |
504 |
||
505 |
//----------------------------------------------------------------------- |
|
506 |
/** |
|
507 |
* Checks if the year is a leap year, according to the Hijrah calendar system rules. |
|
508 |
* |
|
509 |
* @return true if this date is in a leap year |
|
510 |
*/ |
|
511 |
@Override |
|
512 |
public boolean isLeapYear() { |
|
16852 | 513 |
return chrono.isLeapYear(prolepticYear); |
15658 | 514 |
} |
515 |
||
516 |
//----------------------------------------------------------------------- |
|
517 |
@Override |
|
518 |
HijrahDate plusYears(long years) { |
|
519 |
if (years == 0) { |
|
520 |
return this; |
|
521 |
} |
|
16852 | 522 |
int newYear = Math.addExact(this.prolepticYear, (int)years); |
523 |
return resolvePreviousValid(newYear, monthOfYear, dayOfMonth); |
|
15658 | 524 |
} |
525 |
||
526 |
@Override |
|
16852 | 527 |
HijrahDate plusMonths(long monthsToAdd) { |
528 |
if (monthsToAdd == 0) { |
|
15658 | 529 |
return this; |
530 |
} |
|
16852 | 531 |
long monthCount = prolepticYear * 12L + (monthOfYear - 1); |
532 |
long calcMonths = monthCount + monthsToAdd; // safe overflow |
|
533 |
int newYear = chrono.checkValidYear(Math.floorDiv(calcMonths, 12L)); |
|
534 |
int newMonth = (int)Math.floorMod(calcMonths, 12L) + 1; |
|
535 |
return resolvePreviousValid(newYear, newMonth, dayOfMonth); |
|
15658 | 536 |
} |
537 |
||
538 |
@Override |
|
539 |
HijrahDate plusWeeks(long weeksToAdd) { |
|
16852 | 540 |
return super.plusWeeks(weeksToAdd); |
15658 | 541 |
} |
542 |
||
543 |
@Override |
|
544 |
HijrahDate plusDays(long days) { |
|
16852 | 545 |
return new HijrahDate(chrono, toEpochDay() + days); |
15658 | 546 |
} |
547 |
||
548 |
@Override |
|
549 |
public HijrahDate plus(long amountToAdd, TemporalUnit unit) { |
|
16852 | 550 |
return super.plus(amountToAdd, unit); |
15658 | 551 |
} |
552 |
||
553 |
@Override |
|
554 |
public HijrahDate minus(long amountToSubtract, TemporalUnit unit) { |
|
16852 | 555 |
return super.minus(amountToSubtract, unit); |
15658 | 556 |
} |
557 |
||
558 |
@Override |
|
559 |
HijrahDate minusYears(long yearsToSubtract) { |
|
16852 | 560 |
return super.minusYears(yearsToSubtract); |
15658 | 561 |
} |
562 |
||
563 |
@Override |
|
564 |
HijrahDate minusMonths(long monthsToSubtract) { |
|
16852 | 565 |
return super.minusMonths(monthsToSubtract); |
15658 | 566 |
} |
567 |
||
568 |
@Override |
|
569 |
HijrahDate minusWeeks(long weeksToSubtract) { |
|
16852 | 570 |
return super.minusWeeks(weeksToSubtract); |
15658 | 571 |
} |
572 |
||
573 |
@Override |
|
574 |
HijrahDate minusDays(long daysToSubtract) { |
|
16852 | 575 |
return super.minusDays(daysToSubtract); |
15658 | 576 |
} |
577 |
||
578 |
@Override // for javadoc and covariant return type |
|
19030 | 579 |
@SuppressWarnings("unchecked") |
15658 | 580 |
public final ChronoLocalDateTime<HijrahDate> atTime(LocalTime localTime) { |
19030 | 581 |
return (ChronoLocalDateTime<HijrahDate>)super.atTime(localTime); |
15658 | 582 |
} |
583 |
||
584 |
@Override |
|
19030 | 585 |
public Period until(ChronoLocalDate endDate) { |
15658 | 586 |
// TODO: untested |
16852 | 587 |
HijrahDate end = getChronology().date(endDate); |
588 |
long totalMonths = (end.prolepticYear - this.prolepticYear) * 12 + (end.monthOfYear - this.monthOfYear); // safe |
|
15658 | 589 |
int days = end.dayOfMonth - this.dayOfMonth; |
590 |
if (totalMonths > 0 && days < 0) { |
|
591 |
totalMonths--; |
|
592 |
HijrahDate calcDate = this.plusMonths(totalMonths); |
|
593 |
days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe |
|
594 |
} else if (totalMonths < 0 && days > 0) { |
|
595 |
totalMonths++; |
|
596 |
days -= end.lengthOfMonth(); |
|
597 |
} |
|
598 |
long years = totalMonths / 12; // safe |
|
599 |
int months = (int) (totalMonths % 12); // safe |
|
600 |
return Period.of(Math.toIntExact(years), months, days); |
|
601 |
} |
|
602 |
||
603 |
//----------------------------------------------------------------------- |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
604 |
/** |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
605 |
* Defend against malicious streams. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
606 |
* @return never |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
607 |
* @throws InvalidObjectException always |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
608 |
*/ |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
609 |
private Object readResolve() throws InvalidObjectException { |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
610 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
611 |
} |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
612 |
|
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
613 |
/** |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
614 |
* Writes the object using a |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
615 |
* <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
|
616 |
* @serialData |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
617 |
* <pre> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
618 |
* out.writeByte(6); // identifies a HijrahDate |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
619 |
* out.writeObject(chrono); // the HijrahChronology variant |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
620 |
* out.writeInt(get(YEAR)); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
621 |
* out.writeByte(get(MONTH_OF_YEAR)); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
622 |
* out.writeByte(get(DAY_OF_MONTH)); |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
623 |
* </pre> |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
624 |
* |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
625 |
* @return the instance of {@code Ser}, not null |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
626 |
*/ |
15658 | 627 |
private Object writeReplace() { |
628 |
return new Ser(Ser.HIJRAH_DATE_TYPE, this); |
|
629 |
} |
|
630 |
||
631 |
void writeExternal(ObjectOutput out) throws IOException { |
|
632 |
// HijrahChronology is implicit in the Hijrah_DATE_TYPE |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
633 |
out.writeObject(getChronology()); |
15658 | 634 |
out.writeInt(get(YEAR)); |
635 |
out.writeByte(get(MONTH_OF_YEAR)); |
|
636 |
out.writeByte(get(DAY_OF_MONTH)); |
|
637 |
} |
|
638 |
||
19030 | 639 |
static HijrahDate readExternal(ObjectInput in) throws IOException, ClassNotFoundException { |
16852 | 640 |
HijrahChronology chrono = (HijrahChronology) in.readObject(); |
15658 | 641 |
int year = in.readInt(); |
642 |
int month = in.readByte(); |
|
643 |
int dayOfMonth = in.readByte(); |
|
644 |
return chrono.date(year, month, dayOfMonth); |
|
645 |
} |
|
646 |
||
647 |
} |