AccountingEra.java

  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. import java.time.DateTimeException;
  34. import java.time.chrono.Era;

  35. /**
  36.  * An era in the Accounting calendar system.
  37.  * <p>
  38.  * The Accounting calendar system has two eras.
  39.  * The current era, for years from 1 onwards, is known as the 'Current Era'.
  40.  * All previous years, zero or earlier in the proleptic count or one and greater
  41.  * in the year-of-era count, are part of the 'Before Current Era' era.
  42.  * <p>
  43.  * The start of accounting epochs {@code 0001-01-01 (Accounting)} will vary against the ISO calendar.
  44.  * Depending on options chosen, it can start as early as {@code 0000-01-26 (ISO)} or as late as {@code 0001-01-04 (ISO)}.
  45.  * <p>
  46.  * <b>Do not use {@code ordinal()} to obtain the numeric representation of {@code AccountingEra}.
  47.  * Use {@code getValue()} instead.</b>
  48.  *
  49.  * <h3>Implementation Requirements:</h3>
  50.  * This is an immutable and thread-safe enum.
  51.  */
  52. public enum AccountingEra implements Era {

  53.     /**
  54.      * The singleton instance for the era before the current one, 'Before Current Era',
  55.      * which has the numeric value 0.
  56.      */
  57.     BCE,
  58.     /**
  59.      * The singleton instance for the current era, 'Current Era',
  60.      * which has the numeric value 1.
  61.      */
  62.     CE;

  63.     //-----------------------------------------------------------------------
  64.     /**
  65.      * Obtains an instance of {@code AccountingEra} from an {@code int} value.
  66.      * <p>
  67.      * {@code AccountingEra} is an enum representing the Accounting eras of BCE/CE.
  68.      * This factory allows the enum to be obtained from the {@code int} value.
  69.      *
  70.      * @param era  the BCE/CE value to represent, from 0 (BCE) to 1 (CE)
  71.      * @return the era singleton, not null
  72.      * @throws DateTimeException if the value is invalid
  73.      */
  74.     public static AccountingEra of(int era) {
  75.         switch (era) {
  76.             case 0:
  77.                 return BCE;
  78.             case 1:
  79.                 return CE;
  80.             default:
  81.                 throw new DateTimeException("Invalid era: " + era);
  82.         }
  83.     }

  84.     //-----------------------------------------------------------------------
  85.     /**
  86.      * Gets the numeric era {@code int} value.
  87.      * <p>
  88.      * The era BCE has the value 0, while the era CE has the value 1.
  89.      *
  90.      * @return the era value, from 0 (BCE) to 1 (CE)
  91.      */
  92.     @Override
  93.     public int getValue() {
  94.         return ordinal();
  95.     }

  96. }