重构增强流:

1.移除SimpleStreamWrapper;
2.将EasyStream中部分方法移动至接口中;
3.调整类名、方法名、变量名与部分注释;
4.为实例方法添加空值校验;
5.补充测试用例;
This commit is contained in:
huangchengxing
2022-09-06 11:21:38 +08:00
parent 2d1255cbff
commit 741f0aa53a
13 changed files with 1787 additions and 870 deletions

View File

@@ -0,0 +1,705 @@
package cn.hutool.core.stream;
import cn.hutool.core.collection.ListUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.junit.Assert;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.stream.*;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
/**
* {@link AbstractEnhancedWrappedStream}、{@link TerminableWrappedStream}、{@link TransformableWrappedStream}的测试用例。
* 此用例用于保证通过{@link AbstractEnhancedWrappedStream}获得的默认方法,在子类不重写的情况下能够按照预期效果生效
*
* @author huangchengxing
*/
public class AbstractEnhancedWrappedStreamTest {
@Test
public void testToList() {
List<Integer> list = asList(1, 2, 3);
List<Integer> toList = wrap(list).toList();
Assert.assertEquals(list, toList);
}
@Test
public void testToUnmodifiableList() {
List<Integer> list = wrap(1, 2, 3)
.toUnmodifiableList();
Assert.assertThrows(UnsupportedOperationException.class, () -> list.remove(0));
}
@Test
public void testToSet() {
List<Integer> list = asList(1, 2, 3);
Set<String> toSet = wrap(list).map(String::valueOf).toSet();
Assert.assertEquals(new HashSet<>(asList("1", "2", "3")), toSet);
}
@Test
public void testToUnmodifiableSet() {
Set<Integer> set = wrap(1, 2, 3)
.toUnmodifiableSet();
Assert.assertThrows(UnsupportedOperationException.class, () -> set.remove(0));
}
@Test
public void testToCollection() {
List<Integer> list = asList(1, 2, 3);
List<String> toCollection = wrap(list).map(String::valueOf).toColl(LinkedList::new);
Assert.assertEquals(asList("1", "2", "3"), toCollection);
}
@Test
public void testToMap() {
List<Integer> list = asList(1, 2, 3);
Map<String, Integer> identityMap = wrap(list).toMap(String::valueOf);
Assert.assertEquals(new HashMap<String, Integer>() {{
put("1", 1);
put("2", 2);
put("3", 3);
}}, identityMap);
}
@Test
public void testToUnmodifiableMap() {
Map<Integer, Integer> map1 = wrap(1, 2, 3).toUnmodifiableMap(Function.identity(), Function.identity());
Assert.assertThrows(UnsupportedOperationException.class, () -> map1.remove(1));
Map<Integer, Integer> map2 = wrap(1, 2, 3).toUnmodifiableMap(Function.identity(), Function.identity(), (t1, t2) -> t1);
Assert.assertThrows(UnsupportedOperationException.class, () -> map2.remove(1));
}
@Test
public void testToZip() {
List<Integer> orders = asList(1, 2, 3);
List<String> list = asList("dromara", "hutool", "sweet");
Map<Integer, String> toZip = wrap(orders).toZip(list);
Assert.assertEquals(new HashMap<Integer, String>() {{
put(1, "dromara");
put(2, "hutool");
put(3, "sweet");
}}, toZip);
}
@Test
public void testTransform() {
List<Integer> list = wrap(1, 2, 3).transform(Wrapper::toList).orElse(null);
Assert.assertEquals(asList(1, 2, 3), list);
}
@Test
public void testFindFirst() {
List<Integer> list = asList(1, 2, 3);
Assert.assertEquals((Integer)1, wrap(list).findFirst(t -> (t & 1) == 1).orElse(null));
Assert.assertEquals((Integer)1, wrap(list).filter(t -> (t & 1) == 1).findFirst().orElse(null));
}
@Test
public void testFindFirstIdx() {
List<Integer> list = asList(1, 2, 3);
Assert.assertEquals(1, wrap(list).findFirstIdx(t -> (t & 1) == 0));
}
@Test
public void testFindLast() {
List<Integer> list = asList(1, 2, 3);
Assert.assertEquals((Integer)3, wrap(list).findLast(t -> (t & 1) == 1).orElse(null));
}
@Test
public void testFindLastIdx() {
List<Integer> list = asList(1, 2, 3);
Assert.assertEquals(1, wrap(list).findLastIdx(t -> (t & 1) == 0));
}
@Test
public void testAt() {
List<Integer> list = asList(1, 2, 3);
Assert.assertEquals((Integer)3, wrap(list).at(2).orElse(null));
}
@Test
public void testIsEmpty() {
Assert.assertTrue(wrap(Collections.emptyList()).isEmpty());
Assert.assertFalse(wrap(asList(1, 2, 3)).isEmpty());
}
@Test
public void testIsNotEmpty() {
Assert.assertFalse(wrap(Collections.emptyList()).isNotEmpty());
Assert.assertTrue(wrap(asList(1, 2, 3)).isNotEmpty());
}
@Test
public void testJoining() {
List<Integer> list = asList(1, 2, 3);
String joining = wrap(list).join();
Assert.assertEquals("123", joining);
Assert.assertEquals("1,2,3", wrap(list).join(","));
Assert.assertEquals("(1,2,3)", wrap(list).join(",", "(", ")"));
}
@Test
public void testGrouping() {
List<Integer> list = asList(1, 2, 3);
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>() {{
put("1", singletonList(1));
put("2", singletonList(2));
put("3", singletonList(3));
}};
Map<String, List<Integer>> group = wrap(list).group(String::valueOf, HashMap::new, Collectors.toList());
Assert.assertEquals(map, group);
group = wrap(list).group(String::valueOf, Collectors.toList());
Assert.assertEquals(map, group);
group = wrap(list).group(String::valueOf);
Assert.assertEquals(map, group);
}
@Test
public void testPartitioning() {
List<Integer> list = asList(1, 2, 3);
Map<Boolean, List<Integer>> map = new HashMap<Boolean, List<Integer>>() {{
put(Boolean.TRUE, singletonList(2));
put(Boolean.FALSE, asList(1, 3));
}};
Map<Boolean, List<Integer>> partition = wrap(list).partitioning(t -> (t & 1) == 0, Collectors.toList());
Assert.assertEquals(map, partition);
partition = wrap(list).partitioning(t -> (t & 1) == 0);
Assert.assertEquals(map, partition);
}
@Test
public void testForEachIdx() {
List<Integer> elements = new ArrayList<>();
List<Integer> indexes = new ArrayList<>();
wrap(1, 2, 3).forEachIdx((t, i) -> {
elements.add(t);
indexes.add(i);
});
Assert.assertEquals(asList(1, 2, 3), elements);
Assert.assertEquals(asList(0, 1, 2), indexes);
}
@Test
public void testForEachOrderedIdx() {
List<Integer> elements = new ArrayList<>();
List<Integer> indexes = new ArrayList<>();
wrap(1, 2, 3).forEachOrderedIdx((t, i) -> {
elements.add(t);
indexes.add(i);
});
Assert.assertEquals(asList(1, 2, 3), elements);
Assert.assertEquals(asList(0, 1, 2), indexes);
}
@Test
public void testForEachOrdered() {
List<Integer> elements = new ArrayList<>();
wrap(1, 2, 3).forEachOrdered(elements::add);
Assert.assertEquals(asList(1, 2, 3), elements);
}
@Test
public void testForEach() {
List<Integer> elements = new ArrayList<>();
wrap(1, 2, 3).forEach(elements::add);
Assert.assertEquals(asList(1, 2, 3), elements);
}
@Test
public void testMapToInt() {
int[] array = wrap(1, 2, 3).mapToInt(Integer::intValue).toArray();
Assert.assertArrayEquals(new int[] {1, 2, 3}, array);
}
@Test
public void testMapToLong() {
long[] array = wrap(1L, 2L, 3L).mapToLong(Long::intValue).toArray();
Assert.assertArrayEquals(new long[] {1L, 2L, 3L}, array);
}
@Test
public void testMapToDouble() {
double[] array = wrap(1d, 2d, 3d).mapToDouble(Double::intValue).toArray();
Assert.assertEquals(1d, array[0], 0.01);
Assert.assertEquals(2d, array[1], 0.01);
Assert.assertEquals(3d, array[2], 0.01);
}
@Test
public void testFlatMapToInt() {
int[] array = wrap(1, 2, 3).flatMapToInt(IntStream::of).toArray();
Assert.assertArrayEquals(new int[] {1, 2, 3}, array);
}
@Test
public void testFlatMapToLong() {
long[] array = wrap(1L, 2L, 3L).flatMapToLong(LongStream::of).toArray();
Assert.assertArrayEquals(new long[] {1L, 2L, 3L}, array);
}
@Test
public void testFlatMapToDouble() {
double[] array = wrap(1d, 2d, 3d).flatMapToDouble(DoubleStream::of).toArray();
Assert.assertEquals(1d, array[0], 0.01);
Assert.assertEquals(2d, array[1], 0.01);
Assert.assertEquals(3d, array[2], 0.01);
}
@Test
public void testSorted() {
List<Integer> list = wrap(3, 1, 2).sorted().toList();
Assert.assertEquals(asList(1, 2, 3), list);
}
@Test
public void testPeek() {
List<Integer> elements = new ArrayList<>();
wrap(1, 2, 3).peek(elements::add).exec();
Assert.assertEquals(asList(1, 2, 3), elements);
}
@Test
public void testPeekIdx() {
List<Integer> elements = new ArrayList<>();
List<Integer> indexes = new ArrayList<>();
wrap(1, 2, 3).peekIdx((t, i) -> {
elements.add(t);
indexes.add(i);
}).exec();
Assert.assertEquals(asList(1, 2, 3), elements);
Assert.assertEquals(asList(0, 1, 2), indexes);
Set<Integer> elements2 = new HashSet<>();
Set<Integer> indexes2 = new HashSet<>();
wrap(1, 2, null).parallel().peekIdx((t, i) -> {
elements2.add(t);
indexes2.add(i);
}).exec();
Assert.assertEquals(new HashSet<>(asList(1, null, 2)), elements2);
Assert.assertEquals(new HashSet<>(asList(-1, -1, -1)), indexes2);
}
@Test
public void testLimit() {
List<Integer> list = wrap(1, 2, 3).limit(2L).toList();
Assert.assertEquals(asList(1, 2), list);
}
@Test
public void testSkip() {
List<Integer> list = wrap(1, 2, 3).skip(1L).toList();
Assert.assertEquals(asList(2, 3), list);
}
@Test
public void testToArray() {
Object[] array1 = wrap(1, 2, 3).toArray();
Assert.assertArrayEquals(new Object[]{1, 2, 3}, array1);
array1 = wrap(1, 2, 3).toArray(Object[]::new);
Assert.assertArrayEquals(new Object[]{1, 2, 3}, array1);
}
@Test
public void testReduce() {
Assert.assertEquals((Integer)6, wrap(1, 2, 3).reduce(Integer::sum).orElse(null));
Assert.assertEquals((Integer)6, wrap(1, 2, 3).reduce(0, Integer::sum));
Assert.assertEquals((Integer)6, wrap(1, 2, 3).reduce(0, Integer::sum, Integer::sum));
}
@Test
public void testCollect() {
Assert.assertEquals(asList(1, 2, 3), wrap(1, 2, 3).collect(Collectors.toList()));
Assert.assertEquals(
asList(1, 2, 3),
wrap(1, 2, 3).collect(ArrayList::new, List::add, List::addAll)
);
}
@Test
public void testMin() {
Assert.assertEquals((Integer)1, wrap(1, 2, 3).min(Comparator.comparingInt(Integer::intValue)).orElse(null));
}
@Test
public void testMax() {
Assert.assertEquals((Integer)3, wrap(1, 2, 3).max(Comparator.comparingInt(Integer::intValue)).orElse(null));
}
@Test
public void testCount() {
Assert.assertEquals(3, wrap(1, 2, 3).count());
}
@Test
public void testAnyMatch() {
Assert.assertTrue(wrap(1, 2, 3).anyMatch(t -> (t & 1) == 0));
Assert.assertFalse(wrap(1, 3).anyMatch(t -> (t & 1) == 0));
}
@Test
public void testAllMatch() {
Assert.assertFalse(wrap(1, 2, 3).allMatch(t -> (t & 1) == 0));
Assert.assertTrue(wrap(2, 4).anyMatch(t -> (t & 1) == 0));
}
@Test
public void testNoneMatch() {
Assert.assertFalse(wrap(1, 2, 3).noneMatch(t -> (t & 1) == 0));
Assert.assertTrue(wrap(1, 3).noneMatch(t -> (t & 1) == 0));
}
@Test
public void testFindAny() {
Assert.assertNotNull(wrap(1, 2, 3).findAny());
}
@Test
public void testIterator() {
Iterator<Integer> iter1 = Stream.of(1, 2, 3).iterator();
Iterator<Integer> iter2 = wrap(1, 2, 3).iterator();
while (iter1.hasNext() && iter2.hasNext()) {
Assert.assertEquals(iter1.next(), iter2.next());
}
}
@Test
public void testSpliterator() {
Spliterator<Integer> iter1 = Stream.of(1, 2, 3).spliterator();
Spliterator<Integer> iter2 = wrap(1, 2, 3).spliterator();
Assert.assertEquals(iter1.trySplit().estimateSize(), iter2.trySplit().estimateSize());
}
@Test
public void testIsParallel() {
Assert.assertTrue(wrap(Stream.of(1, 2, 3).parallel()).isParallel());
}
@Test
public void testSequential() {
Assert.assertFalse(wrap(Stream.of(1, 2, 3).parallel()).sequential().isParallel());
}
@Test
public void testUnordered() {
Assert.assertNotNull(wrap(Stream.of(1, 2, 3)).unordered());
}
@Test
public void testOnClose() {
AtomicBoolean atomicBoolean = new AtomicBoolean(false);
wrap(Stream.of(1, 2, 3).onClose(() -> atomicBoolean.set(true))).close();
Assert.assertTrue(atomicBoolean.get());
}
@Test
public void testClose() {
Wrapper<Integer> stream = wrap(Stream.of(1, 2, 3));
stream.close();
Assert.assertThrows(IllegalStateException.class, stream::exec);
}
@Test
public void testReverse() {
Assert.assertEquals(
asList(3, 2, 1), wrap(1, 2, 3).reverse().toList()
);
}
@Test
public void testParallel() {
Assert.assertTrue(wrap(1, 2, 3).parallel().isParallel());
}
@Test
public void testSplice() {
Assert.assertEquals(
asList(1, 4, 5), wrap(1, 2, 3).splice(1, 2, 4, 5).toList()
);
}
@Test
public void testTakeWhile() {
Assert.assertEquals(
asList(1, 2),
wrap(1, 2, 3, 4).takeWhile(i -> !Objects.equals(i, 3)).toList()
);
}
@Test
public void testDropWhile() {
Assert.assertEquals(
asList(3, 4),
wrap(1, 2, 3, 4).dropWhile(i -> !Objects.equals(i, 3)).toList()
);
}
@Test
public void testDistinct() {
Assert.assertEquals(
asList(1, 2, 3), wrap(1, 1, 2, 3).distinct().toList()
);
}
@Test
public void testLog() {
Assert.assertNotNull(wrap(1, 2, 3).log().toList());
}
@Test
public void testPush() {
Assert.assertEquals(
asList(1, 2, 3), wrap(1).push(2, 3).toList()
);
}
@Test
public void testUnshift() {
Assert.assertEquals(
asList(1, 2, 3), wrap(3).unshift(1, 2).toList()
);
}
@Test
public void testAppend() {
Assert.assertEquals(
asList(1, 2, 3), wrap(1).append(asList(2, 3)).toList()
);
Assert.assertEquals(
asList(1, 2, 3), wrap(1, 2, 3).append(null).toList()
);
}
@Test
public void testPrepend() {
Assert.assertEquals(
asList(1, 2, 3), wrap(3).prepend(asList(1, 2)).toList()
);
Assert.assertEquals(
asList(1, 2, 3), wrap(1, 2, 3).prepend(null).toList()
);
}
@Test
public void testNonNull() {
Assert.assertEquals(
asList(1, 3), wrap(1, null, 3).nonNull().toList()
);
}
@Test
public void testFilterIdx() {
List<Integer> indexes = new ArrayList<>();
Assert.assertEquals(
asList(1, 3),
wrap(1, 2, 3).filterIdx((t, i) -> {
indexes.add(i);
return (t & 1) == 1;
}).toList()
);
Assert.assertEquals(asList(0, 1, 2), indexes);
}
@Test
public void testFilter() {
Assert.assertEquals(
asList(1, 3), wrap(1, 2, 3).filter(i -> (i & 1) == 1).toList()
);
}
@Test
public void testFlatMap() {
Assert.assertEquals(
asList(1, 2, 3), wrap(1, 2, 3).flatMap(Stream::of).toList()
);
}
@Test
public void testFlatMapIdx() {
List<Integer> indexes = new ArrayList<>();
Assert.assertEquals(
asList(1, 2, 3), wrap(1, 2, 3).flatMapIdx((t, i) -> {
indexes.add(i);
return Stream.of(t);
}).toList()
);
Assert.assertEquals(asList(0, 1, 2), indexes);
}
@Test
public void testFlat() {
Assert.assertEquals(
asList(1, 2, 3), wrap(1, 2, 3).flat(Collections::singletonList).toList()
);
}
@Test
public void testFlatNonNull() {
Assert.assertEquals(
asList(2, 3), wrap(null, 2, 3).flatNonNull(Collections::singletonList).toList()
);
}
@Test
public void testFlatTree() {
Tree root = new Tree(1, asList(new Tree(2, asList(new Tree(3, Collections.emptyList())))));
Assert.assertEquals(3L, wrap(root).flatTree(Tree::getChildren, Tree::setChildren).count());
}
@Test
public void testMap() {
Assert.assertEquals(
asList("1", "2", "3"), wrap(1, 2, 3).map(String::valueOf).toList()
);
}
@Test
public void testMapNonNull() {
Assert.assertEquals(
asList("3"), wrap(null, 2, 3, 4).mapNonNull(t -> ((t & 1) == 0) ? null : String.valueOf(t)).toList()
);
}
@Test
public void testMapIdx() {
List<Integer> indexes = new ArrayList<>();
Assert.assertEquals(
asList("1", "2", "3"), wrap(1, 2, 3).mapIdx((t, i) -> {
indexes.add(i);
return String.valueOf(t);
}).toList()
);
Assert.assertEquals(asList(0, 1, 2), indexes);
}
@Test
public void testMapMulti() {
Assert.assertEquals(
asList(1, 2, 3),
wrap(1, 2, 3).mapMulti((t, builder) -> {
builder.accept(t);
}).toList()
);
}
@Test
public void testHashCode() {
Stream<Integer> stream = Stream.of(1, 2, 3);
Assert.assertEquals(stream.hashCode(), wrap(stream).hashCode());
}
@Test
public void testEquals() {
Stream<Integer> stream = Stream.of(1, 2, 3);
Assert.assertEquals(wrap(stream), stream);
}
@Test
public void testToString() {
Stream<Integer> stream = Stream.of(1, 2, 3);
Assert.assertEquals(stream.toString(), wrap(stream).toString());
}
@Test
public void testToEntries() {
Map<Integer, Integer> expect = new HashMap<Integer, Integer>(){{
put(1, 1);
put(2, 2);
put(3, 3);
}};
Map<Integer, Integer> map = EasyStream.of(1, 2, 3)
.toEntries(Function.identity(), Function.identity())
.toMap();
Assert.assertEquals(expect, map);
map = EasyStream.of(1, 2, 3)
.toEntries(Function.identity())
.toMap();
Assert.assertEquals(expect, map);
}
@Test
public void testZip() {
final List<Integer> orders = Arrays.asList(1, 2, 3);
final List<String> list = Arrays.asList("dromara", "hutool", "sweet");
List<String> zip = wrap(orders).zip(list, (e1, e2) -> e1 + "." + e2).toList();
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
zip = wrap((Stream<? extends Object>)EasyStream.iterate(1, i -> i + 1)).zip(list, (e1, e2) -> e1 + "." + e2).toList();
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
}
@Test
public void testListSplit() {
final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> lists = wrap(list).split(2).map(TerminableWrappedStream::toList).toList();
Assert.assertEquals(ListUtil.split(list, 2), lists);
// 指定长度 大于等于 列表长度
lists = wrap(list).split(list.size()).map(TerminableWrappedStream::toList).toList();
Assert.assertEquals(singletonList(list), lists);
}
@Test
public void testSplitList() {
final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> lists = wrap(list).splitList(2).toList();
Assert.assertEquals(ListUtil.split(list, 2), lists);
// 指定长度 大于等于 列表长度
lists = wrap(list).splitList(list.size()).toList();
Assert.assertEquals(singletonList(list), lists);
}
@SafeVarargs
private static <T> Wrapper<T> wrap(T... array) {
return new Wrapper<>(Stream.of(array));
}
private static <T> Wrapper<T> wrap(Iterable<T> iterable) {
return new Wrapper<>(StreamSupport.stream(iterable.spliterator(), false));
}
private static <T> Wrapper<T> wrap(Stream<T> stream) {
return new Wrapper<>(stream);
}
private static class Wrapper<T> extends AbstractEnhancedWrappedStream<T, Wrapper<T>> {
/**
* 创建一个流包装器
*
* @param stream 包装的流对象
* @throws NullPointerException 当{@code stream}为{@code null}时抛出
*/
protected Wrapper(Stream<T> stream) {
super(stream);
}
@Override
public Wrapper<T> wrapping(Stream<T> source) {
return new Wrapper<>(source);
}
}
@Setter
@Getter
@AllArgsConstructor
private static class Tree {
private final Integer id;
private List<Tree> children;
}
}

