All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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> 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 filter) {
		requireNonNull(filter);
		return new EntryPipeline<>(entries().filter(e -> filter.test(e.getKey(), e.getValue())));
	}

	@Override
	public MapStream filterKey(Predicate filter) {
		requireNonNull(filter);
		return new EntryPipeline<>(entries().filter(e -> filter.test(e.getKey())));
	}

	@Override
	public MapStream filterValue(Predicate filter) {
		requireNonNull(filter);
		return new EntryPipeline<>(entries().filter(e -> filter.test(e.getValue())));
	}

	@Override
	public  MapStream map(
		BiFunction> mapper) {
		requireNonNull(mapper);
		return new EntryPipeline<>(entries().map(e -> mapper.apply(e.getKey(), e.getValue())));
	}

	@Override
	public  MapStream mapKey(Function mapper) {
		requireNonNull(mapper);
		return new EntryPipeline<>(entries().map(e -> MapStream.entry(mapper.apply(e.getKey()), e.getValue())));
	}

	@Override
	public  MapStream mapValue(Function mapper) {
		requireNonNull(mapper);
		return new EntryPipeline<>(entries().map(e -> MapStream.entry(e.getKey(), mapper.apply(e.getValue()))));
	}

	@Override
	public  Stream mapToObj(BiFunction mapper) {
		requireNonNull(mapper);
		return entries().map(e -> mapper.apply(e.getKey(), e.getValue()));
	}

	@Override
	public IntStream mapToInt(ToIntBiFunction mapper) {
		requireNonNull(mapper);
		return entries().mapToInt(e -> mapper.applyAsInt(e.getKey(), e.getValue()));
	}

	@Override
	public LongStream mapToLong(ToLongBiFunction mapper) {
		requireNonNull(mapper);
		return entries().mapToLong(e -> mapper.applyAsLong(e.getKey(), e.getValue()));
	}

	@Override
	public DoubleStream mapToDouble(ToDoubleBiFunction mapper) {
		requireNonNull(mapper);
		return entries().mapToDouble(e -> mapper.applyAsDouble(e.getKey(), e.getValue()));
	}

	@Override
	public  MapStream flatMap(
		BiFunction> mapper) {
		requireNonNull(mapper);
		return new EntryPipeline<>(entries().flatMap(e -> mapper.apply(e.getKey(), e.getValue())
			.entries()));
	}

	@Override
	public  MapStream flatMapKey(Function> 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> 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> mapper) {
		requireNonNull(mapper);
		return entries().flatMap(e -> mapper.apply(e.getKey(), e.getValue()));
	}

	@Override
	public IntStream flatMapToInt(BiFunction mapper) {
		requireNonNull(mapper);
		return entries().flatMapToInt(e -> mapper.apply(e.getKey(), e.getValue()));
	}

	@Override
	public LongStream flatMapToLong(BiFunction mapper) {
		requireNonNull(mapper);
		return entries().flatMapToLong(e -> mapper.apply(e.getKey(), e.getValue()));
	}

	@Override
	public DoubleStream flatMapToDouble(BiFunction mapper) {
		requireNonNull(mapper);
		return entries().flatMapToDouble(e -> mapper.apply(e.getKey(), e.getValue()));
	}

	@Override
	public MapStream peek(BiConsumer peek) {
		requireNonNull(peek);
		return new EntryPipeline<>(entries().peek(e -> peek.accept(e.getKey(), e.getValue())));
	}

	@Override
	public MapStream peekKey(Consumer peek) {
		requireNonNull(peek);
		return new EntryPipeline<>(entries().peek(e -> peek.accept(e.getKey())));
	}

	@Override
	public MapStream peekValue(Consumer 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> comparator) {
		return new EntryPipeline<>(entries().sorted(comparator));
	}

	@Override
	public MapStream sortedByKey() {
		return sorted(comparingByKey());
	}

	@Override
	public MapStream sortedByKey(Comparator comparator) {
		return sorted(Entry.comparingByKey(comparator));
	}

	@Override
	public MapStream sortedByValue() {
		return sorted(comparingByValue());
	}

	@Override
	public MapStream sortedByValue(Comparator 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 consumer) {
		requireNonNull(consumer);
		entries().forEach(e -> consumer.accept(e.getKey(), e.getValue()));
	}

	@Override
	public void forEachOrdered(BiConsumer consumer) {
		requireNonNull(consumer);
		entries().forEachOrdered(e -> consumer.accept(e.getKey(), e.getValue()));
	}

	@Override
	public long count() {
		return entries().count();
	}

	@Override
	public boolean anyMatch(BiPredicate predicate) {
		requireNonNull(predicate);
		return entries().anyMatch(e -> predicate.test(e.getKey(), e.getValue()));
	}

	@Override
	public boolean allMatch(BiPredicate predicate) {
		requireNonNull(predicate);
		return entries().allMatch(e -> predicate.test(e.getKey(), e.getValue()));
	}

	@Override
	public boolean noneMatch(BiPredicate predicate) {
		requireNonNull(predicate);
		return entries().noneMatch(e -> predicate.test(e.getKey(), e.getValue()));
	}

	@Override
	public  R collect(Collector, 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> comparator) {
		return entries().max(comparator);
	}

	@Override
	public Optional> maxByKey(Comparator comparator) {
		return max(Entry.comparingByKey(comparator));
	}

	@Override
	public Optional> maxByValue(Comparator comparator) {
		return max(Entry.comparingByValue(comparator));
	}

	@Override
	public Optional> min(Comparator> comparator) {
		return entries().min(comparator);
	}

	@Override
	public Optional> minByKey(Comparator comparator) {
		return min(Entry.comparingByKey(comparator));
	}

	@Override
	public Optional> minByValue(Comparator 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 predicate) {
		requireNonNull(predicate);
		return new EntryPipeline<>(entries().takeWhile(e -> predicate.test(e.getKey(), e.getValue())));
	}

	@Override
	public MapStream takeWhileKey(Predicate predicate) {
		requireNonNull(predicate);
		return new EntryPipeline<>(entries().takeWhile(e -> predicate.test(e.getKey())));
	}

	@Override
	public MapStream takeWhileValue(Predicate predicate) {
		requireNonNull(predicate);
		return new EntryPipeline<>(entries().takeWhile(e -> predicate.test(e.getValue())));
	}

	@Override
	public MapStream dropWhile(BiPredicate predicate) {
		requireNonNull(predicate);
		return new EntryPipeline<>(entries().dropWhile(e -> predicate.test(e.getKey(), e.getValue())));
	}

	@Override
	public MapStream dropWhileKey(Predicate predicate) {
		requireNonNull(predicate);
		return new EntryPipeline<>(entries().dropWhile(e -> predicate.test(e.getKey())));
	}

	@Override
	public MapStream dropWhileValue(Predicate predicate) {
		requireNonNull(predicate);
		return new EntryPipeline<>(entries().dropWhile(e -> predicate.test(e.getValue())));
	}

	@Override
	public  MapStream mapMulti(TriConsumer> 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> 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> 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> mapper) {
		requireNonNull(mapper);
		return entries(). mapMulti((e, multi) -> mapper.accept(e.getKey(), e.getValue(), multi));
	}

	@Override
	public IntStream mapMultiToInt(TriConsumer mapper) {
		requireNonNull(mapper);
		return entries().mapMultiToInt((e, multi) -> mapper.accept(e.getKey(), e.getValue(), multi));
	}

	@Override
	public LongStream mapMultiToLong(TriConsumer mapper) {
		requireNonNull(mapper);
		return entries().mapMultiToLong((e, multi) -> mapper.accept(e.getKey(), e.getValue(), multi));
	}

	@Override
	public DoubleStream mapMultiToDouble(TriConsumer mapper) {
		requireNonNull(mapper);
		return entries().mapMultiToDouble((e, multi) -> mapper.accept(e.getKey(), e.getValue(), multi));
	}

}