]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.uuid/src/org/argeo/api/uuid/TypedUuid.java
Kerberos does not try to use shared state
[lgpl/argeo-commons.git] / org.argeo.api.uuid / src / org / argeo / api / uuid / TypedUuid.java
1 package org.argeo.api.uuid;
2
3 import java.util.Objects;
4 import java.util.UUID;
5
6 /**
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.
14 */
15 public abstract class TypedUuid extends UuidHolder {
16 private static final long serialVersionUID = APM.SERIAL;
17
18 /** Default constructor. */
19 public TypedUuid(UUID uuid) {
20 super(uuid);
21 }
22
23 /**
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.
26 */
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
31 return true;
32 } else {
33 return true;
34 }
35 }
36
37 /**
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
41 * more useful.
42 */
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()) {
47 case 1:
48 return new TimeUuid(uuid);
49 case 4:
50 return new RandomUuid(uuid);
51 case 3:
52 case 5:
53 return new BasicNameUuid(uuid);
54 default:
55 throw new IllegalArgumentException("UUIDs with version " + uuid.version() + " are not supported.");
56 }
57 } else if (uuid.variant() == 6) {// GUID
58 return new GUID(uuid);
59 } else {
60 throw new IllegalArgumentException("UUIDs with variant " + uuid.variant() + " are not supported.");
61 }
62 }
63
64 }