Class UUIDIdentifierService

java.lang.Object
com.buralotech.oss.identifier.uuid.UUIDIdentifierService
All Implemented Interfaces:
IdentifierService

public final class UUIDIdentifierService extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    private static final int[]
    Look-up table used during decoding.
    private final UUIDVersionDelegate
    Delegate that encapsulates logic that is specific to the UUID format.
    private static final char[]
    Look-up table used during encoding.
    private static final long
    The adjustment to apply to convert UUID epoch to Unix Epoch.
    private static final long
    Number of ticks per millisecond.
    private static final long
    Number of ticks per second.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor used to inject the delegate that encapsulates the logic that is specific to the UUID format.
  • Method Summary

    Modifier and Type
    Method
    Description
    Generate a lower-bound identifier for temporal value that can be used in range queries.
    Generate an upper-bound identifier for temporal value that can be used in range queries.
    private int
    decode(char ch)
    Decode a single Base64 digit.
    private byte[]
    Decode a 22 Base64 digit string into 16 bytes.
    private void
    decode2(byte[] bytes, int dest, String string, int src)
    Decode 2 Base64 digits into 1 byte.
    private void
    decode4(byte[] bytes, int i, String str, int j)
    Decode 4 Base64 digits into 3 bytes.
    private String
    encode(byte[] bytes)
    Encode 16 bytes as 22 Base64 digit string.
    private void
    encode1(byte b1, char[] chars, int j)
    Encode a single byte as 2 Base64 digits.
    private void
    encode3(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 an identifier using an underlying UUID generator.
    toInstant(Identifier identifier)
    Extract an instant from an identifier.
    private long
    toTicks(Temporal temporal, boolean upper)
    Given a temporal value extract the number of UUID ticks (100 nanoseconds).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ENCODING

      private static final char[] ENCODING
      Look-up table used during encoding.
    • DECODING

      private static final int[] DECODING
      Look-up table used during decoding.
    • TICKS_PER_MILLISECOND

      private static final long TICKS_PER_MILLISECOND
      Number of ticks per millisecond. The UUID tick is 100 nanoseconds.
      See Also:
    • TICKS_PER_SECOND

      private static final long TICKS_PER_SECOND
      Number of ticks per second. TheUUID thick is 100 nanoseconds.
      See Also:
    • EPOCH_ADJ

      private static final long EPOCH_ADJ
      The 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

      private final UUIDVersionDelegate delegate
      Delegate that encapsulates logic that is specific to the UUID format.
  • Constructor Details

    • UUIDIdentifierService

      public UUIDIdentifierService(UUIDVersionDelegate delegate)
      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

      public Identifier generate()
      Generate an identifier using an underlying UUID generator.
      Specified by:
      generate in interface IdentifierService
      Returns:
      The generated identifier.
    • fromText

      public Identifier fromText(String text)
      Decode an identifier using its text representation.
      Specified by:
      fromText in interface IdentifierService
      Parameters:
      text - The text representation.
      Returns:
      The identifier.
    • fromBinary

      public Identifier fromBinary(byte[] binary)
      Decode an identifier using its binary representation.
      Specified by:
      fromBinary in interface IdentifierService
      Parameters:
      binary - The binary representation.
      Returns:
      The identifier.
    • encode

      private String encode(byte[] bytes)
      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

      private byte[] decode(String str)
      Decode a 22 Base64 digit string into 16 bytes.
      Parameters:
      str - The Base64 digit string.
      Returns:
      The 16 bytes.
    • decode4

      private void decode4(byte[] bytes, int i, String str, int j)
      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

      private void decode2(byte[] bytes, int dest, String string, int src)
      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

      public Instant toInstant(Identifier identifier)
      Extract an instant from an identifier.
      Specified by:
      toInstant in interface IdentifierService
      Parameters:
      identifier - The identifier.
      Returns:
      The instant.
    • asLowerBound

      public Identifier asLowerBound(Temporal time)
      Generate a lower-bound identifier for temporal value that can be used in range queries.
      Specified by:
      asLowerBound in interface IdentifierService
      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

      public Identifier asUpperBound(Temporal time)
      Generate an upper-bound identifier for temporal value that can be used in range queries.
      Specified by:
      asUpperBound in interface IdentifierService
      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

      private long toTicks(Temporal temporal, boolean upper)
      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.