]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.uuid/src/org/argeo/api/uuid/UuidHolder.java
Kerberos does not try to use shared state
[lgpl/argeo-commons.git] / org.argeo.api.uuid / src / org / argeo / api / uuid / UuidHolder.java
1 package org.argeo.api.uuid;
2
3 import java.io.Serializable;
4 import java.util.Objects;
5 import java.util.UUID;
6 import java.util.function.Supplier;
7
8 /**
9 * An immutable wrapper for an {@link UUID}, which can be used as a base for a
10 * derivation hierarchy, while strongly enforcing semantic equality with the
11 * underlying {@link UUID}. It is therefore immutable, and all base methods are
12 * directly and trivially based on {@link UUID} methods; they do represent the
13 * same unique "thing" (be it an entity, a point in time, etc.), consistently
14 * with the fundamental concept of uuid.
15 */
16 public class UuidHolder implements Supplier<UUID>, Serializable {
17 private static final long serialVersionUID = APM.SERIAL;
18
19 /**
20 * The wrapped {@link UUID}. Protected rather than private, since it is
21 * immutable and a {@link UUID} is itself immutable.
22 */
23 protected final UUID uuid;
24
25 /**
26 * Constructs a new {@link UuidHolder} based on this uuid.
27 *
28 * @param uuid the UUID to wrap, cannot be null.
29 * @throws NullPointerException if the provided uuid is null.
30 */
31 protected UuidHolder(UUID uuid) {
32 Objects.requireNonNull(uuid, "UUID cannot be null");
33 this.uuid = uuid;
34 }
35
36 /** The wrapped {@link UUID}. */
37 public final UUID getUuid() {
38 return uuid;
39 }
40
41 /** The wrapped {@link UUID}. */
42 @Override
43 public final UUID get() {
44 return getUuid();
45 }
46
47 /** Calls {@link UUID#hashCode()} on the wrapped {@link UUID}. */
48 @Override
49 public final int hashCode() {
50 return uuid.hashCode();
51 }
52
53 /**
54 * Equals only with non-null {@link UuidHolder} if and only if their wrapped
55 * uuid are equals by calling {@link UUID#equals(Object)}.
56 */
57 @Override
58 public final boolean equals(Object obj) {
59 if (obj == null || !(obj instanceof UuidHolder))
60 return false;
61 UuidHolder typedUuid = (UuidHolder) obj;
62 return uuid.equals(typedUuid.uuid);
63 }
64
65 /** Calls {@link UUID#toString()} on the wrapped {@link UUID}. */
66 @Override
67 public final String toString() {
68 return uuid.toString();
69 }
70
71 }