Class IdGenerator

  • All Implemented Interfaces:
    java.io.Externalizable, java.io.Serializable

    public class IdGenerator
    extends java.lang.Object
    implements java.io.Externalizable
    IdGenerator generates 27-character, time-ordered, k-sortable, URL-safe, globally unique identifiers.

    The identifiers are encoded with Radix-64, using an alphabet which is both URL-safe and which preserves lexical ordering. Each ID consists of a 32-bit, big-endian timestamp (the number of seconds since 1.4e9 seconds after the Unix epoch), plus 128 bits of random data.

    Random data is produced via ChaCha20 in a fast-key-erasure construction.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String MAX_VALUE
      Lexically, the last possible ID.
      static java.lang.String MIN_VALUE
      Lexically, the first possible ID.
    • Method Summary

      Modifier and Type Method Description
      static java.time.Instant createdAt​(java.lang.String id)
      Returns the Instant at which the ID was created.
      java.lang.String generate()
      Generates a new ID.
      void readExternal​(java.io.ObjectInput in)  
      void writeExternal​(java.io.ObjectOutput out)  
      • Methods inherited from class java.lang.Object

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

      • MIN_VALUE

        public static final java.lang.String MIN_VALUE
        Lexically, the first possible ID.
        See Also:
        Constant Field Values
      • MAX_VALUE

        public static final java.lang.String MAX_VALUE
        Lexically, the last possible ID.
        See Also:
        Constant Field Values
    • Constructor Detail

      • IdGenerator

        public IdGenerator()
        Creates a new IdGenerator.
    • Method Detail

      • generate

        public java.lang.String generate()
        Generates a new ID.
        Returns:
        a new 27-character ID
      • createdAt

        public static java.time.Instant createdAt​(java.lang.String id)
        Returns the Instant at which the ID was created.
        Parameters:
        id - a time ID
        Returns:
        the ID's timestamp
      • writeExternal

        public void writeExternal​(java.io.ObjectOutput out)
        Specified by:
        writeExternal in interface java.io.Externalizable
      • readExternal

        public void readExternal​(java.io.ObjectInput in)
        Specified by:
        readExternal in interface java.io.Externalizable