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

org.jinq.orm.stream.LazyWrappedStream Maven / Gradle / Ivy

Go to download

Jinq public API for extending Java 8 streams with database functionality

There is a newer version: 2.0.2
Show newest version
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();
   }
   
   protected  Stream wrap(Stream toWrap)
   {
      return toWrap;
   }
   
   protected Stream createWrappedStream() 
   {
      return null;
   }
   
   @Override
   public Stream filter(Predicate predicate)
   {
      realizeStream();
      return wrap(wrappedStream.filter(predicate));
   }

   @Override
   public  Stream map(Function mapper)
   {
      realizeStream();
      return wrap(wrappedStream.map(mapper));
   }

   @Override
   public IntStream mapToInt(ToIntFunction mapper)
   {
      realizeStream();
      return wrappedStream.mapToInt(mapper);
   }

   @Override
   public LongStream mapToLong(ToLongFunction mapper)
   {
      realizeStream();
      return wrappedStream.mapToLong(mapper);
   }

   @Override
   public DoubleStream mapToDouble(ToDoubleFunction mapper)
   {
      realizeStream();
      return wrappedStream.mapToDouble(mapper);
   }

   @Override
   public  Stream flatMap(
         Function> mapper)
   {
      realizeStream();
      return wrap(wrappedStream.flatMap(mapper));
   }

   @Override
   public IntStream flatMapToInt(Function mapper)
   {
      realizeStream();
      return wrappedStream.flatMapToInt(mapper);
   }

   @Override
   public LongStream flatMapToLong(
         Function mapper)
   {
      realizeStream();
      return wrappedStream.flatMapToLong(mapper);
   }

   @Override
   public DoubleStream flatMapToDouble(
         Function 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 comparator)
   {
      realizeStream();
      return wrap(wrappedStream.sorted(comparator));
   }

   @Override
   public Stream peek(Consumer 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 action)
   {
      realizeStream();
      wrappedStream.forEach(action);
   }

   @Override
   public void forEachOrdered(Consumer 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 collector)
   {
      realizeStream();
      return wrappedStream.collect(collector);
   }

   @Override
   public Optional min(Comparator comparator)
   {
      realizeStream();
      return wrappedStream.min(comparator);
   }

   @Override
   public Optional max(Comparator comparator)
   {
      realizeStream();
      return wrappedStream.max(comparator);
   }

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

   @Override
   public boolean anyMatch(Predicate predicate)
   {
      realizeStream();
      return wrappedStream.anyMatch(predicate);
   }

   @Override
   public boolean allMatch(Predicate predicate)
   {
      realizeStream();
      return wrappedStream.allMatch(predicate);
   }

   @Override
   public boolean noneMatch(Predicate 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();
   }
}