All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nervousync.utils.IDUtils Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * 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.utils;

import org.nervousync.annotations.generator.GeneratorProvider;
import org.nervousync.commons.Globals;
import org.nervousync.generator.IGenerator;
import org.nervousync.generator.nano.NanoGenerator;
import org.nervousync.generator.snowflake.SnowflakeGenerator;
import org.nervousync.generator.uuid.UUIDGenerator;
import org.nervousync.generator.uuid.impl.*;
import org.nervousync.generator.uuid.timer.TimeSynchronizer;

import java.util.*;

/**
 * 

ID generator utilities

*

ID生成器工具集

* * @author Steven Wee [email protected] * @version $Revision: 1.2.0 $ $Date: Sep 13, 2017 11:27:28 $ */ public final class IDUtils { /** * Static value for provider name of UUIDv1 Generator * 静态值用于UUIDv1生成器的提供名称 */ public static final String UUIDv1 = "UUIDv1"; /** * Static value for provider name of UUIDv2 Generator * 静态值用于UUIDv2生成器的提供名称 */ public static final String UUIDv2 = "UUIDv2"; /** * Static value for provider name of UUIDv3 Generator * 静态值用于UUIDv3生成器的提供名称 */ public static final String UUIDv3 = "UUIDv3"; /** * Static value for provider name of UUIDv4 Generator * 静态值用于UUIDv4生成器的提供名称 */ public static final String UUIDv4 = "UUIDv4"; /** * Static value for provider name of UUIDv5 Generator * 静态值用于UUIDv5生成器的提供名称 */ public static final String UUIDv5 = "UUIDv5"; /** * Static value for provider name of NanoID Generator * 静态值用于NanoID生成器的提供名称 */ public static final String NANO_ID = "NanoID"; /** * Static value for provider name of Snowflake Generator * 静态值用于雪花算法生成器的提供名称 */ public static final String SNOWFLAKE = "Snowflake"; /** * Logger instance * 日志实例 */ private static final LoggerUtils.Logger LOGGER = LoggerUtils.getLogger(IDUtils.class); /** * Registered ID generator provider map * 已注册的ID生成器提供名称映射表 */ private static final Map> INITIALIZE_MAP = new HashMap<>(); static { // Using Java SPI to loading ID generator implements classes ServiceLoader.load(IGenerator.class) .forEach(iGenerator -> { Class generatorClass = iGenerator.getClass(); if (generatorClass.isAnnotationPresent(GeneratorProvider.class)) { INITIALIZE_MAP.put(generatorClass.getAnnotation(GeneratorProvider.class).value(), iGenerator); } }); if (LOGGER.isDebugEnabled()) { LOGGER.info("Names_Generator_Registered_ID_Info", String.join(", ", IDUtils.registeredGenerators().toArray(new String[0]))); } Runtime.getRuntime().addShutdownHook(new Thread(IDUtils::destroy)); } /** *

Private constructor for IDUtils

*

ID生成器工具集的私有构造方法

*/ private IDUtils() { } /** *

Static method for configure NanoID generator

*

静态方法用于设置NanoID生成器

* * @param alphabetConfig Alphabet configure string * 输出字符设置 * @param generateLength Generated result length * 生成结果的长度 */ public static void nanoConfig(final String alphabetConfig, final int generateLength) { if (INITIALIZE_MAP.containsKey(NANO_ID)) { synchronized (INITIALIZE_MAP) { NanoGenerator generator = (NanoGenerator) INITIALIZE_MAP.get(NANO_ID); generator.config(alphabetConfig, generateLength); INITIALIZE_MAP.put(NANO_ID, generator); } } } /** *

Static method for configure Snowflake 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 static void snowflakeConfig(final long referenceTime, final long deviceId, final long instanceId) { if (INITIALIZE_MAP.containsKey(SNOWFLAKE)) { synchronized (INITIALIZE_MAP) { SnowflakeGenerator generator = (SnowflakeGenerator) INITIALIZE_MAP.get(SNOWFLAKE); generator.config(referenceTime, deviceId, instanceId); INITIALIZE_MAP.put(SNOWFLAKE, generator); } } } /** *

Static method for configure UUIDv2 generator

*

静态方法用于设置UUIDv2生成器

* * @param synchronizer Time synchronizer instance * 时间同步器实例对象 */ public static void uuidConfig(final TimeSynchronizer synchronizer) { if (INITIALIZE_MAP.containsKey(UUIDv2)) { synchronized (INITIALIZE_MAP) { UUIDv2Generator generator = (UUIDv2Generator) INITIALIZE_MAP.get(UUIDv2); generator.config(synchronizer); INITIALIZE_MAP.put(UUIDv2, generator); } } } /** *

Static method for generate NanoID value

*

静态方法用于生成随机NanoID值

* * @return Generated value * 生成的值 */ public static String nano() { return Optional.ofNullable(INITIALIZE_MAP.get(NANO_ID)) .map(generator -> ((NanoGenerator) generator).generate()) .orElse(Globals.DEFAULT_VALUE_STRING); } /** *

Static method for generate Snowflake value

*

静态方法用于生成随机雪花算法值

* * @return Generated value * 生成的值 */ public static Long snowflake() { return Optional.ofNullable(INITIALIZE_MAP.get(SNOWFLAKE)) .map(generator -> ((SnowflakeGenerator) generator).generate()) .orElse(Globals.DEFAULT_VALUE_LONG); } /** *

Static method for generate UUIDv1 value

*

静态方法用于生成随机UUIDv1值

* * @return Generated value * 生成的值 */ public static String UUIDv1() { return Optional.ofNullable(INITIALIZE_MAP.get(UUIDv1)) .map(generator -> ((UUIDGenerator) generator).generate()) .orElse(Globals.DEFAULT_VALUE_STRING); } /** *

Static method for generate UUIDv2 value

*

静态方法用于生成随机UUIDv2值

* * @return Generated value * 生成的值 */ public static String UUIDv2() { return Optional.ofNullable(INITIALIZE_MAP.get(UUIDv2)) .map(generator -> ((UUIDGenerator) generator).generate()) .orElse(Globals.DEFAULT_VALUE_STRING); } /** *

Static method for generate UUIDv3 value

*

静态方法用于生成随机UUIDv3值

* * @param dataBytes Given parameter * 给定的参数 * * @return Generated value * 生成的值 */ public static String UUIDv3(final byte[] dataBytes) { return Optional.ofNullable(INITIALIZE_MAP.get(UUIDv3)) .map(generator -> ((UUIDGenerator) generator).generate(dataBytes)) .orElse(Globals.DEFAULT_VALUE_STRING); } /** *

Static method for generate UUIDv4 value

*

静态方法用于生成随机UUIDv4值

* * @return Generated value * 生成的值 */ public static String UUIDv4() { return Optional.ofNullable(INITIALIZE_MAP.get(UUIDv4)) .map(generator -> ((UUIDGenerator) generator).generate()) .orElse(Globals.DEFAULT_VALUE_STRING); } /** *

Static method for generate UUIDv5 value

*

静态方法用于生成随机UUIDv5值

* * @param dataBytes Given parameter * 给定的参数 * * @return Generated value * 生成的值 */ public static String UUIDv5(final byte[] dataBytes) { return Optional.ofNullable(INITIALIZE_MAP.get(UUIDv5)) .map(generator -> ((UUIDGenerator) generator).generate(dataBytes)) .orElse(Globals.DEFAULT_VALUE_STRING); } /** *

Read the registered generator name list

*

读取已注册的生成器提供名列表

* * @return Registered generator name list * 注册的生成器名称列表 */ public static List registeredGenerators() { return new ArrayList<>(INITIALIZE_MAP.keySet()); } /** *

Destroy all registered generator instance and clear map

*

销毁所有已注册的生成器实例对象并清空映射表

*/ public static void destroy() { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Destroy_Generator_Registered_ID_Debug"); } INITIALIZE_MAP.values().forEach(IGenerator::destroy); INITIALIZE_MAP.clear(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy