changeset 8996:39a02b18b386

8029909: Clarify equals/hashcode behavior for java.time types Summary: Document the behavior of equals and hashcode in java.time.chrono date types Reviewed-by: sherman, scolebourne
author rriggs
date Fri, 20 Dec 2013 13:06:32 -0500
parents 7186275e6ef1
children f3c714eeef6c
files src/share/classes/java/time/chrono/HijrahDate.java src/share/classes/java/time/chrono/JapaneseDate.java src/share/classes/java/time/chrono/MinguoDate.java src/share/classes/java/time/chrono/ThaiBuddhistDate.java
diffstat 4 files changed, 93 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/time/chrono/HijrahDate.java	Fri Dec 20 13:06:23 2013 -0500
+++ b/src/share/classes/java/time/chrono/HijrahDate.java	Fri Dec 20 13:06:32 2013 -0500
@@ -608,6 +608,48 @@
         return getChronology().period(Math.toIntExact(years), months, days);
     }
 
+    //-------------------------------------------------------------------------
+    /**
+     * Compares this date to another date, including the chronology.
+     * <p>
+     * Compares this {@code HijrahDate} with another ensuring that the date is the same.
+     * <p>
+     * Only objects of type {@code HijrahDate} are compared, other types return false.
+     * To compare the dates of two {@code TemporalAccessor} instances, including dates
+     * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.
+     *
+     * @param obj  the object to check, null returns false
+     * @return true if this is equal to the other date and the Chronologies are equal
+     */
+    @Override  // override for performance
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof HijrahDate) {
+            HijrahDate otherDate = (HijrahDate) obj;
+            return prolepticYear == otherDate.prolepticYear
+                && this.monthOfYear == otherDate.monthOfYear
+                && this.dayOfMonth == otherDate.dayOfMonth
+                && getChronology().equals(otherDate.getChronology());
+        }
+        return false;
+    }
+
+    /**
+     * A hash code for this date.
+     *
+     * @return a suitable hash code based only on the Chronology and the date
+     */
+    @Override  // override for performance
+    public int hashCode() {
+        int yearValue = prolepticYear;
+        int monthValue = monthOfYear;
+        int dayValue = dayOfMonth;
+        return getChronology().getId().hashCode() ^ (yearValue & 0xFFFFF800)
+                ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
+    }
+
     //-----------------------------------------------------------------------
     /**
      * Defend against malicious streams.
--- a/src/share/classes/java/time/chrono/JapaneseDate.java	Fri Dec 20 13:06:23 2013 -0500
+++ b/src/share/classes/java/time/chrono/JapaneseDate.java	Fri Dec 20 13:06:32 2013 -0500
@@ -678,6 +678,18 @@
     }
 
     //-------------------------------------------------------------------------
+    /**
+     * Compares this date to another date, including the chronology.
+     * <p>
+     * Compares this {@code JapaneseDate} with another ensuring that the date is the same.
+     * <p>
+     * Only objects of type {@code JapaneseDate} are compared, other types return false.
+     * To compare the dates of two {@code TemporalAccessor} instances, including dates
+     * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.
+     *
+     * @param obj  the object to check, null returns false
+     * @return true if this is equal to the other date
+     */
     @Override  // override for performance
     public boolean equals(Object obj) {
         if (this == obj) {
@@ -690,6 +702,11 @@
         return false;
     }
 
+    /**
+     * A hash code for this date.
+     *
+     * @return a suitable hash code based only on the Chronology and the date
+     */
     @Override  // override for performance
     public int hashCode() {
         return getChronology().getId().hashCode() ^ isoDate.hashCode();
--- a/src/share/classes/java/time/chrono/MinguoDate.java	Fri Dec 20 13:06:23 2013 -0500
+++ b/src/share/classes/java/time/chrono/MinguoDate.java	Fri Dec 20 13:06:32 2013 -0500
@@ -440,6 +440,18 @@
     }
 
     //-------------------------------------------------------------------------
+    /**
+     * Compares this date to another date, including the chronology.
+     * <p>
+     * Compares this {@code MinguoDate} with another ensuring that the date is the same.
+     * <p>
+     * Only objects of type {@code MinguoDate} are compared, other types return false.
+     * To compare the dates of two {@code TemporalAccessor} instances, including dates
+     * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.
+     *
+     * @param obj  the object to check, null returns false
+     * @return true if this is equal to the other date
+     */
     @Override  // override for performance
     public boolean equals(Object obj) {
         if (this == obj) {
@@ -452,6 +464,11 @@
         return false;
     }
 
+    /**
+     * A hash code for this date.
+     *
+     * @return a suitable hash code based only on the Chronology and the date
+     */
     @Override  // override for performance
     public int hashCode() {
         return getChronology().getId().hashCode() ^ isoDate.hashCode();
--- a/src/share/classes/java/time/chrono/ThaiBuddhistDate.java	Fri Dec 20 13:06:23 2013 -0500
+++ b/src/share/classes/java/time/chrono/ThaiBuddhistDate.java	Fri Dec 20 13:06:32 2013 -0500
@@ -440,6 +440,18 @@
     }
 
     //-------------------------------------------------------------------------
+    /**
+     * Compares this date to another date, including the chronology.
+     * <p>
+     * Compares this {@code ThaiBuddhistDate} with another ensuring that the date is the same.
+     * <p>
+     * Only objects of type {@code ThaiBuddhistDate} are compared, other types return false.
+     * To compare the dates of two {@code TemporalAccessor} instances, including dates
+     * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.
+     *
+     * @param obj  the object to check, null returns false
+     * @return true if this is equal to the other date
+     */
     @Override  // override for performance
     public boolean equals(Object obj) {
         if (this == obj) {
@@ -452,6 +464,11 @@
         return false;
     }
 
+    /**
+     * A hash code for this date.
+     *
+     * @return a suitable hash code based only on the Chronology and the date
+     */
     @Override  // override for performance
     public int hashCode() {
         return getChronology().getId().hashCode() ^ isoDate.hashCode();