|
|
@@ -8,8 +8,18 @@ import net.ranides.assira.functional.Predicates;
|
|
|
import net.ranides.assira.math.MathUtils;
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
+/**
|
|
|
+ * Adapter query: it returns only specified fragment of source query.
|
|
|
+ * Used by {@link CQuery#slice(int, int)} operation.
|
|
|
+ *
|
|
|
+ * It reports all features supported by source except "each".
|
|
|
+ * Fast "each" is reported only, if selected slice does not skip more elements than "FAST_SKIP_LIMIT"
|
|
|
+ *
|
|
|
+ * @param <T> T
|
|
|
+ */
|
|
|
public class CQSlice<T> extends CQueryAbstract<T> {
|
|
|
|
|
|
private static final int FAST_SKIP_LIMIT = 1;
|
|
|
@@ -18,6 +28,13 @@ public class CQSlice<T> extends CQueryAbstract<T> {
|
|
|
private final int begin;
|
|
|
private final int end;
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates new slice with provided range.
|
|
|
+ *
|
|
|
+ * @param source source
|
|
|
+ * @param begin begin
|
|
|
+ * @param end end
|
|
|
+ */
|
|
|
public CQSlice(CQuery<T> source, int begin, int end) {
|
|
|
this.source = source;
|
|
|
this.begin = Math.max(0, begin);
|
|
|
@@ -46,7 +63,7 @@ public class CQSlice<T> extends CQueryAbstract<T> {
|
|
|
|
|
|
@Override
|
|
|
public boolean hasFastList() {
|
|
|
- return false;
|
|
|
+ return source.features().hasFastList();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -74,6 +91,14 @@ public class CQSlice<T> extends CQueryAbstract<T> {
|
|
|
return source.stream().skip(begin).limit(end-begin);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<T> list() {
|
|
|
+ if(hasFastList()) {
|
|
|
+ return source.list().subList(begin, Math.min(source.size(), end));
|
|
|
+ }
|
|
|
+ return super.list();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public int size() {
|
|
|
return Math.max(0, Math.min(source.size(), end) - begin);
|