Compare commits

...

4 Commits

10 changed files with 145 additions and 327 deletions

View File

@ -1,7 +1,7 @@
[ ] 未开始测试 - 15 (21.43%)
[-] 测试未完成 - 10 (14.29%)
[Y] 测试完成 - 24 (34.29%)
[x] 无需测试 - 21 (30.00%)
[ ] 未开始测试 - 10 (14.93%)
[-] 测试未完成 - 9 (13.43%)
[Y] 测试完成 - 27 (40.30%)
[x] 无需测试 - 21 (31.34%)
xyz.zhouxy.plusone.commons
├───annotation
@ -20,7 +20,6 @@ xyz.zhouxy.plusone.commons
│ IWithCode.java [Y]
│ IWithIntCode.java [Y]
│ IWithLongCode.java [Y]
│ JRE.java [ ]
│ LongRef.java [Y]
│ Ref.java [Y]
@ -28,7 +27,6 @@ xyz.zhouxy.plusone.commons
│ AbstractMapWrapper.java [ ]
│ CollectionTools.java [Y]
│ MapWrapper.java [ ]
│ SafeConcurrentHashMap.java [ ]
├───constant
│ PatternConsts.java [ ]
@ -86,16 +84,15 @@ xyz.zhouxy.plusone.commons
ArrayTools.java [-]
AssertTools.java [Y]
BigDecimals.java [Y]
ConcurrentHashMapTools.java [-]
DateTimeTools.java [-]
Enumeration.java [Y]
EnumTools.java [Y]
IdGenerator.java [ ]
IdWorker.java [ ]
IdGenerator.java [Y]
IdWorker.java [Y]
Numbers.java [Y]
OptionalTools.java [Y]
RandomTools.java [ ]
RegexTools.java [ ]
SnowflakeIdGenerator.java [ ]
SnowflakeIdGenerator.java [Y]
StringTools.java [Y]
TreeBuilder.java [Y]

View File

@ -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");
}
}

View File

@ -23,7 +23,6 @@ import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
@ -33,7 +32,6 @@ import javax.annotation.Nullable;
import com.google.common.annotations.Beta;
import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools;
/**
* AbstractMapWrapper
@ -159,12 +157,7 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
}
return value;
};
if (this.map instanceof ConcurrentHashMap) {
return ConcurrentHashMapTools.computeIfAbsent(
(ConcurrentHashMap<K, V>) this.map, key, func);
} else {
return this.map.computeIfAbsent(key, func);
}
return this.map.computeIfAbsent(key, func);
}
public final Map<K, V> exportMap() {

View File

@ -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);
}
}

View File

@ -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: Dubboissues#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");
}
}

View File

@ -21,9 +21,17 @@ import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import com.google.common.annotations.Beta;
@Beta
/**
* ID
*
* <p>
* UUID IDSeata
* </p>
*
* @see UUID
* @see IdWorker
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a>
*/
public class IdGenerator {
// ===== UUID =====

View File

@ -24,7 +24,26 @@ import java.util.concurrent.atomic.AtomicLong;
import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
/**
* Seata ID
* <p>
*
* <ol>
* <li>线idid</li>
* <li> 4096 id id</li>
* </ol>
* </p>
* <p>
*
* <ul>
* <li><a href="https://seata.apache.org/zh-cn/blog/seata-analysis-UUID-generator/">SeataUUID</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 {
/**

View File

@ -18,12 +18,9 @@ package xyz.zhouxy.plusone.commons.util;
import java.util.concurrent.TimeUnit;
import com.google.common.annotations.Beta;
/**
* Twitter_Snowflake
* Twitter
*/
@Beta
public class SnowflakeIdGenerator {
// ==============================Fields===========================================
@ -67,9 +64,6 @@ public class SnowflakeIdGenerator {
/** 上次生成 ID 的时间截 */
private long lastTimestamp = -1L;
/** 锁对象 */
private final Object lock = new Object();
// ==============================Constructors=====================================
/**
@ -93,51 +87,47 @@ public class SnowflakeIdGenerator {
*
* @return SnowflakeId
*/
public long nextId() {
long timestamp;
synchronized (lock) {
timestamp = timeGen();
public synchronized long nextId() {
long timestamp = timeGen();
// 发生了回拨,此刻时间小于上次发号时间
if (timestamp < lastTimestamp) {
long offset = lastTimestamp - timestamp;
if (offset <= 5) {
// 时间偏差大小小于5ms则等待两倍时间
try {
TimeUnit.MILLISECONDS.sleep(offset << 1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
timestamp = timeGen();
if (timestamp < lastTimestamp) {
// 还是小于,抛异常上报
throwClockBackwardsEx(lastTimestamp, timestamp);
}
} else {
// 发生了回拨,此刻时间小于上次发号时间
if (timestamp < lastTimestamp) {
long offset = lastTimestamp - timestamp;
if (offset <= 5) {
// 时间偏差大小小于5ms则等待两倍时间
try {
TimeUnit.MILLISECONDS.sleep(offset << 1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
timestamp = timeGen();
if (timestamp < lastTimestamp) {
// 还是小于,抛异常上报
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
return ((timestamp - TWEPOCH) << TIMESTAMP_LEFT_SHIFT) | datacenterIdAndWorkerId | sequence;
}

View File

@ -925,9 +925,10 @@ public class AssertToolsTests {
// #region - Condition
static final class MyException extends RuntimeException {}
@Test
void testCheckCondition() {
class MyException extends RuntimeException {}
AssertTools.checkCondition(true, MyException::new);

View File

@ -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));
}
}