Asynchronous UUID factory.
[lgpl/argeo-commons.git] / org.argeo.api.acr / src / org / argeo / api / acr / uuid / SimpleUuidFactory.java
index 2d522fa98d42db819ed09ee3ab095b7366d4c51d..cffd446bd29adbf853eacbb1d2e4caa9e2e6402b 100644 (file)
@@ -8,7 +8,6 @@ import java.security.DrbgParameters;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 import java.time.Clock;
-import java.util.Random;
 import java.util.UUID;
 
 /**
@@ -17,19 +16,29 @@ import java.util.UUID;
  * 
  * @see https://datatracker.ietf.org/doc/html/rfc4122
  */
-public class SimpleUuidFactory extends AbstractUuidFactory {
+public class SimpleUuidFactory extends AbstractAsyncUuidFactory {
        private final static Logger logger = System.getLogger(SimpleUuidFactory.class.getName());
        public final static UuidFactory DEFAULT = new SimpleUuidFactory(null, -1, null);
 
-       private SecureRandom secureRandom;
-       private final TimeUuidState timeUuidState;
-
 //     private NodeId macAddressNodeId;
 //     private NodeId defaultNodeId;
        private byte[] macAddressNodeId;
        private byte[] defaultNodeId;
 
        public SimpleUuidFactory(byte[] nodeId, int offset, Clock clock) {
+               byte[] hardwareAddress = getHardwareAddress();
+//             macAddressNodeId = hardwareAddress != null ? new NodeId(hardwareAddress, 0) : null;
+               macAddressNodeId = toNodeId(hardwareAddress, 0);
+
+//             defaultNodeId = nodeId != null ? new NodeId(nodeId, offset) : macAddressNodeId;
+               defaultNodeId = nodeId != null ? toNodeId(nodeId, offset) : toNodeId(macAddressNodeId, 0);
+               if (defaultNodeId == null)
+                       throw new IllegalStateException("No default node id specified");
+       }
+
+       @Override
+       protected SecureRandom newSecureRandom() {
+               SecureRandom secureRandom;
                try {
                        secureRandom = SecureRandom.getInstance("DRBG",
                                        DrbgParameters.instantiation(256, DrbgParameters.Capability.PR_AND_RESEED, "UUID".getBytes()));
@@ -42,17 +51,7 @@ public class SimpleUuidFactory extends AbstractUuidFactory {
                                secureRandom = new SecureRandom();
                        }
                }
-
-               byte[] hardwareAddress = getHardwareAddress();
-//             macAddressNodeId = hardwareAddress != null ? new NodeId(hardwareAddress, 0) : null;
-               macAddressNodeId = toNodeId(hardwareAddress, 0);
-
-//             defaultNodeId = nodeId != null ? new NodeId(nodeId, offset) : macAddressNodeId;
-               defaultNodeId = nodeId != null ? toNodeId(nodeId, offset) : toNodeId(macAddressNodeId, 0);
-               if (defaultNodeId == null)
-                       throw new IllegalStateException("No default node id specified");
-
-               timeUuidState = new ConcurrentTimeUuidState(secureRandom, clock);
+               return secureRandom;
        }
 
        /*
@@ -60,43 +59,30 @@ public class SimpleUuidFactory extends AbstractUuidFactory {
         */
 
        @Override
-       public UUID timeUUIDwithMacAddress() {
+       public UUID newTimeUUIDwithMacAddress() {
                if (macAddressNodeId == null)
                        throw new UnsupportedOperationException("No MAC address is available");
                return newTimeUUID(timeUuidState.useTimestamp(), timeUuidState.getClockSequence(), macAddressNodeId, 0);
        }
 
        @Override
-       public UUID timeUUID() {
+       public UUID newTimeUUID() {
                return newTimeUUID(timeUuidState.useTimestamp(), timeUuidState.getClockSequence(), defaultNodeId, 0);
        }
 
-       /*
-        * NAME BASED (version 3 and 5)
-        */
-       @Override
-       public UUID nameUUIDv5(UUID namespace, byte[] name) {
-               return newNameUUIDv5(namespace, name);
-       }
-
-       @Override
-       public UUID nameUUIDv3(UUID namespace, byte[] name) {
-               return newNameUUIDv3(namespace, name);
-       }
-
        /*
         * RANDOM v4
         */
-       @Override
-       public UUID randomUUID(Random random) {
-               return newRandomUUID(random);
-       }
-
-       @Override
-       public UUID randomUUID() {
-               return randomUUID(secureRandom);
-       }
+//     @Override
+//     public UUID randomUUID(Random random) {
+//             return newRandomUUID(random);
+//     }
 
+//     @Override
+//     public UUID randomUUID() {
+//             return randomUUID(secureRandom);
+//     }
+//
 //     static class NodeId extends ThreadLocal<byte[]> {
 //             private byte[] source;
 //             private int offset;