Class NanoIdGenerator

java.lang.Object
cloud.opencode.base.id.nanoid.NanoIdGenerator
All Implemented Interfaces:
IdGenerator<String>

public final class NanoIdGenerator extends Object implements IdGenerator<String>
NanoID Generator NanoID生成器

Generates compact, URL-friendly unique string identifiers. Default length is 21 characters with 126 bits of entropy.

生成紧凑、URL友好的唯一字符串标识符。 默认长度为21个字符,具有126位熵。

Features | 主要功能:

  • URL-safe by default - 默认URL安全
  • Configurable length - 可配置长度
  • Custom alphabet support - 自定义字母表支持
  • Cryptographically secure - 加密安全

Usage Examples | 使用示例:

// Default 21 characters
NanoIdGenerator gen = NanoIdGenerator.create();
String id = gen.generate();
// -> "V1StGXR8_Z5jdHi6B-myT"

// Custom length
String shortId = NanoIdGenerator.randomNanoId(10);

// Custom alphabet
String numericId = NanoIdGenerator.randomNanoId(8, "0123456789");

Performance | 性能特性:

  • ~1.5M ops/sec single thread - 单线程约1.5M次/秒
  • ~10M ops/sec with 8 threads - 8线程约10M次/秒

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-id V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • DEFAULT_SIZE

      public static final int DEFAULT_SIZE
      Default ID length 默认ID长度
      See Also:
    • DEFAULT_ALPHABET

      public static final String DEFAULT_ALPHABET
      Default alphabet 默认字母表
  • Method Details

    • create

      public static NanoIdGenerator create()
      Creates a default generator 创建默认生成器
      Returns:
      generator | 生成器
    • create

      public static NanoIdGenerator create(int size)
      Creates a generator with specific size 使用指定长度创建生成器
      Parameters:
      size - the ID length | ID长度
      Returns:
      generator | 生成器
    • builder

      public static NanoIdBuilder builder()
      Creates a builder for customized generator 创建用于自定义生成器的构建器
      Returns:
      builder | 构建器
    • randomNanoId

      public static String randomNanoId()
      Generates a random NanoID with default settings 使用默认设置生成随机NanoID
      Returns:
      NanoID string | NanoID字符串
    • randomNanoId

      public static String randomNanoId(int size)
      Generates a random NanoID with specific size 使用指定长度生成随机NanoID
      Parameters:
      size - the ID length | ID长度
      Returns:
      NanoID string | NanoID字符串
    • randomNanoId

      public static String randomNanoId(int size, String alphabet)
      Generates a random NanoID with specific size and alphabet 使用指定长度和字母表生成随机NanoID
      Parameters:
      size - the ID length | ID长度
      alphabet - the character set | 字符集
      Returns:
      NanoID string | NanoID字符串
    • generate

      public String generate()
      Description copied from interface: IdGenerator
      Generates the next ID 生成下一个ID
      Specified by:
      generate in interface IdGenerator<String>
      Returns:
      generated ID | 生成的ID
    • getSize

      public int getSize()
      Gets the ID length 获取ID长度
      Returns:
      length | 长度
    • getAlphabet

      public String getAlphabet()
      Gets the alphabet 获取字母表
      Returns:
      alphabet | 字母表
    • collisionProbability

      public static double collisionProbability(int size, String alphabet, long countToGen)
      Calculates collision probability 计算碰撞概率
      Parameters:
      size - the ID length | ID长度
      alphabet - the character set | 字符集
      countToGen - the number of IDs to generate | 要生成的ID数量
      Returns:
      collision probability | 碰撞概率
    • analyzeCollision

      public static NanoIdGenerator.CollisionAnalysis analyzeCollision(long countToGen, String alphabet)
      Analyzes collision risk and provides recommendations 分析碰撞风险并提供建议
      Parameters:
      countToGen - the expected number of IDs to generate | 预期生成的ID数量
      alphabet - the character set | 字符集
      Returns:
      collision analysis result | 碰撞分析结果
    • analyzeCollision

      public static NanoIdGenerator.CollisionAnalysis analyzeCollision(long countToGen, String alphabet, double targetProbability)
      Analyzes collision risk with custom target probability 使用自定义目标概率分析碰撞风险
      Parameters:
      countToGen - the expected number of IDs to generate | 预期生成的ID数量
      alphabet - the character set | 字符集
      targetProbability - the target collision probability | 目标碰撞概率
      Returns:
      collision analysis result | 碰撞分析结果
    • recommendedSize

      public static int recommendedSize(long countToGen, String alphabet, double targetProbability)
      Calculates the recommended ID size for a given number of IDs 计算给定ID数量的推荐ID长度
      Parameters:
      countToGen - the expected number of IDs | 预期的ID数量
      alphabet - the character set | 字符集
      targetProbability - the target collision probability | 目标碰撞概率
      Returns:
      recommended size | 推荐长度
    • getType

      public String getType()
      Description copied from interface: IdGenerator
      Gets the generator type name 获取生成器类型名称
      Specified by:
      getType in interface IdGenerator<String>
      Returns:
      type name | 类型名称