Class ULID

java.lang.Object
org.miaixz.bus.core.data.ULID
All Implemented Interfaces:
Serializable, Comparable<ULID>

public class ULID extends Object implements Comparable<ULID>, Serializable
ULID(Universally Unique Lexicographically Sortable Identifier)通用唯一词典分类标识符,特性:
  • 与UUID的128位兼容性
  • 每毫秒1.21e + 24个唯一ULID
  • 按字典顺序(也就是字母顺序)排序
  • 规范地编码为26个字符串,而不是UUID的36个字符
  • 使用Crockford的base32获得更好的效率和可读性(每个字符5位)
  • 不区分大小写
  • 没有特殊字符(URL安全)
  • 单调排序顺序(正确检测并处理相同的毫秒)

参考:https://github.com/zjcscut/framework-mesh/blob/master/ulid4j/src/main/java/cn/vlts/ulid/ULID.java


   01AN4Z07BY      79KA1307SR9X4MV3
  |----------|    |----------------|
   Timestamp          Randomness
    48bits             80bits
 
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    ULID(No128 no128)
    构造
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    boolean
     
    long
    获取最低有效位(Least Significant Bit),64 bit(8 bytes)
    long
    获取最高有效位(Most Significant Bit),64 bit(8 bytes)
    byte[]
    获取ULID的随机数部分
    long
    获取ULID的时间戳部分
    int
     
    自增ULID
    nextMonotonic(long timestamp)
    获取下一个有序的ULID
    static ULID
    of()
    创建一个新的ULID,使用当前系统时间戳和随机数
    static ULID
    of(byte[] data)
    从bytes解析ULID
    static ULID
    of(long timestamp)
    创建一个新的ULID,使用指定系统时间戳和随机数
    static ULID
    of(long timestamp, byte[] randomness)
    创建一个新的ULID,使用指定系统时间戳和指定填充数
    static ULID
    of(long timestamp, Random random)
    创建一个新的ULID,使用指定系统时间戳和指定随机对象
    static ULID
    of(String ulidString)
    解析一个Crockford`s Base32的ULID
    byte[]
    转为bytes值,16位,0-7为mostSignificantBits,8-15为leastSignificantBits
    转为JDK的UUID
     
    转为UUID

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ULID

      public ULID(No128 no128)
      构造
      Parameters:
      no128 - 16位数字
  • Method Details

    • of

      public static ULID of()
      创建一个新的ULID,使用当前系统时间戳和随机数
      Returns:
      ULID
    • of

      public static ULID of(long timestamp)
      创建一个新的ULID,使用指定系统时间戳和随机数
      Parameters:
      timestamp - 时间戳
      Returns:
      ULID
    • of

      public static ULID of(long timestamp, Random random)
      创建一个新的ULID,使用指定系统时间戳和指定随机对象
      Parameters:
      timestamp - 时间戳
      random - Random
      Returns:
      ULID
    • of

      public static ULID of(long timestamp, byte[] randomness)
      创建一个新的ULID,使用指定系统时间戳和指定填充数
      Parameters:
      timestamp - 时间戳
      randomness - 指定填充数
      Returns:
      ULID
    • of

      public static ULID of(String ulidString)
      解析一个Crockford`s Base32的ULID
      Parameters:
      ulidString - Crockford`s Base32的ULID
      Returns:
      ULID
    • of

      public static ULID of(byte[] data)
      从bytes解析ULID
      Parameters:
      data - bytes,16位,0-7为mostSignificantBits,8-15为leastSignificantBits
      Returns:
      ULID
    • getMostSignificantBits

      public long getMostSignificantBits()
      获取最高有效位(Most Significant Bit),64 bit(8 bytes)
      Returns:
      最高有效位(Most Significant Bit),64 bit(8 bytes)
    • getLeastSignificantBits

      public long getLeastSignificantBits()
      获取最低有效位(Least Significant Bit),64 bit(8 bytes)
      Returns:
      最低有效位(Least Significant Bit),64 bit(8 bytes)
    • getTimestamp

      public long getTimestamp()
      获取ULID的时间戳部分
      Returns:
      时间戳
    • getRandomness

      public byte[] getRandomness()
      获取ULID的随机数部分
      Returns:
      随机数部分
    • increment

      public ULID increment()
      自增ULID
      Returns:
      返回自增ULID
    • nextMonotonic

      public ULID nextMonotonic(long timestamp)
      获取下一个有序的ULID
      Parameters:
      timestamp - 时间戳
      Returns:
      如果给定时间戳与当前ULID相同,则返回自增ULID,否则返回一个新的ULID
    • toBytes

      public byte[] toBytes()
      转为bytes值,16位,0-7为mostSignificantBits,8-15为leastSignificantBits
      Returns:
      bytes值
    • toUUID

      public UUID toUUID()
      转为UUID
      Returns:
      UUID
    • toJdkUUID

      public UUID toJdkUUID()
      转为JDK的UUID
      Returns:
      UUID
    • compareTo

      public int compareTo(ULID o)
      Specified by:
      compareTo in interface Comparable<ULID>
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object