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

org.jinq.orm.stream.QueryJinqStream 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.Iterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.jinq.orm.stream.JinqStream.AggregateSelect;
import org.jinq.tuples.Pair;

import ch.epfl.labos.iu.orm.DateSorter;
import ch.epfl.labos.iu.orm.DoubleSorter;
import ch.epfl.labos.iu.orm.IntSorter;
import ch.epfl.labos.iu.orm.QueryComposer;
import ch.epfl.labos.iu.orm.StringSorter;

public class QueryJinqStream extends NonQueryJinqStream implements JinqStream
{
   QueryComposer queryComposer;
   public QueryJinqStream(QueryComposer query)
   {
      this.queryComposer = query;
   }
   
   protected Stream createWrappedStream() 
   {
      return StreamSupport.stream(
            Spliterators.spliteratorUnknownSize(
                  queryComposer.executeAndReturnResultIterator( err -> propagateException(this, err) ), 
                  Spliterator.CONCURRENT), 
            false);
   }
   
   @Override
   public  JinqStream where(final Where test)
   {
      QueryComposer newComposer = queryComposer.where(test);
      if (newComposer != null) return new QueryJinqStream(newComposer);
      return super.where(test);
   }

   @Override
   public  JinqStream select(Select select)
   {
      QueryComposer newComposer = queryComposer.select(select);
      if (newComposer != null) return new QueryJinqStream(newComposer);
      return super.select(select);
   }
   
   @Override
   public  JinqStream> join(Join join)
   {
      QueryComposer> newComposer = queryComposer.join(join);
      if (newComposer != null) return new QueryJinqStream>(newComposer);
      return super.join(join);
   }
   
   @Override
   public JinqStream unique()
   {
      QueryComposer newComposer = queryComposer.unique();
      if (newComposer != null) return new QueryJinqStream(newComposer);
      return super.unique();
   }
   
   @Override
   public  JinqStream> group(Select select, AggregateGroup aggregate)
   {
      QueryComposer> newComposer = queryComposer.group(select, aggregate);
      if (newComposer != null) return new QueryJinqStream>(newComposer);
      return super.group(select, aggregate);
   }

   @Override
   public double sumDouble(AggregateDouble aggregate)
   {
      Double val = queryComposer.sumDouble(aggregate);
      if (val != null) return val;
      return super.sumDouble(aggregate);
   }
   
   @Override
   public int sumInt(AggregateInteger aggregate)
   {
      Integer val = queryComposer.sumInt(aggregate);
      if (val != null) return val;
      return super.sumInt(aggregate);
   }

   @Override
   public double maxDouble(AggregateDouble aggregate)
   {
      Double val = queryComposer.maxDouble(aggregate);
      if (val != null) return val;
      return super.maxDouble(aggregate);
   }
   
   @Override
   public int maxInt(AggregateInteger aggregate)
   {
      Integer val = queryComposer.maxInt(aggregate);
      if (val != null) return val;
      return super.maxInt(aggregate);
   }
   
   @Override
   public  U selectAggregates(AggregateSelect aggregate)
   {
      U val = queryComposer.selectAggregates(aggregate);
      if (val != null) return val;
      return super.selectAggregates(aggregate);
   }

   @Override
   public JinqStream sortedByIntAscending(IntSorter sorter)
   {
      QueryComposer newComposer = queryComposer.sortedByInt(sorter, true);
      if (newComposer != null) return new QueryJinqStream<>(newComposer);
      return super.sortedByIntAscending(sorter);
   }

   @Override
   public JinqStream sortedByIntDescending(IntSorter sorter)
   {
      QueryComposer newComposer = queryComposer.sortedByInt(sorter, false);
      if (newComposer != null) return new QueryJinqStream<>(newComposer);
      return super.sortedByIntDescending(sorter);
   }

   @Override
   public JinqStream sortedByDoubleAscending(DoubleSorter sorter)
   {
      QueryComposer newComposer = queryComposer.sortedByDouble(sorter, true);
      if (newComposer != null) return new QueryJinqStream<>(newComposer);
      return super.sortedByDoubleAscending(sorter);
   }

   @Override
   public JinqStream sortedByDoubleDescending(DoubleSorter sorter)
   {
      QueryComposer newComposer = queryComposer.sortedByDouble(sorter, false);
      if (newComposer != null) return new QueryJinqStream<>(newComposer);
      return super.sortedByDoubleDescending(sorter);
   }

   @Override
   public JinqStream sortedByStringAscending(StringSorter sorter)
   {
      QueryComposer newComposer = queryComposer.sortedByString(sorter, true);
      if (newComposer != null) return new QueryJinqStream<>(newComposer);
      return super.sortedByStringAscending(sorter);
   }

   @Override
   public JinqStream sortedByStringDescending(StringSorter sorter)
   {
      QueryComposer newComposer = queryComposer.sortedByString(sorter, false);
      if (newComposer != null) return new QueryJinqStream<>(newComposer);
      return super.sortedByStringDescending(sorter);
   }

   @Override
   public JinqStream sortedByDateAscending(DateSorter sorter)
   {
      QueryComposer newComposer = queryComposer.sortedByDate(sorter, true);
      if (newComposer != null) return new QueryJinqStream<>(newComposer);
      return super.sortedByDateAscending(sorter);
   }

   @Override
   public JinqStream sortedByDateDescending(DateSorter sorter)
   {
      QueryComposer newComposer = queryComposer.sortedByDate(sorter, false);
      if (newComposer != null) return new QueryJinqStream<>(newComposer);
      return super.sortedByDateDescending(sorter);
   }

   @Override
   public JinqStream firstN(int n)
   {
      QueryComposer newComposer = queryComposer.firstN(n);
      if (newComposer != null) return new QueryJinqStream<>(newComposer);
      return super.firstN(n);
   }
   
   @Override
   public String getDebugQueryString()
   {
      return queryComposer.getDebugQueryString();
   }
   
   @Override
   Object[] multiaggregate(AggregateSelect[] aggregates)
   {
      Object [] vals = queryComposer.multiaggregate(aggregates);
      if (vals != null) return vals;
      return super.multiaggregate(aggregates);
   }
   
   @Override
   public JinqStream setHint(String name, Object value)
   {
      queryComposer.setHint(name, value);
      return this;
   }
}