test/java/time/tck/java/time/chrono/CopticDate.java

Print this page

        

@@ -51,11 +51,11 @@
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
-package tck.java.time.calendar;
+package tck.java.time.chrono;
 
 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;

@@ -65,33 +65,30 @@
 
 import java.io.Serializable;
 
 import java.time.DateTimeException;
 import java.time.LocalDate;
+import java.time.Period;
+import java.time.chrono.ChronoLocalDate;
 import java.time.temporal.ChronoField;
-import java.time.temporal.ChronoLocalDate;
 import java.time.temporal.ChronoUnit;
-import java.time.temporal.Era;
 import java.time.temporal.Temporal;
-import java.time.temporal.TemporalAdjuster;
-import java.time.temporal.TemporalAdder;
-import java.time.temporal.TemporalSubtractor;
 import java.time.temporal.TemporalField;
 import java.time.temporal.TemporalUnit;
 import java.time.temporal.ValueRange;
-import java.time.temporal.Year;
+import java.time.Year;
 
 /**
  * A date in the Coptic calendar system.
  * <p>
- * This implements {@code ChronoLocalDate} for the {@link CopticChrono Coptic calendar}.
+ * This implements {@code ChronoLocalDate} for the {@link CopticChronology Coptic calendar}.
  *
  * <h4>Implementation notes</h4>
  * This class is immutable and thread-safe.
  */
-final class CopticDate
-        implements ChronoLocalDate<CopticChrono>, Serializable {
+public final class CopticDate
+        implements ChronoLocalDate<CopticDate>, Serializable {
 
     /**
      * Serialization version.
      */
     private static final long serialVersionUID = -7920528871688876868L;

@@ -131,11 +128,11 @@
         return new CopticDate(prolepticYear, month, dom);
     }
 
     private static CopticDate resolvePreviousValid(int prolepticYear, int month, int day) {
         if (month == 13 && day > 5) {
-            day = CopticChrono.INSTANCE.isLeapYear(prolepticYear) ? 6 : 5;
+            day = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? 6 : 5;
         }
         return new CopticDate(prolepticYear, month, day);
     }
 
     //-----------------------------------------------------------------------

@@ -146,16 +143,16 @@
      * @param month  the Coptic month, from 1 to 13
      * @param dayOfMonth  the Coptic day-of-month, from 1 to 30
      * @throws DateTimeException if the date is invalid
      */
     CopticDate(int prolepticYear, int month, int dayOfMonth) {
-        CopticChrono.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
+        CopticChronology.MOY_RANGE.checkValidValue(month, MONTH_OF_YEAR);
         ValueRange range;
         if (month == 13) {
-            range = CopticChrono.INSTANCE.isLeapYear(prolepticYear) ? CopticChrono.DOM_RANGE_LEAP : CopticChrono.DOM_RANGE_NONLEAP;
+            range = CopticChronology.INSTANCE.isLeapYear(prolepticYear) ? CopticChronology.DOM_RANGE_LEAP : CopticChronology.DOM_RANGE_NONLEAP;
         } else {
-            range = CopticChrono.DOM_RANGE;
+            range = CopticChronology.DOM_RANGE;
         }
         range.checkValidValue(dayOfMonth, DAY_OF_MONTH);
 
         this.prolepticYear = prolepticYear;
         this.month = (short) month;

@@ -172,12 +169,12 @@
         return this;
     }
 
     //-----------------------------------------------------------------------
     @Override
-    public CopticChrono getChrono() {
-        return CopticChrono.INSTANCE;
+    public CopticChronology getChronology() {
+        return CopticChronology.INSTANCE;
     }
 
     //-----------------------------------------------------------------------
     @Override
     public int lengthOfMonth() {

@@ -200,15 +197,15 @@
                     case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, month == 13 ? 1 : 5);
                     case YEAR:
                     case YEAR_OF_ERA: return (prolepticYear <= 0 ?
                             ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));  // TODO
                 }
