类 Ulid

java.lang.Object
cn.vonce.sql.uitls.Ulid
所有已实现的接口:
Serializable, Comparable<Ulid>

public final class Ulid extends Object implements Serializable, Comparable<Ulid>
A class that represents ULIDs.

ULID is a 128-bit value that has two components:

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

ULID has 128-bit compatibility with UUID. Like a UUID, a ULID can also be stored as a 16-byte array.

Instances of this class are immutable.

另请参阅:
  • 字段概要

    字段
    修饰符和类型
    字段
    说明
    static final int
    Number of bytes of the random component of a ULID.
    static final int
    Number of characters of the random component of a ULID.
    static final int
    Number of bytes of the time component of a ULID.
    static final int
    Number of characters of the time component of a ULID.
    static final int
    Number of bytes of a ULID.
    static final int
    Number of characters of a ULID.
  • 构造器概要

    构造器
    构造器
    说明
    Ulid(long time, byte[] random)
    Creates a new ULID.
    Ulid(long mostSignificantBits, long leastSignificantBits)
    Creates a new ULID.
    Ulid(Ulid ulid)
    Creates a new ULID.
  • 方法概要

    修饰符和类型
    方法
    说明
    int
    Compares two ULIDs as unsigned 128-bit integers.
    boolean
    equals(Object other)
    Checks if some other ULID is equal to this one.
    static Ulid
    Returns a fast new ULID.
    static Ulid
    from(byte[] bytes)
    Converts a byte array into a ULID.
    static Ulid
    from(String string)
    Converts a canonical string into a ULID.
    static Ulid
    from(UUID uuid)
    Converts a UUID into a ULID.
    java.time.Instant
    Returns the instant of creation.
    static java.time.Instant
    Returns the instant of creation.
    long
    Returns the least significant bits as a number.
    long
    Returns the most significant bits as a number.
    byte[]
    Returns the random component as a byte array.
    static byte[]
    getRandom(String string)
    Returns the random component as a byte array.
    long
    Returns the time component as a number.
    static long
    getTime(String string)
    Returns the time component as a number.
    int
    Returns a hash code value for the ULID.
    Returns a new ULID by incrementing the random component of the current ULID.
    static boolean
    isValid(String string)
    Checks if the input string is valid.
    byte[]
    Convert the ULID into a byte array.
    Converts the ULID into a canonical string in lower case.
    Converts the ULID into into another ULID that is compatible with UUIDv4.
    Converts the ULID into a canonical string in upper case.
    Convert the ULID into a UUID.

    从类继承的方法 java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • 字段详细资料

    • ULID_CHARS

      public static final int ULID_CHARS
      Number of characters of a ULID.
      另请参阅:
    • TIME_CHARS

      public static final int TIME_CHARS
      Number of characters of the time component of a ULID.
      另请参阅:
    • RANDOM_CHARS

      public static final int RANDOM_CHARS
      Number of characters of the random component of a ULID.
      另请参阅:
    • ULID_BYTES

      public static final int ULID_BYTES
      Number of bytes of a ULID.
      另请参阅:
    • TIME_BYTES

      public static final int TIME_BYTES
      Number of bytes of the time component of a ULID.
      另请参阅:
    • RANDOM_BYTES

      public static final int RANDOM_BYTES
      Number of bytes of the random component of a ULID.
      另请参阅:
  • 构造器详细资料

    • Ulid

      public Ulid(Ulid ulid)
      Creates a new ULID.

      Useful to make copies of ULIDs.

      参数:
      ulid - a ULID
    • Ulid

      public Ulid(long mostSignificantBits, long leastSignificantBits)
      Creates a new ULID.

      If you want to make a copy of a UUID, use from(UUID) instead.

      参数:
      mostSignificantBits - the first 8 bytes as a long value
      leastSignificantBits - the last 8 bytes as a long value
    • Ulid

      public Ulid(long time, byte[] random)
      Creates a new ULID.

      Time parameter is the number of milliseconds since 1970-01-01 (Unix epoch). It must be a positive number not larger than 2^48-1.

      Random parameter must be an array of 10 bytes.

      参数:
      time - the the number of milliseconds since 1970-01-01
      random - an array of 10 bytes
      抛出:
      IllegalArgumentException - if time is negative or larger than 2^48-1
      IllegalArgumentException - if random is null or its length is not 10
  • 方法详细资料

    • fast

      public static Ulid fast()
      Returns a fast new ULID.

      This static method is a quick alternative to UlidCreator.getUlid().

      It employs SplittableRandom which works very well, although not cryptographically strong.

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

      返回:
      a ULID
      从以下版本开始:
      5.1.0
    • from

      public static Ulid from(UUID uuid)
      Converts a UUID into a ULID.
      参数:
      uuid - a UUID
      返回:
      a ULID
    • from

      public static Ulid from(byte[] bytes)
      Converts a byte array into a ULID.
      参数:
      bytes - an array of 16 bytes
      返回:
      a ULID
      抛出:
      IllegalArgumentException - if bytes are null or its length is not 16
    • from

      public static Ulid from(String string)
      Converts a canonical string into a ULID.

      The input string must be 26 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 7.

      参数:
      string - a canonical string
      返回:
      a ULID
      抛出:
      IllegalArgumentException - if the input string is invalid
      另请参阅:
    • toUuid

      public UUID toUuid()
      Convert the ULID into a UUID.

      A ULID has 128-bit compatibility with a UUID.

      If you need a RFC-4122 UUIDv4 do this: Ulid.toRfc4122().toUuid().

      返回:
      a UUID.
    • toBytes

      public byte[] toBytes()
      Convert the ULID into a byte array.
      返回:
      an byte array.
    • toString

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

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

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

      覆盖:
      toString 在类中 Object
      返回:
      a ULID string
      另请参阅:
    • toLowerCase

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

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

      It is a shorthand at least twice as fast as Ulid.toString().toLowerCase().

      返回:
      a string
      另请参阅:
    • toRfc4122

      public Ulid toRfc4122()
      Converts the ULID into into another ULID that is compatible with UUIDv4.

      The bytes of the returned ULID are compliant with the RFC-4122 version 4.

      If you need a RFC-4122 UUIDv4 do this: Ulid.toRfc4122().toUuid().

      Note: If you use this method, you can not get the original ULID, since it changes 6 bits of it to generate a UUIDv4.

      返回:
      a ULID
      另请参阅:
    • getInstant

      public java.time.Instant getInstant()
      Returns the instant of creation.

      The instant of creation is extracted from the time component.

      返回:
      the Instant of creation
    • getInstant

      public static java.time.Instant getInstant(String string)
      Returns the instant of creation.

      The instant of creation is extracted from the time component.

      参数:
      string - a canonical string
      返回:
      the Instant of creation
      抛出:
      IllegalArgumentException - if the input string is invalid
    • getTime

      public long getTime()
      Returns the time component as a number.

      The time component is a number between 0 and 2^48-1. It is equivalent to the count of milliseconds since 1970-01-01 (Unix epoch).

      返回:
      a number of milliseconds
    • getTime

      public static long getTime(String string)
      Returns the time component as a number.

      The time component is a number between 0 and 2^48-1. It is equivalent to the count of milliseconds since 1970-01-01 (Unix epoch).

      参数:
      string - a canonical string
      返回:
      a number of milliseconds
      抛出:
      IllegalArgumentException - if the input string is invalid
    • getRandom

      public byte[] getRandom()
      Returns the random component as a byte array.

      The random component is an array of 10 bytes (80 bits).

      返回:
      a byte array
    • getRandom

      public static byte[] getRandom(String string)
      Returns the random component as a byte array.

      The random component is an array of 10 bytes (80 bits).

      参数:
      string - a canonical string
      返回:
      a byte array
      抛出:
      IllegalArgumentException - if the input string is invalid
    • getMostSignificantBits

      public long getMostSignificantBits()
      Returns the most significant bits as a number.
      返回:
      a number.
    • getLeastSignificantBits

      public long getLeastSignificantBits()
      Returns the least significant bits as a number.
      返回:
      a number.
    • increment

      public Ulid increment()
      Returns a new ULID by incrementing the random component of the current ULID.

      Since the random component contains 80 bits:

      • (1) This method can generate up to 1208925819614629174706176 (2^80) ULIDs per millisecond;
      • (2) This method can generate monotonic increasing ULIDs 99.999999999999992% ((2^80 - 10^9) / (2^80)) of the time within a single millisecond interval, considering an unrealistic rate of 1,000,000,000 ULIDs per millisecond.

      Due to (1) and (2), it does not throw the error message recommended by the specification. When an overflow occurs in the random 80 bits, the time component is simply incremented to maintain monotonicity.

      返回:
      a ULID
    • isValid

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

      The input string must be 26 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 7.

      参数:
      string - a canonical string
      返回:
      true if the input string is valid
      另请参阅:
    • hashCode

      public int hashCode()
      Returns a hash code value for the ULID.
      覆盖:
      hashCode 在类中 Object
    • equals

      public boolean equals(Object other)
      Checks if some other ULID is equal to this one.
      覆盖:
      equals 在类中 Object
    • compareTo

      public int compareTo(Ulid that)
      Compares two ULIDs as unsigned 128-bit integers.

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

      指定者:
      compareTo 在接口中 Comparable<Ulid>
      参数:
      that - a ULID to be compared with
      返回:
      -1, 0 or 1 as this is less than, equal to, or greater than that