Document and clean up UUID factory API and implementation.
[lgpl/argeo-commons.git] / org.argeo.api.uuid / src / org / argeo / api / uuid / ConcurrentUuidFactory.java
index 05b2f05c6b62345e0160363c7129983301d32ade..6debd83b803426453c9e56b1594b6793bcaf6dcb 100644 (file)
@@ -8,6 +8,7 @@ import java.security.DrbgParameters;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 import java.util.Objects;
+import java.util.UUID;
 
 /**
  * A configurable implementation of an {@link AsyncUuidFactory}, which can be
@@ -18,21 +19,44 @@ import java.util.Objects;
 public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory {
        private final static Logger logger = System.getLogger(ConcurrentUuidFactory.class.getName());
 
-//     private byte[] defaultNodeId;
-
-       private Long nodeIdBase;
+       public ConcurrentUuidFactory(byte[] nodeId) {
+               this(nodeId, 0);
+       }
 
        public ConcurrentUuidFactory(byte[] nodeId, int offset) {
                Objects.requireNonNull(nodeId);
                if (offset + 6 > nodeId.length)
                        throw new IllegalArgumentException("Offset too big: " + offset);
                byte[] defaultNodeId = toNodeIdBytes(nodeId, offset);
-               nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId);
+               long nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId);
                setNodeIdSupplier(() -> nodeIdBase);
        }
 
-       protected ConcurrentUuidFactory() {
+       /**
+        * Empty constructor for use with component life cycle. A {@link NodeIdSupplier}
+        * must be set externally, otherwise time based UUID won't work.
+        */
+       public ConcurrentUuidFactory() {
+               super();
+       }
 
+//     public ConcurrentUuidFactory() {
+//             byte[] defaultNodeId = getIpBytes();
+//             nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId);
+//             setNodeIdSupplier(() -> nodeIdBase);
+//             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
@@ -53,12 +77,4 @@ public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory {
                return secureRandom;
        }
 
-       /*
-        * TIME-BASED (version 1)
-        */
-//
-//     @Override
-//     public UUID newTimeUUID() {
-//             return newTimeUUID(timeUuidState.useTimestamp(), timeUuidState.getClockSequence(), defaultNodeId, 0);
-//     }
-}
+}
\ No newline at end of file