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.calendar; 57 58 import static java.time.temporal.ChronoField.ERA; 59 60 import java.util.Locale; 61 62 import java.time.DateTimeException; 63 import java.time.temporal.ChronoField; 64 import java.time.temporal.Queries; 65 import java.time.temporal.Temporal; 66 import java.time.temporal.TemporalField; 67 import java.time.temporal.TemporalQuery; 68 import java.time.temporal.ValueRange; 69 import java.time.temporal.ChronoLocalDate; 70 import java.time.temporal.Era; 71 import java.time.format.DateTimeFormatterBuilder; 72 import java.time.format.TextStyle; 73 74 /** 75 * An era in the Coptic calendar system. 76 * <p> 77 * The Coptic calendar system uses the 'Era of the Martyrs'. 78 * The start of the Coptic epoch {@code 0001-01-01 (Coptic)} is {@code 0284-08-29 (ISO)}. 79 * <p> 80 * <b>Do not use {@code ordinal()} to obtain the numeric representation of {@code CopticEra}. 81 * Use {@code getValue()} instead.</b> 82 * 83 * <h4>Implementation notes</h4> 84 * This is an immutable and thread-safe enum. 85 */ 86 enum CopticEra implements Era<CopticChrono> { 87 88 /** 89 * The singleton instance for the era BEFORE_AM, 'Before Era of the Martyrs'. 90 * This has the numeric value of {@code 0}. 91 */ 92 BEFORE_AM, 93 /** 94 * The singleton instance for the era AM, 'Era of the Martyrs'. 95 * This has the numeric value of {@code 1}. 96 */ 97 AM; 98 99 //----------------------------------------------------------------------- 100 /** 101 * Obtains an instance of {@code CopticEra} from an {@code int} value. 102 * <p> 103 * {@code CopticEra} is an enum representing the Coptic eras of BEFORE_AM/AM. 104 * This factory allows the enum to be obtained from the {@code int} value. 105 * 106 * @param era the BEFORE_AM/AM value to represent, from 0 (BEFORE_AM) to 1 (AM) 107 * @return the era singleton, not null 108 * @throws DateTimeException if the value is invalid 109 */ 110 public static CopticEra of(int era) { 111 switch (era) { 112 case 0: 113 return BEFORE_AM; 114 case 1: 115 return AM; 116 default: 117 throw new DateTimeException("Invalid era: " + era); 118 } 119 } 120 121 //----------------------------------------------------------------------- 122 /** 123 * Gets the numeric era {@code int} value. 124 * <p> 125 * The era BEFORE_AM has the value 0, while the era AM has the value 1. 126 * 127 * @return the era value, from 0 (BEFORE_AM) to 1 (AM) 128 */ 129 public int getValue() { 130 return ordinal(); 131 } 132 133 @Override 134 public CopticChrono getChrono() { 135 return CopticChrono.INSTANCE; 136 } 137 138 // JDK8 default methods: 139 //----------------------------------------------------------------------- 140 @Override 141 public ChronoLocalDate<CopticChrono> date(int year, int month, int day) { 142 return getChrono().date(this, year, month, day); 143 } 144 145 @Override 146 public ChronoLocalDate<CopticChrono> dateYearDay(int year, int dayOfYear) { 147 return getChrono().dateYearDay(this, year, dayOfYear); 148 } 149 150 //----------------------------------------------------------------------- 151 @Override 152 public boolean isSupported(TemporalField field) { 153 if (field instanceof ChronoField) { 154 return field == ERA; 155 } 156 return field != null && field.doIsSupported(this); 157 } 158 159 @Override 160 public ValueRange range(TemporalField field) { 161 if (field == ERA) { 162 return field.range(); 163 } else if (field instanceof ChronoField) { 164 throw new DateTimeException("Unsupported field: " + field.getName()); 165 } 166 return field.doRange(this); 167 } 168 169 @Override 170 public int get(TemporalField field) { 171 if (field == ERA) { 172 return getValue(); 173 } 174 return range(field).checkValidIntValue(getLong(field), field); 175 } 176 177 @Override 178 public long getLong(TemporalField field) { 179 if (field == ERA) { 180 return getValue(); 181 } else if (field instanceof ChronoField) { 182 throw new DateTimeException("Unsupported field: " + field.getName()); 183 } 184 return field.doGet(this); 185 } 186 187 //------------------------------------------------------------------------- 188 @Override 189 public Temporal adjustInto(Temporal dateTime) { 190 return dateTime.with(ERA, getValue()); 191 } 192 193 @SuppressWarnings("unchecked") 194 @Override 195 public <R> R query(TemporalQuery<R> query) { 196 if (query == Queries.zoneId()) { 197 return null; 198 } else if (query == Queries.chrono()) { 199 return (R) getChrono(); 200 } 201 return query.queryFrom(this); 202 } 203 204 //----------------------------------------------------------------------- 205 @Override 206 public String getText(TextStyle style, Locale locale) { 207 return new DateTimeFormatterBuilder().appendText(ERA, style).toFormatter(locale).print(this); 208 } 209 210 }