summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/javacheck/Iterators.java
blob: 33652b2e30b0d403696e1936d4b4fa72b6faac31 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package au.id.zancanaro.javacheck;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Function;
import java.util.function.Predicate;

public final class Iterators {
    private Iterators() {
    }

    public static <T> RangeIterator<T> rangeIterator(int countTo, Function<Integer,T> fn) {
        return new RangeIterator<>(countTo, fn);
    }

    private static class RangeIterator<T> implements Iterator<T> {
        private final Function<Integer, T> action;
        private final int countTo;
        private int index = 0;

        public RangeIterator(int countTo, Function<Integer, T> action) {
            this.countTo = countTo;
            this.action = action;
        }

        @Override
        public boolean hasNext() {
            return index < countTo;
        }

        @Override
        public T next() {
            return action.apply(index++);
        }
    }

    public static <T> FlattenIterator<T> flatten(Iterator<Iterator<T>> iterators) {
        return new FlattenIterator<>(iterators);
    }
    public static class FlattenIterator<T> implements Iterator<T> {
        private final Iterator<Iterator<T>> iterators;

        private Iterator<T> current;

        public FlattenIterator(Iterator<Iterator<T>> iterators) {
            this.current = Iterators.emptyIterator();
            this.iterators = iterators;
        }

        private Iterator<T> getCurrent() {
            while (!current.hasNext() && iterators.hasNext()) {
                current = iterators.next();
            }
            return current;
        }

        @Override
        public boolean hasNext() {
            return getCurrent().hasNext();
        }
        @Override
        public T next() {
            return getCurrent().next();
        }

    }

    public static <T> ConcatIterator<T> concat(Iterator<T> left, Iterator<T> right) {
        return new ConcatIterator<>(left, right);
    }
    public static class ConcatIterator<T> implements Iterator<T> {
        private final Iterator<T> left;

        private final Iterator<T> right;

        public ConcatIterator(Iterator<T> left, Iterator<T> right) {
            this.left = left;
            this.right = right;
        }

        @Override
        public boolean hasNext() {
            return left.hasNext() || right.hasNext();
        }
        @Override
        public T next() {
            if (left.hasNext()) {
                return left.next();
            } else {
                return right.next();
            }
        }

    }

    public static <T> EmptyIterator<T> emptyIterator() {
        return new EmptyIterator<>();
    }
    public static class EmptyIterator<T> implements Iterator<T> {

        @Override
        public boolean hasNext() {
            return false;
        }
        @Override
        public T next() {
            throw new NoSuchElementException("Empty iterators contain no elements");
        }

    }

    public static <T,R> MappingIterator<T,R> mappingIterator(Function<T,R> f, Iterator<T> iterator) {
        return new MappingIterator<>(f, iterator);
    }
    private static class MappingIterator<T, R> implements Iterator<R> {
        private final Function<T, R> mapping;

        private final Iterator<T> iterator;

        public MappingIterator(Function<T, R> mapping, Iterator<T> iterator) {
            this.mapping = mapping;
            this.iterator = iterator;
        }

        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }
        @Override
        public R next() {
            return mapping.apply(iterator.next());
        }

    }

    public static <T> FilteringIterator<T> filteringIterator(Predicate<T> predicate, Iterator<T> iterator) {
        return new FilteringIterator<>(predicate, iterator);
    }

    private static class FilteringIterator<T> implements Iterator<T> {
        private final Predicate<T> predicate;
        private final Iterator<T> iterator;
        private List<T> nextValue;

        public FilteringIterator(Predicate<T> predicate, Iterator<T> iterator) {
            this.predicate = predicate;
            this.iterator = iterator;
            this.nextValue = null;
        }

        private void populateNext() {
            while (nextValue == null && iterator.hasNext()) {
                T value = iterator.next();
                if (predicate.test(value)) {
                    nextValue = Collections.singletonList(value);
                } else {
                    nextValue = null;
                }
            }
        }

        @Override
        public boolean hasNext() {
            populateNext();
            return nextValue != null;
        }

        @Override
        public T next() {
            populateNext();
            T result = nextValue.get(0);
            nextValue = null;
            return result;
        }
    }
}