|
| 1 | +/* |
| 2 | + * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of JSR-310 nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package org.threeten.extra.chrono; |
| 33 | + |
| 34 | +import java.io.Serializable; |
| 35 | +import java.time.Clock; |
| 36 | +import java.time.DateTimeException; |
| 37 | +import java.time.Instant; |
| 38 | +import java.time.ZoneId; |
| 39 | +import java.time.chrono.ChronoLocalDateTime; |
| 40 | +import java.time.chrono.ChronoZonedDateTime; |
| 41 | +import java.time.chrono.Era; |
| 42 | +import java.time.format.ResolverStyle; |
| 43 | +import java.time.temporal.ChronoField; |
| 44 | +import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; |
| 45 | +import static java.time.temporal.ChronoField.DAY_OF_WEEK; |
| 46 | +import java.time.temporal.TemporalAccessor; |
| 47 | +import java.time.temporal.TemporalField; |
| 48 | +import java.time.temporal.ValueRange; |
| 49 | +import java.time.temporal.WeekFields; |
| 50 | +import java.util.Arrays; |
| 51 | +import java.util.List; |
| 52 | +import java.util.Map; |
| 53 | + |
| 54 | +/** |
| 55 | +
|
| 56 | + */ |
| 57 | +public final class FrenchRepublicChronology |
| 58 | + extends AbstractNileChronology |
| 59 | + implements Serializable { |
| 60 | + |
| 61 | + /** |
| 62 | + * Singleton instance for the FrenchRepublic chronology. |
| 63 | + */ |
| 64 | + public static final FrenchRepublicChronology INSTANCE = new FrenchRepublicChronology(); |
| 65 | + |
| 66 | + /** |
| 67 | + * Serialization version. |
| 68 | + */ |
| 69 | + private static final long serialVersionUID = 7291205177830286973L; |
| 70 | + |
| 71 | + /** |
| 72 | + * Range of days of week. |
| 73 | + */ |
| 74 | + static final ValueRange DOW_RANGE = ValueRange.of(1, 10); |
| 75 | + /** |
| 76 | + * Range of weeks. |
| 77 | + */ |
| 78 | + static final ValueRange ALIGNED_WOM_RANGE = ValueRange.of(1, 1, 3); |
| 79 | + |
| 80 | + /** |
| 81 | + * Private constructor, that is public to satisfy the {@code ServiceLoader}. |
| 82 | + * @deprecated Use the singleton {@link #INSTANCE} instead. |
| 83 | + */ |
| 84 | + @Deprecated |
| 85 | + public FrenchRepublicChronology() { |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Resolve singleton. |
| 90 | + * |
| 91 | + * @return the singleton instance, not null |
| 92 | + */ |
| 93 | + private Object readResolve() { |
| 94 | + return INSTANCE; |
| 95 | + } |
| 96 | + |
| 97 | + //----------------------------------------------------------------------- |
| 98 | + /** |
| 99 | + * Gets the ID of the chronology - 'FrenchRepublic'. |
| 100 | + * <p> |
| 101 | + * The ID uniquely identifies the {@code Chronology}. |
| 102 | + * It can be used to lookup the {@code Chronology} using {@link #of(String)}. |
| 103 | + * |
| 104 | + * @return the chronology ID - 'FrenchRepublic' |
| 105 | + * @see #getCalendarType() |
| 106 | + */ |
| 107 | + @Override |
| 108 | + public String getId() { |
| 109 | + return "French Republican"; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Gets the calendar type of the underlying calendar system - 'frenchrepublican'. |
| 114 | + * <p> |
| 115 | + * The <em>Unicode Locale Data Markup Language (LDML)</em> specification |
| 116 | + * does not define an identifier for the French Revolutionary calendar, but |
| 117 | + * were it to do so, 'frenchrepublican' is likely to be chosen. |
| 118 | + * |
| 119 | + * @return the calendar system type - 'frenchrepublican' |
| 120 | + * @see #getId() |
| 121 | + */ |
| 122 | + @Override |
| 123 | + public String getCalendarType() { |
| 124 | + return "frenchrepublican"; |
| 125 | + } |
| 126 | + |
| 127 | + //----------------------------------------------------------------------- |
| 128 | + /** |
| 129 | + * Obtains a local date in FrenchRepublic calendar system from the |
| 130 | + * era, year-of-era, month-of-year and day-of-month fields. |
| 131 | + * |
| 132 | + * @param era the FrenchRepublic era, not null |
| 133 | + * @param yearOfEra the year-of-era |
| 134 | + * @param month the month-of-year |
| 135 | + * @param dayOfMonth the day-of-month |
| 136 | + * @return the FrenchRepublic local date, not null |
| 137 | + * @throws DateTimeException if unable to create the date |
| 138 | + * @throws ClassCastException if the {@code era} is not a {@code FrenchRepublicEra} |
| 139 | + */ |
| 140 | + @Override |
| 141 | + public FrenchRepublicDate date(Era era, int yearOfEra, int month, int dayOfMonth) { |
| 142 | + return date(prolepticYear(era, yearOfEra), month, dayOfMonth); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Obtains a local date in FrenchRepublic calendar system from the |
| 147 | + * proleptic-year, month-of-year and day-of-month fields. |
| 148 | + * |
| 149 | + * @param prolepticYear the proleptic-year |
| 150 | + * @param month the month-of-year |
| 151 | + * @param dayOfMonth the day-of-month |
| 152 | + * @return the FrenchRepublic local date, not null |
| 153 | + * @throws DateTimeException if unable to create the date |
| 154 | + */ |
| 155 | + @Override |
| 156 | + public FrenchRepublicDate date(int prolepticYear, int month, int dayOfMonth) { |
| 157 | + return FrenchRepublicDate.of(prolepticYear, month, dayOfMonth); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Obtains a local date in FrenchRepublic calendar system from the |
| 162 | + * era, year-of-era and day-of-year fields. |
| 163 | + * |
| 164 | + * @param era the FrenchRepublic era, not null |
| 165 | + * @param yearOfEra the year-of-era |
| 166 | + * @param dayOfYear the day-of-year |
| 167 | + * @return the FrenchRepublic local date, not null |
| 168 | + * @throws DateTimeException if unable to create the date |
| 169 | + * @throws ClassCastException if the {@code era} is not a {@code FrenchRepublicEra} |
| 170 | + */ |
| 171 | + @Override |
| 172 | + public FrenchRepublicDate dateYearDay(Era era, int yearOfEra, int dayOfYear) { |
| 173 | + return dateYearDay(prolepticYear(era, yearOfEra), dayOfYear); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Obtains a local date in FrenchRepublic calendar system from the |
| 178 | + * proleptic-year and day-of-year fields. |
| 179 | + * |
| 180 | + * @param prolepticYear the proleptic-year |
| 181 | + * @param dayOfYear the day-of-year |
| 182 | + * @return the FrenchRepublic local date, not null |
| 183 | + * @throws DateTimeException if unable to create the date |
| 184 | + */ |
| 185 | + @Override |
| 186 | + public FrenchRepublicDate dateYearDay(int prolepticYear, int dayOfYear) { |
| 187 | + return FrenchRepublicDate.ofYearDay(prolepticYear, dayOfYear); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Obtains a local date in the FrenchRepublic calendar system from the epoch-day. |
| 192 | + * |
| 193 | + * @param epochDay the epoch day |
| 194 | + * @return the FrenchRepublic local date, not null |
| 195 | + * @throws DateTimeException if unable to create the date |
| 196 | + */ |
| 197 | + @Override // override with covariant return type |
| 198 | + public FrenchRepublicDate dateEpochDay(long epochDay) { |
| 199 | + return FrenchRepublicDate.ofEpochDay(epochDay); |
| 200 | + } |
| 201 | + |
| 202 | + //------------------------------------------------------------------------- |
| 203 | + /** |
| 204 | + * Obtains the current FrenchRepublic local date from the system clock in the default time-zone. |
| 205 | + * <p> |
| 206 | + * This will query the {@link Clock#systemDefaultZone() system clock} in the default |
| 207 | + * time-zone to obtain the current date. |
| 208 | + * <p> |
| 209 | + * Using this method will prevent the ability to use an alternate clock for testing |
| 210 | + * because the clock is hard-coded. |
| 211 | + * |
| 212 | + * @return the current FrenchRepublic local date using the system clock and default time-zone, not null |
| 213 | + * @throws DateTimeException if unable to create the date |
| 214 | + */ |
| 215 | + @Override // override with covariant return type |
| 216 | + public FrenchRepublicDate dateNow() { |
| 217 | + return FrenchRepublicDate.now(); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Obtains the current FrenchRepublic local date from the system clock in the specified time-zone. |
| 222 | + * <p> |
| 223 | + * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. |
| 224 | + * Specifying the time-zone avoids dependence on the default time-zone. |
| 225 | + * <p> |
| 226 | + * Using this method will prevent the ability to use an alternate clock for testing |
| 227 | + * because the clock is hard-coded. |
| 228 | + * |
| 229 | + * @param zone the zone ID to use, not null |
| 230 | + * @return the current FrenchRepublic local date using the system clock, not null |
| 231 | + * @throws DateTimeException if unable to create the date |
| 232 | + */ |
| 233 | + @Override // override with covariant return type |
| 234 | + public FrenchRepublicDate dateNow(ZoneId zone) { |
| 235 | + return FrenchRepublicDate.now(zone); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Obtains the current FrenchRepublic local date from the specified clock. |
| 240 | + * <p> |
| 241 | + * This will query the specified clock to obtain the current date - today. |
| 242 | + * Using this method allows the use of an alternate clock for testing. |
| 243 | + * The alternate clock may be introduced using {@link Clock dependency injection}. |
| 244 | + * |
| 245 | + * @param clock the clock to use, not null |
| 246 | + * @return the current FrenchRepublic local date, not null |
| 247 | + * @throws DateTimeException if unable to create the date |
| 248 | + */ |
| 249 | + @Override // override with covariant return type |
| 250 | + public FrenchRepublicDate dateNow(Clock clock) { |
| 251 | + return FrenchRepublicDate.now(clock); |
| 252 | + } |
| 253 | + |
| 254 | + //------------------------------------------------------------------------- |
| 255 | + /** |
| 256 | + * Obtains a FrenchRepublic local date from another date-time object. |
| 257 | + * |
| 258 | + * @param temporal the date-time object to convert, not null |
| 259 | + * @return the FrenchRepublic local date, not null |
| 260 | + * @throws DateTimeException if unable to create the date |
| 261 | + */ |
| 262 | + @Override |
| 263 | + public FrenchRepublicDate date(TemporalAccessor temporal) { |
| 264 | + return FrenchRepublicDate.from(temporal); |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * Obtains a FrenchRepublic local date-time from another date-time object. |
| 269 | + * |
| 270 | + * @param temporal the date-time object to convert, not null |
| 271 | + * @return the FrenchRepublic local date-time, not null |
| 272 | + * @throws DateTimeException if unable to create the date-time |
| 273 | + */ |
| 274 | + @Override |
| 275 | + @SuppressWarnings("unchecked") |
| 276 | + public ChronoLocalDateTime<FrenchRepublicDate> localDateTime(TemporalAccessor temporal) { |
| 277 | + return (ChronoLocalDateTime<FrenchRepublicDate>) super.localDateTime(temporal); |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * Obtains a FrenchRepublic zoned date-time from another date-time object. |
| 282 | + * |
| 283 | + * @param temporal the date-time object to convert, not null |
| 284 | + * @return the FrenchRepublic zoned date-time, not null |
| 285 | + * @throws DateTimeException if unable to create the date-time |
| 286 | + */ |
| 287 | + @Override |
| 288 | + @SuppressWarnings("unchecked") |
| 289 | + public ChronoZonedDateTime<FrenchRepublicDate> zonedDateTime(TemporalAccessor temporal) { |
| 290 | + return (ChronoZonedDateTime<FrenchRepublicDate>) super.zonedDateTime(temporal); |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * Obtains a FrenchRepublic zoned date-time in this chronology from an {@code Instant}. |
| 295 | + * |
| 296 | + * @param instant the instant to create the date-time from, not null |
| 297 | + * @param zone the time-zone, not null |
| 298 | + * @return the FrenchRepublic zoned date-time, not null |
| 299 | + * @throws DateTimeException if the result exceeds the supported range |
| 300 | + */ |
| 301 | + @Override |
| 302 | + @SuppressWarnings("unchecked") |
| 303 | + public ChronoZonedDateTime<FrenchRepublicDate> zonedDateTime(Instant instant, ZoneId zone) { |
| 304 | + return (ChronoZonedDateTime<FrenchRepublicDate>) super.zonedDateTime(instant, zone); |
| 305 | + } |
| 306 | + |
| 307 | + @Override |
| 308 | + public ValueRange range(ChronoField field) { |
| 309 | + if (field == DAY_OF_WEEK) |
| 310 | + return DOW_RANGE; |
| 311 | + else if (WeekFields.ISO.dayOfWeek().equals(field)) |
| 312 | + return DOW_RANGE; |
| 313 | + else if (field == ALIGNED_WEEK_OF_MONTH) |
| 314 | + return ALIGNED_WOM_RANGE; |
| 315 | + return super.range(field); |
| 316 | + } |
| 317 | + |
| 318 | + //----------------------------------------------------------------------- |
| 319 | + @Override |
| 320 | + public int prolepticYear(Era era, int yearOfEra) { |
| 321 | + if (! (era instanceof FrenchRepublicEra)) { |
| 322 | + throw new ClassCastException("Era must be FrenchRepublicEra"); |
| 323 | + } |
| 324 | + return (era == FrenchRepublicEra.REPUBLICAN ? yearOfEra : 1 - yearOfEra); |
| 325 | + } |
| 326 | + |
| 327 | + @Override |
| 328 | + public FrenchRepublicEra eraOf(int eraValue) { |
| 329 | + return FrenchRepublicEra.of(eraValue); |
| 330 | + } |
| 331 | + |
| 332 | + @Override |
| 333 | + public List<Era> eras() { |
| 334 | + return Arrays.<Era>asList(FrenchRepublicEra.values()); |
| 335 | + } |
| 336 | + |
| 337 | + //----------------------------------------------------------------------- |
| 338 | + @Override // override for return type |
| 339 | + public FrenchRepublicDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { |
| 340 | + return (FrenchRepublicDate) super.resolveDate(fieldValues, resolverStyle); |
| 341 | + } |
| 342 | + |
| 343 | +} |
0 commit comments