Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Override Timestamp hashCode for consistent hashing #1218

Merged
merged 3 commits into from
Feb 18, 2015

Conversation

sttts
Copy link
Contributor

@sttts sttts commented Feb 18, 2015

Timestamp's hashCode was based on the parameter dateTime which might have
a timezone != UTC. The consequence:

val t = Timestamp.now
t.hashCode != Timestamp(t.toString).hashCode

This patch overrides hashCode to use the UTC normalized time variable of the
Timestamp case class.

This probably fixes #1082

Timestamp's hashCode was based on the parameter dataTime which might have
a timezone != UTC. The consequence:

  val t = Timestamp.now
  t.hashCode != Timestamp(t.toString).hashCode

This patch overrides hashCode to use the UTC normalized time variable of the
Timestamp case class.

This probably fixes d2iq-archive#1082
@@ -15,6 +15,8 @@ case class Timestamp(dateTime: DateTime) extends Ordered[Timestamp] {
case _ => false
}

override def hashCode: Int = time.hashCode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment to make it explicit that we depend upon time being a UTC DateTime to uphold the equals/hashCode contract?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

@sttts
Copy link
Contributor Author

sttts commented Feb 18, 2015

Was wondering whether it would be cleaner to move the UTC conversion into the apply constructors and turn the current UTC time into the case class parameter.

case class Timestamp(utcDateTime: DateTime) extends Ordered[Timestamp]

object Timestamp {
   def apply(time: String): Timestamp = Timestamp(
      DateTime.parse(time).toDateTime(DateTimeZone.UTC)
   )
}

Then we can probably get rid of most of the overrides in the case class.

@ConnorDoyle
Copy link
Contributor

@sttts, we could map the time zone in the companion's apply method. We'd also have to make the constructor private to disallow invalid instances from being built. IMO, it is quite clear as written. Let's leave a refactor out of this bugfix for a subsequent PR.

@ConnorDoyle
Copy link
Contributor

Thanks! :shipit:

ConnorDoyle added a commit that referenced this pull request Feb 18, 2015
Override Timestamp hashCode for consistent hashing
@ConnorDoyle ConnorDoyle merged commit 65cfe5b into d2iq-archive:master Feb 18, 2015
@bobrik
Copy link
Contributor

bobrik commented Feb 19, 2015

🎊 🎉

@marcomonaco marcomonaco added the pr label Mar 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

firstSuccess in health checks is incorrect
4 participants