]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.uuid/src/org/argeo/api/uuid/TypedUuid.java
1 package org
.argeo
.api
.uuid
;
3 import java
.util
.Objects
;
7 * Base class for objects which are explicitly typed, based on the various
8 * variant 2 (RFC 4122) UUID versions (and variant 6 with {@link GUID}, for
9 * completion). Such a derivation hierarchy still represents the {@link UUID}
10 * itself, not the objects, data or concept that it identifies. Just like
11 * {@link UUID}s, {@link TypedUuid} should be used as identifier, not as base
12 * class for complex objects. It should rather be seen as a framework to build
13 * richer IDs, which are strictly compliant with the UUID specifications.
15 public abstract class TypedUuid
extends UuidHolder
{
16 private static final long serialVersionUID
= APM
.SERIAL
;
18 /** Default constructor. */
19 public TypedUuid(UUID uuid
) {
24 * Whether this {@link UUID} has no meaning in itself (RFC4122 v3, v4 and v5,
25 * and Microsoft GUID). Only RFC4122 v1 and v2 can be interpreted.
27 public boolean isOpaque() {
28 if (uuid
.variant() == 2) {// RFC4122
29 return uuid
.version() == 4 || uuid
.version() == 5 || uuid
.version() == 3;
30 } else if (uuid
.variant() == 6) {// Microsoft
38 * Constructs a {@link TypedUuid} of the most appropriate subtype, based on this
39 * {@link UUID}. For name based UUIDs, it will return an opaque
40 * {@link BasicNameUuid}; {@link NameUuid} and {@link BinaryNameUuid} may be
43 public static TypedUuid
of(UUID uuid
) {
44 Objects
.requireNonNull(uuid
, "UUID cannot be null");
45 if (uuid
.variant() == 2) {// RFC 4122
46 switch (uuid
.version()) {
48 return new TimeUuid(uuid
);
50 return new RandomUuid(uuid
);
53 return new BasicNameUuid(uuid
);
55 throw new IllegalArgumentException("UUIDs with version " + uuid
.version() + " are not supported.");
57 } else if (uuid
.variant() == 6) {// GUID
58 return new GUID(uuid
);
60 throw new IllegalArgumentException("UUIDs with variant " + uuid
.variant() + " are not supported.");