org.nervousync.generator.nano.NanoGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-jdk11 Show documentation
Show all versions of utils-jdk11 Show documentation
Java utility collections, development by Nervousync Studio (NSYC)
/*
* Licensed to the Nervousync Studio (NSYC) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nervousync.generator.nano;
import org.nervousync.annotations.generator.GeneratorProvider;
import org.nervousync.generator.IGenerator;
import org.nervousync.utils.IDUtils;
import org.nervousync.utils.LoggerUtils;
import org.nervousync.utils.StringUtils;
import java.security.SecureRandom;
/**
* NanoID generator
* NanoID生成器
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0.0 $ $Date: Jul 06, 2022 12:39:54 $
*/
@GeneratorProvider(IDUtils.NANO_ID)
public final class NanoGenerator implements IGenerator {
/**
* Logger instance
* 日志实例
*/
private final LoggerUtils.Logger logger = LoggerUtils.getLogger(this.getClass());
/**
* Default alphabet string
* 默认的字母表
*/
private static final String DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz";
/**
* Default length of generated result
* 默认的生成结果长度
*/
private static final int DEFAULT_LENGTH = 27;
/**
* Secure Random instance
* 安全随机数对象
*/
private final SecureRandom secureRandom = new SecureRandom();
/**
* Result used alphabet character array
* 结果用到的字母字符数组
*/
private char[] alphabetArray = DEFAULT_ALPHABET.toCharArray();
/**
* Generated result length, default length: 27
* 生成结果的长度,默认值:27
*/
private int generateLength = DEFAULT_LENGTH;
/**
* Configure current generator
* 修改当前生成器的配置
*
* @param alphabetConfig Alphabet configure string
* 输出字符设置
* @param generateLength Generated result length
* 生成结果的长度
*/
public void config(final String alphabetConfig, final int generateLength) {
if (StringUtils.notBlank(alphabetConfig)) {
if (alphabetConfig.length() > 255) {
this.logger.error("Alphabet_Nano_Error");
} else {
this.alphabetArray = alphabetConfig.toCharArray();
}
}
this.generateLength = generateLength > 0 ? generateLength : DEFAULT_LENGTH;
}
/**
* Generate ID value
* 生成ID值
*
* @return Generated value
* 生成的ID值
*/
@Override
public String generate() {
final int mask = (2 << (int) Math.floor(Math.log(this.alphabetArray.length - 1) / Math.log(2))) - 1;
final int length = (int) Math.ceil(1.6 * mask * this.generateLength / this.alphabetArray.length);
final StringBuilder idBuilder = new StringBuilder();
while (true) {
final byte[] dataBytes = new byte[length];
this.secureRandom.nextBytes(dataBytes);
for (int i = 0; i < length; i++) {
final int alphabetIndex = dataBytes[i] & mask;
if (alphabetIndex < this.alphabetArray.length) {
idBuilder.append(this.alphabetArray[alphabetIndex]);
if (idBuilder.length() == this.generateLength) {
return idBuilder.toString();
}
}
}
}
}
/**
* Generate ID value using given parameter
* 使用给定的参数生成ID值
*
* @param dataBytes Given parameter
* 给定的参数
*
* @return Generated value
* 生成的ID值
*/
@Override
public String generate(byte[] dataBytes) {
return this.generate();
}
/**
* Destroy current generator instance
* 销毁当前生成器实例对象
*/
@Override
public void destroy() {
}
}