|
@@ -26,8 +26,7 @@ import net.ranides.assira.collection.sets.HashSet;
|
|
|
import net.ranides.assira.collection.sets.RBTreeSet;
|
|
import net.ranides.assira.collection.sets.RBTreeSet;
|
|
|
import net.ranides.assira.functional.Consumers.EachConsumer;
|
|
import net.ranides.assira.functional.Consumers.EachConsumer;
|
|
|
import net.ranides.assira.functional.Functions.EachFunction;
|
|
import net.ranides.assira.functional.Functions.EachFunction;
|
|
|
-import net.ranides.assira.functional.checked.CheckedFunction;
|
|
|
|
|
-import net.ranides.assira.functional.checked.CheckedSupplier;
|
|
|
|
|
|
|
+import net.ranides.assira.functional.Predicates.EachPredicate;
|
|
|
import net.ranides.assira.functional.special.Fold;
|
|
import net.ranides.assira.functional.special.Fold;
|
|
|
import net.ranides.assira.generic.CompareUtils;
|
|
import net.ranides.assira.generic.CompareUtils;
|
|
|
import net.ranides.assira.generic.SerializationUtils;
|
|
import net.ranides.assira.generic.SerializationUtils;
|
|
@@ -147,21 +146,46 @@ public abstract class CQueryAbstract<T> implements CQuery<T> {
|
|
|
return new CQIterable<>(() -> IteratorUtils.map(iterator(), f));
|
|
return new CQIterable<>(() -> IteratorUtils.map(iterator(), f));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CQuery<T> mapIf(Predicate<? super T> p, Function<? super T, ? extends T> f) {
|
|
|
|
|
+ return map(v -> p.test(v) ? v : f.apply(v));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CQuery<T> mapIf(EachPredicate<? super T> p, EachFunction<? super T, ? extends T> f) {
|
|
|
|
|
+ return map((i,v) -> p.test(i, v) ? v : f.apply(i,v));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public final <R> CQuery<R> unfold(Function<? super T, CQuery<R>> f) {
|
|
public final <R> CQuery<R> unfold(Function<? super T, CQuery<R>> f) {
|
|
|
return new CQIterable<>(() -> IteratorUtils.flat(IteratorUtils.map(iterator(), (v) -> f.apply(v).iterator())));
|
|
return new CQIterable<>(() -> IteratorUtils.flat(IteratorUtils.map(iterator(), (v) -> f.apply(v).iterator())));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CQuery<T> unfoldIf(Predicate<? super T> p, Function<? super T, CQuery<T>> f) {
|
|
|
|
|
+ return flat(v -> p.test(v) ? Collections.singleton(v) : f.apply(v));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public final <R> CQuery<R> flat(Function<? super T, Iterable<R>> f) {
|
|
public final <R> CQuery<R> flat(Function<? super T, Iterable<R>> f) {
|
|
|
return new CQIterable<>(() -> IteratorUtils.flat(IteratorUtils.map(iterator(), (v) -> f.apply(v).iterator())));
|
|
return new CQIterable<>(() -> IteratorUtils.flat(IteratorUtils.map(iterator(), (v) -> f.apply(v).iterator())));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CQuery<T> flatIf(Predicate<? super T> p, Function<? super T, Iterable<T>> f) {
|
|
|
|
|
+ return flat(v -> p.test(v) ? Collections.singleton(v) : f.apply(v));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public <R> CQuery<R> split(Function<? super T, R[]> f) {
|
|
public <R> CQuery<R> split(Function<? super T, R[]> f) {
|
|
|
return unfold(v -> CQuery.from().array(f.apply(v)) );
|
|
return unfold(v -> CQuery.from().array(f.apply(v)) );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CQuery<T> splitIf(Predicate<? super T> p, Function<? super T, T[]> f) {
|
|
|
|
|
+ return flat(v -> p.test(v) ? Collections.singleton(v) : Arrays.asList(f.apply(v)));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public final T fold(T identity, BinaryOperator<T> accumulator) {
|
|
public final T fold(T identity, BinaryOperator<T> accumulator) {
|
|
|
Iterator<T> i = iterator();
|
|
Iterator<T> i = iterator();
|
|
@@ -412,7 +436,7 @@ public abstract class CQueryAbstract<T> implements CQuery<T> {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
protected static class CQIterable<T> extends CQueryAbstract<T> {
|
|
protected static class CQIterable<T> extends CQueryAbstract<T> {
|
|
|
|
|
|
|
|
private final Supplier<Iterable<T>> source;
|
|
private final Supplier<Iterable<T>> source;
|
|
@@ -557,6 +581,61 @@ public abstract class CQueryAbstract<T> implements CQuery<T> {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ protected static class CQSingle<T> extends CQueryAbstract<T> {
|
|
|
|
|
+
|
|
|
|
|
+ private final T value;
|
|
|
|
|
+
|
|
|
|
|
+ public CQSingle(T value) {
|
|
|
|
|
+ this.value = value;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Iterator<T> iterator() {
|
|
|
|
|
+ return IteratorUtils.singletonIterator(value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Spliterator<T> spliterator() {
|
|
|
|
|
+ return IteratorUtils.singletonSpliterator(value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Stream<T> stream() {
|
|
|
|
|
+ return Stream.of(value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int size() {
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CQuery<T> limit(int n) {
|
|
|
|
|
+ return n == 0 ? CQuery.empty() : this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CQuery<T> skip(int n) {
|
|
|
|
|
+ return n > 0 ? CQuery.empty() : this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Optional<T> at(int index) {
|
|
|
|
|
+ return index > 0 ? Optional.empty() : Optional.of(value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public final Optional<T> last() {
|
|
|
|
|
+ return Optional.of(value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CQuery<T> reverse() {
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private static final class BIterator<T> implements Iterator<T> {
|
|
private static final class BIterator<T> implements Iterator<T> {
|
|
|
|
|
|
|
|
private final Iterator<T> src;
|
|
private final Iterator<T> src;
|