Skip to content

Commit

Permalink
Merge pull request #98 from mprevel/hashcode
Browse files Browse the repository at this point in the history
Add hashCode to TypeID
  • Loading branch information
ant8e authored Oct 18, 2024
2 parents e244116 + de7dcc7 commit 070ad3c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/shared/src/main/scala/tech/ant8e/uuid4cats/TypeID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ object TypeID {
case _ => false
}

private lazy val hashCodeComputation: Int = {
val prime = 31
prime * (prime + prefix_.hashCode) + uuid_.hashCode
}

override def hashCode(): Int = hashCodeComputation

}

private[uuid4cats] def validatePrefix(
Expand Down
17 changes: 17 additions & 0 deletions core/shared/src/test/scala/tech/ant8e/uuid4cats/TypeIDSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ class TypeIDSuite extends FunSuite {
)
}

test("TypeID should have a hashCode") {
val idString = "prefix_01h455vb4pex5vsknk084sn02q"
val id1_1 = TypeID.decode(idString).toOption.get
val id1_2 = TypeID.decode(idString).toOption.get
val id2 = TypeID.decode("prefix_01h455vb4pex5vsknk084sn02r").toOption.get
assertEquals(
id1_1.hashCode,
id1_2.hashCode,
"hashCode should be consistent for same data in different instances"
)
assertNotEquals(
id1_1.hashCode,
id2.hashCode,
"hashCode should be different for different data"
)
}

private def assertValidEncoding(
typeID: String,
prefix: String,
Expand Down

0 comments on commit 070ad3c

Please sign in to comment.