aQute.bnd.stream.EntryPipeline Maven / Gradle / Ivy
The newest version!
package aQute.bnd.stream;
import static java.util.Objects.requireNonNull;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleBiFunction;
import java.util.function.ToIntBiFunction;
import java.util.function.ToLongBiFunction;
import java.util.stream.Collector;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
final class EntryPipeline implements MapStream {
private final Stream> stream;
@SuppressWarnings("unchecked")
EntryPipeline(Stream extends Entry extends K, ? extends V>> stream) {
this.stream = requireNonNull((Stream>) stream);
}
@Override
public Stream> entries() {
return stream;
}
@Override
public Stream keys() {
return entries().map(Entry::getKey);
}
@Override
public Stream values() {
return entries().map(Entry::getValue);
}
@Override
public Iterator> iterator() {
return entries().iterator();
}
@Override
public Spliterator> spliterator() {
return entries().spliterator();
}
@Override
public boolean isParallel() {
return entries().isParallel();
}
@Override
public MapStream sequential() {
Stream> sequential = entries().sequential();
return (entries() == sequential) ? this : new EntryPipeline<>(sequential);
}
@Override
public MapStream parallel() {
Stream> parallel = entries().parallel();
return (entries() == parallel) ? this : new EntryPipeline<>(parallel);
}
@Override
public MapStream unordered() {
Stream> unordered = entries().unordered();
return (entries() == unordered) ? this : new EntryPipeline<>(unordered);
}
@Override
public MapStream onClose(Runnable closeHandler) {
Stream> onClose = entries().onClose(closeHandler);
return (entries() == onClose) ? this : new EntryPipeline<>(onClose);
}
@Override
public void close() {
entries().close();
}
@Override
public MapStream distinct() {
return new EntryPipeline<>(entries().distinct());
}
@Override
public MapStream filter(BiPredicate super K, ? super V> filter) {
requireNonNull(filter);
return new EntryPipeline<>(entries().filter(e -> filter.test(e.getKey(), e.getValue())));
}
@Override
public MapStream filterKey(Predicate super K> filter) {
requireNonNull(filter);
return new EntryPipeline<>(entries().filter(e -> filter.test(e.getKey())));
}
@Override
public MapStream filterValue(Predicate super V> filter) {
requireNonNull(filter);
return new EntryPipeline<>(entries().filter(e -> filter.test(e.getValue())));
}
@Override
public MapStream map(
BiFunction super K, ? super V, ? extends Entry extends R, ? extends S>> mapper) {
requireNonNull(mapper);
return new EntryPipeline<>(entries().map(e -> mapper.apply(e.getKey(), e.getValue())));
}
@Override
public MapStream mapKey(Function super K, ? extends R> mapper) {
requireNonNull(mapper);
return new EntryPipeline<>(entries().map(e -> MapStream.entry(mapper.apply(e.getKey()), e.getValue())));
}
@Override
public MapStream mapValue(Function super V, ? extends S> mapper) {
requireNonNull(mapper);
return new EntryPipeline<>(entries().map(e -> MapStream.entry(e.getKey(), mapper.apply(e.getValue()))));
}
@Override
public Stream mapToObj(BiFunction super K, ? super V, ? extends R> mapper) {
requireNonNull(mapper);
return entries().map(e -> mapper.apply(e.getKey(), e.getValue()));
}
@Override
public IntStream mapToInt(ToIntBiFunction super K, ? super V> mapper) {
requireNonNull(mapper);
return entries().mapToInt(e -> mapper.applyAsInt(e.getKey(), e.getValue()));
}
@Override
public LongStream mapToLong(ToLongBiFunction super K, ? super V> mapper) {
requireNonNull(mapper);
return entries().mapToLong(e -> mapper.applyAsLong(e.getKey(), e.getValue()));
}
@Override
public DoubleStream mapToDouble(ToDoubleBiFunction super K, ? super V> mapper) {
requireNonNull(mapper);
return entries().mapToDouble(e -> mapper.applyAsDouble(e.getKey(), e.getValue()));
}
@Override
public MapStream flatMap(
BiFunction super K, ? super V, ? extends MapStream extends R, ? extends S>> mapper) {
requireNonNull(mapper);
return new EntryPipeline<>(entries().flatMap(e -> mapper.apply(e.getKey(), e.getValue())
.entries()));
}
@Override
public MapStream flatMapKey(Function super K, ? extends Stream extends R>> mapper) {
requireNonNull(mapper);
return new EntryPipeline<>(entries().flatMap(e -> mapper.apply(e.getKey())
.map(k -> MapStream.entry(k, e.getValue()))));
}
@Override
public MapStream flatMapValue(Function super V, ? extends Stream extends S>> mapper) {
requireNonNull(mapper);
return new EntryPipeline<>(entries().flatMap(e -> mapper.apply(e.getValue())
.map(v -> MapStream.entry(e.getKey(), v))));
}
@Override
public Stream flatMapToObj(BiFunction super K, ? super V, ? extends Stream extends R>> mapper) {
requireNonNull(mapper);
return entries().flatMap(e -> mapper.apply(e.getKey(), e.getValue()));
}
@Override
public IntStream flatMapToInt(BiFunction super K, ? super V, ? extends IntStream> mapper) {
requireNonNull(mapper);
return entries().flatMapToInt(e -> mapper.apply(e.getKey(), e.getValue()));
}
@Override
public LongStream flatMapToLong(BiFunction super K, ? super V, ? extends LongStream> mapper) {
requireNonNull(mapper);
return entries().flatMapToLong(e -> mapper.apply(e.getKey(), e.getValue()));
}
@Override
public DoubleStream flatMapToDouble(BiFunction super K, ? super V, ? extends DoubleStream> mapper) {
requireNonNull(mapper);
return entries().flatMapToDouble(e -> mapper.apply(e.getKey(), e.getValue()));
}
@Override
public MapStream peek(BiConsumer super K, ? super V> peek) {
requireNonNull(peek);
return new EntryPipeline<>(entries().peek(e -> peek.accept(e.getKey(), e.getValue())));
}
@Override
public MapStream peekKey(Consumer super K> peek) {
requireNonNull(peek);
return new EntryPipeline<>(entries().peek(e -> peek.accept(e.getKey())));
}
@Override
public MapStream peekValue(Consumer super V> peek) {
requireNonNull(peek);
return new EntryPipeline<>(entries().peek(e -> peek.accept(e.getValue())));
}
@SuppressWarnings({
"unchecked", "rawtypes"
})
// If K is not Comparable, a ClassCastException may be thrown when the
// terminal operation is executed.
private static Comparator> comparingByKey() {
return (Comparator) Entry.comparingByKey();
}
@SuppressWarnings({
"unchecked", "rawtypes"
})
// If V is not Comparable, a ClassCastException may be thrown when the
// terminal operation is executed.
private static Comparator> comparingByValue() {
return (Comparator) Entry.comparingByValue();
}
@SuppressWarnings({
"unchecked", "rawtypes"
})
// If K and V are not Comparable, a ClassCastException may be thrown
// when the terminal operation is executed.
private static Comparator> comparing() {
return ((Comparator) Entry.comparingByKey()).thenComparing(Entry.comparingByValue());
}
@Override
public MapStream sorted() {
return sorted(comparing());
}
@Override
public MapStream sorted(Comparator super Entry> comparator) {
return new EntryPipeline<>(entries().sorted(comparator));
}
@Override
public MapStream sortedByKey() {
return sorted(comparingByKey());
}
@Override
public MapStream sortedByKey(Comparator super K> comparator) {
return sorted(Entry.comparingByKey(comparator));
}
@Override
public MapStream sortedByValue() {
return sorted(comparingByValue());
}
@Override
public MapStream sortedByValue(Comparator super V> comparator) {
return sorted(Entry.comparingByValue(comparator));
}
@Override
public MapStream limit(long maxSize) {
return new EntryPipeline<>(entries().limit(maxSize));
}
@Override
public MapStream skip(long n) {
return new EntryPipeline<>(entries().skip(n));
}
@Override
public void forEach(BiConsumer super K, ? super V> consumer) {
requireNonNull(consumer);
entries().forEach(e -> consumer.accept(e.getKey(), e.getValue()));
}
@Override
public void forEachOrdered(BiConsumer super K, ? super V> consumer) {
requireNonNull(consumer);
entries().forEachOrdered(e -> consumer.accept(e.getKey(), e.getValue()));
}
@Override
public long count() {
return entries().count();
}
@Override
public boolean anyMatch(BiPredicate super K, ? super V> predicate) {
requireNonNull(predicate);
return entries().anyMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
@Override
public boolean allMatch(BiPredicate super K, ? super V> predicate) {
requireNonNull(predicate);
return entries().allMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
@Override
public boolean noneMatch(BiPredicate super K, ? super V> predicate) {
requireNonNull(predicate);
return entries().noneMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
@Override
public R collect(Collector super Entry extends K, ? extends V>, A, R> collector) {
return entries().collect(collector);
}
@Override
public R collect(Supplier supplier, BiConsumer> accumulator,
BiConsumer combiner) {
return entries().collect(supplier, accumulator, combiner);
}
@Override
public Optional> max(Comparator super Entry> comparator) {
return entries().max(comparator);
}
@Override
public Optional> maxByKey(Comparator super K> comparator) {
return max(Entry.comparingByKey(comparator));
}
@Override
public Optional> maxByValue(Comparator super V> comparator) {
return max(Entry.comparingByValue(comparator));
}
@Override
public Optional> min(Comparator super Entry> comparator) {
return entries().min(comparator);
}
@Override
public Optional> minByKey(Comparator super K> comparator) {
return min(Entry.comparingByKey(comparator));
}
@Override
public Optional> minByValue(Comparator super V> comparator) {
return min(Entry.comparingByValue(comparator));
}
@Override
public Optional> findAny() {
return entries().findAny();
}
@Override
public Optional> findFirst() {
return entries().findFirst();
}
@Override
public Entry[] toArray() {
@SuppressWarnings("unchecked")
Entry[] array = entries().toArray(Entry[]::new);
return array;
}
@Override
public MapStream takeWhile(BiPredicate super K, ? super V> predicate) {
requireNonNull(predicate);
return new EntryPipeline<>(entries().takeWhile(e -> predicate.test(e.getKey(), e.getValue())));
}
@Override
public MapStream takeWhileKey(Predicate super K> predicate) {
requireNonNull(predicate);
return new EntryPipeline<>(entries().takeWhile(e -> predicate.test(e.getKey())));
}
@Override
public MapStream takeWhileValue(Predicate super V> predicate) {
requireNonNull(predicate);
return new EntryPipeline<>(entries().takeWhile(e -> predicate.test(e.getValue())));
}
@Override
public MapStream dropWhile(BiPredicate super K, ? super V> predicate) {
requireNonNull(predicate);
return new EntryPipeline<>(entries().dropWhile(e -> predicate.test(e.getKey(), e.getValue())));
}
@Override
public MapStream dropWhileKey(Predicate super K> predicate) {
requireNonNull(predicate);
return new EntryPipeline<>(entries().dropWhile(e -> predicate.test(e.getKey())));
}
@Override
public MapStream dropWhileValue(Predicate super V> predicate) {
requireNonNull(predicate);
return new EntryPipeline<>(entries().dropWhile(e -> predicate.test(e.getValue())));
}
@Override
public MapStream mapMulti(TriConsumer super K, ? super V, ? super BiConsumer> mapper) {
requireNonNull(mapper);
return new EntryPipeline<>(entries().> mapMulti((e, multi) -> {
BiConsumer biconsumer = (r, s) -> multi.accept(MapStream.entry(r, s));
mapper.accept(e.getKey(), e.getValue(), biconsumer);
}));
}
@Override
public MapStream mapMultiKey(BiConsumer super K, ? super Consumer> mapper) {
requireNonNull(mapper);
return new EntryPipeline<>(entries().> mapMulti((e, multi) -> {
Consumer consumer = (r) -> multi.accept(MapStream.entry(r, e.getValue()));
mapper.accept(e.getKey(), consumer);
}));
}
@Override
public MapStream mapMultiValue(BiConsumer super V, ? super Consumer> mapper) {
requireNonNull(mapper);
return new EntryPipeline<>(entries().> mapMulti((e, multi) -> {
Consumer consumer = (s) -> multi.accept(MapStream.entry(e.getKey(), s));
mapper.accept(e.getValue(), consumer);
}));
}
@Override
public Stream mapMultiToObj(TriConsumer super K, ? super V, ? super Consumer> mapper) {
requireNonNull(mapper);
return entries(). mapMulti((e, multi) -> mapper.accept(e.getKey(), e.getValue(), multi));
}
@Override
public IntStream mapMultiToInt(TriConsumer super K, ? super V, ? super IntConsumer> mapper) {
requireNonNull(mapper);
return entries().mapMultiToInt((e, multi) -> mapper.accept(e.getKey(), e.getValue(), multi));
}
@Override
public LongStream mapMultiToLong(TriConsumer super K, ? super V, ? super LongConsumer> mapper) {
requireNonNull(mapper);
return entries().mapMultiToLong((e, multi) -> mapper.accept(e.getKey(), e.getValue(), multi));
}
@Override
public DoubleStream mapMultiToDouble(TriConsumer super K, ? super V, ? super DoubleConsumer> mapper) {
requireNonNull(mapper);
return entries().mapMultiToDouble((e, multi) -> mapper.accept(e.getKey(), e.getValue(), multi));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy