1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
  27  *
  28  * All rights reserved.
  29  *
  30  * Redistribution and use in source and binary forms, with or without
  31  * modification, are permitted provided that the following conditions are met:
  32  *
  33  *  * Redistributions of source code must retain the above copyright notice,
  34  *    this list of conditions and the following disclaimer.
  35  *
  36  *  * Redistributions in binary form must reproduce the above copyright notice,
  37  *    this list of conditions and the following disclaimer in the documentation
  38  *    and/or other materials provided with the distribution.
  39  *
  40  *  * Neither the name of JSR-310 nor the names of its contributors
  41  *    may be used to endorse or promote products derived from this software
  42  *    without specific prior written permission.
  43  *
  44  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  45  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  46  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  47  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  48  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  49  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  50  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  51  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  52  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  53  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  54  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  55  */
  56 package tck.java.time.chrono;
  57 
  58 
  59 import java.time.DateTimeException;
  60 import java.time.chrono.Era;
  61 
  62 /**
  63  * An era in the Coptic calendar system.
  64  * <p>
  65  * The Coptic calendar system uses the 'Era of the Martyrs'.
  66  * The start of the Coptic epoch {@code 0001-01-01 (Coptic)} is {@code 0284-08-29 (ISO)}.
  67  * <p>
  68  * <b>Do not use {@code ordinal()} to obtain the numeric representation of {@code CopticEra}.
  69  * Use {@code getValue()} instead.</b>
  70  *
  71  * <h4>Implementation notes</h4>
  72  * This is an immutable and thread-safe enum.
  73  */
  74 enum CopticEra implements Era {
  75 
  76     /**
  77      * The singleton instance for the era BEFORE_AM, 'Before Era of the Martyrs'.
  78      * This has the numeric value of {@code 0}.
  79      */
  80     BEFORE_AM,
  81     /**
  82      * The singleton instance for the era AM, 'Era of the Martyrs'.
  83      * This has the numeric value of {@code 1}.
  84      */
  85     AM;
  86 
  87     //-----------------------------------------------------------------------
  88     /**
  89      * Obtains an instance of {@code CopticEra} from an {@code int} value.
  90      * <p>
  91      * {@code CopticEra} is an enum representing the Coptic eras of BEFORE_AM/AM.
  92      * This factory allows the enum to be obtained from the {@code int} value.
  93      *
  94      * @param era  the BEFORE_AM/AM value to represent, from 0 (BEFORE_AM) to 1 (AM)
  95      * @return the era singleton, not null
  96      * @throws DateTimeException if the value is invalid
  97      */
  98     public static CopticEra of(int era) {
  99         switch (era) {
 100             case 0:
 101                 return BEFORE_AM;
 102             case 1:
 103                 return AM;
 104             default:
 105                 throw new DateTimeException("Invalid era: " + era);
 106         }
 107     }
 108 
 109     //-----------------------------------------------------------------------
 110     /**
 111      * Gets the numeric era {@code int} value.
 112      * <p>
 113      * The era BEFORE_AM has the value 0, while the era AM has the value 1.
 114      *
 115      * @return the era value, from 0 (BEFORE_AM) to 1 (AM)
 116      */
 117     public int getValue() {
 118         return ordinal();
 119     }
 120 
 121     @Override
 122     public CopticChronology getChronology() {
 123         return CopticChronology.INSTANCE;
 124     }
 125 
 126     // JDK8 default methods:
 127     //-----------------------------------------------------------------------
 128     @Override
 129     public CopticDate date(int year, int month, int day) {
 130         return (CopticDate)(getChronology().date(this, year, month, day));
 131     }
 132 
 133     @Override
 134     public CopticDate dateYearDay(int year, int dayOfYear) {
 135         return (CopticDate)(getChronology().dateYearDay(this, year, dayOfYear));
 136     }
 137 
 138 }