]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.api.uuid/src/org/argeo/api/uuid/RandomUuid.java
Introduce typed UUIDs.
[lgpl/argeo-commons.git] / org.argeo.api.uuid / src / org / argeo / api / uuid / RandomUuid.java
diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/RandomUuid.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/RandomUuid.java
new file mode 100644 (file)
index 0000000..ea71c48
--- /dev/null
@@ -0,0 +1,29 @@
+package org.argeo.api.uuid;
+
+import java.util.UUID;
+
+/** An opaque variant 2 random {@link UUID} (v4). */
+public final class RandomUuid extends TypedUuid {
+       private static final long serialVersionUID = APM.SERIAL;
+
+       public RandomUuid(UUID uuid) {
+               super(uuid);
+               if (uuid.version() != 4 && uuid.variant() != 2)
+                       throw new IllegalArgumentException("The provided UUID is not a time-based UUID.");
+       }
+
+       /**
+        * Always returns <code>true</code> since random UUIDs are by definition not
+        * opaque.
+        */
+       @Override
+       public final boolean isOpaque() {
+               return true;
+       }
+
+       /** Creates a new {@link RandomUuid} using {@link UUID#randomUUID()}. */
+       public static RandomUuid newRandomUuid() {
+               return new RandomUuid(UUID.randomUUID());
+       }
+
+}