From 0809a8fe34f2de820f583506d32719823d90e4ff Mon Sep 17 00:00:00 2001 From: Pat Garrity Date: Wed, 20 Dec 2023 22:07:25 -0600 Subject: [PATCH] Initializing the gs-uuid project with a tested implementation. --- .gitignore | 4 + .pre-commit-config.yaml | 11 ++ .scalafmt.conf | 71 +++++++ LICENSE | 202 ++++++++++++++++++++ README.md | 20 ++ build.sbt | 140 ++++++++++++++ project/build.properties | 1 + project/plugins.sbt | 1 + src/main/java/gs/uuid/v0/UUIDFormat.java | 216 ++++++++++++++++++++++ src/main/scala/gs/uuid/v0/UUID.scala | 114 ++++++++++++ src/test/scala/gs/uuid/v0/UUIDTests.scala | 57 ++++++ 11 files changed, 837 insertions(+) create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 .scalafmt.conf create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.sbt create mode 100644 project/build.properties create mode 100644 project/plugins.sbt create mode 100644 src/main/java/gs/uuid/v0/UUIDFormat.java create mode 100644 src/main/scala/gs/uuid/v0/UUID.scala create mode 100644 src/test/scala/gs/uuid/v0/UUIDTests.scala diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89fee26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +target/ +project/target/ +project/project/ +modules/core/target/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..d13a4e9 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,11 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: end-of-file-fixer + - id: trailing-whitespace + - repo: https://git.meager.home/meager/meager-pre-commit-scala + rev: v0.1.2 + hooks: + - id: scalafmt diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..17e4b30 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,71 @@ +// See: https://github.com/scalameta/scalafmt/tags for the latest tags. +version = 3.7.11 +runner.dialect = scala3 +maxColumn = 80 + +rewrite { + rules = [RedundantBraces, RedundantParens, Imports, SortModifiers] + imports.expand = true + imports.sort = scalastyle + redundantBraces.ifElseExpressions = true + redundantBraces.stringInterpolation = true +} + +indent { + main = 2 + callSite = 2 + defnSite = 2 + extendSite = 4 + withSiteRelativeToExtends = 2 + commaSiteRelativeToExtends = 2 +} + +align { + preset = more + openParenCallSite = false + openParenDefnSite = false +} + +newlines { + implicitParamListModifierForce = [before,after] + topLevelStatementBlankLines = [ + { + blanks = 1 + } + ] + afterCurlyLambdaParams = squash +} + +danglingParentheses { + defnSite = true + callSite = true + ctrlSite = true + exclude = [] +} + +verticalMultiline { + atDefnSite = true + arityThreshold = 2 + newlineAfterOpenParen = true +} + +comments { + wrap = standalone +} + +docstrings { + style = "SpaceAsterisk" + oneline = unfold + wrap = yes + forceBlankLineBefore = true +} + +project { + excludePaths = [ + "glob:**target/**", + "glob:**.metals/**", + "glob:**.bloop/**", + "glob:**.bsp/**", + "glob:**metals.sbt" + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8bb0713 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# gs-uuid + +[License (Apache 2.0)](./LICENSE) + +UUID's for Scala 3 with generation based on JUG, and serialization based on code +from Jackson Databind. The only dependency is JUG, whereas the relevant Jackson +code is copied to this implementation (and slightly modified). + +- [Usage](#usage) + +## Usage + +This library is not yet published. + +```scala +object GS { + val UUID: ModuleID = + "gs" %% "gs-uuid-v0" % "0.1.0" +} +``` diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..1f4494d --- /dev/null +++ b/build.sbt @@ -0,0 +1,140 @@ +val scala3: String = "3.3.1" + +ThisBuild / organizationName := "garrity software" +ThisBuild / organization := "gs" +ThisBuild / organizationHomepage := Some(url("https://garrity.co/")) +ThisBuild / scalaVersion := scala3 +ThisBuild / versionScheme := Some("early-semver") + +externalResolvers := Seq( + "Garrity Software Releases" at "https://maven.garrity.co/releases" +) + +val ProjectName: String = "gs-uuid" +val Description: String = "Garrity Software UUID Implementation" + +/** + * Helper to extract the value from `-Dproperty=value`. + * + * @param name The property name. + * @param conv The conversion function to the output type. + * @return The converted value, or `None` if no value exists. + */ +def getProperty[A]( + name: String, + conv: String => A +): Option[A] = + Option(System.getProperty(name)).map(conv) + +/** + * Use `sbt -Dversion=` to provide the version, minus the SNAPSHOT + * modifier. This is the typical approach for producing releases. + */ +val VersionProperty: String = "version" + +/** + * Use `sbt -Drelease=true` to trigger a release build. + */ +val ReleaseProperty: String = "release" + +/** + * The value of `-Dversion=`. + * + * @return The version passed as input to SBT. + */ +lazy val InputVersion: Option[String] = + getProperty(VersionProperty, identity) + +/** + * @return "-SNAPSHOT" if this is NOT a release, empty string otherwise. + */ +lazy val Modifier: String = + if (getProperty(ReleaseProperty, _.toBoolean).getOrElse(false)) "" + else "-SNAPSHOT" + +/** + * Version used if no version is passed as input. This helps with default/local + * builds. + */ +val DefaultVersion: String = "0.1.0-SNAPSHOT" + +/** + * This is the output version of the published artifact. If this build is not + * a release, the suffix "-SNAPSHOT" will be appended. + * + * @return The project version. + */ +lazy val SelectedVersion: String = + InputVersion + .map(v => s"$v$Modifier") + .getOrElse(DefaultVersion) + +/** + * The major version (first segment) value. Used to label releases. + * + * @return The major version of the project. + */ +lazy val MajorVersion: String = + SelectedVersion.split('.').apply(0) + +val sharedSettings = Seq( + scalaVersion := scala3, + version := SelectedVersion +) + +lazy val publishSettings = Seq( + publishMavenStyle := true, + Test / publishArtifact := false, + pomIncludeRepository := Function.const(false), + scmInfo := Some( + ScmInfo( + url(s"https://git.garrity.co/garrity-software/$ProjectName"), + s"git@git.garrity.co:garrity-software/$ProjectName.git" + ) + ), + description := Description, + licenses := List( + "Apache 2.0" -> url("https://www.apache.org/licenses/LICENSE-2.0.html") + ), + homepage := Some(url(s"https://git.garrity.co/garrity-software/$ProjectName")), + publishTo := { + val repo = "https://maven.garrity.co/" + if (SelectedVersion.endsWith("SNAPSHOT")) Some("snapshots" at repo + "snapshots") + else Some("releases" at repo + "releases") + } +) + +lazy val testSettings = Seq( + libraryDependencies ++= Seq( + "org.scalameta" %% "munit" % "1.0.0-M10" % Test + ) +) + +lazy val `gs-uuid` = (project.in(file("."))) + .settings(sharedSettings) + .settings(publishSettings) + .settings(testSettings) + .settings(name := s"$ProjectName-v$MajorVersion") + .settings( + libraryDependencies ++= Seq( + "com.fasterxml.uuid" % "java-uuid-generator" % "4.1.1" + ) + ) + +ThisBuild / scalacOptions ++= Seq( + "-encoding", + "utf8", // Set source file character encoding. + "-deprecation", // Emit warning and location for usages of deprecated APIs. + "-feature", // Emit warning and location for usages of features that should be imported explicitly. + "-explain", // Explain errors in more detail. + "-unchecked", // Enable additional warnings where generated code depends on assumptions. + "-explain-types", // Explain type errors in more detail. + "-Xfatal-warnings", // Fail the compilation if there are any warnings. + "-language:strictEquality", // Enable multiversal equality (require CanEqual) + "-Wunused:implicits", // Warn if an implicit parameter is unused. + "-Wunused:explicits", // Warn if an explicit parameter is unused. + "-Wunused:imports", // Warn if an import selector is not referenced. + "-Wunused:locals", // Warn if a local definition is unused. + "-Wunused:privates", // Warn if a private member is unused. + "-Ysafe-init" // Enable the experimental safe initialization check. +) diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..abbbce5 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.9.8 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..604b256 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.8") diff --git a/src/main/java/gs/uuid/v0/UUIDFormat.java b/src/main/java/gs/uuid/v0/UUIDFormat.java new file mode 100644 index 0000000..a4d5a33 --- /dev/null +++ b/src/main/java/gs/uuid/v0/UUIDFormat.java @@ -0,0 +1,216 @@ +package gs.uuid.v0; + +import java.util.UUID; +import java.util.Arrays; + +/** + * UUID serialization and deserialization. This is a direct copy of Jackson + * Databind (also under the Apache 2.0 license at time of writing) with + * extremely minor modifications to remove dashes from the output and to + * likewise support parsing with/without dashes. + */ +public final class UUIDFormat { + private UUIDFormat() {} + + private final static char[] HEX_CHARS = "0123456789abcdef".toCharArray(); + + + private final static int[] HEX_DIGITS = new int[127]; + + static { + Arrays.fill(HEX_DIGITS, -1); + for (int i = 0; i < 10; ++i) { HEX_DIGITS['0' + i] = i; } + for (int i = 0; i < 6; ++i) { + HEX_DIGITS['a' + i] = 10 + i; + HEX_DIGITS['A' + i] = 10 + i; + } + } + + /** + *

