add method

This commit is contained in:
Looly
2021-02-22 16:21:43 +08:00
parent 66bdcac2b0
commit 76eb65249a
6 changed files with 70 additions and 23 deletions

View File

@@ -246,7 +246,7 @@ public class ListUtil {
// 每页条目数大于总数直接返回所有
if (resultSize <= pageSize) {
if (pageNo < (PageUtil.getFirstPageNo() + 1)) {
return Collections.unmodifiableList(list);
return unmodifiable(list);
} else {
// 越界直接返回空
return new ArrayList<>(0);
@@ -262,11 +262,11 @@ public class ListUtil {
if (startEnd[1] > resultSize) {
startEnd[1] = resultSize;
if (startEnd[0] > startEnd[1]) {
return empty();
return new ArrayList<>(0);
}
}
return list.subList(startEnd[0], startEnd[1]);
return sub(list, startEnd[0], startEnd[1]);
}
/**
@@ -366,7 +366,8 @@ public class ListUtil {
}
/**
* 截取集合的部分
* 截取集合的部分<br>
* 此方法与{@link List#subList(int, int)} 不同在于子列表是新的副本,操作子列表不会影响原列表。
*
* @param <T> 集合元素类型
* @param list 被截取的数组
@@ -407,8 +408,8 @@ public class ListUtil {
end = size;
}
if (step <= 1) {
return list.subList(start, end);
if (step < 1) {
step = 1;
}
final List<T> result = new ArrayList<>();

View File

@@ -101,4 +101,15 @@ public class ListUtilTest {
int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray();
Assert.assertArrayEquals(new int[]{1,2,3,4,5},d1);
}
@Test
public void subTest(){
final List<Integer> of = ListUtil.of(1, 2, 3, 4);
final List<Integer> sub = ListUtil.sub(of, 2, 4);
sub.remove(0);
// 对子列表操作不影响原列表
Assert.assertEquals(4, of.size());
Assert.assertEquals(1, sub.size());
}
}

View File

@@ -17,7 +17,7 @@ import java.util.concurrent.CountDownLatch;
/**
* {@link IdUtil} 单元测试
*
*
* @author looly
*
*/
@@ -31,12 +31,12 @@ public class IdUtilTest {
String randomUUID = IdUtil.randomUUID();
Assert.assertEquals(36, randomUUID.length());
}
@Test
public void fastUUIDTest() {
String simpleUUID = IdUtil.fastSimpleUUID();
Assert.assertEquals(32, simpleUUID.length());
String randomUUID = IdUtil.fastUUID();
Assert.assertEquals(36, randomUUID.length());
}
@@ -60,25 +60,26 @@ public class IdUtilTest {
}
Console.log(timer.interval());
}
@Test
public void objectIdTest() {
String id = IdUtil.objectId();
Assert.assertEquals(24, id.length());
}
@Test
public void createSnowflakeTest() {
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
long id = snowflake.nextId();
Assert.assertTrue(id > 0);
}
@Test
@Ignore
public void snowflakeBenchTest() {
final Set<Long> set = new ConcurrentHashSet<>();
final Snowflake snowflake = IdUtil.createSnowflake(1, 1);
//线程数
int threadCount = 100;
//每个线程生成的ID数
@@ -94,7 +95,7 @@ public class IdUtilTest {
latch.countDown();
});
}
//等待全部线程结束
try {
latch.await();
@@ -103,11 +104,12 @@ public class IdUtilTest {
}
Assert.assertEquals(threadCount * idCountPerThread, set.size());
}
@Test
@Ignore
public void snowflakeBenchTest2() {
final Set<Long> set = new ConcurrentHashSet<>();
//线程数
int threadCount = 100;
//每个线程生成的ID数
@@ -123,7 +125,7 @@ public class IdUtilTest {
latch.countDown();
});
}
//等待全部线程结束
try {
latch.await();