jdk/src/share/classes/java/time/calendar/ThaiBuddhistDate.java
changeset 15658 55b829ca2334
parent 15657 c588664d547e
child 15659 e575dab44ff5
equal deleted inserted replaced
15657:c588664d547e 15658:55b829ca2334
     1 /*
       
     2  * Copyright (c) 2012, 2013, 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
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
       
    28  *
       
    29  * All rights reserved.
       
    30  *
       
    31  * Redistribution and use in source and binary forms, with or without
       
    32  * modification, are permitted provided that the following conditions are met:
       
    33  *
       
    34  *  * Redistributions of source code must retain the above copyright notice,
       
    35  *    this list of conditions and the following disclaimer.
       
    36  *
       
    37  *  * Redistributions in binary form must reproduce the above copyright notice,
       
    38  *    this list of conditions and the following disclaimer in the documentation
       
    39  *    and/or other materials provided with the distribution.
       
    40  *
       
    41  *  * Neither the name of JSR-310 nor the names of its contributors
       
    42  *    may be used to endorse or promote products derived from this software
       
    43  *    without specific prior written permission.
       
    44  *
       
    45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
       
    49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       
    53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
       
    54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    56  */
       
    57 package java.time.calendar;
       
    58 
       
    59 import static java.time.calendar.ThaiBuddhistChrono.YEARS_DIFFERENCE;
       
    60 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
       
    61 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
       
    62 import static java.time.temporal.ChronoField.YEAR;
       
    63 
       
    64 import java.io.DataInput;
       
    65 import java.io.DataOutput;
       
    66 import java.io.IOException;
       
    67 import java.io.Serializable;
       
    68 import java.time.DateTimeException;
       
    69 import java.time.LocalDate;
       
    70 import java.time.temporal.ChronoField;
       
    71 import java.time.temporal.ChronoLocalDate;
       
    72 import java.time.temporal.TemporalField;
       
    73 import java.time.temporal.ValueRange;
       
    74 import java.util.Objects;
       
    75 
       
    76 /**
       
    77  * A date in the Thai Buddhist calendar system.
       
    78  * <p>
       
    79  * This implements {@code ChronoLocalDate} for the {@link ThaiBuddhistChrono Thai Buddhist calendar}.
       
    80  *
       
    81  * <h3>Specification for implementors</h3>
       
    82  * This class is immutable and thread-safe.
       
    83  *
       
    84  * @since 1.8
       
    85  */
       
    86 final class ThaiBuddhistDate
       
    87         extends ChronoDateImpl<ThaiBuddhistChrono>
       
    88         implements ChronoLocalDate<ThaiBuddhistChrono>, Serializable {
       
    89     // this class is package-scoped so that future conversion to public
       
    90     // would not change serialization
       
    91 
       
    92     /**
       
    93      * Serialization version.
       
    94      */
       
    95     private static final long serialVersionUID = -8722293800195731463L;
       
    96 
       
    97     /**
       
    98      * The underlying date.
       
    99      */
       
   100     private final LocalDate isoDate;
       
   101 
       
   102     /**
       
   103      * Creates an instance from an ISO date.
       
   104      *
       
   105      * @param isoDate  the standard local date, validated not null
       
   106      */
       
   107     ThaiBuddhistDate(LocalDate isoDate) {
       
   108         Objects.requireNonNull(isoDate, "isoDate");
       
   109         this.isoDate = isoDate;
       
   110     }
       
   111 
       
   112     //-----------------------------------------------------------------------
       
   113     @Override
       
   114     public ThaiBuddhistChrono getChrono() {
       
   115         return ThaiBuddhistChrono.INSTANCE;
       
   116     }
       
   117 
       
   118     @Override
       
   119     public int lengthOfMonth() {
       
   120         return isoDate.lengthOfMonth();
       
   121     }
       
   122 
       
   123     @Override
       
   124     public ValueRange range(TemporalField field) {
       
   125         if (field instanceof ChronoField) {
       
   126             if (isSupported(field)) {
       
   127                 ChronoField f = (ChronoField) field;
       
   128                 switch (f) {
       
   129                     case DAY_OF_MONTH:
       
   130                     case DAY_OF_YEAR:
       
   131                     case ALIGNED_WEEK_OF_MONTH:
       
   132                         return isoDate.range(field);
       
   133                     case YEAR_OF_ERA: {
       
   134                         ValueRange range = YEAR.range();
       
   135                         long max = (getProlepticYear() <= 0 ? -(range.getMinimum() + YEARS_DIFFERENCE) + 1 : range.getMaximum() + YEARS_DIFFERENCE);
       
   136                         return ValueRange.of(1, max);
       
   137                     }
       
   138                 }
       
   139                 return getChrono().range(f);
       
   140             }
       
   141             throw new DateTimeException("Unsupported field: " + field.getName());
       
   142         }
       
   143         return field.doRange(this);
       
   144     }
       
   145 
       
   146     @Override
       
   147     public long getLong(TemporalField field) {
       
   148         if (field instanceof ChronoField) {
       
   149             switch ((ChronoField) field) {
       
   150                 case YEAR_OF_ERA: {
       
   151                     int prolepticYear = getProlepticYear();
       
   152                     return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
       
   153                 }
       
   154                 case YEAR:
       
   155                     return getProlepticYear();
       
   156                 case ERA:
       
   157                     return (getProlepticYear() >= 1 ? 1 : 0);
       
   158             }
       
   159             return isoDate.getLong(field);
       
   160         }
       
   161         return field.doGet(this);
       
   162     }
       
   163 
       
   164     private int getProlepticYear() {
       
   165         return isoDate.getYear() + YEARS_DIFFERENCE;
       
   166     }
       
   167 
       
   168     //-----------------------------------------------------------------------
       
   169     @Override
       
   170     public ThaiBuddhistDate with(TemporalField field, long newValue) {
       
   171         if (field instanceof ChronoField) {
       
   172             ChronoField f = (ChronoField) field;
       
   173             if (getLong(f) == newValue) {
       
   174                 return this;
       
   175             }
       
   176             switch (f) {
       
   177                 case YEAR_OF_ERA:
       
   178                 case YEAR:
       
   179                 case ERA: {
       
   180                     f.checkValidValue(newValue);
       
   181                     int nvalue = (int) newValue;
       
   182                     switch (f) {
       
   183                         case YEAR_OF_ERA:
       
   184                             return with(isoDate.withYear((getProlepticYear() >= 1 ? nvalue : 1 - nvalue)  - YEARS_DIFFERENCE));
       
   185                         case YEAR:
       
   186                             return with(isoDate.withYear(nvalue - YEARS_DIFFERENCE));
       
   187                         case ERA:
       
   188                             return with(isoDate.withYear((1 - getProlepticYear()) - YEARS_DIFFERENCE));
       
   189                     }
       
   190                 }
       
   191             }
       
   192             return with(isoDate.with(field, newValue));
       
   193         }
       
   194         return (ThaiBuddhistDate) ChronoLocalDate.super.with(field, newValue);
       
   195     }
       
   196 
       
   197     //-----------------------------------------------------------------------
       
   198     @Override
       
   199     ThaiBuddhistDate plusYears(long years) {
       
   200         return with(isoDate.plusYears(years));
       
   201     }
       
   202 
       
   203     @Override
       
   204     ThaiBuddhistDate plusMonths(long months) {
       
   205         return with(isoDate.plusMonths(months));
       
   206     }
       
   207 
       
   208     @Override
       
   209     ThaiBuddhistDate plusDays(long days) {
       
   210         return with(isoDate.plusDays(days));
       
   211     }
       
   212 
       
   213     private ThaiBuddhistDate with(LocalDate newDate) {
       
   214         return (newDate.equals(isoDate) ? this : new ThaiBuddhistDate(newDate));
       
   215     }
       
   216 
       
   217     @Override  // override for performance
       
   218     public long toEpochDay() {
       
   219         return isoDate.toEpochDay();
       
   220     }
       
   221 
       
   222     //-------------------------------------------------------------------------
       
   223     @Override  // override for performance
       
   224     public boolean equals(Object obj) {
       
   225         if (this == obj) {
       
   226             return true;
       
   227         }
       
   228         if (obj instanceof ThaiBuddhistDate) {
       
   229             ThaiBuddhistDate otherDate = (ThaiBuddhistDate) obj;
       
   230             return this.isoDate.equals(otherDate.isoDate);
       
   231         }
       
   232         return false;
       
   233     }
       
   234 
       
   235     @Override  // override for performance
       
   236     public int hashCode() {
       
   237         return getChrono().getId().hashCode() ^ isoDate.hashCode();
       
   238     }
       
   239 
       
   240     //-----------------------------------------------------------------------
       
   241     private Object writeReplace() {
       
   242         return new Ser(Ser.THAIBUDDHIST_DATE_TYPE, this);
       
   243     }
       
   244 
       
   245     void writeExternal(DataOutput out) throws IOException {
       
   246         // MinguoChrono is implicit in the THAIBUDDHIST_DATE_TYPE
       
   247         out.writeInt(this.get(YEAR));
       
   248         out.writeByte(this.get(MONTH_OF_YEAR));
       
   249         out.writeByte(this.get(DAY_OF_MONTH));
       
   250     }
       
   251 
       
   252     static ChronoLocalDate<ThaiBuddhistChrono> readExternal(DataInput in) throws IOException {
       
   253         int year = in.readInt();
       
   254         int month = in.readByte();
       
   255         int dayOfMonth = in.readByte();
       
   256         return ThaiBuddhistChrono.INSTANCE.date(year, month, dayOfMonth);
       
   257     }
       
   258 
       
   259 }