From 98f2c711418690e62e35c931e35d1dc92fd07926 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Mon, 14 Nov 2022 06:30:36 +0100 Subject: [PATCH] Create time UUID from timestamp. --- .../api/uuid/ConcurrentTimeUuidState.java | 4 +--- .../src/org/argeo/api/uuid/TimeUuid.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentTimeUuidState.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentTimeUuidState.java index 6dbd403dd..fd158fb83 100644 --- a/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentTimeUuidState.java +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentTimeUuidState.java @@ -143,9 +143,7 @@ public class ConcurrentTimeUuidState implements UuidFactory.TimeUuidState { @Override public long getMostSignificantBits() { long timestamp = useTimestamp(); - long mostSig = UuidFactory.MOST_SIG_VERSION1 | ((timestamp & 0xFFFFFFFFL) << 32) // time_low - | (((timestamp >> 32) & 0xFFFFL) << 16) // time_mid - | ((timestamp >> 48) & 0x0FFFL);// time_hi_and_version + long mostSig = TimeUuid.toMostSignificantBits(timestamp); return mostSig; } diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/TimeUuid.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/TimeUuid.java index 2276cd268..2e0587d12 100644 --- a/org.argeo.api.uuid/src/org/argeo/api/uuid/TimeUuid.java +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/TimeUuid.java @@ -76,4 +76,23 @@ public class TimeUuid extends TypedUuid { Duration duration = Duration.between(TimeUuid.TIMESTAMP_ZERO, instant); return durationToTimestamp(duration); } + + /** + * Crate a time UUID with this instant as timestamp and clock and node id set to + * zero. + */ + public static UUID fromInstant(Instant instant) { + long timestamp = instantToTimestamp(instant); + long mostSig = toMostSignificantBits(timestamp); + UUID uuid = new UUID(mostSig, UuidFactory.LEAST_SIG_RFC4122_VARIANT); + return uuid; + } + + /** Convert timestamp in UUID format to most significant bits of a time UUID. */ + static long toMostSignificantBits(long timestamp) { + long mostSig = UuidFactory.MOST_SIG_VERSION1 | ((timestamp & 0xFFFFFFFFL) << 32) // time_low + | (((timestamp >> 32) & 0xFFFFL) << 16) // time_mid + | ((timestamp >> 48) & 0x0FFFL);// time_hi_and_version + return mostSig; + } } -- 2.30.2