This project aims to provide Tools and Helpers to make working with UUIDs more fun and generally be nice to the user.
The library provides the string interpolators uuid
and the shorthand u
.
Using the interpolator makes sure that all strings provided within the interpolator are valid UUIDs. This check happens at compile time.
import biz.neumann.NiceUUID._
// invalid UUID
scala> uuid"hello-i-am-no-uuid"
<console>:15: error: You provided a malformed UUID: hello-i-am-no-uuid
uuid"hello-i-am-no-uuid"
^
// valid uuid
scala> uuid"9ecce884-47fe-4ba4-a1bb-1a3d71ed6530"
res2: String = 9ecce884-47fe-4ba4-a1bb-1a3d71ed6530
Adds method .uuid
to String
. It is a nice way of calling Try( UUID.fromString )
.
import biz.neumann.NiceUUID._
//valid UUID
scala> "9ecce884-47fe-4ba4-a1bb-1a3d71ed6530".uuid
res1: scala.util.Try[java.util.UUID] = Success(9ecce884-47fe-4ba4-a1bb-1a3d71ed6530)
// Invalid UUID
scala> "abc".uuid
res2: scala.util.Try[java.util.UUID] = Failure(java.lang.IllegalArgumentException: Invalid UUID string: abc)
This implicit conversion allows to use an UUID
in place of a String
import biz.neumann.NiceUUID._
scala> def countChars(s: String): Int = s.size
countChars: (s: String)Int
scala> countChars( java.util.UUID.randomUUID )
res3: Int = 36
A method, that returns a String encoding an UUID. It is checked at compile time and doesn't compile if given String is not a valid UUID.
//valid
scala> stringEncodingUUID("9ecce884-47fe-4ba4-a1bb-1a3d71ed6530")
res0: String = 9ecce884-47fe-4ba4-a1bb-1a3d71ed6530
invalid
scala> stringEncodingUUID("hello world!")
<console>:14: error: You provided a malformed UUID: hello world!
stringEncodingUUID("hello world!")
add this to your build.sbt
libraryDependencies += "biz.neumann" %% "nice-uuid" % "1.z"
<dependency>
<groupId>biz.neumann</groupId>
<artifactId>nice-uuid_2.13</artifactId>
<version>1.7</version>
</dependency>