mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code and test case
This commit is contained in:
parent
ce7a075b1e
commit
7b5683a3c4
@ -1,8 +1,10 @@
|
||||
package cn.hutool.core.collection.iter;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@ -44,7 +46,7 @@ public class CopiedIter<E> implements IterableIter<E>, Serializable {
|
||||
* @param iterator 被复制的Iterator
|
||||
*/
|
||||
public CopiedIter(final Iterator<E> iterator) {
|
||||
final List<E> eleList = ListUtil.of(iterator);
|
||||
final List<E> eleList = ListUtil.of(ObjUtil.defaultIfNull(iterator, Collections.emptyIterator()));
|
||||
this.listIterator = eleList.iterator();
|
||||
}
|
||||
|
||||
|
@ -32,6 +32,7 @@ public class FilterIter<E> implements Iterator<E> {
|
||||
*
|
||||
* @param iterator 被包装的{@link Iterator}
|
||||
* @param filter 过滤函数,{@code null}表示不过滤
|
||||
* @throws NullPointerException {@code iterator}为{@code null}时抛出
|
||||
*/
|
||||
public FilterIter(final Iterator<? extends E> iterator, final Predicate<? super E> filter) {
|
||||
this.iterator = Assert.notNull(iterator);
|
||||
|
@ -11,16 +11,7 @@ import cn.hutool.core.util.ObjUtil;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Dictionary;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
@ -198,7 +189,7 @@ public class IterUtil {
|
||||
* @return 某个字段值与对象对应Map
|
||||
* @since 4.6.2
|
||||
*/
|
||||
public static <V> List<Object> fieldValueList(final Iterable<V> iterable, final String fieldName) {
|
||||
public static <V, R> List<R> fieldValueList(final Iterable<V> iterable, final String fieldName) {
|
||||
return fieldValueList(getIter(iterable), fieldName);
|
||||
}
|
||||
|
||||
@ -211,13 +202,14 @@ public class IterUtil {
|
||||
* @return 某个字段值与对象对应Map
|
||||
* @since 4.0.10
|
||||
*/
|
||||
public static <V> List<Object> fieldValueList(final Iterator<V> iter, final String fieldName) {
|
||||
final List<Object> result = new ArrayList<>();
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <V, R> List<R> fieldValueList(final Iterator<V> iter, final String fieldName) {
|
||||
final List<R> result = new ArrayList<>();
|
||||
if (null != iter) {
|
||||
V value;
|
||||
while (iter.hasNext()) {
|
||||
value = iter.next();
|
||||
result.add(FieldUtil.getFieldValue(value, fieldName));
|
||||
result.add((R)FieldUtil.getFieldValue(value, fieldName));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@ -378,7 +370,7 @@ public class IterUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将列表转成值为List的HashMap
|
||||
* 将列表转成值为List的Map集合
|
||||
*
|
||||
* @param resultMap 结果Map,可自定义结果Map类型
|
||||
* @param iterable 值列表
|
||||
@ -702,38 +694,40 @@ public class IterUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断两个{@link Iterable} 是否元素和顺序相同,返回{@code true}的条件是:
|
||||
* <p>判断两个{@link Iterable}中的元素与其顺序是否相同 <br>
|
||||
* 当满足下列情况时返回{@code true}:
|
||||
* <ul>
|
||||
* <li>两个{@link Iterable}必须长度相同</li>
|
||||
* <li>两个{@link Iterable}元素相同index的对象必须equals,满足{@link Objects#equals(Object, Object)}</li>
|
||||
* <li>两个{@link Iterable}都为{@code null};</li>
|
||||
* <li>两个{@link Iterable}满足{@code iterable1 == iterable2};</li>
|
||||
* <li>两个{@link Iterable}所有具有相同下标的元素皆满足{@link Objects#equals(Object, Object)};</li>
|
||||
* </ul>
|
||||
* 此方法来自Apache-Commons-Collections4。
|
||||
*
|
||||
* @param list1 列表1
|
||||
* @param list2 列表2
|
||||
* @param iterable1 列表1
|
||||
* @param iterable2 列表2
|
||||
* @return 是否相同
|
||||
* @since 5.6.0
|
||||
*/
|
||||
public static boolean isEqualList(final Iterable<?> list1, final Iterable<?> list2) {
|
||||
if (list1 == list2) {
|
||||
public static boolean isEqualList(final Iterable<?> iterable1, final Iterable<?> iterable2) {
|
||||
if (iterable1 == iterable2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final Iterator<?> it1 = list1.iterator();
|
||||
final Iterator<?> it2 = list2.iterator();
|
||||
if (iterable1 == null || iterable2 == null) {
|
||||
return false;
|
||||
}
|
||||
final Iterator<?> iter1 = iterable1.iterator();
|
||||
final Iterator<?> iter2 = iterable2.iterator();
|
||||
Object obj1;
|
||||
Object obj2;
|
||||
while (it1.hasNext() && it2.hasNext()) {
|
||||
obj1 = it1.next();
|
||||
obj2 = it2.next();
|
||||
|
||||
while (iter1.hasNext() && iter2.hasNext()) {
|
||||
obj1 = iter1.next();
|
||||
obj2 = iter2.next();
|
||||
if (false == Objects.equals(obj1, obj2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 当两个Iterable长度不一致时返回false
|
||||
return false == (it1.hasNext() || it2.hasNext());
|
||||
return false == (iter1.hasNext() || iter2.hasNext());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,4 +15,5 @@ public interface IterableIter<T> extends Iterable<T>, Iterator<T> {
|
||||
default Iterator<T> iterator() {
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ public class IteratorEnumeration<E> implements Enumeration<E>, Serializable{
|
||||
|
||||
private final Iterator<E> iterator;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param iterator {@link Iterator}对象
|
||||
|
@ -1,6 +1,5 @@
|
||||
package cn.hutool.core.collection.partition;
|
||||
|
||||
import cn.hutool.core.collection.partition.Partition;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -0,0 +1,46 @@
|
||||
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.Enumeration;
|
||||
|
||||
/**
|
||||
* @author huangchengxing
|
||||
*/
|
||||
public class EnumerationIterTest {
|
||||
|
||||
@Test
|
||||
public void testHasNext() {
|
||||
Enumeration<Integer> enumeration = new IteratorEnumeration<>(Arrays.asList(1, 2, 3).iterator());
|
||||
EnumerationIter<Integer> iter = new EnumerationIter<>(enumeration);
|
||||
Assert.assertTrue(iter.hasNext());
|
||||
Assert.assertFalse(new EnumerationIter<>(new IteratorEnumeration<>(Collections.emptyIterator())).hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNext() {
|
||||
Enumeration<Integer> enumeration = new IteratorEnumeration<>(Arrays.asList(1, 2, 3).iterator());
|
||||
EnumerationIter<Integer> iter = new EnumerationIter<>(enumeration);
|
||||
Assert.assertEquals((Integer)1, iter.next());
|
||||
Assert.assertEquals((Integer)2, iter.next());
|
||||
Assert.assertEquals((Integer)3, iter.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
Enumeration<Integer> enumeration = new IteratorEnumeration<>(Arrays.asList(1, 2, 3).iterator());
|
||||
EnumerationIter<Integer> iter = new EnumerationIter<>(enumeration);
|
||||
Assert.assertThrows(UnsupportedOperationException.class, iter::remove);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterator() {
|
||||
Enumeration<Integer> enumeration = new IteratorEnumeration<>(Arrays.asList(1, 2, 3).iterator());
|
||||
EnumerationIter<Integer> iter = new EnumerationIter<>(enumeration);
|
||||
Assert.assertSame(iter, iter.iterator());
|
||||
}
|
||||
|
||||
}
|
@ -13,17 +13,20 @@ import java.util.Iterator;
|
||||
public class IterChainTest {
|
||||
|
||||
@Test
|
||||
public void addChain() {
|
||||
public void testAddChain() {
|
||||
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());
|
||||
|
||||
iterChain = new IterChain<>(iter1, iter2);
|
||||
Assert.assertEquals(2, iterChain.allIterators.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasNext() {
|
||||
public void testHasNext() {
|
||||
IterChain<Integer> iterChain = new IterChain<>();
|
||||
Assert.assertFalse(iterChain.hasNext());
|
||||
Assert.assertFalse(iterChain.addChain(Collections.emptyIterator()).hasNext());
|
||||
@ -31,7 +34,7 @@ public class IterChainTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void next() {
|
||||
public void testNext() {
|
||||
Iterator<Integer> iter1 = Arrays.asList(1, 2).iterator();
|
||||
Iterator<Integer> iter2 = Arrays.asList(3, 4).iterator();
|
||||
IterChain<Integer> iterChain = new IterChain<>();
|
||||
@ -44,14 +47,14 @@ public class IterChainTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void remove() {
|
||||
public void testRemove() {
|
||||
IterChain<Integer> iterChain = new IterChain<>();
|
||||
iterChain.addChain(Arrays.asList(1, 2).iterator());
|
||||
Assert.assertThrows(IllegalStateException.class, iterChain::remove);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void iterator() {
|
||||
public void testIterator() {
|
||||
Iterator<Integer> iter1 = Arrays.asList(1, 2).iterator();
|
||||
Iterator<Integer> iter2 = Arrays.asList(3, 4).iterator();
|
||||
IterChain<Integer> iterChain = new IterChain<>();
|
||||
|
@ -1,12 +1,12 @@
|
||||
package cn.hutool.core.collection.iter;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* test for {@link IterUtil}
|
||||
@ -17,6 +17,13 @@ public class IterUtilTest {
|
||||
public void testGetIter() {
|
||||
Assert.assertNull(IterUtil.getIter(null));
|
||||
Assert.assertEquals(Collections.emptyIterator(), IterUtil.getIter(Collections.emptyList()));
|
||||
|
||||
Assert.assertNull(IterUtil.getIter((Object)null));
|
||||
Assert.assertNotNull(IterUtil.getIter(Collections.emptyIterator()));
|
||||
Assert.assertNotNull(IterUtil.getIter((Object)Collections.emptyList()));
|
||||
Assert.assertNotNull(IterUtil.getIter(new Integer[0]));
|
||||
Assert.assertNotNull(IterUtil.getIter(Collections.emptyMap()));
|
||||
Assert.assertNull(IterUtil.getIter((NodeList)null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -64,4 +71,236 @@ public class IterUtilTest {
|
||||
Assert.assertEquals((Integer)1, countMap.get(o2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldValueMap() {
|
||||
Bean bean1 = new Bean(1, "A");
|
||||
Bean bean2 = new Bean(2, "B");
|
||||
Map<Integer, Bean> map = IterUtil.fieldValueMap(Arrays.asList(bean1, bean2).iterator(), "id");
|
||||
Assert.assertEquals(bean1, map.get(1));
|
||||
Assert.assertEquals(bean2, map.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldValueAsMap() {
|
||||
Bean bean1 = new Bean(1, "A");
|
||||
Bean bean2 = new Bean(2, "B");
|
||||
Map<Integer, String> map = IterUtil.fieldValueAsMap(
|
||||
Arrays.asList(bean1, bean2).iterator(), "id", "name"
|
||||
);
|
||||
Assert.assertEquals("A", map.get(1));
|
||||
Assert.assertEquals("B", map.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldValueList() {
|
||||
Bean bean1 = new Bean(1, "A");
|
||||
Bean bean2 = new Bean(2, "B");
|
||||
Assert.assertEquals(Arrays.asList(1, 2), IterUtil.fieldValueList(Arrays.asList(bean1, bean2), "id"));
|
||||
Assert.assertEquals(
|
||||
Arrays.asList(1, 2),
|
||||
IterUtil.fieldValueList(Arrays.asList(bean1, bean2).iterator(), "id")
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoin() {
|
||||
List<String> stringList = Arrays.asList("1", "2", "3");
|
||||
Assert.assertEquals("123", IterUtil.join(stringList.iterator(), ""));
|
||||
Assert.assertEquals("-1--2--3-", IterUtil.join(stringList.iterator(), "", "-", "-"));
|
||||
Assert.assertEquals("123", IterUtil.join(stringList.iterator(), "", Function.identity()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMap() {
|
||||
List<Integer> keys = Arrays.asList(1, 2, 3);
|
||||
|
||||
Map<Integer, Integer> map = IterUtil.toMap(keys, keys);
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.keySet()));
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.values()));
|
||||
|
||||
map = IterUtil.toMap(keys.iterator(), keys.iterator());
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.keySet()));
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.values()));
|
||||
|
||||
map = IterUtil.toMap(keys.iterator(), keys.iterator(), true);
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.keySet()));
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.values()));
|
||||
|
||||
map = IterUtil.toMap(keys, keys, true);
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.keySet()));
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.values()));
|
||||
|
||||
map = IterUtil.toMap(keys, Function.identity());
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.keySet()));
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.values()));
|
||||
|
||||
map = IterUtil.toMap(keys, Function.identity(), Function.identity());
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.keySet()));
|
||||
Assert.assertEquals(keys, new ArrayList<>(map.values()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToListMap() {
|
||||
List<Integer> keys = Arrays.asList(1, 2, 3, 4);
|
||||
|
||||
Map<Boolean, List<Integer>> map = IterUtil.toListMap(keys, i -> (i & 1) == 0, Function.identity());
|
||||
Assert.assertEquals(Arrays.asList(2, 4), map.get(true));
|
||||
Assert.assertEquals(Arrays.asList(1, 3), map.get(false));
|
||||
|
||||
map = IterUtil.toListMap(keys, i -> (i & 1) == 0);
|
||||
Assert.assertEquals(Arrays.asList(2, 4), map.get(true));
|
||||
Assert.assertEquals(Arrays.asList(1, 3), map.get(false));
|
||||
|
||||
map = new LinkedHashMap<>();
|
||||
Map<Boolean, List<Integer>> rawMap = IterUtil.toListMap(map, keys, i -> (i & 1) == 0, Function.identity());
|
||||
Assert.assertSame(rawMap, map);
|
||||
Assert.assertEquals(Arrays.asList(2, 4), rawMap.get(true));
|
||||
Assert.assertEquals(Arrays.asList(1, 3), rawMap.get(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsIterable() {
|
||||
Iterator<Integer> iter = Arrays.asList(1, 2, 3).iterator();
|
||||
Assert.assertEquals(iter, IterUtil.asIterable(iter).iterator());
|
||||
Assert.assertNull(IterUtil.asIterable(null).iterator());
|
||||
|
||||
Enumeration<Integer> enumeration = new IteratorEnumeration<>(iter);
|
||||
Iterator<Integer> iter2 = IterUtil.asIterator(enumeration);
|
||||
Assert.assertEquals((Integer)1, iter2.next());
|
||||
Assert.assertEquals((Integer)2, iter2.next());
|
||||
Assert.assertEquals((Integer)3, iter2.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() {
|
||||
Iterator<Integer> iter = Arrays.asList(1, 2, 3, 4).iterator();
|
||||
Assert.assertEquals((Integer)3, IterUtil.get(iter, 2));
|
||||
Assert.assertThrows(IllegalArgumentException.class, () -> IterUtil.get(iter, -1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFirst() {
|
||||
Iterator<Integer> iter = Arrays.asList(1, 2, 3, 4).iterator();
|
||||
Assert.assertEquals((Integer)1, IterUtil.getFirst(iter));
|
||||
Assert.assertNull(IterUtil.getFirst(null));
|
||||
Assert.assertNull(IterUtil.getFirst(Collections.emptyIterator()));
|
||||
|
||||
Assert.assertEquals((Integer)2, IterUtil.getFirst(iter, t -> (t & 1) == 0));
|
||||
Assert.assertNull(IterUtil.getFirst((Iterator<Integer>)null, t -> (t & 1) == 0));
|
||||
Assert.assertNull(IterUtil.getFirst(Collections.emptyIterator(), Objects::nonNull));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFirstNoneNull() {
|
||||
Iterator<Integer> iter = Arrays.asList(null, 2, null, 4).iterator();
|
||||
Assert.assertEquals((Integer)2, IterUtil.getFirstNoneNull(iter));
|
||||
Assert.assertNull(IterUtil.getFirstNoneNull(null));
|
||||
Assert.assertNull(IterUtil.getFirstNoneNull(Collections.emptyIterator()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetElementType() {
|
||||
List<Object> list = Arrays.asList(null, "str", null);
|
||||
Assert.assertEquals(String.class, IterUtil.getElementType(list));
|
||||
Assert.assertNull(IterUtil.getElementType((Iterable<?>)null));
|
||||
Assert.assertNull(IterUtil.getElementType(Collections.emptyList()));
|
||||
|
||||
Assert.assertEquals(String.class, IterUtil.getElementType(list.iterator()));
|
||||
Assert.assertNull(IterUtil.getElementType((Iterator<?>)null));
|
||||
Assert.assertNull(IterUtil.getElementType(Collections.emptyIterator()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEdit() {
|
||||
Assert.assertEquals(
|
||||
Collections.singletonList("str"),
|
||||
IterUtil.edit(Arrays.asList(null, "str", null).iterator(), t -> t)
|
||||
);
|
||||
Assert.assertEquals(
|
||||
Collections.singletonList("str"),
|
||||
IterUtil.edit(Arrays.asList(null, "str", null).iterator(), null)
|
||||
);
|
||||
Assert.assertEquals(Collections.emptyList(), IterUtil.edit(null, t -> t));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
List<Integer> list = new ArrayList<>(Arrays.asList(1, null, null, 3));
|
||||
IterUtil.remove(list.iterator(), Objects::isNull);
|
||||
Assert.assertEquals(Arrays.asList(1, 3), list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilterToList() {
|
||||
List<Integer> list1 = new ArrayList<>(Arrays.asList(1, null, null, 3));
|
||||
List<Integer> list2 = IterUtil.filterToList(list1.iterator(), Objects::nonNull);
|
||||
Assert.assertSame(list1, list1);
|
||||
Assert.assertEquals(Arrays.asList(1, 3), list2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFiltered() {
|
||||
Assert.assertNotNull(IterUtil.filtered(Collections.emptyIterator(), t -> true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmpty() {
|
||||
Assert.assertSame(Collections.emptyIterator(), IterUtil.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrans() {
|
||||
Assert.assertNotNull(IterUtil.trans(Collections.emptyIterator(), t -> true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize() {
|
||||
Assert.assertEquals(0, IterUtil.size((Iterator<?>)null));
|
||||
Assert.assertEquals(0, IterUtil.size(Collections.emptyIterator()));
|
||||
Assert.assertEquals(3, IterUtil.size(Arrays.asList(1, 2, 3).iterator()));
|
||||
|
||||
Assert.assertEquals(0, IterUtil.size((Iterable<?>)null));
|
||||
Assert.assertEquals(0, IterUtil.size(Collections.emptyList()));
|
||||
Assert.assertEquals(3, IterUtil.size(Arrays.asList(1, 2, 3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEqualList() {
|
||||
Assert.assertFalse(IterUtil.isEqualList(null, Collections.emptyList()));
|
||||
Assert.assertFalse(IterUtil.isEqualList(Arrays.asList(1, 2, 3), Collections.emptyList()));
|
||||
Assert.assertFalse(IterUtil.isEqualList(Arrays.asList(1, 2, 3), Arrays.asList(1, 2)));
|
||||
Assert.assertTrue(IterUtil.isEqualList(Arrays.asList(1, 2, 3), Arrays.asList(1, 2, 3)));
|
||||
Assert.assertTrue(IterUtil.isEqualList(null, null));
|
||||
Assert.assertTrue(IterUtil.isEqualList(Collections.emptyList(), Collections.emptyList()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3));
|
||||
IterUtil.clear(list.iterator());
|
||||
Assert.assertTrue(list.isEmpty());
|
||||
Assert.assertThrows(UnsupportedOperationException.class, () -> IterUtil.clear(Arrays.asList(1, 2).iterator()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToStr() {
|
||||
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||
Assert.assertEquals("[1, 2, 3]", IterUtil.toStr(list.iterator()));
|
||||
Assert.assertEquals("[1, 2, 3]", IterUtil.toStr(list.iterator(), Objects::toString));
|
||||
Assert.assertEquals("{1:2:3}", IterUtil.toStr(list.iterator(), Objects::toString, ":", "{", "}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForEach() {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
IterUtil.forEach(Arrays.asList(1, 2, 3, 4).iterator(), list::add);
|
||||
Assert.assertEquals(Arrays.asList(1, 2, 3, 4), list);
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
private static class Bean {
|
||||
private final Integer id;
|
||||
private final String name;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
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.List;
|
||||
|
||||
/**
|
||||
* test for {@link IteratorEnumeration}
|
||||
*/
|
||||
public class IteratorEnumerationTest {
|
||||
|
||||
@Test
|
||||
public void testHasMoreElements() {
|
||||
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||
IteratorEnumeration<Integer> enumeration = new IteratorEnumeration<>(list.iterator());
|
||||
Assert.assertTrue(enumeration.hasMoreElements());
|
||||
Assert.assertFalse(new IteratorEnumeration<>(Collections.emptyIterator()).hasMoreElements());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextElement() {
|
||||
List<Integer> list = Arrays.asList(1, 2, 3);
|
||||
IteratorEnumeration<Integer> enumeration = new IteratorEnumeration<>(list.iterator());
|
||||
Assert.assertEquals((Integer)1, enumeration.nextElement());
|
||||
Assert.assertEquals((Integer)2, enumeration.nextElement());
|
||||
Assert.assertEquals((Integer)3, enumeration.nextElement());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package cn.hutool.core.collection.iter;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* test for {@link LineIter}
|
||||
*/
|
||||
public class LineIterTest {
|
||||
|
||||
@Test
|
||||
public void testHasNext() {
|
||||
LineIter iter = getItrFromClasspathFile();
|
||||
Assert.assertTrue(iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNext() {
|
||||
LineIter iter = getItrFromClasspathFile();
|
||||
Assert.assertEquals("is first line", iter.next());
|
||||
Assert.assertEquals("is second line", iter.next());
|
||||
Assert.assertEquals("is third line", iter.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
LineIter iter = getItrFromClasspathFile();
|
||||
iter.next();
|
||||
Assert.assertThrows(UnsupportedOperationException.class, iter::remove);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinish() {
|
||||
LineIter iter = getItrFromClasspathFile();
|
||||
iter.finish();
|
||||
Assert.assertThrows(NoSuchElementException.class, iter::next);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClose() throws IOException {
|
||||
URL url = LineIterTest.class.getClassLoader().getResource("text.txt");
|
||||
Assert.assertNotNull(url);
|
||||
FileInputStream inputStream = new FileInputStream(url.getFile());
|
||||
LineIter iter = new LineIter(inputStream, StandardCharsets.UTF_8);
|
||||
iter.close();
|
||||
Assert.assertThrows(NoSuchElementException.class, iter::next);
|
||||
Assert.assertThrows(IOException.class, inputStream::read);
|
||||
}
|
||||
|
||||
private static LineIter getItrFromClasspathFile() {
|
||||
URL url = LineIterTest.class.getClassLoader().getResource("text.txt");
|
||||
Assert.assertNotNull(url);
|
||||
FileInputStream inputStream = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(url.getFile());
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
return new LineIter(bufferedReader);
|
||||
}
|
||||
|
||||
}
|
@ -13,7 +13,7 @@ import java.util.Iterator;
|
||||
public class PartitionIterTest {
|
||||
|
||||
@Test
|
||||
public void hasNext() {
|
||||
public void testHasNext() {
|
||||
Iterator<Integer> iter = Arrays.asList(1, 2, 3, 4).iterator();
|
||||
PartitionIter<Integer> partitionIter = new PartitionIter<>(iter, 2);
|
||||
Assert.assertTrue(partitionIter.hasNext());
|
||||
@ -21,7 +21,7 @@ public class PartitionIterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void next() {
|
||||
public void testNext() {
|
||||
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());
|
||||
|
@ -0,0 +1,44 @@
|
||||
package cn.hutool.core.collection.iter;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author huangchengxing
|
||||
*/
|
||||
public class TransIterTest {
|
||||
|
||||
@Test
|
||||
public void testHasNext() {
|
||||
TransIter<Integer, String> iter = new TransIter<>(Arrays.asList(1, 2, 3).iterator(), String::valueOf);
|
||||
Assert.assertTrue(iter.hasNext());
|
||||
Assert.assertFalse(new TransIter<>(Collections.emptyIterator(), Function.identity()).hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNext() {
|
||||
TransIter<Integer, String> iter = new TransIter<>(Arrays.asList(1, 2, 3).iterator(), String::valueOf);
|
||||
Assert.assertEquals("1", iter.next());
|
||||
Assert.assertEquals("2", iter.next());
|
||||
Assert.assertEquals("3", iter.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
List<Integer> list = ListUtil.of(1, 2, 3);
|
||||
TransIter<Integer, String> iter = new TransIter<>(list.iterator(), String::valueOf);
|
||||
iter.next();
|
||||
iter.remove();
|
||||
iter.next();
|
||||
iter.remove();
|
||||
iter.next();
|
||||
iter.remove();
|
||||
Assert.assertTrue(list.isEmpty());
|
||||
}
|
||||
}
|
3
hutool-core/src/test/resources/text.txt
Normal file
3
hutool-core/src/test/resources/text.txt
Normal file
@ -0,0 +1,3 @@
|
||||
is first line
|
||||
is second line
|
||||
is third line
|
Loading…
x
Reference in New Issue
Block a user