106 *
107 * <p>
108 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
109 * class; use of identity-sensitive operations (including reference equality
110 * ({@code ==}), identity hash code, or synchronization) on instances of
111 * {@code HijrahDate} may have unpredictable results and should be avoided.
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 public final class HijrahDate
120 extends ChronoLocalDateImpl<HijrahDate>
121 implements ChronoLocalDate, Serializable {
122
123 /**
124 * Serialization version.
125 */
126 private static final long serialVersionUID = -5207853542612002020L;
127 /**
128 * The Chronology of this HijrahDate.
129 */
130 private final transient HijrahChronology chrono;
131 /**
132 * The proleptic year.
133 */
134 private final transient int prolepticYear;
135 /**
136 * The month-of-year.
137 */
138 private final transient int monthOfYear;
139 /**
140 * The day-of-month.
141 */
142 private final transient int dayOfMonth;
143
144 //-------------------------------------------------------------------------
145 /**
640 * A hash code for this date.
641 *
642 * @return a suitable hash code based only on the Chronology and the date
643 */
644 @Override // override for performance
645 public int hashCode() {
646 int yearValue = prolepticYear;
647 int monthValue = monthOfYear;
648 int dayValue = dayOfMonth;
649 return getChronology().getId().hashCode() ^ (yearValue & 0xFFFFF800)
650 ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
651 }
652
653 //-----------------------------------------------------------------------
654 /**
655 * Defend against malicious streams.
656 *
657 * @param s the stream to read
658 * @throws InvalidObjectException always
659 */
660 private void readObject(ObjectInputStream s) throws InvalidObjectException {
661 throw new InvalidObjectException("Deserialization via serialization delegate");
662 }
663
664 /**
665 * Writes the object using a
666 * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
667 * @serialData
668 * <pre>
669 * out.writeByte(6); // identifies a HijrahDate
670 * out.writeObject(chrono); // the HijrahChronology variant
671 * out.writeInt(get(YEAR));
672 * out.writeByte(get(MONTH_OF_YEAR));
673 * out.writeByte(get(DAY_OF_MONTH));
674 * </pre>
675 *
676 * @return the instance of {@code Ser}, not null
677 */
678 private Object writeReplace() {
679 return new Ser(Ser.HIJRAH_DATE_TYPE, this);
680 }
681
682 void writeExternal(ObjectOutput out) throws IOException {
683 // HijrahChronology is implicit in the Hijrah_DATE_TYPE
684 out.writeObject(getChronology());
685 out.writeInt(get(YEAR));
686 out.writeByte(get(MONTH_OF_YEAR));
687 out.writeByte(get(DAY_OF_MONTH));
688 }
689
690 static HijrahDate readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
691 HijrahChronology chrono = (HijrahChronology) in.readObject();
692 int year = in.readInt();
693 int month = in.readByte();
694 int dayOfMonth = in.readByte();
695 return chrono.date(year, month, dayOfMonth);
696 }
697
|
106 *
107 * <p>
108 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
109 * class; use of identity-sensitive operations (including reference equality
110 * ({@code ==}), identity hash code, or synchronization) on instances of
111 * {@code HijrahDate} may have unpredictable results and should be avoided.
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 public final class HijrahDate
120 extends ChronoLocalDateImpl<HijrahDate>
121 implements ChronoLocalDate, Serializable {
122
123 /**
124 * Serialization version.
125 */
126 @java.io.Serial
127 private static final long serialVersionUID = -5207853542612002020L;
128 /**
129 * The Chronology of this HijrahDate.
130 */
131 private final transient HijrahChronology chrono;
132 /**
133 * The proleptic year.
134 */
135 private final transient int prolepticYear;
136 /**
137 * The month-of-year.
138 */
139 private final transient int monthOfYear;
140 /**
141 * The day-of-month.
142 */
143 private final transient int dayOfMonth;
144
145 //-------------------------------------------------------------------------
146 /**
641 * A hash code for this date.
642 *
643 * @return a suitable hash code based only on the Chronology and the date
644 */
645 @Override // override for performance
646 public int hashCode() {
647 int yearValue = prolepticYear;
648 int monthValue = monthOfYear;
649 int dayValue = dayOfMonth;
650 return getChronology().getId().hashCode() ^ (yearValue & 0xFFFFF800)
651 ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
652 }
653
654 //-----------------------------------------------------------------------
655 /**
656 * Defend against malicious streams.
657 *
658 * @param s the stream to read
659 * @throws InvalidObjectException always
660 */
661 @java.io.Serial
662 private void readObject(ObjectInputStream s) throws InvalidObjectException {
663 throw new InvalidObjectException("Deserialization via serialization delegate");
664 }
665
666 /**
667 * Writes the object using a
668 * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
669 * @serialData
670 * <pre>
671 * out.writeByte(6); // identifies a HijrahDate
672 * out.writeObject(chrono); // the HijrahChronology variant
673 * out.writeInt(get(YEAR));
674 * out.writeByte(get(MONTH_OF_YEAR));
675 * out.writeByte(get(DAY_OF_MONTH));
676 * </pre>
677 *
678 * @return the instance of {@code Ser}, not null
679 */
680 @java.io.Serial
681 private Object writeReplace() {
682 return new Ser(Ser.HIJRAH_DATE_TYPE, this);
683 }
684
685 void writeExternal(ObjectOutput out) throws IOException {
686 // HijrahChronology is implicit in the Hijrah_DATE_TYPE
687 out.writeObject(getChronology());
688 out.writeInt(get(YEAR));
689 out.writeByte(get(MONTH_OF_YEAR));
690 out.writeByte(get(DAY_OF_MONTH));
691 }
692
693 static HijrahDate readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
694 HijrahChronology chrono = (HijrahChronology) in.readObject();
695 int year = in.readInt();
696 int month = in.readByte();
697 int dayOfMonth = in.readByte();
698 return chrono.date(year, month, dayOfMonth);
699 }
700
|