Skip to content

Commit

Permalink
Fix hashCode and toString.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturaz committed Dec 6, 2024
1 parent fad532b commit 9f1ca2b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ULID scala
[![Build Status](https://github.com/jkugiya/ulid-scala/workflows/CI/badge.svg)
![Build Status](https://github.com/jkugiya/ulid-scala/workflows/CI/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/jkugiya/ulid-scala/badge.svg?branch=master)](https://coveralls.io/github/jkugiya/ulid-scala?branch=master)

ULID (Universally Unique Lexicographically Sortable Identifier) generator and parser for Scala.
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ThisBuild / scalaVersion := "3.3.3"
// Make sure to update .github/workflows/ci.yml when updating this list
ThisBuild / crossScalaVersions := Seq("3.3.3", "2.13.14", "2.12.19")
ThisBuild / version := "1.0.4-SNAPSHOT"
ThisBuild / version := "1.0.5-SNAPSHOT"
ThisBuild / organization := "com.github.jkugiya"
ThisBuild / organizationName := "jkugiya"

Expand Down
38 changes: 37 additions & 1 deletion shared/src/main/scala/jkugiya/ulid/ULID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,48 @@ class ULID private[ulid](val time: Long, private[ulid] val originalRandomness: A
}
}

override def toString(): String = {
val sb = new StringBuilder
sb.append("ULID(b32=")
sb.append(base32)
sb.append(", uuid=")
sb.append(uuid)
sb.append(")")
sb.toString
}

override def equals(obj: Any): Boolean = obj match {
case other: ULID =>
(time == other.time) && (originalRandomness sameElements other.originalRandomness)
if (this eq other) return true
if (time != other.time) return false

{
var idx = 0
while (idx < originalRandomness.length) {
if (originalRandomness(idx) != other.originalRandomness(idx)) return false
idx += 1
}
}

true
case _ =>
false
}

override def hashCode(): Int = {
val prime = 31
var result = 1
result = prime * result + time.hashCode

{
var idx = 0
while (idx < originalRandomness.length) {
result = prime * result + originalRandomness(idx)
idx += 1
}
}

result
}
}

0 comments on commit 9f1ca2b

Please sign in to comment.