|
|
@@ -0,0 +1,182 @@
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim {@literal <ranides@gmail.com>}
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira
|
|
|
+ */
|
|
|
+package net.ranides.assira.collection.query;
|
|
|
+
|
|
|
+import net.ranides.assira.collection.HashFunction;
|
|
|
+import net.ranides.assira.functional.Consumers.EachConsumer;
|
|
|
+import net.ranides.assira.functional.Functions.EachFunction;
|
|
|
+import net.ranides.assira.functional.Predicates.EachPredicate;
|
|
|
+import net.ranides.assira.functional.checked.CheckedFunction;
|
|
|
+import net.ranides.assira.reflection.*;
|
|
|
+
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.function.Predicate;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @param <S>
|
|
|
+ * @param <T>
|
|
|
+ * @author Ranides Atterwim {@literal <ranides@gmail.com>}
|
|
|
+ */
|
|
|
+public interface CQueryFunction<S, T> extends Function<CQuery<S>, CQuery<T>> {
|
|
|
+
|
|
|
+ static <R> CQueryFunction<R,R> prepare() {
|
|
|
+ return s -> s;
|
|
|
+ }
|
|
|
+
|
|
|
+ default <Q> CQueryFunction<S,Q> andQuery(Function<? super CQuery<T>, ? extends CQuery<Q>> after) {
|
|
|
+ return s -> after.apply(apply(s));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> fetch() {
|
|
|
+ return andQuery(CQuery::fetch);
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> prefetch() {
|
|
|
+ return andQuery(CQuery::prefetch);
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> cache(Path filename) {
|
|
|
+ return andQuery(CheckedFunction.sneaky(s -> s.cache(filename)));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> append(CQuery<T> next) {
|
|
|
+ return andQuery(s -> s.append(next));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> limit(Predicate<? super T> p) {
|
|
|
+ return andQuery(s -> s.limit(p));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> limit(int n) {
|
|
|
+ return andQuery(s -> s.limit(n));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> skip(int n) {
|
|
|
+ return andQuery(s -> s.skip(n));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> slice(int begin, int end) {
|
|
|
+ return andQuery(s -> s.slice(begin, end));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> filter(Predicate<? super T> p) {
|
|
|
+ return andQuery(s -> s.filter(p));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> filterEach(EachPredicate<? super T> p) {
|
|
|
+ return andQuery(s -> s.filterEach(p));
|
|
|
+ }
|
|
|
+
|
|
|
+ default <Q> CQueryFunction<S, Q> filter(Class<Q> p) {
|
|
|
+ return andQuery(s -> s.filter(p));
|
|
|
+ }
|
|
|
+
|
|
|
+ default <Q> CQueryFunction<S, Q> filter(IClass<Q> p) {
|
|
|
+ return andQuery(s -> s.filter(p));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> discard(Predicate<? super T> p) {
|
|
|
+ return andQuery(s -> s.discard(p));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> discardEach(EachPredicate<? super T> p) {
|
|
|
+ return andQuery(s -> s.discardEach(p));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> discard(Class<?> p) {
|
|
|
+ return andQuery(s -> s.discard(p));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> discard(IClass<?> p) {
|
|
|
+ return andQuery(s -> s.discard(p));
|
|
|
+ }
|
|
|
+
|
|
|
+ default <R> CQueryFunction<S,R> map(Function<? super T, ? extends R> f) {
|
|
|
+ return andQuery(s -> s.map(f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default <R> CQueryFunction<S,R> mapOptional(Function<? super T, Optional<R>> f) {
|
|
|
+ return andQuery(s -> s.mapOptional(f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default <R> CQueryFunction<S,R> mapEach(EachFunction<? super T, ? extends R> f) {
|
|
|
+ return andQuery(s -> s.mapEach(f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default <R> CQueryFunction<S,R> unfold(Function<? super T, CQuery<R>> f) {
|
|
|
+ return andQuery(s -> s.unfold(f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default <R> CQueryFunction<S,R> flat(Function<? super T, Iterable<R>> f) {
|
|
|
+ return andQuery(s -> s.flat(f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default <R> CQueryFunction<S,R> split(Function<? super T, R[]> f) {
|
|
|
+ return andQuery(s -> s.split(f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> mapIf(Predicate<? super T> p, Function<? super T, ? extends T> f) {
|
|
|
+ return andQuery(s -> s.mapIf(p,f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> mapEachIf(EachPredicate<? super T> p, EachFunction<? super T, ? extends T> f) {
|
|
|
+ return andQuery(s -> s.mapEachIf(p,f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> mapOptionalIf(Predicate<? super T> p, Function<? super T, Optional<? extends T>> f) {
|
|
|
+ return andQuery(s -> s.mapOptionalIf(p,f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> unfoldIf(Predicate<? super T> p, Function<? super T, CQuery<T>> f) {
|
|
|
+ return andQuery(s -> s.unfoldIf(p, f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> flatIf(Predicate<? super T> p, Function<? super T, Iterable<T>> f) {
|
|
|
+ return andQuery(s -> s.flatIf(p, f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> splitIf(Predicate<? super T> p, Function<? super T, T[]> f) {
|
|
|
+ return andQuery(s -> s.splitIf(p,f));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> distinct() {
|
|
|
+ return andQuery(s -> s.distinct());
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> distinct(HashFunction<T> eq) {
|
|
|
+ return andQuery(s -> s.distinct(eq));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> distinct(Comparator<? super T> cmp) {
|
|
|
+ return andQuery(s -> s.distinct(cmp));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> sort() {
|
|
|
+ return andQuery(s -> s.sort());
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> sort(Comparator<? super T> cmp) {
|
|
|
+ return andQuery(s -> s.sort(cmp));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> reverse() {
|
|
|
+ return andQuery(s -> s.reverse());
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> onEach(Consumer<? super T> consumer) {
|
|
|
+ return andQuery(s -> s.onEach(consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+ default CQueryFunction<S, T> onEach(EachConsumer<? super T> consumer) {
|
|
|
+ return andQuery(s -> s.onEach(consumer));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|