Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8366686

Timestamp.hashCode contradicts its own specification using nanos indirectly

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      The specification of java.sql.Timestamp.hashCode explicitly states that nanos not be included in its computation. However, the current implementation returns (int)(this.getTime()^(this.getTime() >>> 32)), and since Timestamp overrides getTime() to include nanos/1_000_000 in the returned millisecond value, the hash code is indirectly affected by the nanos field.
      Original documentation: "The hashCode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation."


      ---------- BEGIN SOURCE ----------
      import java.sql.Timestamp;
      import org.junit.jupiter.api.Test;
      import static org.junit.jupiter.api.Assertions.*;

      public class TimestampTest {
          @Test
          public void testHashCodeConsistency() {
              long t = 1000;
              Timestamp ts1 = new Timestamp(t);
              Timestamp ts2 = new Timestamp(t);
              ts1.setNanos(123456789);
              ts2.setNanos(987654321);
              
              // Verify hashCodes are equal despite different nanos values
              assertEquals(ts1.hashCode(), ts2.hashCode());
          }
      }
      ---------- END SOURCE ----------

            lancea Lance Andersen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: