From d141827c4ed1a3da836f31c81bd1fde454607bef Mon Sep 17 00:00:00 2001 From: Meno Hochschild Date: Sat, 6 Jan 2018 21:42:58 +0100 Subject: [PATCH] first draft of cyclic year see issue #638 --- .../java/net/time4j/calendar/CyclicYear.java | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 calendar/src/main/java/net/time4j/calendar/CyclicYear.java diff --git a/calendar/src/main/java/net/time4j/calendar/CyclicYear.java b/calendar/src/main/java/net/time4j/calendar/CyclicYear.java new file mode 100644 index 000000000..e7a417b7f --- /dev/null +++ b/calendar/src/main/java/net/time4j/calendar/CyclicYear.java @@ -0,0 +1,151 @@ +/* + * ----------------------------------------------------------------------- + * Copyright © 2013-2018 Meno Hochschild, + * ----------------------------------------------------------------------- + * This file (CyclicYear.java) is part of project Time4J. + * + * Time4J is free software: You can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 2.1 of the License, or + * (at your option) any later version. + * + * Time4J is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Time4J. If not, see . + * ----------------------------------------------------------------------- + */ + +package net.time4j.calendar; + +import java.io.InvalidObjectException; +import java.io.ObjectInputStream; +import java.io.Serializable; + + +/** + *

Represents the cyclic year used in East Asian calendars.

+ * + * @author Meno Hochschild + * @since 3.39/4.34 + * @doctags.concurrency {immutable} + */ +/*[deutsch] + *

Repräsentiert das zyklische Jahr, das in ostasiatischen Kalendern verwendet wird.

+ * + * @author Meno Hochschild + * @since 3.39/4.34 + * @doctags.concurrency {immutable} + */ +public final class CyclicYear + implements Comparable, Serializable { + + //~ Statische Felder/Initialisierungen ------------------------------------ + + //~ Instanzvariablen ------------------------------------------------------ + + /** + * @serial the number of cyclic year + */ + private final int year; + + //~ Konstruktoren --------------------------------------------------------- + + private CyclicYear(int year) { + super(); + + if ((year < 1) || (year > 60)) { + throw new IllegalArgumentException("Out of range: " + year); + } + + this.year = year; + } + + //~ Methoden -------------------------------------------------------------- + + /** + *

Obtains an instance of cyclic year.

+ * + * @param yearOfCycle year number in the range 1-60 + * @return CyclicYear + * @throws IllegalArgumentException if the parameter is out of range + */ + /*[deutsch] + *

Liefert eine Instanz eines zyklischen Jahres.

+ * + * @param yearOfCycle year number in the range 1-60 + * @return CyclicYear + * @throws IllegalArgumentException if the parameter is out of range + */ + public static CyclicYear of(int yearOfCycle) { + + return new CyclicYear(yearOfCycle); + + } + + /** + *

Obtains the associated year number in the range 1-60.

+ * + * @return int + */ + /*[deutsch] + *

Liefert die Jahreszahl im Bereich 1-60.

+ * + * @return int + */ + public int getNumber() { + + return this.year; + + } + + @Override + public int compareTo(CyclicYear other) { + + return (this.year - other.year); + + } + + @Override + public boolean equals(Object obj) { + + if (obj instanceof CyclicYear) { + return (this.year == ((CyclicYear) obj).year); + } else { + return false; + } + + } + + @Override + public int hashCode() { + + return Integer.hashCode(this.year); + + } + + @Override + public String toString() { + + return String.valueOf(this.year); + + } + + /** + * @serialData Checks the consistency of deserialized data + * @param in object input stream + * @throws InvalidObjectException if the year is not in range 1-60 + */ + private void readObject(ObjectInputStream in) + throws InvalidObjectException { + + if ((year < 1) || (year > 60)) { + throw new InvalidObjectException("Out of range: " + this.year); + } + + } + +}