Create separate project for UUID API.
[lgpl/argeo-commons.git] / org.argeo.api.acr / src / org / argeo / api / acr / uuid / ConcurrentTimeUuidState.java
index bf4039678564ab38bc15813e213ac43e8da066b5..db72a4b21d72ac58e7eee9bbd65ce7e12d85dabb 100644 (file)
@@ -1,6 +1,5 @@
 package org.argeo.api.acr.uuid;
 
-import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 import java.time.Clock;
 import java.time.Duration;
@@ -12,13 +11,21 @@ import java.util.Objects;
  * different clock sequences for each thread.
  */
 public class ConcurrentTimeUuidState implements TimeUuidState {
+//     public final static ThreadLocal<Boolean> isTimeUuidThread = new ThreadLocal<>() {
+//
+//             @Override
+//             protected Boolean initialValue() {
+//                     return false;
+//             }
+//     };
+
        /** The maximum possible value of the clocksequence. */
        private final static int MAX_CLOCKSEQUENCE = 16384;
 
-       private final byte[] nodeId = new byte[6];
        private final ThreadLocal<Holder> holder;
 
        private final Instant startInstant;
+       /** A start timestamp to which {@link System#nanoTime()}/100 can be added. */
        private final long startTimeStamp;
 
        private final Clock clock;
@@ -26,22 +33,12 @@ public class ConcurrentTimeUuidState implements TimeUuidState {
 
        private final SecureRandom secureRandom;
 
-       public ConcurrentTimeUuidState(byte[] nodeId, int offset, SecureRandom secureRandom, Clock clock) {
+       public ConcurrentTimeUuidState(SecureRandom secureRandom, Clock clock) {
                useClockForMeasurement = clock != null;
                this.clock = clock != null ? clock : Clock.systemUTC();
 
                Objects.requireNonNull(secureRandom);
                this.secureRandom = secureRandom;
-               if (nodeId != null) {
-                       // copy array in case it should change in the future
-                       if (offset + 6 > nodeId.length)
-                               throw new IllegalArgumentException(
-                                               "Node id array is too small: " + nodeId.length + ", offset=" + offset);
-                       System.arraycopy(nodeId, offset, this.nodeId, 0, 6);
-               } else {
-                       this.secureRandom.nextBytes(this.nodeId);
-                       assert TimeUuidState.isNoMacAddressNodeId(this.nodeId);
-               }
 
                // compute the start reference
                startInstant = Instant.now(this.clock);
@@ -57,7 +54,7 @@ public class ConcurrentTimeUuidState implements TimeUuidState {
                                Holder value = new Holder();
                                value.lastTimestamp = startTimeStamp;
                                value.clockSequence = newClockSequence();
-                               System.arraycopy(ConcurrentTimeUuidState.this.nodeId, 0, value.nodeId, 0, 6);
+//                             isTimeUuidThread.set(true);
                                return value;
                        }
                };
@@ -68,6 +65,7 @@ public class ConcurrentTimeUuidState implements TimeUuidState {
         */
 
        public long useTimestamp() {
+
                long previousTimestamp = holder.get().lastTimestamp;
                long now = computeNow();
 
@@ -116,16 +114,6 @@ public class ConcurrentTimeUuidState implements TimeUuidState {
                return (duration.getSeconds() * 10000000 + duration.getNano() / 100);
        }
 
-       protected SecureRandom initSecureRandom() {
-               SecureRandom secureRandom;
-               try {
-                       secureRandom = SecureRandom.getInstanceStrong();
-               } catch (NoSuchAlgorithmException e) {
-                       secureRandom = new SecureRandom();
-               }
-               return secureRandom;
-       }
-
        /*
         * STATE OPERATIONS
         */
@@ -138,16 +126,15 @@ public class ConcurrentTimeUuidState implements TimeUuidState {
         * ACCESSORS
         */
 
-       @Override
-       public byte[] getNodeId() {
-               byte[] arr = new byte[6];
-               System.arraycopy(holder.get().nodeId, 0, arr, 0, 6);
-               return arr;
-       }
+//     @Override
+//     public byte[] getNodeId() {
+//             byte[] arr = new byte[6];
+//             System.arraycopy(holder.get().nodeId, 0, arr, 0, 6);
+//             return arr;
+//     }
 
        @Override
        public long getClockSequence() {
                return holder.get().clockSequence;
        }
-
 }