Predicate utils

This commit is contained in:
Andrea Cavalli 2023-06-21 02:43:19 +02:00
parent e9c765f7da
commit 7f7f13d7f3

View File

@ -5,6 +5,7 @@ import com.google.common.collect.Streams;
import it.cavallium.dbengine.utils.PartitionByIntSpliterator.IntPartition;
import it.cavallium.dbengine.utils.PartitionBySpliterator.Partition;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
@ -26,6 +27,7 @@ import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
import java.util.stream.Collector;
@ -377,6 +379,32 @@ public class StreamUtils {
}
}
public static <T> Predicate<T> and(Collection<? extends Predicate<? super T>> predicateList) {
Predicate<T> result = null;
for (Predicate<? super T> predicate : predicateList) {
if (result == null) {
//noinspection unchecked
result = (Predicate<T>) predicate;
} else {
result = result.and(predicate);
}
}
return result;
}
public static <T> Predicate<T> or(Collection<? extends Predicate<? super T>> predicateList) {
Predicate<T> result = null;
for (Predicate<? super T> predicate : predicateList) {
if (result == null) {
//noinspection unchecked
result = (Predicate<T>) predicate;
} else {
result = result.and(predicate);
}
}
return result;
}
private record BatchSpliterator<E>(Spliterator<E> base, int batchSize) implements Spliterator<List<E>> {
@Override