From: Mathieu Baudier Date: Tue, 25 Jan 2022 07:19:39 +0000 (+0100) Subject: No-operation UUID factory, providing only default Java random UUIDs. X-Git-Tag: argeo-commons-2.3.5~60 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=475dc8545eb01b541c63be6a480fdd8d808e75a4 No-operation UUID factory, providing only default Java random UUIDs. --- diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentUuidFactory.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentUuidFactory.java index 4ad2290b9..14f6d54ad 100644 --- a/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentUuidFactory.java +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentUuidFactory.java @@ -17,6 +17,7 @@ import java.security.SecureRandom; import java.util.BitSet; import java.util.Enumeration; import java.util.Objects; +import java.util.UUID; /** * A configurable implementation of an {@link AsyncUuidFactory}, which can be @@ -46,6 +47,18 @@ public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory { assert newTimeUUID().node() == BitSet.valueOf(defaultNodeId).toLongArray()[0]; } + /* + * DEFAULT + */ + /** + * The default {@link UUID} to provide. This implementations returns + * {@link #timeUUID()} because it is fast and uses few resources. + */ + @Override + public UUID get() { + return timeUUID(); + } + @Override protected SecureRandom newSecureRandom() { SecureRandom secureRandom; diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/NoOpUuidFactory.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/NoOpUuidFactory.java new file mode 100644 index 000000000..cca147e3f --- /dev/null +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/NoOpUuidFactory.java @@ -0,0 +1,83 @@ +package org.argeo.api.uuid; + +import java.util.UUID; + +/** + * An {@link UuidFactory} which does not implement any algorith and returns + * {@link UnsupportedOperationException} for methods requiring them. Only + * {@link UuidFactory#get()} and {@link UuidFactory#randomUUID()} are + * implemented, trivially based on {@link UUID#randomUUID()}. It can be useful + * as a base class for partial implementations, or whren only random + * {@link UUID}s are needed, but one wants to integrate with this UUID API via + * {@link UuidFactory}. + */ +public class NoOpUuidFactory implements UuidFactory { + public static final UuidFactory onlyJavaRandom = new NoOpUuidFactory(); + + /** Returns {@link #randomUUID()}. */ + @Override + public UUID get() { + return randomUUID(); + } + + /** + * Creates a random UUID (v4) with {@link UUID#randomUUID()}. + * + * @return a random {@link UUID} + */ + @Override + public UUID randomUUID() { + return UUID.randomUUID(); + } + + /** + * Throws an {@link UnsupportedOperationException}. + * + * @throws UnsupportedOperationException always + */ + @Override + public UUID timeUUID() { + throw new UnsupportedOperationException("Time based UUIDs are not implemented"); + } + + /** + * Throws an {@link UnsupportedOperationException}. + * + * @throws UnsupportedOperationException always + */ + @Override + public UUID nameUUIDv5(UUID namespace, byte[] data) { + throw new UnsupportedOperationException("Name based UUIDs are not implemented"); + } + + /** + * Throws an {@link UnsupportedOperationException}. + * + * @throws UnsupportedOperationException always + */ + @Override + public UUID nameUUIDv3(UUID namespace, byte[] data) { + throw new UnsupportedOperationException("Name based UUIDs are not implemented"); + } + + /** + * Throws an {@link UnsupportedOperationException}. + * + * @throws UnsupportedOperationException always + */ + @Override + public UUID randomUUIDStrong() { + throw new UnsupportedOperationException("Strong random UUIDs are not implemented"); + } + + /** + * Throws an {@link UnsupportedOperationException}. + * + * @throws UnsupportedOperationException always + */ + @Override + public UUID randomUUIDWeak() { + throw new UnsupportedOperationException("Weak random UUIDs are not implemented"); + } + +} diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidFactory.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidFactory.java index 91191dae2..dc83f48ec 100644 --- a/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidFactory.java +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/UuidFactory.java @@ -17,6 +17,17 @@ import java.util.function.Supplier; * @see https://datatracker.ietf.org/doc/html/rfc4122 */ public interface UuidFactory extends Supplier { + /* + * DEFAULT + */ + /** + * The default {@link UUID} to provide, either random (v4) or time based (v1). + * It SHOULD wrap either {@link #timeUUID()} (recommended) or + * {@link #randomUUID()}. + */ + @Override + UUID get(); + /* * TIME-BASED (version 1) */ @@ -120,16 +131,6 @@ public interface UuidFactory extends Supplier { return randomUUIDStrong(); } - /** - * The default {@link UUID} to provide, either random (v4) or time based (v1). - * This default implementations returns {@link #timeUUID()} because it is - * supposed to be fast and use few resources. - */ - @Override - default UUID get() { - return timeUUID(); - } - /* * STANDARD UUIDs */