113 *
114 * <p>
115 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
116 * class; use of identity-sensitive operations (including reference equality
117 * ({@code ==}), identity hash code, or synchronization) on instances of
118 * {@code JapaneseDate} may have unpredictable results and should be avoided.
119 * The {@code equals} method should be used for comparisons.
120 *
121 * @implSpec
122 * This class is immutable and thread-safe.
123 *
124 * @since 1.8
125 */
126 public final class JapaneseDate
127 extends ChronoLocalDateImpl<JapaneseDate>
128 implements ChronoLocalDate, Serializable {
129
130 /**
131 * Serialization version.
132 */
133 private static final long serialVersionUID = -305327627230580483L;
134
135 /**
136 * The underlying ISO local date.
137 */
138 private final transient LocalDate isoDate;
139 /**
140 * The JapaneseEra of this date.
141 */
142 private transient JapaneseEra era;
143 /**
144 * The Japanese imperial calendar year of this date.
145 */
146 private transient int yearOfEra;
147
148 /**
149 * The first day supported by the JapaneseChronology is Meiji 6, January 1st.
150 */
151 static final LocalDate MEIJI_6_ISODATE = LocalDate.of(1873, 1, 1);
152
702 return false;
703 }
704
705 /**
706 * A hash code for this date.
707 *
708 * @return a suitable hash code based only on the Chronology and the date
709 */
710 @Override // override for performance
711 public int hashCode() {
712 return getChronology().getId().hashCode() ^ isoDate.hashCode();
713 }
714
715 //-----------------------------------------------------------------------
716 /**
717 * Defend against malicious streams.
718 *
719 * @param s the stream to read
720 * @throws InvalidObjectException always
721 */
722 private void readObject(ObjectInputStream s) throws InvalidObjectException {
723 throw new InvalidObjectException("Deserialization via serialization delegate");
724 }
725
726 /**
727 * Writes the object using a
728 * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
729 * @serialData
730 * <pre>
731 * out.writeByte(4); // identifies a JapaneseDate
732 * out.writeInt(get(YEAR));
733 * out.writeByte(get(MONTH_OF_YEAR));
734 * out.writeByte(get(DAY_OF_MONTH));
735 * </pre>
736 *
737 * @return the instance of {@code Ser}, not null
738 */
739 private Object writeReplace() {
740 return new Ser(Ser.JAPANESE_DATE_TYPE, this);
741 }
742
743 void writeExternal(DataOutput out) throws IOException {
744 // JapaneseChronology is implicit in the JAPANESE_DATE_TYPE
745 out.writeInt(get(YEAR));
746 out.writeByte(get(MONTH_OF_YEAR));
747 out.writeByte(get(DAY_OF_MONTH));
748 }
749
750 static JapaneseDate readExternal(DataInput in) throws IOException {
751 int year = in.readInt();
752 int month = in.readByte();
753 int dayOfMonth = in.readByte();
754 return JapaneseChronology.INSTANCE.date(year, month, dayOfMonth);
755 }
756
757 }
|
113 *
114 * <p>
115 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
116 * class; use of identity-sensitive operations (including reference equality
117 * ({@code ==}), identity hash code, or synchronization) on instances of
118 * {@code JapaneseDate} may have unpredictable results and should be avoided.
119 * The {@code equals} method should be used for comparisons.
120 *
121 * @implSpec
122 * This class is immutable and thread-safe.
123 *
124 * @since 1.8
125 */
126 public final class JapaneseDate
127 extends ChronoLocalDateImpl<JapaneseDate>
128 implements ChronoLocalDate, Serializable {
129
130 /**
131 * Serialization version.
132 */
133 @java.io.Serial
134 private static final long serialVersionUID = -305327627230580483L;
135
136 /**
137 * The underlying ISO local date.
138 */
139 private final transient LocalDate isoDate;
140 /**
141 * The JapaneseEra of this date.
142 */
143 private transient JapaneseEra era;
144 /**
145 * The Japanese imperial calendar year of this date.
146 */
147 private transient int yearOfEra;
148
149 /**
150 * The first day supported by the JapaneseChronology is Meiji 6, January 1st.
151 */
152 static final LocalDate MEIJI_6_ISODATE = LocalDate.of(1873, 1, 1);
153
703 return false;
704 }
705
706 /**
707 * A hash code for this date.
708 *
709 * @return a suitable hash code based only on the Chronology and the date
710 */
711 @Override // override for performance
712 public int hashCode() {
713 return getChronology().getId().hashCode() ^ isoDate.hashCode();
714 }
715
716 //-----------------------------------------------------------------------
717 /**
718 * Defend against malicious streams.
719 *
720 * @param s the stream to read
721 * @throws InvalidObjectException always
722 */
723 @java.io.Serial
724 private void readObject(ObjectInputStream s) throws InvalidObjectException {
725 throw new InvalidObjectException("Deserialization via serialization delegate");
726 }
727
728 /**
729 * Writes the object using a
730 * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
731 * @serialData
732 * <pre>
733 * out.writeByte(4); // identifies a JapaneseDate
734 * out.writeInt(get(YEAR));
735 * out.writeByte(get(MONTH_OF_YEAR));
736 * out.writeByte(get(DAY_OF_MONTH));
737 * </pre>
738 *
739 * @return the instance of {@code Ser}, not null
740 */
741 @java.io.Serial
742 private Object writeReplace() {
743 return new Ser(Ser.JAPANESE_DATE_TYPE, this);
744 }
745
746 void writeExternal(DataOutput out) throws IOException {
747 // JapaneseChronology is implicit in the JAPANESE_DATE_TYPE
748 out.writeInt(get(YEAR));
749 out.writeByte(get(MONTH_OF_YEAR));
750 out.writeByte(get(DAY_OF_MONTH));
751 }
752
753 static JapaneseDate readExternal(DataInput in) throws IOException {
754 int year = in.readInt();
755 int month = in.readByte();
756 int dayOfMonth = in.readByte();
757 return JapaneseChronology.INSTANCE.date(year, month, dayOfMonth);
758 }
759
760 }
|