mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
!788 补充 collection.iter包下的测试用例,完善注释并为部分方法补充空值校验
Merge pull request !788 from Createsequence/fix-coll
This commit is contained in:
commit
14ff5c43ea
@ -3,6 +3,7 @@ package cn.hutool.core.collection.iter;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数组Iterator对象
|
* 数组Iterator对象
|
||||||
@ -75,7 +76,7 @@ public class ArrayIter<E> implements IterableIter<E>, ResettableIter<E>, Seriali
|
|||||||
* @throws NullPointerException array对象为null
|
* @throws NullPointerException array对象为null
|
||||||
*/
|
*/
|
||||||
public ArrayIter(final Object array, final int startIndex, final int endIndex) {
|
public ArrayIter(final Object array, final int startIndex, final int endIndex) {
|
||||||
this.endIndex = Array.getLength(array);
|
this.endIndex = Array.getLength(Objects.requireNonNull(array));
|
||||||
if (endIndex > 0 && endIndex < this.endIndex) {
|
if (endIndex > 0 && endIndex < this.endIndex) {
|
||||||
this.endIndex = endIndex;
|
this.endIndex = endIndex;
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ public class CopiedIter<E> implements IterableIter<E>, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造,当{@code iterator}为空时,默认复制一个空迭代器
|
||||||
*
|
*
|
||||||
* @param iterator 被复制的Iterator
|
* @param iterator 被复制的Iterator
|
||||||
*/
|
*/
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package cn.hutool.core.collection.iter;
|
package cn.hutool.core.collection.iter;
|
||||||
|
|
||||||
import cn.hutool.core.collection.iter.IterableIter;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package cn.hutool.core.collection.iter;
|
package cn.hutool.core.collection.iter;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Chain;
|
import cn.hutool.core.lang.Chain;
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 组合{@link Iterator},将多个{@link Iterator}组合在一起,便于集中遍历。<br>
|
* 组合{@link Iterator},将多个{@link Iterator}组合在一起,便于集中遍历。<br>
|
||||||
@ -28,16 +26,27 @@ public class IterChain<T> implements Iterator<T>, Chain<Iterator<T>, IterChain<T
|
|||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
* @param iterators 多个{@link Iterator}
|
* @param iterators 多个{@link Iterator}
|
||||||
|
* @throws IllegalArgumentException 当存在重复的迭代器,或添加的迭代器中存在{@code null}时抛出
|
||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public IterChain(final Iterator<T>... iterators) {
|
public IterChain(final Iterator<T>... iterators) {
|
||||||
|
if (ArrayUtil.isNotEmpty(iterators)) {
|
||||||
for (final Iterator<T> iterator : iterators) {
|
for (final Iterator<T> iterator : iterators) {
|
||||||
addChain(iterator);
|
addChain(iterator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加迭代器
|
||||||
|
*
|
||||||
|
* @param iterator 迭代器
|
||||||
|
* @return 当前实例
|
||||||
|
* @throws IllegalArgumentException 当迭代器被重复添加,或待添加的迭代器为{@code null}时抛出
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public IterChain<T> addChain(final Iterator<T> iterator) {
|
public IterChain<T> addChain(final Iterator<T> iterator) {
|
||||||
|
Objects.requireNonNull(iterator);
|
||||||
if (allIterators.contains(iterator)) {
|
if (allIterators.contains(iterator)) {
|
||||||
throw new IllegalArgumentException("Duplicate iterator");
|
throw new IllegalArgumentException("Duplicate iterator");
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
package cn.hutool.core.collection.iter;
|
package cn.hutool.core.collection.iter;
|
||||||
|
|
||||||
import cn.hutool.core.collection.iter.IterableIter;
|
import cn.hutool.core.lang.Assert;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分批迭代工具,可以分批处理数据
|
* 分批迭代工具,可以分批处理数据
|
||||||
@ -36,9 +37,11 @@ public class PartitionIter<T> implements IterableIter<List<T>>, Serializable {
|
|||||||
*
|
*
|
||||||
* @param iterator 迭代器
|
* @param iterator 迭代器
|
||||||
* @param partitionSize 每批大小,最后一批不满一批算一批
|
* @param partitionSize 每批大小,最后一批不满一批算一批
|
||||||
|
* @throws IllegalArgumentException 当{@code partitionSize}小于等于0,或{@code iterator}为{@code null}时抛出
|
||||||
*/
|
*/
|
||||||
public PartitionIter(final Iterator<T> iterator, final int partitionSize) {
|
public PartitionIter(final Iterator<T> iterator, final int partitionSize) {
|
||||||
this.iterator = iterator;
|
Assert.isTrue(partitionSize > 0, "partition size must greater than 0");
|
||||||
|
this.iterator = Objects.requireNonNull(iterator);
|
||||||
this.partitionSize = partitionSize;
|
this.partitionSize = partitionSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
package cn.hutool.core.collection.iter;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link ArrayIter}
|
||||||
|
*/
|
||||||
|
public class ArrayIterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHasNext() {
|
||||||
|
Integer[] arr = new Integer[]{ 1, 2, 3 };
|
||||||
|
ArrayIter<Integer> iter = new ArrayIter<>(arr);
|
||||||
|
Assert.assertTrue(iter.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNext() {
|
||||||
|
Integer[] arr = new Integer[]{ 1, 2, 3 };
|
||||||
|
ArrayIter<Integer> iter = new ArrayIter<>(arr);
|
||||||
|
Assert.assertEquals((Integer)1, iter.next());
|
||||||
|
Assert.assertEquals((Integer)2, iter.next());
|
||||||
|
Assert.assertEquals((Integer)3, iter.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemove() {
|
||||||
|
Integer[] arr = new Integer[]{ 1, 2, 3 };
|
||||||
|
ArrayIter<Integer> iter = new ArrayIter<>(arr);
|
||||||
|
Assert.assertThrows(UnsupportedOperationException.class, iter::remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetArray() {
|
||||||
|
Integer[] arr = new Integer[]{ 1, 2, 3 };
|
||||||
|
ArrayIter<Integer> iter = new ArrayIter<>(arr);
|
||||||
|
Assert.assertEquals(arr, iter.getArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReset() {
|
||||||
|
Integer[] arr = new Integer[]{ 1, 2, 3 };
|
||||||
|
ArrayIter<Integer> iter = new ArrayIter<>(arr);
|
||||||
|
Assert.assertEquals((Integer)1, iter.next());
|
||||||
|
iter.reset();
|
||||||
|
Assert.assertEquals((Integer)1, iter.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package cn.hutool.core.collection.iter;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link CopiedIter}
|
||||||
|
*/
|
||||||
|
public class CopiedIterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void copyOf() {
|
||||||
|
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||||
|
Iterator<Integer> iter = list.iterator();
|
||||||
|
Assert.assertEquals((Integer)1, iter.next());
|
||||||
|
|
||||||
|
Assert.assertEquals((Integer)2, CopiedIter.copyOf(iter).next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hasNext() {
|
||||||
|
Assert.assertTrue(CopiedIter.copyOf(Arrays.asList(1, 2, 3).iterator()).hasNext());
|
||||||
|
Assert.assertFalse(CopiedIter.copyOf(Collections.emptyIterator()).hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void next() {
|
||||||
|
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||||
|
Iterator<Integer> iter = CopiedIter.copyOf(list.iterator());
|
||||||
|
Assert.assertEquals((Integer)1, iter.next());
|
||||||
|
Assert.assertEquals((Integer)2, iter.next());
|
||||||
|
Assert.assertEquals((Integer)3, iter.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void remove() {
|
||||||
|
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||||
|
Iterator<Integer> iter = CopiedIter.copyOf(list.iterator());
|
||||||
|
Assert.assertThrows(UnsupportedOperationException.class, iter::remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,7 +4,10 @@ import cn.hutool.core.collection.ListUtil;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class FilterIterTest {
|
public class FilterIterTest {
|
||||||
|
|
||||||
@ -20,4 +23,40 @@ public class FilterIterTest {
|
|||||||
}
|
}
|
||||||
Assert.assertEquals(2, count);
|
Assert.assertEquals(2, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hasNext() {
|
||||||
|
Iterator<Integer> iter = new FilterIter<>(Arrays.asList(1, 2, 3).iterator(), i -> true);
|
||||||
|
Assert.assertTrue(iter.hasNext());
|
||||||
|
iter = new FilterIter<>(Collections.emptyIterator(), i -> true);
|
||||||
|
Assert.assertFalse(iter.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void next() {
|
||||||
|
// 只保留奇数
|
||||||
|
Iterator<Integer> iter = new FilterIter<>(Arrays.asList(1, 2, 3).iterator(), i -> (i & 1) == 1);
|
||||||
|
Assert.assertEquals((Integer)1, iter.next());
|
||||||
|
Assert.assertEquals((Integer)3, iter.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void remove() {
|
||||||
|
Iterator<Integer> iter = new FilterIter<>(Collections.emptyIterator(), i -> true);
|
||||||
|
Assert.assertThrows(IllegalStateException.class, iter::remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getIterator() {
|
||||||
|
FilterIter<Integer> iter = new FilterIter<>(Collections.emptyIterator(), i -> true);
|
||||||
|
Assert.assertSame(Collections.emptyIterator(), iter.getIterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getFilter() {
|
||||||
|
Predicate<Integer> predicate = i -> true;
|
||||||
|
FilterIter<Integer> iter = new FilterIter<>(Collections.emptyIterator(), predicate);
|
||||||
|
Assert.assertSame(predicate, iter.getFilter());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,66 @@
|
|||||||
|
package cn.hutool.core.collection.iter;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link IterChain}
|
||||||
|
*/
|
||||||
|
public class IterChainTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addChain() {
|
||||||
|
Iterator<Integer> iter1 = Arrays.asList(1, 2).iterator();
|
||||||
|
Iterator<Integer> iter2 = Arrays.asList(3, 4).iterator();
|
||||||
|
IterChain<Integer> iterChain = new IterChain<>();
|
||||||
|
Assert.assertSame(iterChain, iterChain.addChain(iter1));
|
||||||
|
Assert.assertSame(iterChain, iterChain.addChain(iter2));
|
||||||
|
Assert.assertEquals(2, iterChain.allIterators.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hasNext() {
|
||||||
|
IterChain<Integer> iterChain = new IterChain<>();
|
||||||
|
Assert.assertFalse(iterChain.hasNext());
|
||||||
|
Assert.assertFalse(iterChain.addChain(Collections.emptyIterator()).hasNext());
|
||||||
|
Assert.assertTrue(iterChain.addChain(Arrays.asList(3, 4).iterator()).hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void next() {
|
||||||
|
Iterator<Integer> iter1 = Arrays.asList(1, 2).iterator();
|
||||||
|
Iterator<Integer> iter2 = Arrays.asList(3, 4).iterator();
|
||||||
|
IterChain<Integer> iterChain = new IterChain<>();
|
||||||
|
Assert.assertSame(iterChain, iterChain.addChain(iter1));
|
||||||
|
Assert.assertSame(iterChain, iterChain.addChain(iter2));
|
||||||
|
Assert.assertEquals((Integer)1, iterChain.next());
|
||||||
|
Assert.assertEquals((Integer)2, iterChain.next());
|
||||||
|
Assert.assertEquals((Integer)3, iterChain.next());
|
||||||
|
Assert.assertEquals((Integer)4, iterChain.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void remove() {
|
||||||
|
IterChain<Integer> iterChain = new IterChain<>();
|
||||||
|
iterChain.addChain(Arrays.asList(1, 2).iterator());
|
||||||
|
Assert.assertThrows(IllegalStateException.class, iterChain::remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void iterator() {
|
||||||
|
Iterator<Integer> iter1 = Arrays.asList(1, 2).iterator();
|
||||||
|
Iterator<Integer> iter2 = Arrays.asList(3, 4).iterator();
|
||||||
|
IterChain<Integer> iterChain = new IterChain<>();
|
||||||
|
Assert.assertSame(iterChain, iterChain.addChain(iter1));
|
||||||
|
Assert.assertSame(iterChain, iterChain.addChain(iter2));
|
||||||
|
|
||||||
|
Iterator<Iterator<Integer>> iterators = iterChain.iterator();
|
||||||
|
Assert.assertSame(iter1, iterators.next());
|
||||||
|
Assert.assertSame(iter2, iterators.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
package cn.hutool.core.collection.iter;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link IterUtil}
|
||||||
|
*/
|
||||||
|
public class IterUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetIter() {
|
||||||
|
Assert.assertNull(IterUtil.getIter(null));
|
||||||
|
Assert.assertEquals(Collections.emptyIterator(), IterUtil.getIter(Collections.emptyList()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsEmpty() {
|
||||||
|
Assert.assertTrue(IterUtil.isEmpty(Collections.emptyIterator()));
|
||||||
|
Assert.assertFalse(IterUtil.isEmpty(Arrays.asList(1, 2).iterator()));
|
||||||
|
|
||||||
|
Assert.assertTrue(IterUtil.isEmpty(Collections.emptyList()));
|
||||||
|
Assert.assertFalse(IterUtil.isEmpty(Arrays.asList(1, 2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsNotEmpty() {
|
||||||
|
Assert.assertFalse(IterUtil.isNotEmpty(Collections.emptyIterator()));
|
||||||
|
Assert.assertTrue(IterUtil.isNotEmpty(Arrays.asList(1, 2).iterator()));
|
||||||
|
|
||||||
|
Assert.assertFalse(IterUtil.isNotEmpty(Collections.emptyList()));
|
||||||
|
Assert.assertTrue(IterUtil.isNotEmpty(Arrays.asList(1, 2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHasNull() {
|
||||||
|
Assert.assertFalse(IterUtil.hasNull(Arrays.asList(1, 3, 2).iterator()));
|
||||||
|
Assert.assertTrue(IterUtil.hasNull(Arrays.asList(1, null, 2).iterator()));
|
||||||
|
Assert.assertFalse(IterUtil.hasNull(Collections.emptyIterator()));
|
||||||
|
Assert.assertTrue(IterUtil.hasNull(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsAllNull() {
|
||||||
|
Assert.assertTrue(IterUtil.isAllNull(Arrays.asList(null, null)));
|
||||||
|
Assert.assertFalse(IterUtil.isAllNull(Arrays.asList(1, null)));
|
||||||
|
Assert.assertTrue(IterUtil.isAllNull((Iterable<?>)null));
|
||||||
|
Assert.assertTrue(IterUtil.isAllNull(Arrays.asList(null, null).iterator()));
|
||||||
|
Assert.assertFalse(IterUtil.isAllNull(Arrays.asList(1, null).iterator()));
|
||||||
|
Assert.assertTrue(IterUtil.isAllNull((Iterator<?>)null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCountMap() {
|
||||||
|
Object o1 = new Object();
|
||||||
|
Object o2 = new Object();
|
||||||
|
Map<Object, Integer> countMap = IterUtil.countMap(Arrays.asList(o1, o2, o1, o1).iterator());
|
||||||
|
Assert.assertEquals((Integer)3, countMap.get(o1));
|
||||||
|
Assert.assertEquals((Integer)1, countMap.get(o2));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package cn.hutool.core.collection.iter;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for {@link PartitionIter}
|
||||||
|
*/
|
||||||
|
public class PartitionIterTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hasNext() {
|
||||||
|
Iterator<Integer> iter = Arrays.asList(1, 2, 3, 4).iterator();
|
||||||
|
PartitionIter<Integer> partitionIter = new PartitionIter<>(iter, 2);
|
||||||
|
Assert.assertTrue(partitionIter.hasNext());
|
||||||
|
Assert.assertFalse(new PartitionIter<>(Collections.emptyIterator(), 1).hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void next() {
|
||||||
|
Iterator<Integer> iter = Arrays.asList(1, 2, 3, 4).iterator();
|
||||||
|
PartitionIter<Integer> partitionIter = new PartitionIter<>(iter, 2);
|
||||||
|
Assert.assertEquals(Arrays.asList(1, 2), partitionIter.next());
|
||||||
|
Assert.assertEquals(Arrays.asList(3, 4), partitionIter.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user