|
|
@@ -12,6 +12,7 @@ import java.util.stream.Stream;
|
|
|
import java.util.stream.StreamSupport;
|
|
|
|
|
|
import net.ranides.assira.functional.Functions.EachFunction;
|
|
|
+import net.ranides.assira.functional.Predicates.EachPredicate;
|
|
|
import net.ranides.assira.functional.checked.CheckedSupplier;
|
|
|
import net.ranides.assira.generic.CompareUtils;
|
|
|
|
|
|
@@ -98,6 +99,14 @@ public final class IteratorUtils {
|
|
|
public static <T> ListIterator<T> filter(ListIterator<? extends T> iterator, Predicate<? super T> predicate) {
|
|
|
return new FilterListIterator<>(iterator, predicate, false);
|
|
|
}
|
|
|
+
|
|
|
+ public static <T> Iterator<T> filterEach(Iterator<? extends T> iterator, EachPredicate<? super T> predicate) {
|
|
|
+ return new FilterIterator<>(iterator, predicate, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> ListIterator<T> filterEach(ListIterator<? extends T> iterator, EachPredicate<? super T> predicate) {
|
|
|
+ return new FilterListIterator<>(iterator, predicate, false);
|
|
|
+ }
|
|
|
|
|
|
public static <T> Iterator<T> limit(Iterator<? extends T> iterator, Predicate<? super T> predicate) {
|
|
|
return new FilterIterator<>(iterator, predicate, true);
|
|
|
@@ -414,15 +423,21 @@ public final class IteratorUtils {
|
|
|
private static class FilterIterator<T> implements Iterator<T> {
|
|
|
|
|
|
private final Iterator<? extends T> iterator;
|
|
|
- private final Predicate<? super T> predicate;
|
|
|
+ private final EachPredicate<? super T> predicate;
|
|
|
private final boolean limit;
|
|
|
private T next;
|
|
|
private boolean hasNext;
|
|
|
-
|
|
|
+ private int index;
|
|
|
+
|
|
|
public FilterIterator(Iterator<? extends T> iterator, Predicate<? super T> predicate, boolean limit) {
|
|
|
+ this(iterator, (i,v) -> predicate.test(v), limit);
|
|
|
+ }
|
|
|
+
|
|
|
+ public FilterIterator(Iterator<? extends T> iterator, EachPredicate<? super T> predicate, boolean limit) {
|
|
|
this.iterator = iterator;
|
|
|
this.predicate = predicate;
|
|
|
this.limit = limit;
|
|
|
+ this.index = -1;
|
|
|
nextMatch();
|
|
|
}
|
|
|
|
|
|
@@ -436,6 +451,7 @@ public final class IteratorUtils {
|
|
|
if (!hasNext) {
|
|
|
throw new NoSuchElementException();
|
|
|
}
|
|
|
+ index++;
|
|
|
return nextMatch();
|
|
|
}
|
|
|
|
|
|
@@ -444,7 +460,7 @@ public final class IteratorUtils {
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
T value = iterator.next();
|
|
|
- if (predicate.test(value)) {
|
|
|
+ if (predicate.test(index, value)) {
|
|
|
hasNext = true;
|
|
|
next = value;
|
|
|
return last;
|
|
|
@@ -468,13 +484,17 @@ public final class IteratorUtils {
|
|
|
private static class FilterListIterator<T> implements ListIterator<T> {
|
|
|
|
|
|
private final ListIterator<? extends T> iterator;
|
|
|
- private final Predicate<? super T> predicate;
|
|
|
+ private final EachPredicate<? super T> predicate;
|
|
|
private final boolean limit;
|
|
|
- int index;
|
|
|
+ private int index;
|
|
|
private T next;
|
|
|
private boolean hasNext;
|
|
|
|
|
|
public FilterListIterator(ListIterator<? extends T> iterator, Predicate<? super T> predicate, boolean limit) {
|
|
|
+ this(iterator, (i,v) -> predicate.test(v), limit);
|
|
|
+ }
|
|
|
+
|
|
|
+ public FilterListIterator(ListIterator<? extends T> iterator, EachPredicate<? super T> predicate, boolean limit) {
|
|
|
this.iterator = iterator;
|
|
|
this.predicate = predicate;
|
|
|
this.limit = limit;
|
|
|
@@ -514,7 +534,7 @@ public final class IteratorUtils {
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
T value = iterator.next();
|
|
|
- if (predicate.test(value)) {
|
|
|
+ if (predicate.test(index, value)) {
|
|
|
hasNext = true;
|
|
|
next = value;
|
|
|
return ilast;
|
|
|
@@ -530,7 +550,7 @@ public final class IteratorUtils {
|
|
|
private T prevMatch() {
|
|
|
while (iterator.hasPrevious()) {
|
|
|
T value = iterator.previous();
|
|
|
- if (predicate.test(value)) {
|
|
|
+ if (predicate.test(index, value)) {
|
|
|
hasNext = true;
|
|
|
next = value;
|
|
|
return next;
|