jdk/src/share/classes/java/time/chrono/MinguoDate.java
changeset 16852 60207b2b4b42
parent 15658 55b829ca2334
child 17474 8c100beabcc0
equal deleted inserted replaced
16851:3bbdae468b05 16852:60207b2b4b42
    70 import java.time.LocalDate;
    70 import java.time.LocalDate;
    71 import java.time.LocalTime;
    71 import java.time.LocalTime;
    72 import java.time.Period;
    72 import java.time.Period;
    73 import java.time.ZoneId;
    73 import java.time.ZoneId;
    74 import java.time.temporal.ChronoField;
    74 import java.time.temporal.ChronoField;
    75 import java.time.temporal.TemporalQuery;
       
    76 import java.time.temporal.TemporalAccessor;
    75 import java.time.temporal.TemporalAccessor;
    77 import java.time.temporal.TemporalAdjuster;
    76 import java.time.temporal.TemporalAdjuster;
    78 import java.time.temporal.TemporalAmount;
    77 import java.time.temporal.TemporalAmount;
    79 import java.time.temporal.TemporalField;
    78 import java.time.temporal.TemporalField;
       
    79 import java.time.temporal.TemporalQuery;
    80 import java.time.temporal.TemporalUnit;
    80 import java.time.temporal.TemporalUnit;
       
    81 import java.time.temporal.UnsupportedTemporalTypeException;
    81 import java.time.temporal.ValueRange;
    82 import java.time.temporal.ValueRange;
    82 import java.util.Objects;
    83 import java.util.Objects;
    83 
    84 
    84 /**
    85 /**
    85  * A date in the Minguo calendar system.
    86  * A date in the Minguo calendar system.
   203         Objects.requireNonNull(isoDate, "isoDate");
   204         Objects.requireNonNull(isoDate, "isoDate");
   204         this.isoDate = isoDate;
   205         this.isoDate = isoDate;
   205     }
   206     }
   206 
   207 
   207     //-----------------------------------------------------------------------
   208     //-----------------------------------------------------------------------
       
   209     /**
       
   210      * Gets the chronology of this date, which is the Minguo calendar system.
       
   211      * <p>
       
   212      * The {@code Chronology} represents the calendar system in use.
       
   213      * The era and other fields in {@link ChronoField} are defined by the chronology.
       
   214      *
       
   215      * @return the Minguo chronology, not null
       
   216      */
   208     @Override
   217     @Override
   209     public MinguoChronology getChronology() {
   218     public MinguoChronology getChronology() {
   210         return MinguoChronology.INSTANCE;
   219         return MinguoChronology.INSTANCE;
   211     }
   220     }
   212 
   221 
       
   222     /**
       
   223      * Gets the era applicable at this date.
       
   224      * <p>
       
   225      * The Minguo calendar system has two eras, 'ROC' and 'BEFORE_ROC',
       
   226      * defined by {@link MinguoEra}.
       
   227      *
       
   228      * @return the era applicable at this date, not null
       
   229      */
       
   230     @Override
       
   231     public MinguoEra getEra() {
       
   232         return (getProlepticYear() >= 1 ? MinguoEra.ROC : MinguoEra.BEFORE_ROC);
       
   233     }
       
   234 
       
   235     /**
       
   236      * Returns the length of the month represented by this date.
       
   237      * <p>
       
   238      * This returns the length of the month in days.
       
   239      * Month lengths match those of the ISO calendar system.
       
   240      *
       
   241      * @return the length of the month in days
       
   242      */
   213     @Override
   243     @Override
   214     public int lengthOfMonth() {
   244     public int lengthOfMonth() {
   215         return isoDate.lengthOfMonth();
   245         return isoDate.lengthOfMonth();
   216     }
   246     }
   217 
   247 
       
   248     //-----------------------------------------------------------------------
   218     @Override
   249     @Override
   219     public ValueRange range(TemporalField field) {
   250     public ValueRange range(TemporalField field) {
   220         if (field instanceof ChronoField) {
   251         if (field instanceof ChronoField) {
   221             if (isSupported(field)) {
   252             if (isSupported(field)) {
   222                 ChronoField f = (ChronoField) field;
   253                 ChronoField f = (ChronoField) field;
   231                         return ValueRange.of(1, max);
   262                         return ValueRange.of(1, max);
   232                     }
   263                     }
   233                 }
   264                 }
   234                 return getChronology().range(f);
   265                 return getChronology().range(f);
   235             }
   266             }
   236             throw new DateTimeException("Unsupported field: " + field.getName());
   267             throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
   237         }
   268         }
   238         return field.rangeRefinedBy(this);
   269         return field.rangeRefinedBy(this);
   239     }
   270     }
   240 
   271 
   241     @Override
   272     @Override
   242     public long getLong(TemporalField field) {
   273     public long getLong(TemporalField field) {
   243         if (field instanceof ChronoField) {
   274         if (field instanceof ChronoField) {
   244             switch ((ChronoField) field) {
   275             switch ((ChronoField) field) {
       
   276                 case PROLEPTIC_MONTH:
       
   277                     return getProlepticMonth();
   245                 case YEAR_OF_ERA: {
   278                 case YEAR_OF_ERA: {
   246                     int prolepticYear = getProlepticYear();
   279                     int prolepticYear = getProlepticYear();
   247                     return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
   280                     return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
   248                 }
   281                 }
   249                 case YEAR:
   282                 case YEAR:
   254             return isoDate.getLong(field);
   287             return isoDate.getLong(field);
   255         }
   288         }
   256         return field.getFrom(this);
   289         return field.getFrom(this);
   257     }
   290     }
   258 
   291 
       
   292     private long getProlepticMonth() {
       
   293         return getProlepticYear() * 12L + isoDate.getMonthValue() - 1;
       
   294     }
       
   295 
   259     private int getProlepticYear() {
   296     private int getProlepticYear() {
   260         return isoDate.getYear() - YEARS_DIFFERENCE;
   297         return isoDate.getYear() - YEARS_DIFFERENCE;
   261     }
   298     }
   262 
   299 
   263     //-----------------------------------------------------------------------
   300     //-----------------------------------------------------------------------
   267             ChronoField f = (ChronoField) field;
   304             ChronoField f = (ChronoField) field;
   268             if (getLong(f) == newValue) {
   305             if (getLong(f) == newValue) {
   269                 return this;
   306                 return this;
   270             }
   307             }
   271             switch (f) {
   308             switch (f) {
       
   309                 case PROLEPTIC_MONTH:
       
   310                     getChronology().range(f).checkValidValue(newValue, f);
       
   311                     return plusMonths(newValue - getProlepticMonth());
   272                 case YEAR_OF_ERA:
   312                 case YEAR_OF_ERA:
   273                 case YEAR:
   313                 case YEAR:
   274                 case ERA: {
   314                 case ERA: {
   275                     f.checkValidValue(newValue);
   315                     int nvalue = getChronology().range(f).checkValidIntValue(newValue, f);
   276                     int nvalue = (int) newValue;
       
   277                     switch (f) {
   316                     switch (f) {
   278                         case YEAR_OF_ERA:
   317                         case YEAR_OF_ERA:
   279                             return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue)  + YEARS_DIFFERENCE));
   318                             return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue)  + YEARS_DIFFERENCE));
   280                         case YEAR:
   319                         case YEAR:
   281                             return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE));
   320                             return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE));
   284                     }
   323                     }
   285                 }
   324                 }
   286             }
   325             }
   287             return with(isoDate.with(field, newValue));
   326             return with(isoDate.with(field, newValue));
   288         }
   327         }
   289         return (MinguoDate) ChronoLocalDate.super.with(field, newValue);
   328         return ChronoLocalDate.super.with(field, newValue);
   290     }
   329     }
   291 
   330 
   292     /**
   331     /**
   293      * {@inheritDoc}
   332      * {@inheritDoc}
   294      * @throws DateTimeException {@inheritDoc}
   333      * @throws DateTimeException {@inheritDoc}
   295      * @throws ArithmeticException {@inheritDoc}
   334      * @throws ArithmeticException {@inheritDoc}
   296      */
   335      */
   297     @Override
   336     @Override
   298     public  MinguoDate with(TemporalAdjuster adjuster) {
   337     public  MinguoDate with(TemporalAdjuster adjuster) {
   299         return (MinguoDate)super.with(adjuster);
   338         return super.with(adjuster);
   300     }
   339     }
   301 
   340 
   302     /**
   341     /**
   303      * {@inheritDoc}
   342      * {@inheritDoc}
   304      * @throws DateTimeException {@inheritDoc}
   343      * @throws DateTimeException {@inheritDoc}
   305      * @throws ArithmeticException {@inheritDoc}
   344      * @throws ArithmeticException {@inheritDoc}
   306      */
   345      */
   307     @Override
   346     @Override
   308     public MinguoDate plus(TemporalAmount amount) {
   347     public MinguoDate plus(TemporalAmount amount) {
   309         return (MinguoDate)super.plus(amount);
   348         return super.plus(amount);
   310     }
   349     }
   311 
   350 
   312     /**
   351     /**
   313      * {@inheritDoc}
   352      * {@inheritDoc}
   314      * @throws DateTimeException {@inheritDoc}
   353      * @throws DateTimeException {@inheritDoc}
   315      * @throws ArithmeticException {@inheritDoc}
   354      * @throws ArithmeticException {@inheritDoc}
   316      */
   355      */
   317     @Override
   356     @Override
   318     public MinguoDate minus(TemporalAmount amount) {
   357     public MinguoDate minus(TemporalAmount amount) {
   319         return (MinguoDate)super.minus(amount);
   358         return super.minus(amount);
   320     }
   359     }
   321 
   360 
   322     //-----------------------------------------------------------------------
   361     //-----------------------------------------------------------------------
   323     @Override
   362     @Override
   324     MinguoDate plusYears(long years) {
   363     MinguoDate plusYears(long years) {
   335         return with(isoDate.plusDays(days));
   374         return with(isoDate.plusDays(days));
   336     }
   375     }
   337 
   376 
   338     @Override
   377     @Override
   339     public MinguoDate plus(long amountToAdd, TemporalUnit unit) {
   378     public MinguoDate plus(long amountToAdd, TemporalUnit unit) {
   340         return (MinguoDate)super.plus(amountToAdd, unit);
   379         return super.plus(amountToAdd, unit);
   341     }
   380     }
   342 
   381 
   343     @Override
   382     @Override
   344     public MinguoDate minus(long amountToAdd, TemporalUnit unit) {
   383     public MinguoDate minus(long amountToAdd, TemporalUnit unit) {
   345         return (MinguoDate)super.minus(amountToAdd, unit);
   384         return super.minus(amountToAdd, unit);
   346     }
   385     }
   347 
   386 
   348     @Override
   387     @Override
   349     MinguoDate plusWeeks(long weeksToAdd) {
   388     MinguoDate plusWeeks(long weeksToAdd) {
   350         return (MinguoDate)super.plusWeeks(weeksToAdd);
   389         return super.plusWeeks(weeksToAdd);
   351     }
   390     }
   352 
   391 
   353     @Override
   392     @Override
   354     MinguoDate minusYears(long yearsToSubtract) {
   393     MinguoDate minusYears(long yearsToSubtract) {
   355         return (MinguoDate)super.minusYears(yearsToSubtract);
   394         return super.minusYears(yearsToSubtract);
   356     }
   395     }
   357 
   396 
   358     @Override
   397     @Override
   359     MinguoDate minusMonths(long monthsToSubtract) {
   398     MinguoDate minusMonths(long monthsToSubtract) {
   360         return (MinguoDate)super.minusMonths(monthsToSubtract);
   399         return super.minusMonths(monthsToSubtract);
   361     }
   400     }
   362 
   401 
   363     @Override
   402     @Override
   364     MinguoDate minusWeeks(long weeksToSubtract) {
   403     MinguoDate minusWeeks(long weeksToSubtract) {
   365         return (MinguoDate)super.minusWeeks(weeksToSubtract);
   404         return super.minusWeeks(weeksToSubtract);
   366     }
   405     }
   367 
   406 
   368     @Override
   407     @Override
   369     MinguoDate minusDays(long daysToSubtract) {
   408     MinguoDate minusDays(long daysToSubtract) {
   370         return (MinguoDate)super.minusDays(daysToSubtract);
   409         return super.minusDays(daysToSubtract);
   371     }
   410     }
   372 
   411 
   373     private MinguoDate with(LocalDate newDate) {
   412     private MinguoDate with(LocalDate newDate) {
   374         return (newDate.equals(isoDate) ? this : new MinguoDate(newDate));
   413         return (newDate.equals(isoDate) ? this : new MinguoDate(newDate));
   375     }
   414     }
   376 
   415 
   377     @Override        // for javadoc and covariant return type
   416     @Override        // for javadoc and covariant return type
   378     public final ChronoLocalDateTime<MinguoDate> atTime(LocalTime localTime) {
   417     public final ChronoLocalDateTime<MinguoDate> atTime(LocalTime localTime) {
   379         return (ChronoLocalDateTime<MinguoDate>)super.atTime(localTime);
   418         return super.atTime(localTime);
   380     }
   419     }
   381 
   420 
   382     @Override
   421     @Override
   383     public Period periodUntil(ChronoLocalDate<?> endDate) {
   422     public Period periodUntil(ChronoLocalDate<?> endDate) {
   384         return isoDate.periodUntil(endDate);
   423         return isoDate.periodUntil(endDate);
   417         out.writeInt(get(YEAR));
   456         out.writeInt(get(YEAR));
   418         out.writeByte(get(MONTH_OF_YEAR));
   457         out.writeByte(get(MONTH_OF_YEAR));
   419         out.writeByte(get(DAY_OF_MONTH));
   458         out.writeByte(get(DAY_OF_MONTH));
   420     }
   459     }
   421 
   460 
   422     static ChronoLocalDate readExternal(DataInput in) throws IOException {
   461     static ChronoLocalDate<?> readExternal(DataInput in) throws IOException {
   423         int year = in.readInt();
   462         int year = in.readInt();
   424         int month = in.readByte();
   463         int month = in.readByte();
   425         int dayOfMonth = in.readByte();
   464         int dayOfMonth = in.readByte();
   426         return MinguoChronology.INSTANCE.date(year, month, dayOfMonth);
   465         return MinguoChronology.INSTANCE.date(year, month, dayOfMonth);
   427     }
   466     }