public abstract class AbstractTimeBasedUuidCreator extends AbstractUuidCreator
| Modifier and Type | Field and Description |
|---|---|
protected ClockSequenceStrategy |
clockSequenceStrategy |
protected NodeIdentifierStrategy |
nodeIdentifierStrategy |
protected TimestampStrategy |
timestampStrategy |
NAMESPACE_DNS, NAMESPACE_OID, NAMESPACE_URL, NAMESPACE_X500, NIL_UUID, RFC4122_VARIANT_BITS, RFC4122_VERSION_BITS, VARIANT_RESERVED_FUTURE, VARIANT_RESERVED_MICROSOFT, VARIANT_RESERVED_NCS, VARIANT_RFC4122, version, VERSION_0, VERSION_1, VERSION_2, VERSION_3, VERSION_4, VERSION_5| Modifier | Constructor and Description |
|---|---|
protected |
AbstractTimeBasedUuidCreator(int version)
This constructor requires a version number.
|
| Modifier and Type | Method and Description |
|---|---|
UUID |
create()
Returns a new time-based UUID.
### Timestamp
The timestamp has 100-nanoseconds resolution, starting from 1582-10-15.
|
long |
formatLeastSignificantBits(long nodeIdentifier,
long clockSequence)
Returns the least significant bits of the UUID.
### RFC-4122 - 4.2.2.
|
abstract long |
formatMostSignificantBits(long timestamp)
Format the MSB UUID from the current timestamp.
|
<T extends AbstractTimeBasedUuidCreator> |
withClockSequence(int clockSequence)
Set a fixed initial clock sequence value to generate UUIDs.
|
<T extends AbstractTimeBasedUuidCreator> |
withClockSequenceStrategy(ClockSequenceStrategy clockSequenceStrategy)
Use an alternative
ClockSequenceStrategy to generate clock
sequences. |
<T extends AbstractTimeBasedUuidCreator> |
withHardwareAddress()
Set the node identifier to be a real hardware address of the host
machine.
|
<T extends AbstractTimeBasedUuidCreator> |
withInstant(Instant instant)
Set a fixed Instant to generate UUIDs.
|
<T extends AbstractTimeBasedUuidCreator> |
withNodeIdentifier(long nodeIdentifier)
Set a fixed node identifier to generate UUIDs.
|
<T extends AbstractTimeBasedUuidCreator> |
withNodeIdentifierStrategy(NodeIdentifierStrategy nodeIdentifierStrategy)
Use an alternative
NodeIdentifierStrategy to generate node
identifiers. |
<T extends AbstractTimeBasedUuidCreator> |
withTimestamp(long timestamp)
Set a fixed timestamp to generate UUIDs.
|
<T extends AbstractTimeBasedUuidCreator> |
withTimestampStrategy(TimestampStrategy timestampStrategy)
Use an alternative
TimestampStrategy to generate timestamps. |
getVariantBits, getVersion, getVersionBits, setVariantBits, setVersionBits, validprotected TimestampStrategy timestampStrategy
protected ClockSequenceStrategy clockSequenceStrategy
protected NodeIdentifierStrategy nodeIdentifierStrategy
protected AbstractTimeBasedUuidCreator(int version)
version - the version numberpublic UUID create()
s = 10_000_000 m = 60 * s h = 60 * m d = 24 * h y = 365.25 * d 2^60 / y = ~3653 3653 + 1582 = 5235### RFC-4122 - 4.1.4. Timestamp The timestamp is a 60-bit value. For UUID version 1, this is represented by Coordinated Universal Time (UTC) as a count of 100- nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to the Christian calendar). ### RFC-4122 - 4.1.5. Clock Sequence For UUID version 1, the clock sequence is used to help avoid duplicates that could arise when the clock is set backwards in time or if the node ID changes. ### RFC-4122 - 4.1.6. Node For UUID version 1, the node field consists of an IEEE 802 MAC address, usually the host address. For systems with multiple IEEE 802 addresses, any available one can be used. The lowest addressed octet (octet number 10) contains the global/local bit and the unicast/multicast bit, and is the first octet of the address transmitted on an 802.3 LAN. For systems with no IEEE address, a randomly or pseudo-randomly generated value may be used; see Section 4.5. The multicast bit must be set in such addresses, in order that they will never conflict with addresses obtained from network cards. ### RFC-4122 - 4.2.1. Basic Algorithm (1a) Obtain a system-wide global lock (2a) From a system-wide shared stable store (e.g., a file), read the UUID generator state: the values of the timestamp, clock sequence, and node ID used to generate the last UUID. (3a) Get the current time as a 60-bit count of 100-nanosecond intervals since 00:00:00.00, 15 October 1582. (4a) Get the current node ID. (5a) If the state was unavailable (e.g., non-existent or corrupted), or the saved node ID is different than the current node ID, generate a random clock sequence value. (6a) If the state was available, but the saved timestamp is later than the current timestamp, increment the clock sequence value. (7a) Save the state (current timestamp, clock sequence, and node ID) back to the stable store. (8a) Release the global lock. (9a) Format a UUID from the current timestamp, clock sequence, and node ID values according to the steps in Section 4.2.2.
UUIDpublic <T extends AbstractTimeBasedUuidCreator> T withTimestampStrategy(TimestampStrategy timestampStrategy)
TimestampStrategy to generate timestamps. The
DefaultTimestampStrategy has accuracy of milliseconds. If someone
needs a real 100-nanosecond an implementation of
TimestampStrategy may be provided via this method.T - type parametertimestampStrategy - a timestamp strategyAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withNodeIdentifierStrategy(NodeIdentifierStrategy nodeIdentifierStrategy)
NodeIdentifierStrategy to generate node
identifiers. The DefaultNodeIdentifierStrategy generates a random
multicast node identifier and returns it for every call to
DefaultNodeIdentifierStrategy.getNodeIdentifier().T - type parameternodeIdentifierStrategy - a node identifier strategyAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withClockSequenceStrategy(ClockSequenceStrategy clockSequenceStrategy)
ClockSequenceStrategy to generate clock
sequences. By default the strategy DefaultClockSequenceStrategy
is used.
DefaultClockSequenceStrategyT - type parameterclockSequenceStrategy - a clock sequence strategyAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withInstant(Instant instant)
T - type parameterinstant - an InstantAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withTimestamp(long timestamp)
T - type parametertimestamp - a timestampAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withNodeIdentifier(long nodeIdentifier)
T - type parameternodeIdentifier - a node identifierAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withHardwareAddress()
T - type parameterAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withClockSequence(int clockSequence)
T - type parameterclockSequence - a clock sequenceAbstractTimeBasedUuidCreatorpublic abstract long formatMostSignificantBits(long timestamp)
timestamp - a timestamppublic long formatLeastSignificantBits(long nodeIdentifier,
long clockSequence)
nodeIdentifier - a node identifierclockSequence - a clock sequenceCopyright © 2019. All rights reserved.