org.infinispan.functional.impl.Traversables Maven / Gradle / Ivy
package org.infinispan.functional.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
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.stream.Collector;
import java.util.stream.Stream;
import org.infinispan.functional.Traversable;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.commons.util.Closeables;
public final class Traversables {
public static Traversable of(Stream stream) {
return new StreamTraversable<>(stream);
}
public static CloseableIterator asIterator(Traversable traversable) {
if (traversable instanceof StreamTraversable)
return Closeables.iterator(((StreamTraversable) traversable).stream);
else {
List collected = traversable.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
return Closeables.iterator(collected.iterator());
}
}
private Traversables() {
// Cannot be instantiated, it's just a holder class
}
// TODO: Attention! This is a very rudimentary/simplistic implementation!
private static final class StreamTraversable implements Traversable {
final Stream stream;
private StreamTraversable(Stream stream) {
this.stream = stream;
}
@Override
public Traversable filter(Predicate super T> p) {
return new StreamTraversable<>(stream.filter(p));
}
@Override
public Traversable map(Function super T, ? extends R> f) {
return new StreamTraversable<>(stream.map(f));
}
@Override
public Traversable flatMap(Function super T, ? extends Traversable extends R>> f) {
Function super T, ? extends Stream extends R>> mapper = new Function>() {
@Override
public Stream extends R> apply(T t) {
StreamTraversable extends R> applied = (StreamTraversable extends R>) f.apply(t);
return applied.stream;
}
};
return new StreamTraversable<>(stream.flatMap(mapper));
}
@Override
public void forEach(Consumer super T> c) {
stream.forEach(c);
}
@Override
public T reduce(T z, BinaryOperator folder) {
return stream.reduce(z, folder);
}
@Override
public Optional reduce(BinaryOperator folder) {
return stream.reduce(folder);
}
@Override
public U reduce(U z, BiFunction mapper, BinaryOperator folder) {
return stream.reduce(z, mapper, folder);
}
@Override
public R collect(Supplier s, BiConsumer accumulator, BiConsumer combiner) {
return stream.collect(s, accumulator, combiner);
}
@Override
public R collect(Collector super T, A, R> collector) {
return stream.collect(collector);
}
@Override
public long count() {
return stream.count();
}
@Override
public boolean anyMatch(Predicate super T> p) {
return stream.anyMatch(p);
}
@Override
public boolean allMatch(Predicate super T> p) {
return stream.allMatch(p);
}
@Override
public boolean noneMatch(Predicate super T> predicate) {
return stream.noneMatch(predicate);
}
@Override
public Optional findAny() {
return stream.findAny();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy