Module buralotech.identifier.core
Class UUIDIdentifierService
java.lang.Object
com.buralotech.oss.identifier.uuid.UUIDIdentifierService
- All Implemented Interfaces:
IdentifierService
Generate identifiers and parse binary and textual representations of identifiers. The generator uses either a Type 1
UUID generator and juggles the bits so that the binary representations can be ordered by generation time or a new
Type 6 UUID which is reordered in a similar way but is standardised. The textual representation is a modified
URL-safe base 64 encoding that is also sortable.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int[]Look-up table used during decoding.private final UUIDVersionDelegateDelegate that encapsulates logic that is specific to the UUID format.private static final char[]Look-up table used during encoding.private static final longThe adjustment to apply to convert UUID epoch to Unix Epoch.private static final longNumber of ticks per millisecond.private static final longNumber of ticks per second. -
Constructor Summary
ConstructorsConstructorDescriptionUUIDIdentifierService(UUIDVersionDelegate delegate) Constructor used to inject the delegate that encapsulates the logic that is specific to the UUID format. -
Method Summary
Modifier and TypeMethodDescriptionasLowerBound(Temporal time) Generate a lower-bound identifier for temporal value that can be used in range queries.asUpperBound(Temporal time) Generate an upper-bound identifier for temporal value that can be used in range queries.private intdecode(char ch) Decode a single Base64 digit.private byte[]Decode a 22 Base64 digit string into 16 bytes.private voidDecode 2 Base64 digits into 1 byte.private voidDecode 4 Base64 digits into 3 bytes.private Stringencode(byte[] bytes) Encode 16 bytes as 22 Base64 digit string.private voidencode1(byte b1, char[] chars, int j) Encode a single byte as 2 Base64 digits.private voidencode3(byte b1, byte b2, byte b3, char[] chars, int j) Encode three bytes as 4 Base64 digits.fromBinary(byte[] binary) Decode an identifier using its binary representation.Decode an identifier using its text representation.generate()Generate an identifier using an underlying UUID generator.toInstant(Identifier identifier) Extract an instant from an identifier.private longGiven a temporal value extract the number of UUID ticks (100 nanoseconds).
-
Field Details
-
ENCODING
private static final char[] ENCODINGLook-up table used during encoding. -
DECODING
private static final int[] DECODINGLook-up table used during decoding. -
TICKS_PER_MILLISECOND
private static final long TICKS_PER_MILLISECONDNumber of ticks per millisecond. The UUID tick is 100 nanoseconds.- See Also:
-
TICKS_PER_SECOND
private static final long TICKS_PER_SECONDNumber of ticks per second. TheUUID thick is 100 nanoseconds.- See Also:
-
EPOCH_ADJ
private static final long EPOCH_ADJThe adjustment to apply to convert UUID epoch to Unix Epoch. The UUID epoch 15 October 1582, while the Unix epoch is 1 January 1970.- See Also:
-
delegate
Delegate that encapsulates logic that is specific to the UUID format.
-
-
Constructor Details
-
UUIDIdentifierService
Constructor used to inject the delegate that encapsulates the logic that is specific to the UUID format.- Parameters:
delegate- Encapsulates logic that is specific to the UUID format.
-
-
Method Details
-
generate
Generate an identifier using an underlying UUID generator.- Specified by:
generatein interfaceIdentifierService- Returns:
- The generated identifier.
-
fromText
Decode an identifier using its text representation.- Specified by:
fromTextin interfaceIdentifierService- Parameters:
text- The text representation.- Returns:
- The identifier.
-
fromBinary
Decode an identifier using its binary representation.- Specified by:
fromBinaryin interfaceIdentifierService- Parameters:
binary- The binary representation.- Returns:
- The identifier.
-
encode
Encode 16 bytes as 22 Base64 digit string.- Parameters:
bytes- The 16 input bytes.- Returns:
- The 22 digit Base64 string.
-
encode3
private void encode3(byte b1, byte b2, byte b3, char[] chars, int j) Encode three bytes as 4 Base64 digits.- Parameters:
b1- The first byte.b2- The second byte.b3- The third byte.chars- The output character array in which the base 64 digits will be stored.j- The position at whichthe first Base64 digit will be stored.
-
encode1
private void encode1(byte b1, char[] chars, int j) Encode a single byte as 2 Base64 digits.- Parameters:
b1- The input byte.chars- The output character array in which the base 64 digits will be stored.j- The position at whichthe first Base64 digit will be stored.
-
decode
Decode a 22 Base64 digit string into 16 bytes.- Parameters:
str- The Base64 digit string.- Returns:
- The 16 bytes.
-
decode4
Decode 4 Base64 digits into 3 bytes.- Parameters:
bytes- The destination where decoded bytes will be stored.i- The position at which to store the first decoded bytes.str- A string of Base64 digits.j- The position of the first Base64 digit.
-
decode2
Decode 2 Base64 digits into 1 byte.- Parameters:
bytes- The destination where decoded bytes will be stored.dest- The position at which to store the decoded byte.string- A string of Base64 digits.src- The position of the first Base64 digit.
-
decode
private int decode(char ch) Decode a single Base64 digit.- Parameters:
ch- The Base64 digit.- Returns:
- The value of the Base64 digit.
-
toInstant
Extract an instant from an identifier.- Specified by:
toInstantin interfaceIdentifierService- Parameters:
identifier- The identifier.- Returns:
- The instant.
-
asLowerBound
Generate a lower-bound identifier for temporal value that can be used in range queries.- Specified by:
asLowerBoundin interfaceIdentifierService- Parameters:
time- The temporal value (Instant,LocalDate,LocalDateTime,OffsetDateTime,ZonedDateTime)- Returns:
- A lower-bound identifier that can be used in a range query.
- Throws:
IllegalArgumentException- If the temporal type is not supported.
-
asUpperBound
Generate an upper-bound identifier for temporal value that can be used in range queries.- Specified by:
asUpperBoundin interfaceIdentifierService- Parameters:
time- The temporal value (Instant,LocalDate,LocalDateTime,OffsetDateTime,ZonedDateTime)- Returns:
- An upper-bound identifier that can be used in a range query.
- Throws:
IllegalArgumentException- If the temporal type is not supported.
-
toTicks
Given a temporal value extract the number of UUID ticks (100 nanoseconds).- Parameters:
temporal- The temporal value (Instant,LocalDate,LocalDateTime,OffsetDateTime,ZonedDateTime)- Returns:
- The number of ticks.
- Throws:
IllegalArgumentException- If the temporal type is not supported.
-