Class RandomIdGenerator
java.lang.Object
cloud.opencode.base.id.simple.RandomIdGenerator
- All Implemented Interfaces:
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 Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic RandomIdGeneratorcreate(int length) Creates an alphanumeric generator 创建字母数字生成器static RandomIdGeneratorCreates a generator with custom characters 使用自定义字符创建生成器generate()Generates the next ID 生成下一个IDgetChars()Gets the character set 获取字符集intGets the ID length 获取ID长度getType()Gets the generator type name 获取生成器类型名称static RandomIdGeneratorhex(int length) Creates a hex generator 创建十六进制生成器static RandomIdGeneratorhexUpper(int length) Creates a hex uppercase generator 创建十六进制大写生成器static RandomIdGeneratornumeric(int length) Creates a numeric only generator 创建仅数字生成器Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface IdGenerator
generateBatch
-
Field Details
-
ALPHANUMERIC
-
NUMERIC
-
HEX_LOWER
-
HEX_UPPER
-
-
Method Details
-
create
Creates an alphanumeric generator 创建字母数字生成器- Parameters:
length- the ID length | ID长度- Returns:
- generator | 生成器
-
numeric
Creates a numeric only generator 创建仅数字生成器- Parameters:
length- the ID length | ID长度- Returns:
- generator | 生成器
-
hex
Creates a hex generator 创建十六进制生成器- Parameters:
length- the ID length | ID长度- Returns:
- generator | 生成器
-
hexUpper
Creates a hex uppercase generator 创建十六进制大写生成器- Parameters:
length- the ID length | ID长度- Returns:
- generator | 生成器
-
custom
Creates a generator with custom characters 使用自定义字符创建生成器- Parameters:
length- the ID length | ID长度chars- the character set | 字符集- Returns:
- generator | 生成器
-
generate
Description copied from interface:IdGeneratorGenerates the next ID 生成下一个ID- Specified by:
generatein interfaceIdGenerator<String>- Returns:
- generated ID | 生成的ID
-
getLength
public int getLength()Gets the ID length 获取ID长度- Returns:
- length | 长度
-
getChars
-
getType
Description copied from interface:IdGeneratorGets the generator type name 获取生成器类型名称- Specified by:
getTypein interfaceIdGenerator<String>- Returns:
- type name | 类型名称
-