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) 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 } | 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) 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 } |