Render the given UUID as a 32-character string using lowercase + * hexadecimal without dashes.

+ * + * @param uuid The UUID to render. + * @return Hexadecimal representation of the UUID. + */ + public static String toHex(final UUID uuid) { + final char[] ch = new char[32]; + + // Example: + // 9bbe7b63-7928-49c8-a14f-67098b6e4642 + final long msb = uuid.getMostSignificantBits(); + + // Handle the first 8 characters (9bbe7b63) + _appendInt((int) (msb >> 32), ch, 0); + + int i = (int) msb; + // Handle the next 4 characters (7928) (Section 2) + _appendShort(i >>> 16, ch, 8); + + // Handle the next 4 characters (49c8) (Section 3) + _appendShort(i, ch, 12); + + final long lsb = uuid.getLeastSignificantBits(); + + // Handle the next 4 characters (a14ff) (Section 4) + _appendShort((int) (lsb >>> 48), ch, 16); + + // Handle the next 4 characters (6709) (Section 5) + _appendShort((int) (lsb >>> 32), ch, 20); + + // Handle the final 8 characters (8b6e4642) (Section 5) + _appendInt((int) lsb, ch, 24); + + return new String(ch, 0, 32); + } + + /** + *

Render the given UUID as a 16-byte array.

+ * + * @param uuid The UUID to render. + * @return 16-byte array. + */ + public static byte[] toBytes(final UUID uuid) { + return _asBytes(uuid); + } + + /** + *

Parse the given hexadecimal string as a UUID. This method supports + * both 32-character (no dash) and 36-character (dashes) representations, + * and will automatically choose based on input length.

+ * + * @param id The string representation to parse. + * @return The parsed UUID. + */ + public static UUID fromHex(final String id) { + final int len = id.length(); + + if (len == 32) { + // Deserialize without dashes. + + // Get the first 8 characters from index 0 + long l1 = intFromChars(id, 0); + l1 <<= 32; + + // Get the second 4 characters from index 8 + long l2 = ((long) shortFromChars(id, 8)) << 16; + + // Get the third 4 characters from index 12 + l2 |= shortFromChars(id, 12); + long hi = l1 + l2; + + // Get the next two sets of 4 characters from indexes 16 and 20 + // respectively. + int i1 = (shortFromChars(id, 16) << 16) | shortFromChars(id, 20); + l1 = i1; + l1 <<= 32; + + // Get the final 8 characters from index 24 + l2 = intFromChars(id, 24); + l2 = (l2 << 32) >>> 32; + long lo = l1 | l2; + + return new UUID(hi, lo); + } else if (len == 36) { + // Deserialize with dashes. + if ((id.charAt(8) != '-') || (id.charAt(13) != '-') + || (id.charAt(18) != '-') || (id.charAt(23) != '-')) { + throw new IllegalArgumentException("Malformed UUID: 36-character representation does not contain correct dashes."); + } + long l1 = intFromChars(id, 0); + l1 <<= 32; + long l2 = ((long) shortFromChars(id, 9)) << 16; + l2 |= shortFromChars(id, 14); + long hi = l1 + l2; + + int i1 = (shortFromChars(id, 19) << 16) | shortFromChars(id, 24); + l1 = i1; + l1 <<= 32; + l2 = intFromChars(id, 28); + l2 = (l2 << 32) >>> 32; // sign removal, Java-style. Ugh. [Note: Retained this comment from Jackson :) ] + long lo = l1 | l2; + + return new UUID(hi, lo); + } else { + throw new IllegalArgumentException("UUID hexadecimal strings must be either 32 characters or 36 characters long."); + } + } + + public static UUID fromBytes(final byte[] bytes) { + return _fromBytes(bytes); + } + + private static void _appendShort(int bits, char[] ch, int offset) { + ch[offset] = HEX_CHARS[(bits >> 12) & 0xF]; + ch[++offset] = HEX_CHARS[(bits >> 8) & 0xF]; + ch[++offset] = HEX_CHARS[(bits >> 4) & 0xF]; + ch[++offset] = HEX_CHARS[bits & 0xF]; + } + + private static void _appendInt(int bits, char[] ch, int offset) { + _appendShort(bits >> 16, ch, offset); + _appendShort(bits, ch, offset+4); + } + + private final static void _appendInt(int value, byte[] buffer, int offset) { + buffer[offset] = (byte) (value >> 24); + buffer[++offset] = (byte) (value >> 16); + buffer[++offset] = (byte) (value >> 8); + buffer[++offset] = (byte) value; + } + + private final static byte[] _asBytes(UUID uuid) { + byte[] buffer = new byte[16]; + long hi = uuid.getMostSignificantBits(); + long lo = uuid.getLeastSignificantBits(); + _appendInt((int) (hi >> 32), buffer, 0); + _appendInt((int) hi, buffer, 4); + _appendInt((int) (lo >> 32), buffer, 8); + _appendInt((int) lo, buffer, 12); + return buffer; + } + + private static int intFromChars(String str, int index) { + return (byteFromChars(str, index) << 24) + + (byteFromChars(str, index+2) << 16) + + (byteFromChars(str, index+4) << 8) + + byteFromChars(str, index+6); + } + + private static int shortFromChars(String str, int index) { + return (byteFromChars(str, index) << 8) + byteFromChars(str, index+2); + } + + private static int byteFromChars(String str, int index) { + final char c1 = str.charAt(index); + final char c2 = str.charAt(index+1); + + if (c1 <= 127 && c2 <= 127) { + int hex = (HEX_DIGITS[c1] << 4) | HEX_DIGITS[c2]; + if (hex >= 0) { + return hex; + } + } + + throw new IllegalArgumentException("Invalid hexadecimal character detected in byte at index " + index); + } + + private static UUID _fromBytes(byte[] bytes) { + if (bytes.length != 16) { + throw new IllegalArgumentException("Can only construct UUIDs from byte[16]; got " + bytes.length + " bytes"); + } + return new UUID(_long(bytes, 0), _long(bytes, 8)); + } + + private static long _long(byte[] b, int offset) { + long l1 = ((long) _int(b, offset)) << 32; + long l2 = _int(b, offset+4); + // faster to just do it than check if it has sign + l2 = (l2 << 32) >>> 32; // to get rid of sign + return l1 | l2; + } + + private static int _int(byte[] b, int offset) { + return (b[offset] << 24) | ((b[offset+1] & 0xFF) << 16) | ((b[offset+2] & 0xFF) << 8) | (b[offset+3] & 0xFF); + } +} diff --git a/src/main/scala/gs/uuid/v0/UUID.scala b/src/main/scala/gs/uuid/v0/UUID.scala new file mode 100644 index 0000000..7dca251 --- /dev/null +++ b/src/main/scala/gs/uuid/v0/UUID.scala @@ -0,0 +1,114 @@ +package gs.uuid.v0 + +import com.fasterxml.uuid.Generators + +/** Alias for the `java.util.UUID` type, which represents a 128-bit value. + * + * ## ID Generation + * + * This library provides generator implementations for the following types of + * UUID: + * + * - Type 4 + * - Type 7 + * + * These implementations are provided by JUG. + * + * ## Serialization + * + * This library uses a custom variant of the JDK 17 implementation that removes + * dashes from the output and is likewise capable of parsing those values. + * + * {{{ + * val example: UUID = UUID(java.util.UUID.randomUUID()) + * val serialized = example.str() // or example.withoutDashes() + * // example value = 899efa6f40ed45189efa6f40ed9518ed + * }}} + */ +opaque type UUID = java.util.UUID + +object UUID: + /** Express any `java.util.UUID` as a Meager UUID. + * + * @param uuid + * The input UUID. + * @return + * The aliased value. + */ + def apply(uuid: java.util.UUID): UUID = uuid + + given CanEqual[UUID, UUID] = CanEqual.derived + + /** Generate a new UUID. + * + * @param G + * The [[Generator]] type class instance. + * @return + * The new UUID. + */ + def generate( + )( + using + G: Generator + ): UUID = G.next() + + /** Parse the given string as a UUID. + * + * @param str + * The UUID, which is expected to be in a hexadecimal format with no + * dashes. + * @return + * The parsed UUID value, or `None` if the value does not represent a UUID. + */ + def parse(str: String): Option[UUID] = fromString(str) + + def fromString(str: String): Option[UUID] = + scala.util + .Try(UUIDFormat.fromHex(str)) + .map(uuid => Some(apply(uuid))) + .getOrElse(None) + + extension (uid: UUID) + def toUUID: java.util.UUID = uid + + def str(): String = withoutDashes() + + def withoutDashes(): String = UUIDFormat.toHex(uid) + + def lsb(): Long = uid.getLeastSignificantBits() + + def msb(): Long = uid.getMostSignificantBits() + + def isZero(): Boolean = lsb() == 0L && msb() == 0L + + /** Type class for UUID generation. + */ + trait Generator: + /** Generate a new UUID. + */ + def next(): UUID + + object Generator: + /** Instantiate a new Type 4 generator. + */ + def version4: Generator = new Version4 + + /** Instantiate a new Type 7 generator. + */ + def version7: Generator = new Version7 + + /** Type 4 (Random) implementation of a UUID generator. + */ + final class Version4 extends Generator: + private val gen = Generators.randomBasedGenerator() + override def next(): UUID = gen.generate() + + /** Type 7 (Unix Epoch Time + Random) implementation of a UUID generator. + * Consider using this rather than Type 1 or Type 6. + * + * This type is defined in [IETF New UUID Formats + * Draft](https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-04.html#name-uuid-version-7) + */ + final class Version7 extends Generator: + private val gen = Generators.timeBasedEpochGenerator() + override def next(): UUID = gen.generate() diff --git a/src/test/scala/gs/uuid/v0/UUIDTests.scala b/src/test/scala/gs/uuid/v0/UUIDTests.scala new file mode 100644 index 0000000..891fb76 --- /dev/null +++ b/src/test/scala/gs/uuid/v0/UUIDTests.scala @@ -0,0 +1,57 @@ +package gs.uuid.v0 + +class UUIDTests extends munit.FunSuite: + private val v4 = UUID.Generator.version4 + private val v7 = UUID.Generator.version7 + given CanEqual[java.util.UUID, java.util.UUID] = CanEqual.derived + + test( + "should instantiate a type 4 UUID, serialize it, and parse the result" + ) { + val base = v4.next() + val str = base.str() + val parsed = UUID.parse(str) + assert(parsed == Some(base)) + } + + test( + "should instantiate a type 7 UUID, serialize it, and parse the result" + ) { + val base = v7.next() + val str = base.str() + val parsed = UUID.parse(str) + assert(parsed == Some(base)) + } + + test("should instantiate from any java.util.UUID") { + val raw = java.util.UUID.randomUUID() + val base = UUID(raw) + val str = base.str() + val parsed = UUID.fromString(str) + assert(parsed == Some(base)) + assert(parsed.map(_.toUUID) == Some(raw)) + } + + test("should successfully parse a UUID with dashes") { + val base = java.util.UUID.randomUUID() + assert(UUID.parse(base.toString()) == Some(UUID(base))) + } + + test("should fail to parse a non-hex string") { + val input = "ghijklmnoped45189efa6f40ed9518ed" + assert(UUID.parse(input) == None) + } + + test("should generate using an available type class instance") { + given UUID.Generator = v7 + val base = doGen + val str = base.str() + val parsed = UUID.parse(str) + assert(parsed == Some(base)) + } + + private def doGen( + using + UUID.Generator + ): UUID = + UUID.generate()