View File

@@ -1,9 +1,7 @@
package cn.hutool.core.stream;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Tolerate;
import org.junit.Assert;
@@ -22,6 +20,13 @@ import static java.util.Collections.singletonList;
*/
public class EasyStreamTest {
@Test
public void testConcat() {
Stream<Integer> stream1 = Stream.of(1, 2);
Stream<Integer> stream2 = Stream.of(3, 4);
Assert.assertEquals(4, EasyStream.concat(stream1, stream2).count());
}
@Test
public void testBuilder() {
final List<Integer> list = EasyStream.<Integer>builder().add(1).add(2).add(3).build().toList();
@@ -54,7 +59,7 @@ public class EasyStreamTest {
}
@Test
public void testToCollection() {
public void testToColl() {
final List<Integer> list = Arrays.asList(1, 2, 3);
final List<String> toCollection = EasyStream.of(list).map(String::valueOf).toColl(LinkedList::new);
Assert.assertEquals(Arrays.asList("1", "2", "3"), toCollection);
@@ -168,18 +173,22 @@ public class EasyStreamTest {
final List<Integer> collect1 = list.stream().distinct().collect(Collectors.toList());
final List<Integer> collect2 = list.stream().parallel().distinct().collect(Collectors.toList());
// 使用FastStream去重
// 使用EasyStream去重
final List<Integer> distinctBy1 = EasyStream.of(list).distinct().toList();
final List<Integer> distinctBy2 = EasyStream.of(list).parallel().distinct(String::valueOf).toList();
Assert.assertEquals(collect1, distinctBy1);
Assert.assertEquals(collect2, distinctBy2);
Assert.assertEquals(
4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true).distinct(t -> Objects.isNull(t) ? null : t.toString()).count()
);
}
@Test
public void testForeachIdx() {
final List<String> list = Arrays.asList("dromara", "hutool", "sweet");
final EasyStream.FastStreamBuilder<String> builder = EasyStream.builder();
final EasyStream.Builder<String> builder = EasyStream.builder();
EasyStream.of(list).forEachIdx((e, i) -> builder.accept(i + 1 + "." + e));
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), builder.build().toList());
// 并行流时为-1
@@ -189,11 +198,11 @@ public class EasyStreamTest {
@Test
public void testForEachOrderedIdx() {
final List<String> list = Arrays.asList("dromara", "hutool", "sweet");
final EasyStream.FastStreamBuilder<String> builder = EasyStream.builder();
final EasyStream.Builder<String> builder = EasyStream.builder();
EasyStream.of(list).forEachOrderedIdx((e, i) -> builder.accept(i + 1 + "." + e));
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), builder.build().toList());
final EasyStream.FastStreamBuilder<String> streamBuilder = EasyStream.builder();
final EasyStream.Builder<String> streamBuilder = EasyStream.builder();
EasyStream.of(list).parallel().forEachOrderedIdx((e, i) -> streamBuilder.accept(i + 1 + "." + e));
Assert.assertEquals(Arrays.asList("0.dromara", "0.hutool", "0.sweet"), streamBuilder.build().toList());
@@ -360,39 +369,6 @@ public class EasyStreamTest {
Assert.assertEquals(ListUtil.of((Object) null), EasyStream.of((Object) null).reverse().toList());
}
@Test
public void testZip() {
final List<Integer> orders = Arrays.asList(1, 2, 3);
final List<String> list = Arrays.asList("dromara", "hutool", "sweet");
List<String> zip = EasyStream.of(orders).zip(list, (e1, e2) -> e1 + "." + e2).toList();
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
zip = EasyStream.iterate(1, i -> i + 1).zip(list, (e1, e2) -> e1 + "." + e2).toList();
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
}
@Test
public void testListSplit() {
final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> lists = EasyStream.of(list).split(2).map(EasyStream::toList).toList();
Assert.assertEquals(ListUtil.split(list, 2), lists);
// 指定长度 大于等于 列表长度
lists = EasyStream.of(list).split(list.size()).map(EasyStream::toList).toList();
Assert.assertEquals(singletonList(list), lists);
}
@Test
public void testSplitList() {
final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> lists = EasyStream.of(list).splitList(2).toList();
Assert.assertEquals(ListUtil.split(list, 2), lists);
// 指定长度 大于等于 列表长度
lists = EasyStream.of(list).splitList(list.size()).toList();
Assert.assertEquals(singletonList(list), lists);
}
@Test
public void testTakeWhile() {
// 1 到 10
@@ -557,7 +533,7 @@ public class EasyStreamTest {
}
@Data
@Builder
@lombok.Builder
public static class Student {
private String name;
private Integer age;

View File

@@ -31,6 +31,17 @@ public class EntryStreamTest {
EntryStream.merge(Arrays.asList(1, 2), Arrays.asList(1, 2, 3))
.collectKeys(Collectors.toList())
);
Assert.assertEquals(
Arrays.asList(1, 2),
EntryStream.merge(null, Arrays.asList(1, 2))
.collectValues(Collectors.toList())
);
Assert.assertEquals(
Arrays.asList(1, 2),
EntryStream.merge(Arrays.asList(1, 2), null)
.collectKeys(Collectors.toList())
);
}
@Test
@@ -38,15 +49,21 @@ public class EntryStreamTest {
Map<String, String> map = new HashMap<>();
map.put("1", "1");
Assert.assertEquals(1, EntryStream.of(map).count());
Assert.assertEquals(0, EntryStream.of((Map<String, String>)null).count());
Set<Map.Entry<Integer, Integer>> entries = new HashSet<>();
entries.add(new Entry<>(1, 1));
entries.add(null);
Assert.assertEquals(2, EntryStream.of(entries).count());
Assert.assertEquals(0, EntryStream.of((Set<Map.Entry<Integer, Integer>>)null).count());
Assert.assertEquals(2, EntryStream.of(entries.stream()).count());
Assert.assertEquals(0, EntryStream.of((Stream<Map.Entry<Integer, Integer>>)null).count());
Assert.assertEquals(2, new EntryStream<>(entries.stream()).count());
Assert.assertThrows(NullPointerException.class, () -> new EntryStream<>(null));
Iterable<Integer> iterable = Arrays.asList(1, 2, null);
Assert.assertEquals(3, EntryStream.of(iterable, Function.identity(), Function.identity()).count());
Assert.assertEquals(0, EntryStream.of(null, Function.identity(), Function.identity()).count());
}
@Test
@@ -117,11 +134,65 @@ public class EntryStreamTest {
Assert.assertEquals(
5,
EntryStream.of(Arrays.asList(1, 2, 3), Function.identity(), Function.identity())
.append(4, 4)
.append(5, 5)
.push(4, 4)
.push(5, 5)
.count()
);
}
@Test
public void testUnshift() {
Assert.assertEquals(
5,
EntryStream.of(Arrays.asList(1, 2, 3), Function.identity(), Function.identity())
.unshift(4, 4)
.unshift(5, 5)
.count()
);
}
@Test
public void testAppend() {
Map<Integer, Integer> map1 = new HashMap<Integer, Integer>(){{
put(1, 1);
put(2, 2);
}};
Map<Integer, Integer> map2 = new HashMap<Integer, Integer>(){{
put(3, 3);
put(4, 4);
}};
Assert.assertEquals(
new ArrayList<Map.Entry<Integer, Integer>>(){{
addAll(map1.entrySet());
addAll(map2.entrySet());
}},
EntryStream.of(map1).append(map2.entrySet()).toList()
);
Assert.assertEquals(
new ArrayList<>(map1.entrySet()), EntryStream.of(map1).append(null).toList()
);
}
@Test
public void testPrepend() {
Map<Integer, Integer> map1 = new HashMap<Integer, Integer>(){{
put(1, 1);
put(2, 2);
}};
Map<Integer, Integer> map2 = new HashMap<Integer, Integer>(){{
put(3, 3);
put(4, 4);
}};
Assert.assertEquals(
new ArrayList<Map.Entry<Integer, Integer>>(){{
addAll(map2.entrySet());
addAll(map1.entrySet());
}},
EntryStream.of(map1).prepend(map2.entrySet()).toList()
);
Assert.assertEquals(
new ArrayList<>(map1.entrySet()), EntryStream.of(map1).prepend(null).toList()
);
}
@Test
@@ -230,6 +301,12 @@ public class EntryStreamTest {
.map((k, v) -> k.toString() + v.toString())
.collect(Collectors.toList())
);
Assert.assertEquals(
Arrays.asList("11", "22", "33"),
EntryStream.of(map)
.map(e -> e.getKey().toString() + e.getValue().toString())
.collect(Collectors.toList())
);
}
@Test
@@ -457,7 +534,7 @@ public class EntryStreamTest {
Map<Integer, Integer> map = new HashMap<>();
map.put(1, null);
map.put(null, 1);
Assert.assertEquals(0, EntryStream.of(map).nonNull().count());
Assert.assertEquals(0, EntryStream.of(map).nonNullKeyValue().count());
}
@Test
@@ -465,7 +542,7 @@ public class EntryStreamTest {
Map<Integer, Integer> map = new HashMap<>();
map.put(1, null);
map.put(null, 1);
Assert.assertEquals(1, EntryStream.of(map).keyNonNull().count());
Assert.assertEquals(1, EntryStream.of(map).nonNullKey().count());
}
@Test
@@ -473,7 +550,7 @@ public class EntryStreamTest {
Map<Integer, Integer> map = new HashMap<>();
map.put(1, null);
map.put(null, 1);
Assert.assertEquals(1, EntryStream.of(map).valueNonNull().count());
Assert.assertEquals(1, EntryStream.of(map).nonNullValue().count());
}
private static class Entry<K, V> implements Map.Entry<K, V> {