Class UlidSpecCreator


  • public class UlidSpecCreator
    extends Object
    Factory that creates lexicographically sortable GUIDs, based on the ULID specification - Universally Unique Lexicographically Sortable Identifier. ULID specification: https://github.com/ulid/spec
    • Field Detail

      • random1

        protected long random1
      • random2

        protected long random2
      • randomMax1

        protected long randomMax1
      • randomMax2

        protected long randomMax2
      • HALF_RANDOM_COMPONENT

        protected static final long HALF_RANDOM_COMPONENT
        See Also:
        Constant Field Values
      • previousTimestamp

        protected long previousTimestamp
    • Constructor Detail

      • UlidSpecCreator

        public UlidSpecCreator()
    • Method Detail

      • create

        public UUID create()
        Return a GUID based on the ULID specification. A ULID has two parts: 1. A part of 48 bits that represent the amount of milliseconds since Unix Epoch, 1 January 1970. 2. A part of 80 bits that has a random value generated a secure random generator. The random part is reset to a new value every time the millisecond part changes. If more than one GUID is generated within the same millisecond, the random part is incremented by one. The maximum GUIDs that can be generated per millisecond is 2^80. The random part is generated by a secure random number generator: SecureRandom. ### Specification of Universally Unique Lexicographically Sortable ID #### Components ##### Timestamp It is a 48 bit integer. UNIX-time in milliseconds. Won't run out of space 'til the year 10889 AD. ##### Randomness It is a 80 bits integer. Cryptographically secure source of randomness, if possible. #### Sorting The left-most character must be sorted first, and the right-most character sorted last (lexical order). The default ASCII character set must be used. Within the same millisecond, sort order is not guaranteed. #### Monotonicity When generating a ULID within the same millisecond, we can provide some guarantees regarding sort order. Namely, if the same millisecond is detected, the random component is incremented by 1 bit in the least significant bit position (with carrying). If, in the extremely unlikely event that, you manage to generate more than 2^80 ULIDs within the same millisecond, or cause the random component to overflow with less, the generation will fail.
        Returns:
        UUID a GUID value
      • createString

        public String createString()
        Returns a ULID string. The returning string is encoded to Crockford's base32. The random component is generated by a secure random number generator: SecureRandom.
        Returns:
        a ULID string
      • getTimestamp

        protected long getTimestamp()
        Return the current timestamp and resets or increments the random part.
        Returns:
        timestamp
      • reset

        protected void reset()
        Reset the random part of the GUID.
      • increment

        protected void increment()
        Increment the random part of the GUID.
      • withTimestampStrategy

        public <T extends UlidSpecCreator> T withTimestampStrategy​(TimestampStrategy timestampStrategy)
        Used for changing the timestamp strategy.
        Parameters:
        timestampStrategy - a timestamp strategy
        Returns:
        UlidSpecCreator
      • withRandomStrategy

        public <T extends UlidSpecCreator> T withRandomStrategy​(RandomStrategy randomStrategy)
        Replaces the default random strategy with another. The default random strategy uses SecureRandom. See Random.
        Type Parameters:
        T - the type parameter
        Parameters:
        random - a random generator
        Returns:
        AbstractRandomBasedUuidCreator
      • extractRandom1

        protected long extractRandom1​(UUID uuid)
        For unit tests
      • extractRandom2

        protected long extractRandom2​(UUID uuid)
        For unit tests