ResetableIterator.java

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

Mutations

41

1.1
Location : filter
Killed by : pro.verron.officestamper.utils.iterator.ResetableIteratorTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.iterator.ResetableIteratorTest]/[method:testFilter()]
replaced return value with null for pro/verron/officestamper/utils/iterator/ResetableIterator::filter → KILLED

56

1.1
Location : slice
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/iterator/ResetableIterator::slice → NO_COVERAGE

69

1.1
Location : map
Killed by : pro.verron.officestamper.utils.iterator.ResetableIteratorTest.[engine:junit-jupiter]/[class:pro.verron.officestamper.utils.iterator.ResetableIteratorTest]/[method:testMappedIteratorReset()]
replaced return value with null for pro/verron/officestamper/utils/iterator/ResetableIterator::map → KILLED

85

1.1
Location : collect
Killed by : none
replaced return value with null for pro/verron/officestamper/utils/iterator/ResetableIterator::collect → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.23.1 support