forked from plusone/plusone-commons
Compare commits
4 Commits
491499d265
...
4fafc66391
Author | SHA1 | Date |
---|---|---|
ZhouXY108 | 4fafc66391 | |
ZhouXY108 | 26bae49d47 | |
ZhouXY108 | 4ec59e7fd7 | |
ZhouXY108 | 8871f35749 |
|
@ -1,7 +1,7 @@
|
||||||
[ ] 未开始测试 - 15 (21.43%)
|
[ ] 未开始测试 - 10 (14.93%)
|
||||||
[-] 测试未完成 - 10 (14.29%)
|
[-] 测试未完成 - 9 (13.43%)
|
||||||
[Y] 测试完成 - 24 (34.29%)
|
[Y] 测试完成 - 27 (40.30%)
|
||||||
[x] 无需测试 - 21 (30.00%)
|
[x] 无需测试 - 21 (31.34%)
|
||||||
|
|
||||||
xyz.zhouxy.plusone.commons
|
xyz.zhouxy.plusone.commons
|
||||||
├───annotation
|
├───annotation
|
||||||
|
@ -20,7 +20,6 @@ xyz.zhouxy.plusone.commons
|
||||||
│ IWithCode.java [Y]
|
│ IWithCode.java [Y]
|
||||||
│ IWithIntCode.java [Y]
|
│ IWithIntCode.java [Y]
|
||||||
│ IWithLongCode.java [Y]
|
│ IWithLongCode.java [Y]
|
||||||
│ JRE.java [ ]
|
|
||||||
│ LongRef.java [Y]
|
│ LongRef.java [Y]
|
||||||
│ Ref.java [Y]
|
│ Ref.java [Y]
|
||||||
│
|
│
|
||||||
|
@ -28,7 +27,6 @@ xyz.zhouxy.plusone.commons
|
||||||
│ AbstractMapWrapper.java [ ]
|
│ AbstractMapWrapper.java [ ]
|
||||||
│ CollectionTools.java [Y]
|
│ CollectionTools.java [Y]
|
||||||
│ MapWrapper.java [ ]
|
│ MapWrapper.java [ ]
|
||||||
│ SafeConcurrentHashMap.java [ ]
|
|
||||||
│
|
│
|
||||||
├───constant
|
├───constant
|
||||||
│ PatternConsts.java [ ]
|
│ PatternConsts.java [ ]
|
||||||
|
@ -86,16 +84,15 @@ xyz.zhouxy.plusone.commons
|
||||||
ArrayTools.java [-]
|
ArrayTools.java [-]
|
||||||
AssertTools.java [Y]
|
AssertTools.java [Y]
|
||||||
BigDecimals.java [Y]
|
BigDecimals.java [Y]
|
||||||
ConcurrentHashMapTools.java [-]
|
|
||||||
DateTimeTools.java [-]
|
DateTimeTools.java [-]
|
||||||
Enumeration.java [Y]
|
Enumeration.java [Y]
|
||||||
EnumTools.java [Y]
|
EnumTools.java [Y]
|
||||||
IdGenerator.java [ ]
|
IdGenerator.java [Y]
|
||||||
IdWorker.java [ ]
|
IdWorker.java [Y]
|
||||||
Numbers.java [Y]
|
Numbers.java [Y]
|
||||||
OptionalTools.java [Y]
|
OptionalTools.java [Y]
|
||||||
RandomTools.java [ ]
|
RandomTools.java [ ]
|
||||||
RegexTools.java [ ]
|
RegexTools.java [ ]
|
||||||
SnowflakeIdGenerator.java [ ]
|
SnowflakeIdGenerator.java [Y]
|
||||||
StringTools.java [Y]
|
StringTools.java [Y]
|
||||||
TreeBuilder.java [Y]
|
TreeBuilder.java [Y]
|
||||||
|
|
|
@ -1,63 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2023-2024 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed 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
|
|
||||||
*
|
|
||||||
* https://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 xyz.zhouxy.plusone.commons.base;
|
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JRE version
|
|
||||||
*/
|
|
||||||
public class JRE {
|
|
||||||
|
|
||||||
private static final int JAVA_8 = 8;
|
|
||||||
|
|
||||||
public static final int CURRENT_VERSION = getJre();
|
|
||||||
|
|
||||||
public static boolean isJava8() {
|
|
||||||
return CURRENT_VERSION == JAVA_8;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int getJre() {
|
|
||||||
String version = System.getProperty("java.version");
|
|
||||||
boolean isNotBlank = StringTools.isNotBlank(version);
|
|
||||||
if (isNotBlank && version.startsWith("1.8")) {
|
|
||||||
return JAVA_8;
|
|
||||||
}
|
|
||||||
// if JRE version is 9 or above, we can get version from
|
|
||||||
// java.lang.Runtime.version()
|
|
||||||
try {
|
|
||||||
return getMajorVersion(version);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// assuming that JRE version is 8.
|
|
||||||
}
|
|
||||||
// default java 8
|
|
||||||
return JAVA_8;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int getMajorVersion(String version) {
|
|
||||||
if (version.startsWith("1.")) {
|
|
||||||
return Integer.parseInt(version.substring(2, 3));
|
|
||||||
} else {
|
|
||||||
int dotIndex = version.indexOf(".");
|
|
||||||
return (dotIndex != -1) ? Integer.parseInt(version.substring(0, dotIndex)) : Integer.parseInt(version);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private JRE() {
|
|
||||||
throw new IllegalStateException("Utility class");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -23,7 +23,6 @@ import java.util.Map.Entry;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
@ -33,7 +32,6 @@ import javax.annotation.Nullable;
|
||||||
import com.google.common.annotations.Beta;
|
import com.google.common.annotations.Beta;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AbstractMapWrapper
|
* AbstractMapWrapper
|
||||||
|
@ -159,12 +157,7 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
if (this.map instanceof ConcurrentHashMap) {
|
return this.map.computeIfAbsent(key, func);
|
||||||
return ConcurrentHashMapTools.computeIfAbsent(
|
|
||||||
(ConcurrentHashMap<K, V>) this.map, key, func);
|
|
||||||
} else {
|
|
||||||
return this.map.computeIfAbsent(key, func);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final Map<K, V> exportMap() {
|
public final Map<K, V> exportMap() {
|
||||||
|
|
|
@ -1,121 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2023-2024 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed 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
|
|
||||||
*
|
|
||||||
* https://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 xyz.zhouxy.plusone.commons.collection;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.base.JRE;
|
|
||||||
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SafeConcurrentHashMap
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
|
||||||
* 使用 Java 8 时,可使用这个类进行替换。
|
|
||||||
*
|
|
||||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
|
||||||
* @since 1.0
|
|
||||||
* @see ConcurrentHashMap
|
|
||||||
* @see ConcurrentHashMapTools#computeIfAbsentForJava8(ConcurrentHashMap, Object, Function)
|
|
||||||
*/
|
|
||||||
@ThreadSafe
|
|
||||||
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 4352954948768449595L;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new, empty map with the default initial table size (16).
|
|
||||||
*/
|
|
||||||
public SafeConcurrentHashMap() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new, empty map with an initial table size
|
|
||||||
* accommodating the specified number of elements without the need
|
|
||||||
* to dynamically resize.
|
|
||||||
*
|
|
||||||
* @param initialCapacity The implementation performs internal
|
|
||||||
* sizing to accommodate this many elements.
|
|
||||||
* @throws IllegalArgumentException if the initial capacity of
|
|
||||||
* elements is negative
|
|
||||||
*/
|
|
||||||
public SafeConcurrentHashMap(int initialCapacity) {
|
|
||||||
super(initialCapacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new map with the same mappings as the given map.
|
|
||||||
*
|
|
||||||
* @param m the map
|
|
||||||
*/
|
|
||||||
public SafeConcurrentHashMap(Map<? extends K, ? extends V> m) {
|
|
||||||
super(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new, empty map with an initial table size based on
|
|
||||||
* the given number of elements ({@code initialCapacity}) and
|
|
||||||
* initial table density ({@code loadFactor}).
|
|
||||||
*
|
|
||||||
* @param initialCapacity the initial capacity. The implementation
|
|
||||||
* performs internal sizing to accommodate this many elements,
|
|
||||||
* given the specified load factor.
|
|
||||||
* @param loadFactor the load factor (table density) for
|
|
||||||
* establishing the initial table size
|
|
||||||
* @throws IllegalArgumentException if the initial capacity of
|
|
||||||
* elements is negative or the load factor is nonpositive
|
|
||||||
* @since 1.6
|
|
||||||
*/
|
|
||||||
public SafeConcurrentHashMap(int initialCapacity, float loadFactor) {
|
|
||||||
super(initialCapacity, loadFactor);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new, empty map with an initial table size based on
|
|
||||||
* the given number of elements ({@code initialCapacity}), table
|
|
||||||
* density ({@code loadFactor}), and number of concurrently
|
|
||||||
* updating threads ({@code concurrencyLevel}).
|
|
||||||
*
|
|
||||||
* @param initialCapacity the initial capacity. The implementation
|
|
||||||
* performs internal sizing to accommodate this many elements,
|
|
||||||
* given the specified load factor.
|
|
||||||
* @param loadFactor the load factor (table density) for
|
|
||||||
* establishing the initial table size
|
|
||||||
* @param concurrencyLevel the estimated number of concurrently
|
|
||||||
* updating threads. The implementation may use this value as
|
|
||||||
* a sizing hint.
|
|
||||||
* @throws IllegalArgumentException if the initial capacity is
|
|
||||||
* negative or the load factor or concurrencyLevel are
|
|
||||||
* nonpositive
|
|
||||||
*/
|
|
||||||
public SafeConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
|
|
||||||
super(initialCapacity, loadFactor, concurrencyLevel);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
|
||||||
return JRE.isJava8()
|
|
||||||
? ConcurrentHashMapTools.computeIfAbsentForJava8(this, key, mappingFunction)
|
|
||||||
: super.computeIfAbsent(key, mappingFunction);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,74 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2023-2024 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed 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
|
|
||||||
*
|
|
||||||
* https://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 xyz.zhouxy.plusone.commons.util;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.base.JRE;
|
|
||||||
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ConcurrentHashMapTools
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
|
||||||
* 可使用这个工具类的 {@link computeIfAbsentForJava8} 进行替换。
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* <b>NOTE: 方法来自Dubbo,见:issues#2349</b>
|
|
||||||
*
|
|
||||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
|
||||||
* @since 1.0
|
|
||||||
* @see ConcurrentHashMap
|
|
||||||
* @see SafeConcurrentHashMap
|
|
||||||
*/
|
|
||||||
public class ConcurrentHashMapTools {
|
|
||||||
|
|
||||||
public static <K, V> V computeIfAbsent(
|
|
||||||
ConcurrentHashMap<K, V> map, final K key, // NOSONAR
|
|
||||||
final Function<? super K, ? extends V> mappingFunction) {
|
|
||||||
Objects.requireNonNull(map, "map");
|
|
||||||
return JRE.isJava8()
|
|
||||||
? computeIfAbsentForJava8(map, key, mappingFunction)
|
|
||||||
: map.computeIfAbsent(key, mappingFunction);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <K, V> V computeIfAbsentForJava8(
|
|
||||||
ConcurrentHashMap<K, V> map, final K key, // NOSONAR
|
|
||||||
final Function<? super K, ? extends V> mappingFunction) {
|
|
||||||
Objects.requireNonNull(key);
|
|
||||||
Objects.requireNonNull(mappingFunction);
|
|
||||||
V v = map.get(key);
|
|
||||||
if (null == v) {
|
|
||||||
v = mappingFunction.apply(key);
|
|
||||||
if (null == v) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final V res = map.putIfAbsent(key, v);
|
|
||||||
if (null != res) {
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ConcurrentHashMapTools() {
|
|
||||||
throw new IllegalStateException("Utility class");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -21,9 +21,17 @@ import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import com.google.common.annotations.Beta;
|
/**
|
||||||
|
* ID 生成器
|
||||||
@Beta
|
*
|
||||||
|
* <p>
|
||||||
|
* 生成 UUID 和 修改版雪花ID(Seata 版本)
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @see UUID
|
||||||
|
* @see IdWorker
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a>
|
||||||
|
*/
|
||||||
public class IdGenerator {
|
public class IdGenerator {
|
||||||
|
|
||||||
// ===== UUID =====
|
// ===== UUID =====
|
||||||
|
|
|
@ -24,7 +24,26 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
|
import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seata 提供的修改版雪花ID。
|
||||||
|
* <p>
|
||||||
|
* 大体思路为:
|
||||||
|
* <ol>
|
||||||
|
* <li>每个机器线程安全地生成序列,前面加上机器的id,这样就不会与其它机器的id相冲突。</li>
|
||||||
|
* <li>时间戳作为序列的“预留位”,它更像是应用启动时最开始的序列的一部分,在一个时间戳里生成 4096 个 id 之后,直接生成下一个时间戳的 id。</li>
|
||||||
|
* </ol>
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* 详情见以下介绍:
|
||||||
|
* <ul>
|
||||||
|
* <li><a href="https://seata.apache.org/zh-cn/blog/seata-analysis-UUID-generator/">Seata基于改良版雪花算法的分布式UUID生成器分析</a></li>
|
||||||
|
* <li><a href="https://seata.apache.org/zh-cn/blog/seata-snowflake-explain">关于新版雪花算法的答疑</a></li>
|
||||||
|
* <li><a href="https://juejin.cn/post/7264387737276203065">在开源项目中看到一个改良版的雪花算法,现在它是你的了。</a></li>
|
||||||
|
* <li><a href="https://juejin.cn/post/7265516484029743138">关于若干读者,阅读“改良版雪花算法”后提出的几个共性问题的回复。</a></li>
|
||||||
|
* </ul>
|
||||||
|
* </p>
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a>
|
||||||
|
*/
|
||||||
public class IdWorker {
|
public class IdWorker {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -18,12 +18,9 @@ package xyz.zhouxy.plusone.commons.util;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import com.google.common.annotations.Beta;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Twitter_Snowflake
|
* Twitter 版雪花算法
|
||||||
*/
|
*/
|
||||||
@Beta
|
|
||||||
public class SnowflakeIdGenerator {
|
public class SnowflakeIdGenerator {
|
||||||
|
|
||||||
// ==============================Fields===========================================
|
// ==============================Fields===========================================
|
||||||
|
@ -67,9 +64,6 @@ public class SnowflakeIdGenerator {
|
||||||
/** 上次生成 ID 的时间截 */
|
/** 上次生成 ID 的时间截 */
|
||||||
private long lastTimestamp = -1L;
|
private long lastTimestamp = -1L;
|
||||||
|
|
||||||
/** 锁对象 */
|
|
||||||
private final Object lock = new Object();
|
|
||||||
|
|
||||||
// ==============================Constructors=====================================
|
// ==============================Constructors=====================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -93,51 +87,47 @@ public class SnowflakeIdGenerator {
|
||||||
*
|
*
|
||||||
* @return SnowflakeId
|
* @return SnowflakeId
|
||||||
*/
|
*/
|
||||||
public long nextId() {
|
public synchronized long nextId() {
|
||||||
long timestamp;
|
long timestamp = timeGen();
|
||||||
synchronized (lock) {
|
|
||||||
timestamp = timeGen();
|
|
||||||
|
|
||||||
// 发生了回拨,此刻时间小于上次发号时间
|
// 发生了回拨,此刻时间小于上次发号时间
|
||||||
if (timestamp < lastTimestamp) {
|
if (timestamp < lastTimestamp) {
|
||||||
long offset = lastTimestamp - timestamp;
|
long offset = lastTimestamp - timestamp;
|
||||||
if (offset <= 5) {
|
if (offset <= 5) {
|
||||||
// 时间偏差大小小于5ms,则等待两倍时间
|
// 时间偏差大小小于5ms,则等待两倍时间
|
||||||
try {
|
try {
|
||||||
TimeUnit.MILLISECONDS.sleep(offset << 1);
|
TimeUnit.MILLISECONDS.sleep(offset << 1);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
throw new IllegalStateException(e);
|
throw new IllegalStateException(e);
|
||||||
}
|
}
|
||||||
timestamp = timeGen();
|
timestamp = timeGen();
|
||||||
if (timestamp < lastTimestamp) {
|
if (timestamp < lastTimestamp) {
|
||||||
// 还是小于,抛异常上报
|
// 还是小于,抛异常上报
|
||||||
throwClockBackwardsEx(lastTimestamp, timestamp);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throwClockBackwardsEx(lastTimestamp, timestamp);
|
throwClockBackwardsEx(lastTimestamp, timestamp);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throwClockBackwardsEx(lastTimestamp, timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果是同一时间生成的,则进行毫秒内序列
|
|
||||||
if (lastTimestamp == timestamp) {
|
|
||||||
sequence = (sequence + 1) & SEQUENCE_MASK;
|
|
||||||
// 毫秒内序列溢出
|
|
||||||
if (sequence == 0) {
|
|
||||||
// 阻塞到下一个毫秒,获得新的时间戳
|
|
||||||
timestamp = tilNextMillis(lastTimestamp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 时间戳改变,毫秒内序列重置
|
|
||||||
else {
|
|
||||||
sequence = 0L;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 上次生成 ID 的时间截
|
|
||||||
lastTimestamp = timestamp;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果是同一时间生成的,则进行毫秒内序列
|
||||||
|
if (lastTimestamp == timestamp) {
|
||||||
|
sequence = (sequence + 1) & SEQUENCE_MASK;
|
||||||
|
// 毫秒内序列溢出
|
||||||
|
if (sequence == 0) {
|
||||||
|
// 阻塞到下一个毫秒,获得新的时间戳
|
||||||
|
timestamp = tilNextMillis(lastTimestamp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 时间戳改变,毫秒内序列重置
|
||||||
|
else {
|
||||||
|
sequence = 0L;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 上次生成 ID 的时间截
|
||||||
|
lastTimestamp = timestamp;
|
||||||
|
|
||||||
// 移位并通过或运算拼到一起组成64位的ID
|
// 移位并通过或运算拼到一起组成64位的ID
|
||||||
return ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | datacenterIdAndWorkerId | sequence;
|
return ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | datacenterIdAndWorkerId | sequence;
|
||||||
}
|
}
|
||||||
|
|
|
@ -925,9 +925,10 @@ public class AssertToolsTests {
|
||||||
|
|
||||||
// #region - Condition
|
// #region - Condition
|
||||||
|
|
||||||
|
static final class MyException extends RuntimeException {}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCheckCondition() {
|
void testCheckCondition() {
|
||||||
class MyException extends RuntimeException {}
|
|
||||||
|
|
||||||
AssertTools.checkCondition(true, MyException::new);
|
AssertTools.checkCondition(true, MyException::new);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
package xyz.zhouxy.plusone.commons.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.ConcurrentHashSet;
|
||||||
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
|
import cn.hutool.core.lang.Snowflake;
|
||||||
|
import cn.hutool.core.thread.ThreadUtil;
|
||||||
|
|
||||||
|
public class IdGeneratorTests {
|
||||||
|
|
||||||
|
final ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10,
|
||||||
|
0L, TimeUnit.MILLISECONDS,
|
||||||
|
new LinkedBlockingQueue<Runnable>());
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSnowflakeIdGenerator() {
|
||||||
|
final SnowflakeIdGenerator snowflake = new SnowflakeIdGenerator(0, 0);
|
||||||
|
final Set<Long> ids = new ConcurrentHashSet<>();
|
||||||
|
for (int i = 0; i < 10000; i++) {
|
||||||
|
executor.execute(() -> {
|
||||||
|
for (int j = 0; j < 50000; j++) {
|
||||||
|
if (false == ids.add(snowflake.nextId())) {
|
||||||
|
throw new RuntimeException("重复ID!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIdWorker() {
|
||||||
|
final IdWorker idWorker = new IdWorker(0L);
|
||||||
|
final Set<Long> ids = new ConcurrentHashSet<>();
|
||||||
|
for (int i = 0; i < 10000; i++) {
|
||||||
|
executor.execute(() -> {
|
||||||
|
for (int j = 0; j < 50000; j++) {
|
||||||
|
if (false == ids.add(idWorker.nextId())) {
|
||||||
|
throw new RuntimeException("重复ID!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
executor.execute(() -> {
|
||||||
|
for (int j = 0; j < 50000; j++) {
|
||||||
|
if (false == ids.add(IdGenerator.nextSnowflakeId(0))) {
|
||||||
|
throw new RuntimeException("重复ID!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testToSimpleString() {
|
||||||
|
UUID id = UUID.randomUUID();
|
||||||
|
assertEquals(id.toString().replaceAll("-", ""),
|
||||||
|
IdGenerator.toSimpleString(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue