Merge branch 'v6-dev' of gitee.com:dromara/hutool into v6-dev

Signed-off-by: 阿超 <achao1441470436@gmail.com>
This commit is contained in:
阿超
2022-09-07 07:55:05 +00:00
committed by Gitee
10 changed files with 2366 additions and 1188 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() {
final List<Integer> list = asList(1, 2, 3);
final List<Integer> toList = wrap(list).toList();
Assert.assertEquals(list, toList);
}
@Test
public void testToUnmodifiableList() {
final List<Integer> list = wrap(1, 2, 3)
.toUnmodifiableList();
Assert.assertThrows(UnsupportedOperationException.class, () -> list.remove(0));
}
@Test
public void testToSet() {
final 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() {
final Set<Integer> set = wrap(1, 2, 3)
.toUnmodifiableSet();
Assert.assertThrows(UnsupportedOperationException.class, () -> set.remove(0));
}
@Test
public void testToCollection() {
final List<Integer> list = asList(1, 2, 3);
final List<String> toCollection = wrap(list).map(String::valueOf).toColl(LinkedList::new);
Assert.assertEquals(asList("1", "2", "3"), toCollection);
}
@Test
public void testToMap() {
final List<Integer> list = asList(1, 2, 3);
final 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() {
final Map<Integer, Integer> map1 = wrap(1, 2, 3).toUnmodifiableMap(Function.identity(), Function.identity());
Assert.assertThrows(UnsupportedOperationException.class, () -> map1.remove(1));
final 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() {
final List<Integer> orders = asList(1, 2, 3);
final List<String> list = asList("dromara", "hutool", "sweet");
final 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() {
final List<Integer> list = wrap(1, 2, 3).transform(Wrapper::toList).orElse(null);
Assert.assertEquals(asList(1, 2, 3), list);
}
@Test
public void testFindFirst() {
final 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() {
final List<Integer> list = asList(1, 2, 3);
Assert.assertEquals(1, wrap(list).findFirstIdx(t -> (t & 1) == 0));
}
@Test
public void testFindLast() {
final List<Integer> list = asList(1, 2, 3);
Assert.assertEquals((Integer)3, wrap(list).findLast(t -> (t & 1) == 1).orElse(null));
}
@Test
public void testFindLastIdx() {
final List<Integer> list = asList(1, 2, 3);
Assert.assertEquals(1, wrap(list).findLastIdx(t -> (t & 1) == 0));
}
@Test
public void testAt() {
final 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() {
final List<Integer> list = asList(1, 2, 3);
final 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() {
final List<Integer> list = asList(1, 2, 3);
final 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() {
final List<Integer> list = asList(1, 2, 3);
final 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).partition(t -> (t & 1) == 0, Collectors.toList());
Assert.assertEquals(map, partition);
partition = wrap(list).partition(t -> (t & 1) == 0);
Assert.assertEquals(map, partition);
}
@Test
public void testForEachIdx() {
final List<Integer> elements = new ArrayList<>();
final 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() {
final List<Integer> elements = new ArrayList<>();
final 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() {
final List<Integer> elements = new ArrayList<>();
wrap(1, 2, 3).forEachOrdered(elements::add);
Assert.assertEquals(asList(1, 2, 3), elements);
}
@Test
public void testForEach() {
final List<Integer> elements = new ArrayList<>();
wrap(1, 2, 3).forEach(elements::add);
Assert.assertEquals(asList(1, 2, 3), elements);
}
@Test
public void testMapToInt() {
final int[] array = wrap(1, 2, 3).mapToInt(Integer::intValue).toArray();
Assert.assertArrayEquals(new int[] {1, 2, 3}, array);
}
@Test
public void testMapToLong() {
final long[] array = wrap(1L, 2L, 3L).mapToLong(Long::intValue).toArray();
Assert.assertArrayEquals(new long[] {1L, 2L, 3L}, array);
}
@Test
public void testMapToDouble() {
final 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() {
final int[] array = wrap(1, 2, 3).flatMapToInt(IntStream::of).toArray();
Assert.assertArrayEquals(new int[] {1, 2, 3}, array);
}
@Test
public void testFlatMapToLong() {
final long[] array = wrap(1L, 2L, 3L).flatMapToLong(LongStream::of).toArray();
Assert.assertArrayEquals(new long[] {1L, 2L, 3L}, array);
}
@Test
public void testFlatMapToDouble() {
final 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() {
final List<Integer> list = wrap(3, 1, 2).sorted().toList();
Assert.assertEquals(asList(1, 2, 3), list);
}
@Test
public void testPeek() {
final List<Integer> elements = new ArrayList<>();
wrap(1, 2, 3).peek(elements::add).exec();
Assert.assertEquals(asList(1, 2, 3), elements);
}
@Test
public void testPeekIdx() {
final List<Integer> elements = new ArrayList<>();
final 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);
final Set<Integer> elements2 = new HashSet<>();
final 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() {
final List<Integer> list = wrap(1, 2, 3).limit(2L).toList();
Assert.assertEquals(asList(1, 2), list);
}
@Test
public void testSkip() {
final 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() {
final Iterator<Integer> iter1 = Stream.of(1, 2, 3).iterator();
final Iterator<Integer> iter2 = wrap(1, 2, 3).iterator();
while (iter1.hasNext() && iter2.hasNext()) {
Assert.assertEquals(iter1.next(), iter2.next());
}
}
@Test
public void testSpliterator() {
final Spliterator<Integer> iter1 = Stream.of(1, 2, 3).spliterator();
final 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() {
final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
wrap(Stream.of(1, 2, 3).onClose(() -> atomicBoolean.set(true))).close();
Assert.assertTrue(atomicBoolean.get());
}
@Test
public void testClose() {
final 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() {
final 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() {
final 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() {
final 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() {
final 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() {
final Stream<Integer> stream = Stream.of(1, 2, 3);
Assert.assertEquals(stream.hashCode(), wrap(stream).hashCode());
}
@Test
public void testEquals() {
final Stream<Integer> stream = Stream.of(1, 2, 3);
Assert.assertEquals(wrap(stream), stream);
}
@Test
public void testToString() {
final Stream<Integer> stream = Stream.of(1, 2, 3);
Assert.assertEquals(stream.toString(), wrap(stream).toString());
}
@Test
public void testToEntries() {
final 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 unwrap}为{@code null}时抛出
*/
protected Wrapper(Stream<T> stream) {
super(stream);
}
@Override
public Wrapper<T> wrap(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() {
final Stream<Integer> stream1 = Stream.of(1, 2);
final 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()).sequential().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

@@ -6,6 +6,7 @@ import org.junit.Test;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -30,22 +31,39 @@ 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
public void testOf() {
Map<String, String> map = new HashMap<>();
final 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<>();
final 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);
final 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
@@ -55,7 +73,7 @@ public class EntryStreamTest {
@Test
public void testDistinctByKey() {
long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
final long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
.distinctByKey()
.count();
Assert.assertEquals(2, count);
@@ -63,7 +81,7 @@ public class EntryStreamTest {
@Test
public void testDistinctByValue() {
long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
final long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
.distinctByValue()
.count();
Assert.assertEquals(2, count);
@@ -71,7 +89,7 @@ public class EntryStreamTest {
@Test
public void testFilter() {
long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
final long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
.filter((k, v) -> k == 1 && v == 1)
.count();
Assert.assertEquals(1, count);
@@ -79,7 +97,7 @@ public class EntryStreamTest {
@Test
public void testFilterByKey() {
long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
final long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
.filterByKey(k -> k == 1)
.count();
Assert.assertEquals(2, count);
@@ -87,7 +105,7 @@ public class EntryStreamTest {
@Test
public void testFilterByValue() {
long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
final long count = EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
.filterByValue(v -> v == 1)
.count();
Assert.assertEquals(2, count);
@@ -95,7 +113,7 @@ public class EntryStreamTest {
@Test
public void testPeekKey() {
List<Integer> keys = new ArrayList<>();
final List<Integer> keys = new ArrayList<>();
EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
.peekKey(keys::add)
.count();
@@ -104,7 +122,7 @@ public class EntryStreamTest {
@Test
public void testPeekValue() {
List<Integer> values = new ArrayList<>();
final List<Integer> values = new ArrayList<>();
EntryStream.of(Arrays.asList(new Entry<>(1, 1), new Entry<>(1, 2), new Entry<>(2, 1), new Entry<>(2, 2)))
.peekValue(values::add)
.count();
@@ -120,12 +138,66 @@ public class EntryStreamTest {
.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() {
final Map<Integer, Integer> map1 = new HashMap<Integer, Integer>(){{
put(1, 1);
put(2, 2);
}};
final 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() {
final Map<Integer, Integer> map1 = new HashMap<Integer, Integer>(){{
put(1, 1);
put(2, 2);
}};
final 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
public void testSortByKey() {
List<Map.Entry<Integer, Integer>> entries = EntryStream.of(Arrays.asList(new Entry<>(3, 1), new Entry<>(2, 1), new Entry<>(4, 1), new Entry<>(1, 1)))
final List<Map.Entry<Integer, Integer>> entries = EntryStream.of(Arrays.asList(new Entry<>(3, 1), new Entry<>(2, 1), new Entry<>(4, 1), new Entry<>(1, 1)))
.sortByKey(Comparator.comparingInt(Integer::intValue))
.collect(Collectors.toList());
Assert.assertEquals(
@@ -136,7 +208,7 @@ public class EntryStreamTest {
@Test
public void testSortByValue() {
List<Map.Entry<Integer, Integer>> entries = EntryStream.of(Arrays.asList(new Entry<>(4, 4), new Entry<>(2, 2), new Entry<>(1, 1), new Entry<>(3, 3)))
final List<Map.Entry<Integer, Integer>> entries = EntryStream.of(Arrays.asList(new Entry<>(4, 4), new Entry<>(2, 2), new Entry<>(1, 1), new Entry<>(3, 3)))
.sortByValue(Comparator.comparingInt(Integer::intValue))
.collect(Collectors.toList());
Assert.assertEquals(
@@ -147,7 +219,7 @@ public class EntryStreamTest {
@Test
public void testToValueStream() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -158,7 +230,7 @@ public class EntryStreamTest {
@Test
public void testToKeyStream() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -169,7 +241,7 @@ public class EntryStreamTest {
@Test
public void testCollectKey() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -179,7 +251,7 @@ public class EntryStreamTest {
@Test
public void testCollectValue() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -189,7 +261,7 @@ public class EntryStreamTest {
@Test
public void testMapKeys() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -204,7 +276,7 @@ public class EntryStreamTest {
@Test
public void testMapValues() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -219,7 +291,7 @@ public class EntryStreamTest {
@Test
public void testMap() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -229,11 +301,17 @@ 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
public void testFlatMap() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -245,11 +323,11 @@ public class EntryStreamTest {
@Test
public void testFlatMapValue() {
Map<String, Integer> map = new HashMap<>();
final Map<String, Integer> map = new HashMap<>();
map.put("class1", 1);
map.put("class2", 2);
map.put("class3", 3);
List<String> values = EntryStream.of(map)
final List<String> values = EntryStream.of(map)
.flatMapKey(k -> Stream.of(k + "'s student1", k + "'s student2"))
.map((k, v) -> v + "=" + k)
.sorted()
@@ -266,11 +344,11 @@ public class EntryStreamTest {
@Test
public void testInverse() {
Map<String, String> map = new HashMap<>();
final Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
List<String> results = EntryStream.of(map)
final List<String> results = EntryStream.of(map)
.inverse()
.map((k, v) -> k + "=" + v)
.collect(Collectors.toList());
@@ -282,11 +360,11 @@ public class EntryStreamTest {
@Test
public void testFlatMapKey() {
Map<Integer, String> map = new HashMap<>();
final Map<Integer, String> map = new HashMap<>();
map.put(1, "class1");
map.put(2, "class2");
map.put(3, "class3");
List<String> values = EntryStream.of(map)
final List<String> values = EntryStream.of(map)
.flatMapValue(v -> Stream.of(v + "'s student1", v + "'s student2"))
.map((k, v) -> k + "=" + v)
.collect(Collectors.toList());
@@ -302,13 +380,13 @@ public class EntryStreamTest {
@Test
public void testForEach() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
List<Integer> keys = new ArrayList<>();
List<Integer> values = new ArrayList<>();
final List<Integer> keys = new ArrayList<>();
final List<Integer> values = new ArrayList<>();
EntryStream.of(map).forEach((k ,v) -> {
keys.add(k);
values.add(v);
@@ -319,7 +397,7 @@ public class EntryStreamTest {
@Test
public void testToMap() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -327,7 +405,7 @@ public class EntryStreamTest {
Map<Integer, Integer> result = EntryStream.of(map).toMap();
Assert.assertEquals(map, result);
result = EntryStream.of(map).toMap(LinkedHashMap::new);
result = EntryStream.of(map).toMap((Supplier<Map<Integer, Integer>>)LinkedHashMap::new);
Assert.assertEquals(new LinkedHashMap<>(map), result);
result = EntryStream.of(map).toMap(LinkedHashMap::new, (t1, t2) -> t1);
@@ -336,7 +414,7 @@ public class EntryStreamTest {
@Test
public void testToTable() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -360,7 +438,7 @@ public class EntryStreamTest {
@Test
public void testToTableByKey() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -384,7 +462,7 @@ public class EntryStreamTest {
@Test
public void testToTableByValue() {
Map<Integer, Integer> map = new HashMap<>();
final Map<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
@@ -408,19 +486,19 @@ public class EntryStreamTest {
@Test
public void testGroupByKey() {
Map<Integer, List<Integer>> map1 = EntryStream.of(Arrays.asList(1, 1, 2, 2), Function.identity(), Function.identity())
final Map<Integer, List<Integer>> map1 = EntryStream.of(Arrays.asList(1, 1, 2, 2), Function.identity(), Function.identity())
.groupByKey();
Assert.assertEquals(2, map1.size());
Assert.assertEquals(Arrays.asList(1, 1), map1.get(1));
Assert.assertEquals(Arrays.asList(2, 2), map1.get(2));
Map<Integer, Set<Integer>> map2 = EntryStream.of(Arrays.asList(1, 1, 2, 2), Function.identity(), Function.identity())
final Map<Integer, Set<Integer>> map2 = EntryStream.of(Arrays.asList(1, 1, 2, 2), Function.identity(), Function.identity())
.groupByKey(Collectors.toSet());
Assert.assertEquals(2, map2.size());
Assert.assertEquals(Collections.singleton(1), map2.get(1));
Assert.assertEquals(Collections.singleton(2), map2.get(2));
Map<Integer, Set<Integer>> map3 = EntryStream.of(Arrays.asList(1, 1, 2, 2), Function.identity(), Function.identity())
final Map<Integer, Set<Integer>> map3 = EntryStream.of(Arrays.asList(1, 1, 2, 2), Function.identity(), Function.identity())
.groupByKey(LinkedHashMap::new, Collectors.toSet());
Assert.assertEquals(2, map3.size());
Assert.assertEquals(Collections.singleton(1), map3.get(1));
@@ -453,26 +531,26 @@ public class EntryStreamTest {
@Test
public void testNonNull() {
Map<Integer, Integer> map = new HashMap<>();
final 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
public void testKeyNonNull() {
Map<Integer, Integer> map = new HashMap<>();
final 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
public void testValueNonNull() {
Map<Integer, Integer> map = new HashMap<>();
final 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> {