< prev index next >

src/java.base/share/classes/java/time/chrono/ChronoPeriodImpl.java

Print this page




  81 import java.util.Objects;
  82 
  83 /**
  84  * A period expressed in terms of a standard year-month-day calendar system.
  85  * <p>
  86  * This class is used by applications seeking to handle dates in non-ISO calendar systems.
  87  * For example, the Japanese, Minguo, Thai Buddhist and others.
  88  *
  89  * @implSpec
  90  * This class is immutable nad thread-safe.
  91  *
  92  * @since 1.8
  93  */
  94 final class ChronoPeriodImpl
  95         implements ChronoPeriod, Serializable {
  96     // this class is only used by JDK chronology implementations and makes assumptions based on that fact
  97 
  98     /**
  99      * Serialization version.
 100      */

 101     private static final long serialVersionUID = 57387258289L;
 102 
 103     /**
 104      * The set of supported units.
 105      */
 106     private static final List<TemporalUnit> SUPPORTED_UNITS = List.of(YEARS, MONTHS, DAYS);
 107 
 108     /**
 109      * The chronology.
 110      */
 111     private final Chronology chrono;
 112     /**
 113      * The number of years.
 114      */
 115     final int years;
 116     /**
 117      * The number of months.
 118      */
 119     final int months;
 120     /**


 349                 buf.append(days).append('D');
 350             }
 351             return buf.toString();
 352         }
 353     }
 354 
 355     //-----------------------------------------------------------------------
 356     /**
 357      * Writes the Chronology using a
 358      * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
 359      * <pre>
 360      *  out.writeByte(12);  // identifies this as a ChronoPeriodImpl
 361      *  out.writeUTF(getId());  // the chronology
 362      *  out.writeInt(years);
 363      *  out.writeInt(months);
 364      *  out.writeInt(days);
 365      * </pre>
 366      *
 367      * @return the instance of {@code Ser}, not null
 368      */

 369     protected Object writeReplace() {
 370         return new Ser(Ser.CHRONO_PERIOD_TYPE, this);
 371     }
 372 
 373     /**
 374      * Defend against malicious streams.
 375      *
 376      * @param s the stream to read
 377      * @throws InvalidObjectException always
 378      */

 379     private void readObject(ObjectInputStream s) throws ObjectStreamException {
 380         throw new InvalidObjectException("Deserialization via serialization delegate");
 381     }
 382 
 383     void writeExternal(DataOutput out) throws IOException {
 384         out.writeUTF(chrono.getId());
 385         out.writeInt(years);
 386         out.writeInt(months);
 387         out.writeInt(days);
 388     }
 389 
 390     static ChronoPeriodImpl readExternal(DataInput in) throws IOException {
 391         Chronology chrono = Chronology.of(in.readUTF());
 392         int years = in.readInt();
 393         int months = in.readInt();
 394         int days = in.readInt();
 395         return new ChronoPeriodImpl(chrono, years, months, days);
 396     }
 397 
 398 }


  81 import java.util.Objects;
  82 
  83 /**
  84  * A period expressed in terms of a standard year-month-day calendar system.
  85  * <p>
  86  * This class is used by applications seeking to handle dates in non-ISO calendar systems.
  87  * For example, the Japanese, Minguo, Thai Buddhist and others.
  88  *
  89  * @implSpec
  90  * This class is immutable nad thread-safe.
  91  *
  92  * @since 1.8
  93  */
  94 final class ChronoPeriodImpl
  95         implements ChronoPeriod, Serializable {
  96     // this class is only used by JDK chronology implementations and makes assumptions based on that fact
  97 
  98     /**
  99      * Serialization version.
 100      */
 101     @java.io.Serial
 102     private static final long serialVersionUID = 57387258289L;
 103 
 104     /**
 105      * The set of supported units.
 106      */
 107     private static final List<TemporalUnit> SUPPORTED_UNITS = List.of(YEARS, MONTHS, DAYS);
 108 
 109     /**
 110      * The chronology.
 111      */
 112     private final Chronology chrono;
 113     /**
 114      * The number of years.
 115      */
 116     final int years;
 117     /**
 118      * The number of months.
 119      */
 120     final int months;
 121     /**


 350                 buf.append(days).append('D');
 351             }
 352             return buf.toString();
 353         }
 354     }
 355 
 356     //-----------------------------------------------------------------------
 357     /**
 358      * Writes the Chronology using a
 359      * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
 360      * <pre>
 361      *  out.writeByte(12);  // identifies this as a ChronoPeriodImpl
 362      *  out.writeUTF(getId());  // the chronology
 363      *  out.writeInt(years);
 364      *  out.writeInt(months);
 365      *  out.writeInt(days);
 366      * </pre>
 367      *
 368      * @return the instance of {@code Ser}, not null
 369      */
 370     @java.io.Serial
 371     protected Object writeReplace() {
 372         return new Ser(Ser.CHRONO_PERIOD_TYPE, this);
 373     }
 374 
 375     /**
 376      * Defend against malicious streams.
 377      *
 378      * @param s the stream to read
 379      * @throws InvalidObjectException always
 380      */
 381     @java.io.Serial
 382     private void readObject(ObjectInputStream s) throws ObjectStreamException {
 383         throw new InvalidObjectException("Deserialization via serialization delegate");
 384     }
 385 
 386     void writeExternal(DataOutput out) throws IOException {
 387         out.writeUTF(chrono.getId());
 388         out.writeInt(years);
 389         out.writeInt(months);
 390         out.writeInt(days);
 391     }
 392 
 393     static ChronoPeriodImpl readExternal(DataInput in) throws IOException {
 394         Chronology chrono = Chronology.of(in.readUTF());
 395         int years = in.readInt();
 396         int months = in.readInt();
 397         int days = in.readInt();
 398         return new ChronoPeriodImpl(chrono, years, months, days);
 399     }
 400 
 401 }
< prev index next >