-
Notifications
You must be signed in to change notification settings - Fork 842
Override Timestamp hashCode for consistent hashing #1218
Conversation
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
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. |
@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. |
Thanks! |
Override Timestamp hashCode for consistent hashing
🎊 🎉 |
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