| 1 | package pro.verron.officestamper.utils.iterator; | |
| 2 | ||
| 3 | import org.jspecify.annotations.Nullable; | |
| 4 | ||
| 5 | import java.util.NoSuchElementException; | |
| 6 | import java.util.function.Predicate; | |
| 7 | ||
| 8 | /// An iterator that filters elements from another ResetableIterator based on user-provided filter functions. | |
| 9 | /// | |
| 10 | /// This class allows elements to be iterated through with specific filtering applied. The iterator can also be reset to | |
| 11 | /// its initial state, re-evaluating the elements based on the same logic. | |
| 12 | /// | |
| 13 | /// @param <T> the type of the source elements in the parent iterator | |
| 14 | class FilteringIterator<T> | |
| 15 | implements ResetableIterator<T> { | |
| 16 | private final Predicate<T> filter; | |
| 17 | private final ResetableIterator<T> source; | |
| 18 | @Nullable T next; | |
| 19 | ||
| 20 | /// Constructs a MappingIterator with a parent iterator and a mapping function. | |
| 21 | /// | |
| 22 | /// @param source the underlying [ResetableIterator] containing the source elements | |
| 23 | /// @param mapper a function to transform the elements into a different type | |
| 24 | public FilteringIterator(ResetableIterator<T> source, Predicate<T> mapper) { | |
| 25 | this.source = source; | |
| 26 | this.filter = mapper; | |
| 27 |
1
1. <init> : removed call to pro/verron/officestamper/utils/iterator/FilteringIterator::findNext → KILLED |
findNext(); |
| 28 | } | |
| 29 | ||
| 30 | private void findNext() { | |
| 31 | next = null; | |
| 32 |
2
1. findNext : negated conditional → KILLED 2. findNext : negated conditional → KILLED |
while (source.hasNext() && next == null) { |
| 33 | var o = source.next(); | |
| 34 |
1
1. findNext : negated conditional → KILLED |
if (filter.test(o)) next = o; |
| 35 | } | |
| 36 | } | |
| 37 | ||
| 38 | @Override | |
| 39 | public boolean hasNext() { | |
| 40 |
2
1. hasNext : negated conditional → KILLED 2. hasNext : replaced boolean return with true for pro/verron/officestamper/utils/iterator/FilteringIterator::hasNext → KILLED |
return next != null; |
| 41 | } | |
| 42 | ||
| 43 | @Override | |
| 44 | public T next() { | |
| 45 |
1
1. next : negated conditional → KILLED |
if (next == null) throw new NoSuchElementException("No more elements to iterate"); |
| 46 | T result = next; | |
| 47 |
1
1. next : removed call to pro/verron/officestamper/utils/iterator/FilteringIterator::findNext → TIMED_OUT |
findNext(); |
| 48 |
1
1. next : replaced return value with null for pro/verron/officestamper/utils/iterator/FilteringIterator::next → KILLED |
return result; |
| 49 | } | |
| 50 | ||
| 51 | @Override | |
| 52 | public void reset() { | |
| 53 |
1
1. reset : removed call to pro/verron/officestamper/utils/iterator/ResetableIterator::reset → KILLED |
source.reset(); |
| 54 |
1
1. reset : removed call to pro/verron/officestamper/utils/iterator/FilteringIterator::findNext → KILLED |
findNext(); |
| 55 | } | |
| 56 | } | |
Mutations | ||
| 27 |
1.1 |
|
| 32 |
1.1 2.2 |
|
| 34 |
1.1 |
|
| 40 |
1.1 2.2 |
|
| 45 |
1.1 |
|
| 47 |
1.1 |
|
| 48 |
1.1 |
|
| 53 |
1.1 |
|
| 54 |
1.1 |