Class UlidCreator

java.lang.Object
com.github.f4b6a3.ulid.UlidCreator

public final class UlidCreator extends Object
A class that generates ULIDs.

Both types of ULID can be easily created by this generator, i.e. monotonic and non-monotonic.

In addition, a "non-standard" hash-based ULID can also be generated, in which the random component is replaced with the first 10 bytes of an SHA-256 hash.

  • Method Details

    • getUlid

      public static Ulid getUlid()
      Returns a ULID.

      The random component is reset for each new ULID generated.

      Returns:
      a ULID
    • getUlid

      public static Ulid getUlid(long time)
      Returns a ULID.

      The random component is reset for each new ULID generated.

      Parameters:
      time - the current time in milliseconds, measured from the UNIX epoch of 1970-01-01T00:00Z (UTC)
      Returns:
      a ULID
    • getMonotonicUlid

      public static Ulid getMonotonicUlid()
      Returns a Monotonic ULID.

      The random component is incremented for each new ULID generated in the same millisecond.

      Returns:
      a ULID
    • getMonotonicUlid

      public static Ulid getMonotonicUlid(long time)
      Returns a Monotonic ULID.

      The random component is incremented for each new ULID generated in the same millisecond.

      Parameters:
      time - the current time in milliseconds, measured from the UNIX epoch of 1970-01-01T00:00Z (UTC)
      Returns:
      a ULID
    • getHashUlid

      public static Ulid getHashUlid(long time, String string)
      Returns a Hash ULID.

      The random component is replaced with the first 10 bytes of an SHA-256 hash.

      It always returns the same ULID for a specific pair of time and string.

      Usage example:

      
       long time = file.getCreatedAt();
       String name = file.getFileName();
       Ulid ulid = UlidCreator.getHashUlid(time, name);
       
      Parameters:
      time - the time in milliseconds, measured from the UNIX epoch of 1970-01-01T00:00Z (UTC)
      string - a string to be hashed using SHA-256 algorithm.
      Returns:
      a ULID
      Since:
      5.2.0
    • getHashUlid

      public static Ulid getHashUlid(long time, byte[] bytes)
      Returns a Hash ULID.

      The random component is replaced with the first 10 bytes of an SHA-256 hash.

      It always returns the same ULID for a specific pair of time and bytes.

      Usage example:

      
       long time = file.getCreatedAt();
       byte[] bytes = file.getFileBinary();
       Ulid ulid = UlidCreator.getHashUlid(time, bytes);
       
      Parameters:
      time - the time in milliseconds, measured from the UNIX epoch of 1970-01-01T00:00Z (UTC)
      bytes - a byte array to be hashed using SHA-256 algorithm.
      Returns:
      a ULID
      Since:
      5.2.0