Class RandomIdGenerator

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

public final class RandomIdGenerator extends Object implements IdGenerator<String>
Random ID Generator 随机ID生成器

Generates random string IDs using specified character set. Uses SecureRandom by default for cryptographic security.

使用指定字符集生成随机字符串ID。 默认使用SecureRandom以确保加密安全。

Features | 主要功能:

  • Configurable length - 可配置长度
  • Custom character set - 自定义字符集
  • Cryptographically secure - 加密安全

Usage Examples | 使用示例:

// Default alphanumeric
RandomIdGenerator gen = RandomIdGenerator.create(16);
String id = gen.generate();
// -> "aB3xK9mNp2QrStUv"

// Numeric only
RandomIdGenerator numGen = RandomIdGenerator.numeric(8);
String numId = numGen.generate();
// -> "12345678"

// Hex
RandomIdGenerator hexGen = RandomIdGenerator.hex(32);
String hexId = hexGen.generate();
// -> "a1b2c3d4e5f6..."

Security | 安全性:

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

  • Method Details

    • create

      public static RandomIdGenerator create(int length)
      Creates an alphanumeric generator 创建字母数字生成器
      Parameters:
      length - the ID length | ID长度
      Returns:
      generator | 生成器
    • numeric

      public static RandomIdGenerator numeric(int length)
      Creates a numeric only generator 创建仅数字生成器
      Parameters:
      length - the ID length | ID长度
      Returns:
      generator | 生成器
    • hex

      public static RandomIdGenerator hex(int length)
      Creates a hex generator 创建十六进制生成器
      Parameters:
      length - the ID length | ID长度
      Returns:
      generator | 生成器
    • hexUpper

      public static RandomIdGenerator hexUpper(int length)
      Creates a hex uppercase generator 创建十六进制大写生成器
      Parameters:
      length - the ID length | ID长度
      Returns:
      generator | 生成器
    • custom

      public static RandomIdGenerator custom(int length, String chars)
      Creates a generator with custom characters 使用自定义字符创建生成器
      Parameters:
      length - the ID length | ID长度
      chars - the character set | 字符集
      Returns:
      generator | 生成器
    • 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
    • getLength

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

      public String getChars()
      Gets the character set 获取字符集
      Returns:
      character set | 字符集
    • getType

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