org.jinq.orm.stream.LazyWrappedStream Maven / Gradle / Ivy
package org.jinq.orm.stream;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
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.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
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;
/**
* Wraps a regular Stream to provide some Jinq methods for the stream.
*/
public class LazyWrappedStream implements Stream
{
Stream wrappedStream;
protected LazyWrappedStream() {}
protected LazyWrappedStream(Stream wrappedStream)
{
this.wrappedStream = wrappedStream;
}
protected void realizeStream()
{
// TODO: Perhaps this should only be called when the data from the
// stream is actually used and not when the stream is being set-up.
if (wrappedStream == null) wrappedStream = createWrappedStream();
}
/**
* Allows subclasses to wrap streams generated by this class
* with wrappers that provide additional functionality.
*/
protected Stream wrap(Stream toWrap)
{
return toWrap;
}
protected Stream createWrappedStream()
{
return null;
}
@Override
public Stream filter(Predicate super T> predicate)
{
realizeStream();
return wrap(wrappedStream.filter(predicate));
}
@Override
public Stream map(Function super T, ? extends R> mapper)
{
realizeStream();
return wrap(wrappedStream.map(mapper));
}
@Override
public IntStream mapToInt(ToIntFunction super T> mapper)
{
realizeStream();
return wrappedStream.mapToInt(mapper);
}
@Override
public LongStream mapToLong(ToLongFunction super T> mapper)
{
realizeStream();
return wrappedStream.mapToLong(mapper);
}
@Override
public DoubleStream mapToDouble(ToDoubleFunction super T> mapper)
{
realizeStream();
return wrappedStream.mapToDouble(mapper);
}
@Override
public Stream flatMap(
Function super T, ? extends Stream extends R>> mapper)
{
realizeStream();
return wrap(wrappedStream.flatMap(mapper));
}
@Override
public IntStream flatMapToInt(Function super T, ? extends IntStream> mapper)
{
realizeStream();
return wrappedStream.flatMapToInt(mapper);
}
@Override
public LongStream flatMapToLong(
Function super T, ? extends LongStream> mapper)
{
realizeStream();
return wrappedStream.flatMapToLong(mapper);
}
@Override
public DoubleStream flatMapToDouble(
Function super T, ? extends DoubleStream> mapper)
{
realizeStream();
return wrappedStream.flatMapToDouble(mapper);
}
@Override
public Stream distinct()
{
realizeStream();
return wrap(wrappedStream.distinct());
}
@Override
public Stream sorted()
{
realizeStream();
return wrap(wrappedStream.sorted());
}
@Override
public Stream sorted(Comparator super T> comparator)
{
realizeStream();
return wrap(wrappedStream.sorted(comparator));
}
@Override
public Stream peek(Consumer super T> action)
{
realizeStream();
return wrap(wrappedStream.peek(action));
}
@Override
public Stream limit(long maxSize)
{
realizeStream();
return wrap(wrappedStream.limit(maxSize));
}
@Override
public Stream skip(long n)
{
realizeStream();
return wrap(wrappedStream.skip(n));
}
@Override
public void forEach(Consumer super T> action)
{
realizeStream();
wrappedStream.forEach(action);
}
@Override
public void forEachOrdered(Consumer super T> action)
{
realizeStream();
wrappedStream.forEachOrdered(action);
}
@Override
public Object[] toArray()
{
realizeStream();
return wrappedStream.toArray();
}
@Override
public A[] toArray(IntFunction generator)
{
realizeStream();
return wrappedStream.toArray(generator);
}
@Override
public T reduce(T identity, BinaryOperator accumulator)
{
realizeStream();
return wrappedStream.reduce(identity, accumulator);
}
@Override
public Optional reduce(BinaryOperator accumulator)
{
realizeStream();
return wrappedStream.reduce(accumulator);
}
@Override
public U reduce(U identity, BiFunction accumulator,
BinaryOperator combiner)
{
realizeStream();
return wrappedStream.reduce(identity, accumulator, combiner);
}
@Override
public R collect(Supplier supplier,
BiConsumer accumulator, BiConsumer combiner)
{
realizeStream();
return wrappedStream.collect(supplier, accumulator, combiner);
}
@Override
public R collect(Collector super T, A, R> collector)
{
realizeStream();
return wrappedStream.collect(collector);
}
@Override
public Optional min(Comparator super T> comparator)
{
realizeStream();
return wrappedStream.min(comparator);
}
@Override
public Optional max(Comparator super T> comparator)
{
realizeStream();
return wrappedStream.max(comparator);
}
@Override
public long count()
{
realizeStream();
return wrappedStream.count();
}
@Override
public boolean anyMatch(Predicate super T> predicate)
{
realizeStream();
return wrappedStream.anyMatch(predicate);
}
@Override
public boolean allMatch(Predicate super T> predicate)
{
realizeStream();
return wrappedStream.allMatch(predicate);
}
@Override
public boolean noneMatch(Predicate super T> predicate)
{
realizeStream();
return wrappedStream.noneMatch(predicate);
}
@Override
public Optional findFirst()
{
realizeStream();
return wrappedStream.findFirst();
}
@Override
public Optional findAny()
{
realizeStream();
return wrappedStream.findAny();
}
@Override
public Iterator iterator()
{
realizeStream();
return wrappedStream.iterator();
}
@Override
public Spliterator spliterator()
{
realizeStream();
return wrappedStream.spliterator();
}
@Override
public boolean isParallel()
{
realizeStream();
return wrappedStream.isParallel();
}
@Override
public Stream sequential()
{
realizeStream();
return wrap(wrappedStream.sequential());
}
@Override
public Stream parallel()
{
realizeStream();
return wrap(wrappedStream.parallel());
}
@Override
public Stream unordered()
{
realizeStream();
return wrap(wrappedStream.unordered());
}
@Override
public Stream onClose(Runnable closeHandler)
{
realizeStream();
return wrap(wrappedStream.onClose(closeHandler));
}
@Override
public void close()
{
realizeStream();
wrappedStream.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy