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