Class NanoIdGenerator
java.lang.Object
cloud.opencode.base.id.nanoid.NanoIdGenerator
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordCollision analysis result 碰撞分析结果 -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault alphabet 默认字母表static final intDefault ID length 默认ID长度 -
Method Summary
Modifier and TypeMethodDescriptionanalyzeCollision(long countToGen, String alphabet) Analyzes collision risk and provides recommendations 分析碰撞风险并提供建议analyzeCollision(long countToGen, String alphabet, double targetProbability) Analyzes collision risk with custom target probability 使用自定义目标概率分析碰撞风险static NanoIdBuilderbuilder()Creates a builder for customized generator 创建用于自定义生成器的构建器static doublecollisionProbability(int size, String alphabet, long countToGen) Calculates collision probability 计算碰撞概率static NanoIdGeneratorcreate()Creates a default generator 创建默认生成器static NanoIdGeneratorcreate(int size) Creates a generator with specific size 使用指定长度创建生成器generate()Generates the next ID 生成下一个IDGets the alphabet 获取字母表intgetSize()Gets the ID length 获取ID长度getType()Gets the generator type name 获取生成器类型名称static StringGenerates a random NanoID with default settings 使用默认设置生成随机NanoIDstatic StringrandomNanoId(int size) Generates a random NanoID with specific size 使用指定长度生成随机NanoIDstatic StringrandomNanoId(int size, String alphabet) Generates a random NanoID with specific size and alphabet 使用指定长度和字母表生成随机NanoIDstatic intrecommendedSize(long countToGen, String alphabet, double targetProbability) Calculates the recommended ID size for a given number of IDs 计算给定ID数量的推荐ID长度Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface IdGenerator
generateBatch
-
Field Details
-
DEFAULT_SIZE
public static final int DEFAULT_SIZEDefault ID length 默认ID长度- See Also:
-
DEFAULT_ALPHABET
Default alphabet 默认字母表
-
-
Method Details
-
create
-
create
Creates a generator with specific size 使用指定长度创建生成器- Parameters:
size- the ID length | ID长度- Returns:
- generator | 生成器
-
builder
Creates a builder for customized generator 创建用于自定义生成器的构建器- Returns:
- builder | 构建器
-
randomNanoId
Generates a random NanoID with default settings 使用默认设置生成随机NanoID- Returns:
- NanoID string | NanoID字符串
-
randomNanoId
Generates a random NanoID with specific size 使用指定长度生成随机NanoID- Parameters:
size- the ID length | ID长度- Returns:
- NanoID string | NanoID字符串
-
randomNanoId
-
generate
Description copied from interface:IdGeneratorGenerates the next ID 生成下一个ID- Specified by:
generatein interfaceIdGenerator<String>- Returns:
- generated ID | 生成的ID
-
getSize
public int getSize()Gets the ID length 获取ID长度- Returns:
- length | 长度
-
getAlphabet
-
collisionProbability
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
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
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
Description copied from interface:IdGeneratorGets the generator type name 获取生成器类型名称- Specified by:
getTypein interfaceIdGenerator<String>- Returns:
- type name | 类型名称
-