| 1 | package pro.verron.officestamper.utils.iterator; | |
| 2 | ||
| 3 | import java.util.Iterator; | |
| 4 | import java.util.Spliterator; | |
| 5 | import java.util.Spliterators; | |
| 6 | import java.util.function.Function; | |
| 7 | import java.util.function.Predicate; | |
| 8 | import java.util.stream.Collector; | |
| 9 | import java.util.stream.StreamSupport; | |
| 10 | ||
| 11 | /// An interface that extends the [Iterator] interface, providing an additional capability to reset the iterator back to | |
| 12 | /// its initial state. | |
| 13 | /// | |
| 14 | /// This interface is useful in scenarios where an iteration process needs to be repeated or restarted without creating | |
| 15 | /// a new instance of the iterator. The `reset` method ensures that the iterator can be reused and revisits the elements | |
| 16 | /// starting from the beginning. | |
| 17 | /// | |
| 18 | /// @param <T> the type of elements returned by this iterator | |
| 19 | public interface ResetableIterator<T> | |
| 20 | extends Iterator<T> { | |
| 21 | ||
| 22 | /// Resets the iterator to its initial state, allowing for iteration to start over from the beginning. | |
| 23 | /// | |
| 24 | /// This method is intended for scenarios where the same iteration process needs to be repeated multiple times | |
| 25 | /// without recreating a new instance of the iterator. After calling this method, the iterator should behave as | |
| 26 | /// though it was freshly initialized, with any internal state reverted to its starting condition. | |
| 27 | void reset(); | |
| 28 | ||
| 29 | ||
| 30 | /// Returns a new [ResetableIterator] that filters elements based on the provided predicate. | |
| 31 | /// | |
| 32 | /// This method creates a new iterator that wraps the current iterator and applies the given predicate to each | |
| 33 | /// element. Only elements that satisfy the predicate (i.e., for which the predicate returns `true`) will be | |
| 34 | /// included in the iteration. | |
| 35 | /// | |
| 36 | /// @param predicate a [Predicate] used to determine which elements to include in the filtered iterator | |
| 37 | /// | |
| 38 | /// @return a new [ResetableIterator] instance that provides only the elements matching the predicate | |
| 39 | default ResetableIterator<T> filter(Predicate<T> predicate) { | |
| 40 |
1
1. filter : replaced return value with null for pro/verron/officestamper/utils/iterator/ResetableIterator::filter → KILLED |
return new FilteringIterator<>(this, predicate); |
| 41 | } | |
| 42 | ||
| 43 | ||
| 44 | /// Returns a new [ResetableIterator] that provides a slice of the original iterator's elements. | |
| 45 | /// | |
| 46 | /// This method creates a new iterator that wraps the current iterator and provides only the elements between the | |
| 47 | /// specified start and end elements. The slicing is performed by iterating through the elements and including those | |
| 48 | /// that fall within the specified range. | |
| 49 | /// | |
| 50 | /// @param start the starting element of the slice (inclusive) | |
| 51 | /// @param end the ending element of the slice (inclusive) | |
| 52 | /// | |
| 53 | /// @return a new [ResetableIterator] instance that provides the sliced elements | |
| 54 | default ResetableIterator<T> slice(T start, T end) { | |
| 55 |
1
1. slice : replaced return value with null for pro/verron/officestamper/utils/iterator/ResetableIterator::slice → NO_COVERAGE |
return new SlicingIterator<>(this, start, end); |
| 56 | } | |
| 57 | ||
| 58 | /// Returns a new [ResetableIterator] that applies the given function to each element. | |
| 59 | /// | |
| 60 | /// This method creates a new iterator that wraps the current iterator and applies the provided function to | |
| 61 | /// transform each element. The resulting iterator will yield the transformed elements. | |
| 62 | /// | |
| 63 | /// @param function a [Function] used to transform each element | |
| 64 | /// @param <U> the type of elements returned by the function and the resulting iterator | |
| 65 | /// | |
| 66 | /// @return a new [ResetableIterator] instance that provides the transformed elements | |
| 67 | default <U> ResetableIterator<U> map(Function<T, U> function) { | |
| 68 |
1
1. map : replaced return value with null for pro/verron/officestamper/utils/iterator/ResetableIterator::map → KILLED |
return new MappingIterator<>(this, function); |
| 69 | } | |
| 70 | ||
| 71 | ||
| 72 | /// Collects the elements of this iterator into a container using the provided collector. | |
| 73 | /// | |
| 74 | /// This method creates a stream from the iterator's elements and collects them using the specified collector. It | |
| 75 | /// allows for flexible reduction operations such as collecting into lists, sets, maps, or performing other | |
| 76 | /// aggregation operations. | |
| 77 | /// | |
| 78 | /// @param collector the [Collector] used to accumulate elements into a result container | |
| 79 | /// @param <R> the type of the result container | |
| 80 | /// | |
| 81 | /// @return the result of the collection operation | |
| 82 | default <R> R collect(Collector<? super T, ?, R> collector) { | |
| 83 | var spliterator = Spliterators.spliteratorUnknownSize(this, Spliterator.ORDERED); | |
| 84 |
1
1. collect : replaced return value with null for pro/verron/officestamper/utils/iterator/ResetableIterator::collect → NO_COVERAGE |
return StreamSupport.stream(spliterator, false) |
| 85 | .collect(collector); | |
| 86 | } | |
| 87 | } | |
Mutations | ||
| 40 |
1.1 |
|
| 55 |
1.1 |
|
| 68 |
1.1 |
|
| 84 |
1.1 |