1 /*
2 * Copyright (c) 2012, 2025, 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
88 /**
89 * A date in the Hijrah calendar system.
90 * <p>
91 * This date operates using one of several variants of the
92 * {@linkplain HijrahChronology Hijrah calendar}.
93 * <p>
94 * The Hijrah calendar has a different total of days in a year than
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.
99 * <p>
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.
106 * <p>
107 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
108 * class; programmers should treat instances that are
109 * {@linkplain #equals(Object) equal} as interchangeable and should not
110 * use instances for synchronization, or unpredictable behavior may
111 * occur. For example, in a future release, synchronization may fail.
112 * The {@code equals} method should be used for comparisons.
113 *
114 * @implSpec
115 * This class is immutable and thread-safe.
116 *
117 * @since 1.8
118 */
119 @jdk.internal.ValueBased
120 public final class HijrahDate
121 extends ChronoLocalDateImpl<HijrahDate>
122 implements ChronoLocalDate, Serializable {
123
124 /**
125 * Serialization version.
126 */
127 @java.io.Serial
128 private static final long serialVersionUID = -5207853542612002020L;
129 /**
130 * The Chronology of this HijrahDate.
131 */
132 private final transient HijrahChronology chrono;
133 /**
134 * The proleptic year.
135 */
136 private final transient int prolepticYear;
137 /**
138 * The month-of-year.
139 */
140 private final transient byte monthOfYear;
639 *
640 * @return a suitable hash code based only on the Chronology and the date
641 */
642 @Override // override for performance
643 public int hashCode() {
644 int yearValue = prolepticYear;
645 int monthValue = monthOfYear;
646 int dayValue = dayOfMonth;
647 return getChronology().getId().hashCode() ^ (yearValue & 0xFFFFF800)
648 ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
649 }
650
651 //-----------------------------------------------------------------------
652 /**
653 * Defend against malicious streams.
654 *
655 * @param s the stream to read
656 * @throws InvalidObjectException always
657 */
658 @java.io.Serial
659 private void readObject(ObjectInputStream s) throws InvalidObjectException {
660 throw new InvalidObjectException("Deserialization via serialization delegate");
661 }
662
663 /**
664 * Writes the object using a
665 * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
666 * @serialData
667 * <pre>
668 * out.writeByte(6); // identifies a HijrahDate
669 * out.writeObject(chrono); // the HijrahChronology variant
670 * out.writeInt(get(YEAR));
671 * out.writeByte(get(MONTH_OF_YEAR));
672 * out.writeByte(get(DAY_OF_MONTH));
673 * </pre>
674 *
675 * @return the instance of {@code Ser}, not null
676 */
677 @java.io.Serial
678 private Object writeReplace() {
|
1 /*
2 * Copyright (c) 2012, 2026, 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
88 /**
89 * A date in the Hijrah calendar system.
90 * <p>
91 * This date operates using one of several variants of the
92 * {@linkplain HijrahChronology Hijrah calendar}.
93 * <p>
94 * The Hijrah calendar has a different total of days in a year than
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.
99 * <p>
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.
106 * <p>
107 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
108 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
109 * as interchangeable and should not use instances for synchronization or
110 * with {@linkplain java.lang.ref.Reference object references}.
111 *
112 * <div class="preview-block">
113 * <div class="preview-comment">
114 * When preview features are enabled, {@code HijrahDate} is a {@linkplain Class#isValue value class}.
115 * Use of value class instances for synchronization or with
116 * {@linkplain java.lang.ref.Reference object references} result in
117 * {@link IdentityException}.
118 * </div>
119 * </div>
120 *
121 * @implSpec
122 * This class is immutable and thread-safe.
123 *
124 * @since 1.8
125 */
126 @jdk.internal.ValueBased
127 public final /*value*/ class HijrahDate
128 extends ChronoLocalDateImpl<HijrahDate>
129 implements ChronoLocalDate, Serializable {
130
131 /**
132 * Serialization version.
133 */
134 @java.io.Serial
135 private static final long serialVersionUID = -5207853542612002020L;
136 /**
137 * The Chronology of this HijrahDate.
138 */
139 private final transient HijrahChronology chrono;
140 /**
141 * The proleptic year.
142 */
143 private final transient int prolepticYear;
144 /**
145 * The month-of-year.
146 */
147 private final transient byte monthOfYear;
646 *
647 * @return a suitable hash code based only on the Chronology and the date
648 */
649 @Override // override for performance
650 public int hashCode() {
651 int yearValue = prolepticYear;
652 int monthValue = monthOfYear;
653 int dayValue = dayOfMonth;
654 return getChronology().getId().hashCode() ^ (yearValue & 0xFFFFF800)
655 ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
656 }
657
658 //-----------------------------------------------------------------------
659 /**
660 * Defend against malicious streams.
661 *
662 * @param s the stream to read
663 * @throws InvalidObjectException always
664 */
665 @java.io.Serial
666 @SuppressWarnings("serial") // this method is not invoked for value classes
667 private void readObject(ObjectInputStream s) throws InvalidObjectException {
668 throw new InvalidObjectException("Deserialization via serialization delegate");
669 }
670
671 /**
672 * Writes the object using a
673 * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
674 * @serialData
675 * <pre>
676 * out.writeByte(6); // identifies a HijrahDate
677 * out.writeObject(chrono); // the HijrahChronology variant
678 * out.writeInt(get(YEAR));
679 * out.writeByte(get(MONTH_OF_YEAR));
680 * out.writeByte(get(DAY_OF_MONTH));
681 * </pre>
682 *
683 * @return the instance of {@code Ser}, not null
684 */
685 @java.io.Serial
686 private Object writeReplace() {
|