mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add RingIndexUtil
This commit is contained in:
parent
8c5577ab45
commit
6dc2192caa
@ -3,10 +3,11 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.7.15 (2021-10-11)
|
# 5.7.15 (2021-10-14)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【db 】 Db.quietSetAutoCommit增加判空(issue#I4D75B@Gitee)
|
* 【db 】 Db.quietSetAutoCommit增加判空(issue#I4D75B@Gitee)
|
||||||
|
* 【core 】 增加RingIndexUtil(pr#438@Gitee)
|
||||||
*
|
*
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题(issue#1885@Github)
|
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题(issue#1885@Github)
|
||||||
|
@ -13,26 +13,6 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||||||
*/
|
*/
|
||||||
public class RingIndexUtil {
|
public class RingIndexUtil {
|
||||||
|
|
||||||
/**
|
|
||||||
* 通过cas操作 实现对指定值内的回环累加
|
|
||||||
*
|
|
||||||
* @param object 集合
|
|
||||||
* <ul>
|
|
||||||
* <li>Collection - the collection size
|
|
||||||
* <li>Map - the map size
|
|
||||||
* <li>Array - the array size
|
|
||||||
* <li>Iterator - the number of elements remaining in the iterator
|
|
||||||
* <li>Enumeration - the number of elements remaining in the enumeration
|
|
||||||
* </ul>
|
|
||||||
* @param atomicLong 原子操作类
|
|
||||||
* @return 索引位置
|
|
||||||
*/
|
|
||||||
public static long ringNextLongByObj(Object object, AtomicLong atomicLong) {
|
|
||||||
Assert.notNull(object);
|
|
||||||
int modulo = CollUtil.size(object);
|
|
||||||
return ringNextLong(modulo, atomicLong);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过cas操作 实现对指定值内的回环累加
|
* 通过cas操作 实现对指定值内的回环累加
|
||||||
*
|
*
|
||||||
@ -63,7 +43,7 @@ public class RingIndexUtil {
|
|||||||
public static int ringNextInt(int modulo, AtomicInteger atomicInteger) {
|
public static int ringNextInt(int modulo, AtomicInteger atomicInteger) {
|
||||||
Assert.notNull(atomicInteger);
|
Assert.notNull(atomicInteger);
|
||||||
Assert.isTrue(modulo > 0);
|
Assert.isTrue(modulo > 0);
|
||||||
if (modulo == 1) {
|
if (modulo <= 1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for (; ; ) {
|
for (; ; ) {
|
||||||
@ -85,7 +65,7 @@ public class RingIndexUtil {
|
|||||||
public static long ringNextLong(long modulo, AtomicLong atomicLong) {
|
public static long ringNextLong(long modulo, AtomicLong atomicLong) {
|
||||||
Assert.notNull(atomicLong);
|
Assert.notNull(atomicLong);
|
||||||
Assert.isTrue(modulo > 0);
|
Assert.isTrue(modulo > 0);
|
||||||
if (modulo == 1) {
|
if (modulo <= 1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for (; ; ) {
|
for (; ; ) {
|
||||||
@ -96,5 +76,4 @@ public class RingIndexUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,39 +1,22 @@
|
|||||||
package cn.hutool.core.collection;
|
package cn.hutool.core.collection;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.thread.ThreadUtil;
|
import cn.hutool.core.thread.ThreadUtil;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 集合索引环形获取工具类测试类
|
* 集合索引环形获取工具类测试类
|
||||||
*
|
*
|
||||||
* @author ZhouChuGang
|
* @author ZhouChuGang
|
||||||
* @version 1.0
|
|
||||||
* @project hutool
|
|
||||||
* @date 2021/10/13 18:47
|
|
||||||
*/
|
*/
|
||||||
public class RingIndexUtilTest {
|
public class RingIndexUtilTest {
|
||||||
|
|
||||||
private final List<String> strList = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
|
private final List<String> strList = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
|
||||||
|
|
||||||
/**
|
|
||||||
* 观察输出的打印为不重复的
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void ringNextLongByObjTest() {
|
|
||||||
final AtomicLong atomicLong = new AtomicLong();
|
|
||||||
// 开启并发测试,每个线程获取到的元素都是唯一的
|
|
||||||
ThreadUtil.concurrencyTest(strList.size(), () -> {
|
|
||||||
final long index = RingIndexUtil.ringNextLongByObj(strList, atomicLong);
|
|
||||||
final String s = strList.get((int) index);
|
|
||||||
System.out.println(s);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 观察输出的打印为不重复的
|
* 观察输出的打印为不重复的
|
||||||
*/
|
*/
|
||||||
@ -44,7 +27,7 @@ public class RingIndexUtilTest {
|
|||||||
ThreadUtil.concurrencyTest(strList.size(), () -> {
|
ThreadUtil.concurrencyTest(strList.size(), () -> {
|
||||||
final int index = RingIndexUtil.ringNextIntByObj(strList, atomicInteger);
|
final int index = RingIndexUtil.ringNextIntByObj(strList, atomicInteger);
|
||||||
final String s = strList.get(index);
|
final String s = strList.get(index);
|
||||||
System.out.println(s);
|
Assert.notNull(s);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user