]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.uuid/src/org/argeo/api/uuid/TimeUuid.java
1 package org
.argeo
.api
.uuid
;
3 import java
.time
.Duration
;
4 import java
.time
.Instant
;
5 import java
.time
.ZoneOffset
;
6 import java
.time
.ZonedDateTime
;
10 * A time based UUID, whose content can therefore be usefully interpreted as
11 * time and node identifier information.
13 public class TimeUuid
extends TypedUuid
{
14 private static final long serialVersionUID
= APM
.SERIAL
;
16 * Start of the Gregorian time on October 15th 1582, equivalent to
17 * <code>{@link UUID#timestamp()} == 0</code>.
19 public final static Instant TIMESTAMP_ZERO
= ZonedDateTime
.of(1582, 10, 15, 0, 0, 0, 0, ZoneOffset
.UTC
).toInstant();
21 /** Constructor based on a {@link UUID}. */
22 public TimeUuid(UUID uuid
) {
24 if (uuid
.version() != 1 && uuid
.variant() != 2)
25 throw new IllegalArgumentException("The provided UUID is not a time based UUID.");
28 /** {@link UUID#timestamp()} as an {@link Instant}. */
29 public final Instant
getInstant() {
30 long timestamp
= uuid
.timestamp();
31 return TIMESTAMP_ZERO
.plus(timestampDifferenceToDuration(timestamp
));
34 /** {@link UUID#node()} as an hex string. */
35 public final String
getNodeId() {
36 return Long
.toHexString(uuid
.node());
39 /** {@link UUID#clockSequence()} as an hex string. */
40 public final String
getClockSequence() {
41 return Long
.toHexString(uuid
.clockSequence());
45 * Always returns <code>false</code> since time UUIDs are by definition not
49 public final boolean isOpaque() {
56 /** Converts from duration in the time UUID timestamp format. */
57 public static Duration
timestampDifferenceToDuration(long timestampDifference
) {
58 long seconds
= timestampDifference
/ 10000000;
59 long nano
= (timestampDifference
% 10000000) * 100;
60 return Duration
.ofSeconds(seconds
, nano
);
64 * A duration expressed in the time UUID timestamp format based on units of 100
67 public static long durationToTimestamp(Duration duration
) {
68 return (duration
.getSeconds() * 10000000 + duration
.getNano() / 100);
72 * An instant expressed in the time UUID timestamp format based on units of 100
73 * ns since {@link #TIMESTAMP_ZERO}.
75 public static long instantToTimestamp(Instant instant
) {
76 Duration duration
= Duration
.between(TimeUuid
.TIMESTAMP_ZERO
, instant
);
77 return durationToTimestamp(duration
);