org.nervousync.generator.snowflake.SnowflakeGenerator 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.snowflake;
import org.nervousync.annotations.generator.GeneratorProvider;
import org.nervousync.commons.Globals;
import org.nervousync.generator.IGenerator;
import org.nervousync.utils.DateTimeUtils;
import org.nervousync.utils.IDUtils;
import org.nervousync.utils.LoggerUtils;
/**
* SnowflakeID generator
* 雪花ID生成器
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0.0 $ $Date: Jul 06, 2022 12:44:27 $
*/
@GeneratorProvider(IDUtils.SNOWFLAKE)
public final class SnowflakeGenerator implements IGenerator {
/**
* Logger instance
* 日志实例
*/
private final LoggerUtils.Logger logger = LoggerUtils.getLogger(this.getClass());
/**
* Default value of ID
* 默认的ID值
*/
private static final long DEFAULT_ID = 1L;
/**
* Sequence mask code, sequence id bits: 12
* 序号掩码值,序号ID占用位数:12
*/
private static final long SEQUENCE_MASK = ~(-1L << 12L);
/**
* Node device ID (between 0 and 63), default value: 1L
* 节点的机器ID(取值范围:0到63),默认值:1L
*/
private long deviceId = DEFAULT_ID;
/**
* Node instance ID (between 0 and 63), default value: 1L
* 节点的实例ID(取值范围:0到63),默认值:1L
*/
private long instanceId = DEFAULT_ID;
/**
* Reference time, default value: 1303315200000L
* 起始时间戳,默认值:1303315200000L
*/
private long referenceTime = Globals.DEFAULT_REFERENCE_TIME;
/**
* Sequence index of current time
* 当前时间的序列索引
*/
private long sequenceIndex = 0L;
/**
* Previous generate time
* 上次生成ID的时间
*/
private long lastTime = Globals.DEFAULT_VALUE_LONG;
/**
* Configure current generator
* 修改当前生成器的配置
*
* @param referenceTime Reference time, default value: 1303315200000L
* 起始时间戳,默认值:1303315200000L
* @param deviceId Node device ID (between 0 and 63), default value: 1L
* 节点的机器ID(取值范围:0到63),默认值:1L
* @param instanceId Node instance ID (between 0 and 63), default value: 1L
* 节点的实例ID(取值范围:0到63),默认值:1L
*/
public void config(final long referenceTime, final long deviceId, final long instanceId) {
this.referenceTime = (referenceTime >= 0L) ? referenceTime : Globals.DEFAULT_REFERENCE_TIME;
this.deviceId = (deviceId >= 0L && deviceId <= 64L) ? deviceId : DEFAULT_ID;
this.instanceId = (instanceId >= 0L && instanceId <= 64L) ? instanceId : DEFAULT_ID;
this.sequenceIndex = 0L;
if (this.logger.isDebugEnabled()) {
this.logger.debug("Config_Snowflake_Error",
this.referenceTime, this.deviceId, this.instanceId);
}
}
/**
* Generate ID value
* 生成ID值
*
* @return Generated value
* 生成的ID值
*/
@Override
public Long generate() {
long currentTime = DateTimeUtils.currentUTCTimeMillis();
if (currentTime < this.lastTime) {
throw new RuntimeException(
String.format("System clock moved backwards. Refusing to generate id for %d milliseconds",
this.lastTime - currentTime));
}
if (currentTime == this.lastTime) {
this.sequenceIndex = (this.sequenceIndex + 1) & SEQUENCE_MASK;
if (this.sequenceIndex == 0) {
while (true) {
if ((currentTime = DateTimeUtils.currentUTCTimeMillis()) > this.lastTime) {
break;
}
}
}
} else {
this.sequenceIndex = 0L;
}
this.lastTime = currentTime;
if (this.logger.isDebugEnabled()) {
this.logger.debug("Generate_Snowflake_Debug",
this.lastTime, this.referenceTime, this.deviceId, this.instanceId, this.sequenceIndex);
}
return ((this.lastTime - this.referenceTime) << 22L)
| (this.deviceId << 17L) | (this.instanceId << 12L) | this.sequenceIndex;
}
/**
* Generate ID value using given parameter
* 使用给定的参数生成ID值
*
* @param dataBytes Given parameter
* 给定的参数
*
* @return Generated value
* 生成的ID值
*/
@Override
public Long generate(byte[] dataBytes) {
return this.generate();
}
/**
* Destroy current generator instance
* 销毁当前生成器实例对象
*/
@Override
public void destroy() {
}
}