-                return getChrono().range(f);
+                return getChronology().range(f);
             }
             throw new DateTimeException("Unsupported field: " + field.getName());
         }
-        return field.doRange(this);
+        return field.rangeRefinedBy(this);
     }
 
     @Override
     public long getLong(TemporalField field) {
         if (field instanceof ChronoField) {

@@ -226,11 +223,11 @@
                 case YEAR: return prolepticYear;
                 case ERA: return (prolepticYear >= 1 ? 1 : 0);
             }
             throw new DateTimeException("Unsupported field: " + field.getName());
         }
-        return field.doGet(this);
+        return field.getFrom(this);
     }
 
     @Override
     public CopticDate with(TemporalField field, long newValue) {
         if (field instanceof ChronoField) {

@@ -251,11 +248,11 @@
                 case YEAR: return resolvePreviousValid(nvalue, month, day);
                 case ERA: return resolvePreviousValid(1 - prolepticYear, month, day);
             }
             throw new DateTimeException("Unsupported field: " + field.getName());
         }
-        return field.doWith(this, newValue);
+        return field.adjustInto(this, newValue);
     }
 
     //-----------------------------------------------------------------------
     @Override
     public CopticDate plus(long amountToAdd, TemporalUnit unit) {

@@ -270,11 +267,11 @@
                 case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
                 case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
             }
             throw new DateTimeException(unit.getName() + " not valid for CopticDate");
         }
-        return unit.doPlus(this, amountToAdd);
+        return unit.addTo(this, amountToAdd);
     }
 
     //-----------------------------------------------------------------------
     private CopticDate plusYears(long years) {
         return plusMonths(Math.multiplyExact(years, 13));

@@ -302,17 +299,36 @@
     public long periodUntil(Temporal endDateTime, TemporalUnit unit) {
         if (endDateTime instanceof ChronoLocalDate == false) {
             throw new DateTimeException("Unable to calculate period between objects of two different types");
         }
         ChronoLocalDate<?> end = (ChronoLocalDate<?>) endDateTime;
-        if (getChrono().equals(end.getChrono()) == false) {
+        if (getChronology().equals(end.getChronology()) == false) {
             throw new DateTimeException("Unable to calculate period between two different chronologies");
         }
         if (unit instanceof ChronoUnit) {
             return LocalDate.from(this).periodUntil(end, unit);  // TODO: this is wrong
         }
-        return unit.between(this, endDateTime).getAmount();
+        return unit.between(this, endDateTime);
+    }
+
+    @Override
+    public Period periodUntil(ChronoLocalDate<?> endDate) {
+        // TODO: untested
+        CopticDate end = (CopticDate) getChronology().date(endDate);
+        long totalMonths = (end.prolepticYear - this.prolepticYear) * 13 + (end.month - this.month);  // safe
+        int days = end.day - this.day;
+        if (totalMonths > 0 && days < 0) {
+            totalMonths--;
+            CopticDate calcDate = this.plusMonths(totalMonths);
+            days = (int) (end.toEpochDay() - calcDate.toEpochDay());  // safe
+        } else if (totalMonths < 0 && days > 0) {
+            totalMonths++;
+            days -= end.lengthOfMonth();
+        }
+        long years = totalMonths / 13;  // safe
+        int months = (int) (totalMonths % 13);  // safe
+        return Period.of(Math.toIntExact(years), months, days);
     }
 
     //-----------------------------------------------------------------------
     @Override
     public long toEpochDay() {

@@ -326,11 +342,11 @@
         // getLong() reduces chances of exceptions in toString()
         long yoe = getLong(YEAR_OF_ERA);
         long moy = getLong(MONTH_OF_YEAR);
         long dom = getLong(DAY_OF_MONTH);
         StringBuilder buf = new StringBuilder(30);
-        buf.append(getChrono().toString())
+        buf.append(getChronology().toString())
                 .append(" ")
                 .append(getEra())
                 .append(" ")
                 .append(yoe)
                 .append(moy < 10 ? "-0" : "-").append(moy)