+ public static UUID timeUUIDwithRandomNode() {
+ long timestamp = START_TIMESTAMP + System.nanoTime() / 100;
+ return timeUUID(timestamp, RANDOM);
+ }
+
+ public static UUID timeUUID(long timestamp, Random random) {
+ byte[] node = new byte[6];
+ random.nextBytes(node);
+ node[0] = (byte) (node[0] | 1);
+ long clockSequence = CLOCK_SEQUENCE.incrementAndGet();
+ return timeUUID(timestamp, clockSequence, node);
+ }
+
+ public static UUID timeUUID() {
+ long timestamp = START_TIMESTAMP + System.nanoTime() / 100;
+ return timeUUID(timestamp);
+ }
+
+ public static UUID timeUUID(long timestamp) {
+ if (HARDWARE_ADDRESS == null)
+ return timeUUID(timestamp, RANDOM);
+ long clockSequence = CLOCK_SEQUENCE.incrementAndGet();
+ return timeUUID(timestamp, clockSequence, HARDWARE_ADDRESS);
+ }
+
+ public static UUID timeUUID(long timestamp, NetworkInterface nic) {
+ byte[] node;
+ try {
+ node = nic.getHardwareAddress();
+ } catch (SocketException e) {
+ throw new IllegalStateException("Cannot get hardware address", e);
+ }
+ long clockSequence = CLOCK_SEQUENCE.incrementAndGet();
+ return timeUUID(timestamp, clockSequence, node);
+ }
+
+ public static UUID timeUUID(LocalDateTime time, long clockSequence, byte[] node) {
+ Duration duration = Duration.between(GREGORIAN_START, time);
+ // Number of 100 ns intervals in one second: 1000000000 / 100 = 10000000
+ long timestamp = duration.getSeconds() * 10000000 + duration.getNano() / 100;
+ return timeUUID(timestamp, clockSequence, node);
+ }
+
+ public static UUID timeUUID(long timestamp, long clockSequence, byte[] node) {
+ assert node.length >= 6;
+
+ long mostSig = MOST_SIG_VERSION1 // base for version 1 UUID
+ | ((timestamp & 0xFFFFFFFFL) << 32) // time_low
+ | (((timestamp >> 32) & 0xFFFFL) << 16) // time_mid
+ | ((timestamp >> 48) & 0x0FFFL);// time_hi_and_version
+
+ long leastSig = LEAST_SIG_RFC4122_VARIANT // base for Leach–Salz UUID
+ | (((clockSequence & 0x3F00) >> 8) << 56) // clk_seq_hi_res
+ | ((clockSequence & 0xFF) << 48) // clk_seq_low
+ | (node[0] & 0xFFL) //
+ | ((node[1] & 0xFFL) << 8) //
+ | ((node[2] & 0xFFL) << 16) //
+ | ((node[3] & 0xFFL) << 24) //
+ | ((node[4] & 0xFFL) << 32) //
+ | ((node[5] & 0xFFL) << 40); //
+// for (int i = 0; i < 6; i++) {
+// leastSig = leastSig | ((node[i] & 0xFFL) << (8 * i));
+// }
+ UUID uuid = new UUID(mostSig, leastSig);
+
+ // tests
+ assert uuid.node() == BitSet.valueOf(node).toLongArray()[0];
+ assert uuid.timestamp() == timestamp;
+ assert uuid.clockSequence() == clockSequence;
+ assert uuid.version() == 1;
+ assert uuid.variant() == 2;
+ return uuid;
+ }
+
+ @Deprecated