X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FUuidHolder.java;fp=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FUuidHolder.java;h=253f0ad923842e78d1196b2268e7a2fe0963eb85;hb=e8e2c65f356b30b35e4d8a1de66691a789c183bb;hp=0000000000000000000000000000000000000000;hpb=5b33b0f7a92debf285b31d308add6470a31894a9;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidHolder.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidHolder.java new file mode 100644 index 000000000..253f0ad92 --- /dev/null +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidHolder.java @@ -0,0 +1,71 @@ +package org.argeo.api.uuid; + +import java.io.Serializable; +import java.util.Objects; +import java.util.UUID; +import java.util.function.Supplier; + +/** + * An immutable wrapper for an {@link UUID}, which can be used as a base for a + * derivation hierarchy, while strongly enforcing semantic equality with the + * underlying {@link UUID}. It is therefore immutable, and all base methods are + * directly and trivially based on {@link UUID} methods; they do represent the + * same unique "thing" (be it an entity, a point in time, etc.), consistently + * with the fundamental concept of uuid. + */ +public class UuidHolder implements Supplier, Serializable { + private static final long serialVersionUID = APM.SERIAL; + + /** + * The wrapped {@link UUID}. Protected rather than private, since it is + * immutable and a {@link UUID} is itself immutable. + */ + protected final UUID uuid; + + /** + * Constructs a new {@link UuidHolder} based on this uuid. + * + * @param uuid the UUID to wrap, cannot be null. + * @throws NullPointerException if the provided uuid is null. + */ + protected UuidHolder(UUID uuid) { + Objects.requireNonNull(uuid, "UUID cannot be null"); + this.uuid = uuid; + } + + /** The wrapped {@link UUID}. */ + public final UUID getUuid() { + return uuid; + } + + /** The wrapped {@link UUID}. */ + @Override + public final UUID get() { + return getUuid(); + } + + /** Calls {@link UUID#hashCode()} on the wrapped {@link UUID}. */ + @Override + public final int hashCode() { + return uuid.hashCode(); + } + + /** + * Equals only with non-null {@link UuidHolder} if and only if their wrapped + * uuid are equals by calling {@link UUID#equals(Object)}. + */ + @Override + public final boolean equals(Object obj) { + if (obj == null || !(obj instanceof UuidHolder)) + return false; + UuidHolder typedUuid = (UuidHolder) obj; + return uuid.equals(typedUuid.uuid); + } + + /** Calls {@link UUID#toString()} on the wrapped {@link UUID}. */ + @Override + public final String toString() { + return uuid.toString(); + } + +}