jeffrey
jeffrey

Reputation: 3354

How can I create an empty java.util.UUID object?

I don't know why I can't find the answer to this, but I need to pass a blank UUID object in one of my functions to represent a lack of UUID. What is the UUID analagous form of

val x: "" 

, which would be an empty string. I'm essentially trying to get an empty UUID. I tried

UUID.fromString("")

but received an error, as you need a valid UUID string.

EDIT: I am implementing this in Scala.

Upvotes: 24

Views: 33696

Answers (2)

Michael Zajac
Michael Zajac

Reputation: 55569

Let me preface this by saying it would be much better to use Option[UUID] instead, with None representing an empty UUID.

You can't use an empty String, as it does not conform to the UUID format, described here.

You could use

UUID.fromString("00000000-0000-0000-0000-000000000000")

Which would be the same as

new UUID(0L, 0L)

But the usage of that would be arbitrary, and it would be much better to signify the absence or lack of a UUID with Option.

Upvotes: 42

Denis Makarenko
Denis Makarenko

Reputation: 2938

Did you consider using Option[UUID] as a parameter type? In this case you can pass None to indicate lack of UUID. An empty string is not a valid guid that's why it is rejected by UUID.fromString

Upvotes: 9

Related Questions