From 9f1ca2b67dd432b52608cc0b75f4100c27bd0fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Art=C5=ABras=20=C5=A0lajus?= Date: Fri, 6 Dec 2024 12:23:41 +0200 Subject: [PATCH] Fix `hashCode` and `toString`. --- README.md | 2 +- build.sbt | 2 +- shared/src/main/scala/jkugiya/ulid/ULID.scala | 38 ++++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 99f7b56..ad8c522 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/build.sbt b/build.sbt index 1996f9b..36c9e88 100644 --- a/build.sbt +++ b/build.sbt @@ -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" diff --git a/shared/src/main/scala/jkugiya/ulid/ULID.scala b/shared/src/main/scala/jkugiya/ulid/ULID.scala index ecc0472..7a77eb8 100644 --- a/shared/src/main/scala/jkugiya/ulid/ULID.scala +++ b/shared/src/main/scala/jkugiya/ulid/ULID.scala @@ -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 + } }