Class Tsid

  • All Implemented Interfaces:
    Serializable, Comparable<Tsid>

    public final class Tsid
    extends Object
    implements Serializable, Comparable<Tsid>
    A value object that represents a Time-Sorted Unique Identifier (TSID).

    TSID is a 64-bit value that has 2 components:

    • Time component (42 bits): a number of milliseconds since 1970-01-01 (Unix epoch).
    • Random component (22 bits): a sequence of random bits generated by a secure random generator.

    The Random component has 2 sub-parts:

    • Node (0 to 20 bits): a number used to identify the machine or node.
    • Counter (2 to 22 bits): a randomly generated number that is incremented whenever the time component is repeated.

    The random component layout depend on the node bits. If the node bits are 10, the counter bits are limited to 12. In this example, the maximum node value is 2^10-1 = 1023 and the maximum counter value is 2^12-1 = 4093. So the maximum TSIDs that can be generated per millisecond per node is 4096.

    Instances of this class are immutable.

    See Also:
    Snowflake ID, Serialized Form
    • Field Detail

      • TSID_BYTES

        public static final int TSID_BYTES
        Number of bytes of a TSID.
        See Also:
        Constant Field Values
      • TSID_CHARS

        public static final int TSID_CHARS
        Number of characters of a TSID.
        See Also:
        Constant Field Values
      • TSID_EPOCH

        public static final long TSID_EPOCH
        Number of milliseconds of 2020-01-01T00:00:00.000Z.
    • Constructor Detail

      • Tsid

        public Tsid​(long number)
        Creates a new TSID.

        This constructor wraps the input value in an immutable object.

        Parameters:
        number - a number
    • Method Detail

      • from

        public static Tsid from​(long number)
        Converts a number into a TSID.

        This method wraps the input value in an immutable object.

        Parameters:
        number - a number
        Returns:
        a TSID
      • from

        public static Tsid from​(byte[] bytes)
        Converts a byte array into a TSID.
        Parameters:
        bytes - a byte array
        Returns:
        a TSID
        Throws:
        IllegalArgumentException - if bytes are null or its length is not 8
      • from

        public static Tsid from​(String string)
        Converts a canonical string into a TSID.

        The input string must be 13 characters long and must contain only characters from Crockford's base 32 alphabet.

        The first character of the input string must be between 0 and F.

        Parameters:
        string - a canonical string
        Returns:
        a TSID
        Throws:
        IllegalArgumentException - if the input string is invalid
        See Also:
        Crockford's Base 32
      • toLong

        public long toLong()
        Converts the TSID into a number.

        This method simply unwraps the internal value.

        Returns:
        an number.
      • toBytes

        public byte[] toBytes()
        Converts the TSID into a byte array.
        Returns:
        an byte array.
      • fast

        public static Tsid fast()
        Returns a fast new TSID.

        This static method is a quick alternative to TsidCreator.getTsid().

        It employs AtomicInteger to generate up to 2^22 (4,194,304) TSIDs per millisecond. It can be useful, for example, for logging.

        Security-sensitive applications that require a cryptographically secure pseudo-random generator should use TsidCreator.getTsid().

        System property "tsidcreator.node" and environment variable "TSIDCREATOR_NODE" are ignored by this method. Therefore, there will be collisions if more than one process is generating TSIDs using this method. In that case, TsidCreator.getTsid() should be used in conjunction with that property or variable.

        Returns:
        a TSID
        Since:
        5.1.0
      • toString

        public String toString()
        Converts the TSID into a canonical string in upper case.

        The output string is 13 characters long and contains only characters from Crockford's base 32 alphabet.

        For lower case string, use the shorthand Tsid.toLowerCase() instead of Tsid.toString().toLowerCase().

        Overrides:
        toString in class Object
        Returns:
        a TSID string
        See Also:
        Crockford's Base 32
      • toLowerCase

        public String toLowerCase()
        Converts the TSID into a canonical string in lower case.

        The output string is 13 characters long and contains only characters from Crockford's base 32 alphabet.

        It is faster shorthand for Tsid.toString().toLowerCase().

        Returns:
        a string
        See Also:
        Crockford's Base 32
      • getInstant

        public Instant getInstant()
        Returns the instant of creation.

        The instant of creation is extracted from the time component.

        Returns:
        Instant
      • getInstant

        public Instant getInstant​(Instant customEpoch)
        Returns the instant of creation.

        The instant of creation is extracted from the time component.

        Parameters:
        customEpoch - the custom epoch instant
        Returns:
        Instant
      • getUnixMilliseconds

        public long getUnixMilliseconds()
        Returns the time of creation in milliseconds since 1970-01-01.

        The time of creation is extracted from the time component.

        Returns:
        the number of milliseconds since 1970-01-01
      • getUnixMilliseconds

        public long getUnixMilliseconds​(long customEpoch)
        Returns the time of creation in milliseconds since 1970-01-01.

        The time of creation is extracted from the time component.

        Parameters:
        customEpoch - the custom epoch in milliseconds since 1970-01-01
        Returns:
        the number of milliseconds since 1970-01-01
      • isValid

        public static boolean isValid​(String string)
        Checks if the input string is valid.

        The input string must be 13 characters long and must contain only characters from Crockford's base 32 alphabet.

        The first character of the input string must be between 0 and F.

        Parameters:
        string - a string
        Returns:
        true if valid
      • hashCode

        public int hashCode()
        Returns a hash code value for the TSID.
        Overrides:
        hashCode in class Object
      • equals

        public boolean equals​(Object other)
        Checks if some other TSID is equal to this one.
        Overrides:
        equals in class Object
      • compareTo

        public int compareTo​(Tsid that)
        Compares two TSIDs as unsigned 64-bit integers.

        The first of two TSIDs is greater than the second if the most significant byte in which they differ is greater for the first TSID.

        Specified by:
        compareTo in interface Comparable<Tsid>
        Parameters:
        that - a TSID to be compared with
        Returns:
        -1, 0 or 1 as this is less than, equal to, or greater than that