package org.sxkj.common.func;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.google.common.collect.Lists;
|
import com.google.common.collect.Maps;
|
import com.google.common.collect.Sets;
|
import lombok.AccessLevel;
|
import lombok.NoArgsConstructor;
|
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
import org.springframework.util.Assert;
|
|
import java.lang.RuntimeException;
|
import java.util.*;
|
import java.util.function.*;
|
import java.util.stream.Collectors;
|
import java.util.stream.Stream;
|
import java.util.stream.StreamSupport;
|
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
public class Streams {
|
|
public static <T> Stream<T> stream(List<T> elements) {
|
return CollectionUtils.isNotEmpty(elements) ? elements.stream() : Stream.empty();
|
}
|
|
public static <T> T first(Collection<T> elements, Predicate<T> predicate) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().filter(predicate).findFirst().orElse(null);
|
}
|
return null;
|
}
|
|
public static <T> Stream<T> stream(List<T> elements, boolean parallel) {
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return StreamSupport.stream(elements.spliterator(), parallel);
|
}
|
return Stream.empty();
|
}
|
|
public static <T, R> long countBy(List<T> elements, Predicate<T> predicate) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().filter(predicate).count();
|
}
|
return 0;
|
}
|
|
public static <T, R> List<R> toList(T[] elements, Function<T, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
|
if (ArrayUtils.isNotEmpty(elements)) {
|
return Arrays.stream(elements).map(mapper).collect(Collectors.toList());
|
}
|
return Lists.newArrayList();
|
}
|
|
public static <T, R> Set<R> toSet(Set<T> elements, Function<T, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().map(mapper).collect(Collectors.toSet());
|
}
|
return Sets.newHashSet();
|
}
|
|
public static <T> List<T> forEach(List<T> elements, Consumer<T> consumer) {
|
Assert.notNull(consumer, "consumer cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
elements.forEach(consumer);
|
return elements;
|
}
|
return Lists.newArrayList();
|
}
|
|
public static <T> void forEach(Collection<T> elements, Consumer<T> consumer) {
|
Assert.notNull(consumer, "consumer cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
elements.forEach(consumer);
|
}
|
}
|
|
public static <T> List<T> forEach(List<T> elements, Predicate<T> predicate, Consumer<T> consumer) {
|
Assert.notNull(consumer, "consumer cannot be not empty!");
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
elements.stream().filter(predicate).forEach(consumer);
|
return elements;
|
}
|
return Lists.newArrayList();
|
}
|
|
public static <T, R> Set<R> toSet(List<T> elements, Function<T, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().map(mapper).collect(Collectors.toSet());
|
}
|
return Sets.newHashSet();
|
}
|
|
public static <T> List<T> filter(List<T> elements, Predicate<T> predicate) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().filter(predicate).collect(Collectors.toList());
|
}
|
return Lists.newArrayList();
|
}
|
|
public static <K, V, R> R evalAs(Map<K, V> map, K key, Function<V, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
|
if (MapUtils.isNotEmpty(map) && map.containsKey(key)) {
|
return Optional.of(map.get(key)).map(mapper).orElse(null);
|
}
|
return null;
|
}
|
|
public static <T> IPage<T> toPage(IPage<T> page, Consumer<List<T>> consumer) {
|
Assert.notNull(consumer, "consumer cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(page.getRecords())) {
|
consumer.accept(page.getRecords());
|
}
|
return page;
|
}
|
|
public static <T, R> IPage toPage(IPage<T> page, Function<T, R> mapper) {
|
Assert.notNull(mapper, "function cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(page.getRecords())) {
|
List records = page.getRecords();
|
Object collect = records.stream()
|
.map(mapper)
|
.collect(Collectors.toList());
|
page.setRecords((List) collect);
|
}
|
return page;
|
}
|
|
public static <T, R> List<R> toList(Collection<T> elements, Function<T, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().map(mapper).collect(Collectors.toList());
|
}
|
return Lists.newArrayList();
|
}
|
|
public static <T> boolean anyMatch(Collection<T> elements, Predicate<T> predicate) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().anyMatch(predicate);
|
}
|
return false;
|
}
|
|
public static <T> void doContinue(Collection<T> elements, Predicate<T> predicate, Consumer<List<T>> consumer) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
Assert.notNull(consumer, "consumer cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements) && elements.stream().anyMatch(predicate)) {
|
consumer.accept(elements.stream().filter(predicate).collect(Collectors.toList()));
|
}
|
}
|
|
public static <T> boolean allMatch(Collection<T> elements, Predicate<T> predicate) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().allMatch(predicate);
|
}
|
return false;
|
}
|
|
public static <T> boolean noneMatch(Collection<T> elements, Predicate<T> predicate) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().noneMatch(predicate);
|
}
|
return false;
|
}
|
|
public static <K, V> void ifPresent(Map<K, V> map, K key, Consumer<V> consumer) {
|
Assert.notNull(consumer, "consumer cannot be not empty!");
|
|
if (MapUtils.isNotEmpty(map) && map.containsKey(key)) {
|
Optional.ofNullable(map.get(key)).ifPresent(consumer);
|
}
|
}
|
|
public static <T> void nonAccept(List<T> elements, Consumer<List<T>> recordConsumer) {
|
Assert.notNull(recordConsumer, "recordConsumer cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
recordConsumer.accept(elements);
|
}
|
}
|
|
public static <R> R reduce(List<R> elements, R identity, BinaryOperator<R> accumulator) {
|
Assert.notNull(identity, "identity cannot be not empty!");
|
Assert.notNull(accumulator, "accumulator cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().reduce(identity, accumulator);
|
}
|
return identity;
|
}
|
|
public static <T, K> Map<K, List<T>> groupBy(List<T> elements, Function<T, K> classifier) {
|
Assert.notNull(classifier, "classifier cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().collect(Collectors.groupingBy(classifier));
|
}
|
return Maps.newHashMap();
|
}
|
|
public static <K, V, R> Set<R> toSet(Map<K, V> map, Function<Map.Entry<K, V>, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
|
if (map != null && !map.isEmpty()) {
|
return map.entrySet().stream().map(mapper).collect(Collectors.toSet());
|
}
|
return Sets.newHashSet();
|
}
|
|
public static <K, V, R> List<R> toList(Map<K, V> map, Function<Map.Entry<K, V>, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
|
if (MapUtils.isNotEmpty(map)) {
|
return map.entrySet().stream().map(mapper).collect(Collectors.toList());
|
}
|
return Lists.newArrayList();
|
}
|
|
public static <T> void ifPresent(Collection<T> elements, Predicate<T> predicate, Consumer<T> consumer) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
Assert.notNull(consumer, "consumer cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
elements.stream().filter(predicate).findFirst().ifPresent(consumer);
|
}
|
}
|
|
public static <T, R> Set<R> toSet(List<T> elements, Predicate<T> predicate, Function<T, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().filter(predicate).map(mapper).collect(Collectors.toSet());
|
}
|
return Sets.newHashSet();
|
}
|
|
public static <K, V, R> Set<R> toSet(Map<K, V> map, Predicate<Map.Entry<K, V>> predicate, Function<Map.Entry<K, V>, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (map != null && !map.isEmpty()) {
|
return map.entrySet().stream().filter(predicate).map(mapper).collect(Collectors.toSet());
|
}
|
return Sets.newHashSet();
|
}
|
|
public static <T> T firstOrDefault(List<T> elements, Predicate<T> predicate, Supplier<T> supplier) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
Assert.notNull(supplier, "supplier cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().filter(predicate).findFirst().orElseGet(supplier);
|
}
|
return supplier.get();
|
}
|
|
public static <K, V, R> R evalAs(Map<K, V> map, K key, Function<V, R> mapper, R defaultValue) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
Assert.notNull(defaultValue, "defaultValue cannot be not empty!");
|
|
if (MapUtils.isNotEmpty(map) && map.containsKey(key)) {
|
return Optional.of(map.get(key)).map(mapper).orElse(defaultValue);
|
}
|
return defaultValue;
|
}
|
|
public static <K, V, R> R evalAs(Map<K, V> map, K key, Function<V, R> mapper, Supplier<R> supplier) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
Assert.notNull(supplier, "supplier cannot be not empty!");
|
|
if (MapUtils.isNotEmpty(map) && map.containsKey(key)) {
|
return Optional.of(map.get(key)).map(mapper).orElseGet(supplier);
|
}
|
return supplier.get();
|
}
|
|
public static <T, R> List<R> toList(List<T> elements, Predicate<T> predicate, Function<T, R> mapper) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().filter(predicate).map(mapper).collect(Collectors.toList());
|
}
|
return Lists.newArrayList();
|
}
|
|
public static <T> List<T> toSorted(List<T> elements, Comparator<T> comparator) {
|
Assert.notNull(comparator, "comparator cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().sorted(comparator).collect(Collectors.toList());
|
}
|
return Lists.newArrayList();
|
}
|
|
public static <T, R> List<R> toSorted(List<T> elements, Function<T, R> mapper, Comparator<R> comparator) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
Assert.notNull(comparator, "comparator cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().map(mapper).sorted(comparator).collect(Collectors.toList());
|
}
|
return Lists.newArrayList();
|
}
|
|
public static <K, V> void ifPresent(Map<K, List<V>> map, K key, Predicate<V> predicate, Consumer<V> consumer) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
Assert.notNull(consumer, "consumer cannot be not empty!");
|
|
if (MapUtils.isNotEmpty(map) && map.containsKey(key)) {
|
map.get(key).stream().filter(predicate).findFirst().ifPresent(consumer);
|
}
|
}
|
|
public static <T, K, U> Map<K, U> toMap(List<T> elements, Function<T, K> keyMapper, Function<T, U> valueMapper) {
|
if (CollectionUtils.isNotEmpty(elements) && keyMapper != null && valueMapper != null) {
|
return elements.stream().collect(Collectors.toMap(keyMapper, valueMapper));
|
}
|
return Maps.newHashMap();
|
}
|
|
public static <T, K, U> Map<K, U> toMap(T[] elements, Function<T, K> keyMapper, Function<T, U> valueMapper) {
|
if (ArrayUtils.isNotEmpty(elements) && keyMapper != null && valueMapper != null) {
|
return Arrays.stream(elements).collect(Collectors.toMap(keyMapper, valueMapper));
|
}
|
return Maps.newHashMap();
|
}
|
|
public static <T, K, U, M extends Map<K, U>> Map<K, U> toMap(List<T> elements, Function<T, K> keyMapper, Function<T, U> valueMapper, BinaryOperator<U> mergeFunction) {
|
if (CollectionUtils.isNotEmpty(elements) && keyMapper != null && valueMapper != null && mergeFunction != null) {
|
return elements.stream().collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction));
|
}
|
return Maps.newHashMap();
|
}
|
|
public static <T, K, U, M extends Map<K, U>> Map<K, U> toMap(List<T> elements, Function<T, K> keyMapper, Function<T, U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) {
|
if (CollectionUtils.isNotEmpty(elements) && keyMapper != null && valueMapper != null && mergeFunction != null && mapSupplier != null) {
|
return elements.stream().collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier));
|
}
|
return Maps.newLinkedHashMap();
|
}
|
|
public static <T, K> Map<K, List<T>> groupBy(List<T> elements, Predicate<T> predicate, Function<T, K> classifier) {
|
if (CollectionUtils.isNotEmpty(elements) && predicate != null && classifier != null) {
|
return elements.stream().filter(predicate).collect(Collectors.groupingBy(classifier));
|
}
|
return Maps.newHashMap();
|
}
|
|
/**
|
* 分组后计算总数
|
*/
|
public static <T, R> Map<R, Long> groupByCount(List<T> elements, Function<T, R> mapper) {
|
if (CollectionUtils.isNotEmpty(elements) && mapper != null) {
|
return elements.stream().map(mapper).collect(Collectors.groupingBy(
|
e -> e,
|
Collectors.counting()
|
));
|
}
|
return Maps.newHashMap();
|
}
|
|
|
public static <T, R> R reduce(List<T> elements, Function<T, R> mapper, R identity, BinaryOperator<R> accumulator) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
Assert.notNull(identity, "identity cannot be not empty!");
|
Assert.notNull(accumulator, "accumulator cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().map(mapper).reduce(identity, accumulator);
|
}
|
return identity;
|
}
|
|
public static <T, R> R reduce(List<T> elements, Predicate<T> predicate, Function<T, R> mapper, R identity, BinaryOperator<R> accumulator) {
|
Assert.notNull(mapper, "mapper cannot be not empty!");
|
Assert.notNull(identity, "identity cannot be not empty!");
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
Assert.notNull(accumulator, "accumulator cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().filter(predicate).map(mapper).reduce(identity, accumulator);
|
}
|
return identity;
|
}
|
|
public static <T> T minOrThrow(List<T> elements, Comparator<T> comparator, String exMessage) {
|
Assert.notNull(comparator, "comparator cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().min(comparator).orElseThrow(() -> new RuntimeException(exMessage));
|
}
|
throw new RuntimeException("elements cannot be not empty!");
|
}
|
|
public static <T> T maxOrThrow(List<T> elements, Comparator<T> comparator, String exMessage) {
|
Assert.notNull(comparator, "comparator cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().max(comparator).orElseThrow(() -> new java.lang.RuntimeException(exMessage));
|
}
|
throw new RuntimeException("elements cannot be not empty!");
|
}
|
|
public static <T> T firstOrThrow(List<T> elements, Predicate<T> predicate, String exMessage) {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().filter(predicate).findFirst().orElseThrow(() -> new RuntimeException(exMessage));
|
}
|
throw new RuntimeException("elements cannot be not empty!");
|
}
|
|
public static <T, X extends Throwable> T firstOrThrow(List<T> elements, Predicate<T> predicate, Supplier<X> exceptionSupplier) throws X {
|
Assert.notNull(predicate, "predicate cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().filter(predicate).findFirst().orElseThrow(exceptionSupplier);
|
}
|
throw new RuntimeException("elements cannot be not empty!");
|
}
|
|
public static <T, X extends Throwable> T minOrThrow(List<T> elements, Comparator<T> comparator, Supplier<X> exceptionSupplier) throws X {
|
Assert.notNull(comparator, "comparator cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().min(comparator).orElseThrow(exceptionSupplier);
|
}
|
throw new RuntimeException("elements cannot be not empty!");
|
}
|
|
public static <T, X extends Throwable> T maxOrThrow(List<T> elements, Comparator<T> comparator, Supplier<X> exceptionSupplier) throws X {
|
Assert.notNull(comparator, "comparator cannot be not empty!");
|
|
if (CollectionUtils.isNotEmpty(elements)) {
|
return elements.stream().max(comparator).orElseThrow(exceptionSupplier);
|
}
|
throw new RuntimeException("elements cannot be not empty!");
|
}
|
|
public static <T, K, U> Map<K, U> toMap(List<T> elements, Predicate<T> predicate, Function<T, K> keyMapper, Function<T, U> valueMapper) {
|
if (CollectionUtils.isNotEmpty(elements) && predicate != null && keyMapper != null && valueMapper != null) {
|
return elements.stream().filter(predicate).collect(Collectors.toMap(keyMapper, valueMapper));
|
}
|
return Maps.newHashMap();
|
}
|
|
public static <T, R> void doPartition(Collection<T> collection, Consumer<List<T>> consumer, int stepSize) {
|
Assert.notEmpty(collection, "collection cannot be not empty");
|
|
List<List<T>> partitionList = Lists.partition(Lists.newArrayList(collection), stepSize);
|
for (List<T> partition : partitionList) {
|
consumer.accept(partition);
|
}
|
}
|
|
public static <T> void doWhile(Function<IPage<?>, List<T>> recordPageFunction, Consumer<List<T>> recordConsumer, long batchSize) {
|
Assert.notNull(recordConsumer, "recordConsumer cannot be not empty!");
|
Assert.notNull(recordPageFunction, "recordPageFunction cannot be not empty!");
|
|
Page<?> page;
|
long current = 1;
|
List<T> batchRecords;
|
boolean hasNextPage = false;
|
long size = batchSize > 0 ? batchSize : 10;
|
do {
|
page = Page.of(current, size, Boolean.FALSE);
|
batchRecords = recordPageFunction.apply(page);
|
if (CollectionUtils.isNotEmpty(batchRecords)) {
|
recordConsumer.accept(batchRecords);
|
hasNextPage = batchRecords.size() >= size;
|
}
|
current++;
|
|
} while (CollectionUtils.isNotEmpty(batchRecords) && hasNextPage);
|
}
|
|
/**
|
* 数据进行平铺
|
*
|
* @param collections 二维数组平铺
|
* @param <T> 类型
|
* @return 一维数据
|
*/
|
public static <T> List<T> flatMap(List<List<T>> collections) {
|
if (CollectionUtils.isEmpty(collections)) {
|
return Lists.newArrayList();
|
}
|
return collections.stream()
|
.filter(Objects::nonNull)
|
.flatMap(List::stream)
|
.filter(Objects::nonNull)
|
.collect(Collectors.toList());
|
}
|
|
|
|
|
|
}
|