org.jinq.orm.stream.NonQueryJinqStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api Show documentation
Show all versions of api Show documentation
Jinq public API for extending Java 8 streams with database functionality
package org.jinq.orm.stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.jinq.tuples.Pair;
import org.jinq.tuples.Tuple3;
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.StringSorter;
public class NonQueryJinqStream extends LazyWrappedStream implements JinqStream
{
public NonQueryJinqStream(Stream wrapped)
{
super(wrapped);
}
NonQueryJinqStream()
{
super();
}
protected Stream wrap(Stream toWrap)
{
return new NonQueryJinqStream<>(toWrap);
}
@Override
public JinqStream where(Where test)
{
return new NonQueryJinqStream<>(filter(val -> {
try {
return test.where(val);
} catch (Exception e) {
// Record that an exception occurred
propagateException(test, e);
// Throw a runtime exception to try and kill the stream?
throw new RuntimeException(e);
}} ));
}
@Override
public JinqStream select(Select select)
{
return new NonQueryJinqStream<>(map( val -> select.select(val) ));
}
@Override
public JinqStream> join(Join join)
{
// TODO: This stream should be constructed on the fly
final Stream.Builder> streamBuilder = Stream.builder();
forEach( left -> {
join.join(left).forEach( right ->
{ streamBuilder.accept(new Pair<>(left, right)); });
});
return new NonQueryJinqStream<>(streamBuilder.build());
}
@Override
public JinqStream unique()
{
return new NonQueryJinqStream<>(distinct());
}
@Override
public JinqStream> group(Select select, AggregateGroup aggregate)
{
// TODO: This stream should be constructed on the fly
final Stream.Builder> streamBuilder = Stream.builder();
// TODO: Rewrite to use Collectors.groupingBy()
HashMap> map = new HashMap<>();
forEach( val -> {
U group = select.select(val);
if (!map.containsKey(group))
map.put(group, new ArrayList<>());
map.get(group).add(val);
});
for (Map.Entry> entry: map.entrySet())
streamBuilder.accept(new Pair<>(entry.getKey(), aggregate.aggregateSelect(entry.getKey(), new NonQueryJinqStream<>(entry.getValue().stream()))));
return new NonQueryJinqStream<>(streamBuilder.build());
}
@Override
public double sumDouble(AggregateDouble aggregate)
{
// TODO: Rewrite using a summing collector
return reduce(0.0,
(accum, val) -> accum + aggregate.aggregate(val),
(accum1, accum2) -> accum1 + accum2);
}
@Override
public int sumInt(AggregateInteger aggregate)
{
return reduce(0,
(accum, val) -> accum + aggregate.aggregate(val),
(accum1, accum2) -> accum1 + accum2);
}
@Override
public double maxDouble(AggregateDouble aggregate)
{
return reduce(Double.NEGATIVE_INFINITY,
(accum, val) -> Math.max(accum, aggregate.aggregate(val)),
(accum1, accum2) -> Math.max(accum1, accum2));
}
@Override
public int maxInt(AggregateInteger aggregate)
{
return reduce(Integer.MIN_VALUE,
(accum, val) -> Math.max(accum, aggregate.aggregate(val)),
(accum1, accum2) -> Math.max(accum1, accum2));
}
@Override
public U selectAggregates(AggregateSelect aggregate)
{
return aggregate.aggregateSelect(this);
}
@Override
public JinqStream sortedByIntAscending(IntSorter sorter)
{
return new NonQueryJinqStream<>(sorted(
(o1, o2) -> sorter.value(o1) - sorter.value(o2)));
}
@Override
public JinqStream sortedByIntDescending(IntSorter sorter)
{
return new NonQueryJinqStream<>(sorted(
(o1, o2) -> sorter.value(o2) - sorter.value(o1)));
}
@Override
public JinqStream sortedByDoubleAscending(DoubleSorter sorter)
{
return new NonQueryJinqStream<>(sorted(
(o1, o2) -> (int)Math.signum(sorter.value(o1) - sorter.value(o2))));
}
@Override
public JinqStream sortedByDoubleDescending(DoubleSorter sorter)
{
return new NonQueryJinqStream<>(sorted(
(o1, o2) -> (int)Math.signum(sorter.value(o2) - sorter.value(o1))));
}
@Override
public JinqStream sortedByStringAscending(StringSorter sorter)
{
return new NonQueryJinqStream<>(sorted(
(o1, o2) -> sorter.value(o1).compareTo(sorter.value(o2))));
}
@Override
public JinqStream sortedByStringDescending(StringSorter sorter)
{
return new NonQueryJinqStream<>(sorted(
(o1, o2) -> -sorter.value(o1).compareTo(sorter.value(o2))));
}
@Override
public JinqStream sortedByDateAscending(DateSorter sorter)
{
return new NonQueryJinqStream<>(sorted(
(o1, o2) -> sorter.value(o1).compareTo(sorter.value(o2))));
}
@Override
public JinqStream sortedByDateDescending(DateSorter sorter)
{
return new NonQueryJinqStream<>(sorted(
(o1, o2) -> -sorter.value(o1).compareTo(sorter.value(o2))));
}
@Override
public JinqStream firstN(int n)
{
return new NonQueryJinqStream<>(limit(n));
}
@Override
public T getOnlyValue()
{
List vals = collect(Collectors.toList());
if (vals.size() == 1) return vals.get(0);
throw new NoSuchElementException();
}
@Override
public JinqStream with(T toAdd)
{
return new NonQueryJinqStream<>(
Stream.concat(this, Stream.of(toAdd)));
}
@Override
public List toList()
{
return collect(Collectors.toList());
}
@Override
public String getDebugQueryString()
{
// TODO: It would be nice if this could follow the stream chain
// down to get the underlying query (the stream chain isn't currently
// recorded, so this is not possible at the moment).
return null;
}
protected Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy