public abstract class AbstractTimeBasedUuidCreator extends AbstractUuidCreator implements NoArgumentsUuidCreator
| Modifier and Type | Field and Description |
|---|---|
protected ClockSequenceStrategy |
clockSequenceStrategy |
protected NodeIdentifierStrategy |
nodeIdentifierStrategy |
protected TimestampStrategy |
timestampStrategy |
version, versionBits| Modifier | Constructor and Description |
|---|---|
protected |
AbstractTimeBasedUuidCreator(UuidVersion 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.
|
protected long |
formatLeastSignificantBits(long nodeIdentifier,
long clockSequence)
Formats the least significant bits of the UUID.
### RFC-4122 - 4.2.2.
|
protected abstract long |
formatMostSignificantBits(long timestamp)
Formats the most significant bits of the UUID.
|
<T extends AbstractTimeBasedUuidCreator> |
withClockSequence(int clockSequence)
Set a fixed clock sequence value to generate UUIDs.
|
<T extends AbstractTimeBasedUuidCreator> |
withClockSequenceStrategy(ClockSequenceStrategy clockSequenceStrategy)
Use an alternate
ClockSequenceStrategy to generate clock
sequences. |
<T extends AbstractTimeBasedUuidCreator> |
withFingerprintNodeIdentifier()
Set the node identifier to be a system data hash.
|
<T extends AbstractTimeBasedUuidCreator> |
withHardwareAddressNodeIdentifier()
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 alternate
NodeIdentifierStrategy to generate node
identifiers. |
<T extends AbstractTimeBasedUuidCreator> |
withoutOverrunException()
Used to disable the overrun exception.
|
<T extends AbstractTimeBasedUuidCreator> |
withRandomNodeIdentifier()
Set the node identifier to be a random identifier.
|
<T extends AbstractTimeBasedUuidCreator> |
withRandomNodeIdentifier(Random random)
Set the node identifier to be a random identifier.
|
<T extends AbstractTimeBasedUuidCreator> |
withTimestamp(long timestamp)
Set a fixed timestamp to generate UUIDs.
|
<T extends AbstractTimeBasedUuidCreator> |
withTimestampStrategy(TimestampStrategy timestampStrategy)
Use an alternate
TimestampStrategy to generate timestamps. |
<T extends AbstractTimeBasedUuidCreator> |
withUnixMilliseconds(long unixMilliseconds)
Set a fixed Unix Epoch milliseconds value to generate UUIDs.
|
getVariantBits, getVersion, getVersionBits, setVariantBits, setVersionBitsprotected TimestampStrategy timestampStrategy
protected ClockSequenceStrategy clockSequenceStrategy
protected NodeIdentifierStrategy nodeIdentifierStrategy
protected AbstractTimeBasedUuidCreator(UuidVersion 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.
create in interface NoArgumentsUuidCreatorUUID a UUID valueUuidCreatorException - an overrun exception if more than 10 thousand UUIDs are
requested within the same millisecondpublic <T extends AbstractTimeBasedUuidCreator> T withTimestampStrategy(TimestampStrategy timestampStrategy)
TimestampStrategy to generate timestamps. The
DefaultTimestampStrategy has accuracy of milliseconds. If someone
needs a real 100-nanosecond resolution, another 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.T - type parameternodeIdentifierStrategy - a node identifier strategyAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withClockSequenceStrategy(ClockSequenceStrategy clockSequenceStrategy)
ClockSequenceStrategy to generate clock
sequences.T - 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 withUnixMilliseconds(long unixMilliseconds)
T - type parameterunixMilliseconds - a Unix Epoch milliseconds valueAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withNodeIdentifier(long nodeIdentifier)
T - type parameternodeIdentifier - a node identifierAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withHardwareAddressNodeIdentifier()
T - type parameterAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withFingerprintNodeIdentifier()
T - type parameterAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withRandomNodeIdentifier()
T - type parameterAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withRandomNodeIdentifier(Random random)
T - type parameterrandom - a random number generatorAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withClockSequence(int clockSequence)
T - type parameterclockSequence - a clock sequenceAbstractTimeBasedUuidCreatorpublic <T extends AbstractTimeBasedUuidCreator> T withoutOverrunException()
DefaultTimestampStrategy. So
don't use this method if you want the creator to use another
TimestampStrategy.AbstractTimeBasedUuidCreatorprotected abstract long formatMostSignificantBits(long timestamp)
timestamp - a timestampprotected long formatLeastSignificantBits(long nodeIdentifier,
long clockSequence)
nodeIdentifier - a node identifierclockSequence - a clock sequenceCopyright © 2020. All rights reserved.