Skip to content

Commit 32ffdce

Browse files
committed
Refactoring select, reject, and reduce to use Iterables instead of Cursorables.
1 parent edaf1fc commit 32ffdce

File tree

6 files changed

+43
-70
lines changed

6 files changed

+43
-70
lines changed

src/main/java/org/javimmutable/collections/Cursorable.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
package org.javimmutable.collections;
3737

3838
import javax.annotation.Nonnull;
39-
import java.util.function.BiFunction;
4039

4140
/**
4241
* Implemented by objects that can create a Cursor for a given type.
@@ -46,14 +45,4 @@ public interface Cursorable<T>
4645
{
4746
@Nonnull
4847
Cursor<T> cursor();
49-
50-
default <R> R reduce(R initialValue,
51-
@Nonnull BiFunction<R, T, R> operator)
52-
{
53-
R result = initialValue;
54-
for (Cursor<T> cursor = cursor().start(); cursor.hasValue(); cursor = cursor.next()) {
55-
result = operator.apply(result, cursor.getValue());
56-
}
57-
return result;
58-
}
5948
}

src/main/java/org/javimmutable/collections/JImmutableList.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ JImmutableList<T> assign(int index,
258258
default JImmutableList<T> select(@Nonnull Predicate<T> predicate)
259259
{
260260
JImmutableList<T> answer = deleteAll();
261-
for (Cursor<T> cursor = cursor().start(); cursor.hasValue(); cursor = cursor.next()) {
262-
final T value = cursor.getValue();
261+
for (T value : this) {
263262
if (predicate.test(value)) {
264263
answer = answer.insert(value);
265264
}

src/main/java/org/javimmutable/collections/JImmutableRandomAccessList.java

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,20 @@
4545
/**
4646
* Extension of JImmutableList that allows insertion and deletion at arbitrary
4747
* indexes within the list.
48-
*
49-
* @param <T>
5048
*/
5149
@Immutable
5250
public interface JImmutableRandomAccessList<T>
53-
extends JImmutableList<T>
51+
extends JImmutableList<T>
5452
{
5553
interface Builder<T>
56-
extends MutableBuilder<T, JImmutableRandomAccessList<T>>
54+
extends MutableBuilder<T, JImmutableRandomAccessList<T>>
5755
{
5856
}
5957

6058
/**
6159
* Replaces the value at the specified index (which must be within current
6260
* bounds of the list) with the new value.
6361
*
64-
* @param index
65-
* @param value
66-
* @return
6762
* @throws IndexOutOfBoundsException if index is out of bounds
6863
*/
6964
@Nonnull
@@ -72,9 +67,6 @@ JImmutableRandomAccessList<T> assign(int index,
7267

7368
/**
7469
* Adds a value to the end of the list. May be invoked on an empty list.
75-
*
76-
* @param value
77-
* @return
7870
*/
7971
@Nonnull
8072
JImmutableRandomAccessList<T> insert(@Nullable T value);
@@ -83,10 +75,6 @@ JImmutableRandomAccessList<T> assign(int index,
8375
* Insert value at index (which must be within 0 to size).
8476
* Shifts all values at and after index one position to the right and adds 1
8577
* to size of the list.
86-
*
87-
* @param index
88-
* @param value
89-
* @return
9078
*/
9179
@Nonnull
9280
JImmutableRandomAccessList<T> insert(int index,
@@ -95,19 +83,13 @@ JImmutableRandomAccessList<T> insert(int index,
9583
/**
9684
* Adds a value to the front of the list. May be invoked on an empty list.
9785
* Synonym for insert()
98-
*
99-
* @param value
100-
* @return
10186
*/
10287
@Nonnull
10388
JImmutableRandomAccessList<T> insertFirst(@Nullable T value);
10489

10590
/**
10691
* Adds a value to the end of the list. May be invoked on an empty list.
10792
* Synonym for insert().
108-
*
109-
* @param value
110-
* @return
11193
*/
11294
@Nonnull
11395
JImmutableRandomAccessList<T> insertLast(@Nullable T value);
@@ -116,7 +98,6 @@ JImmutableRandomAccessList<T> insert(int index,
11698
* Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
11799
* Synonym for insertAllLast()
118100
*
119-
* @param values
120101
* @return instance of list containing the collection
121102
*/
122103
@Nonnull
@@ -126,7 +107,6 @@ JImmutableRandomAccessList<T> insert(int index,
126107
* Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
127108
* Synonym for insertAllLast()
128109
*
129-
* @param values
130110
* @return instance of list containing the collection
131111
*/
132112
@Nonnull
@@ -136,7 +116,6 @@ JImmutableRandomAccessList<T> insert(int index,
136116
* Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
137117
* Synonym for insertAllLast()
138118
*
139-
* @param values
140119
* @return instance of list containing the collection
141120
*/
142121
@Nonnull
@@ -146,7 +125,6 @@ JImmutableRandomAccessList<T> insert(int index,
146125
* Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
147126
* Synonym for insertAllLast()
148127
*
149-
* @param values
150128
* @return instance of list containing the collection
151129
*/
152130
@Nonnull
@@ -158,8 +136,6 @@ JImmutableRandomAccessList<T> insert(int index,
158136
* Shifts all values at and after index x positions to the right and adds x
159137
* to size of the list, where x is the number of elements being inserted.
160138
*
161-
* @param index
162-
* @param values
163139
* @return instance of list containing the collection
164140
*/
165141
@Nonnull
@@ -172,8 +148,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
172148
* Shifts all values at and after index x positions to the right and adds x
173149
* to size of the list, where x is the number of elements being inserted.
174150
*
175-
* @param index
176-
* @param values
177151
* @return instance of list containing the collection
178152
*/
179153
@Nonnull
@@ -186,8 +160,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
186160
* Shifts all values at and after index x positions to the right and adds x
187161
* to size of the list, where x is the number of elements being inserted.
188162
*
189-
* @param index
190-
* @param values
191163
* @return instance of list containing the collection
192164
*/
193165
@Nonnull
@@ -200,8 +172,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
200172
* Shifts all values at and after index x positions to the right and adds x
201173
* to size of the list, where x is the number of elements being inserted.
202174
*
203-
* @param index
204-
* @param values
205175
* @return instance of list containing the collection
206176
*/
207177
@Nonnull
@@ -211,7 +181,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
211181
/**
212182
* Adds the values to the beginning of the list in the same order they appear in the Iterable. May be invoked on an empty list.
213183
*
214-
* @param values
215184
* @return instance of list containing the collection
216185
*/
217186
@Nonnull
@@ -220,7 +189,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
220189
/**
221190
* Adds the values to the beginning of the list in the same order they appear in the Iterable. May be invoked on an empty list.
222191
*
223-
* @param values
224192
* @return instance of list containing the collection
225193
*/
226194
@Nonnull
@@ -229,7 +197,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
229197
/**
230198
* Adds the values to the beginning of the list in the same order they appear in the Iterable. May be invoked on an empty list.
231199
*
232-
* @param values
233200
* @return instance of list containing the collection
234201
*/
235202
@Nonnull
@@ -238,7 +205,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
238205
/**
239206
* Adds the values to the beginning of the list in the same order they appear in the Iterable. May be invoked on an empty list.
240207
*
241-
* @param values
242208
* @return instance of list containing the collection
243209
*/
244210
@Nonnull
@@ -248,7 +214,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
248214
* Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
249215
* Synonym for insertAll()
250216
*
251-
* @param values
252217
* @return instance of list containing the collection
253218
*/
254219
@Nonnull
@@ -258,7 +223,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
258223
* Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
259224
* Synonym for insertAll()
260225
*
261-
* @param values
262226
* @return instance of list containing the collection
263227
*/
264228
@Nonnull
@@ -268,7 +232,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
268232
* Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
269233
* Synonym for insertAll()
270234
*
271-
* @param values
272235
* @return instance of list containing the collection
273236
*/
274237
@Nonnull
@@ -278,7 +241,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
278241
* Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
279242
* Synonym for insertAll()
280243
*
281-
* @param values
282244
* @return instance of list containing the collection
283245
*/
284246
@Nonnull
@@ -306,9 +268,6 @@ JImmutableRandomAccessList<T> insertAll(int index,
306268
* Delete value at index (which must be within the current bounds of the list).
307269
* Shifts all values at and after index one position to the left and subtracts 1
308270
* from size of the list.
309-
*
310-
* @param index
311-
* @return
312271
*/
313272
@Nonnull
314273
JImmutableRandomAccessList<T> delete(int index);
@@ -333,8 +292,7 @@ default JImmutableRandomAccessList<T> reject(@Nonnull Predicate<T> predicate)
333292
{
334293
JImmutableRandomAccessList<T> answer = this;
335294
int index = 0;
336-
for (Cursor<T> cursor = cursor().start(); cursor.hasValue(); cursor = cursor.next()) {
337-
final T value = cursor.getValue();
295+
for (T value : this) {
338296
assert value == answer.get(index);
339297
if (predicate.test(value)) {
340298
answer = answer.delete(index);

src/main/java/org/javimmutable/collections/SplitableIterable.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
package org.javimmutable.collections;
3737

3838
import javax.annotation.Nonnull;
39+
import java.util.function.BiFunction;
3940

4041
/**
4142
* Extension of Iterable for objects whose iterator method returns a SplitableIterator.
@@ -46,4 +47,18 @@ public interface SplitableIterable<T>
4647
@Override
4748
@Nonnull
4849
SplitableIterator<T> iterator();
50+
51+
/**
52+
* Default reduce operation. Can be used without requiring a stream to quickly collect
53+
* information from an iterable object in a single thread.
54+
*/
55+
default <R> R reduce(R initialValue,
56+
@Nonnull BiFunction<R, T, R> operator)
57+
{
58+
R result = initialValue;
59+
for (T value : this) {
60+
result = operator.apply(result, value);
61+
}
62+
return result;
63+
}
4964
}

src/test/java/org/javimmutable/collections/cursors/StandardCursorTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import java.util.Iterator;
5353
import java.util.List;
5454
import java.util.NoSuchElementException;
55-
import java.util.function.BiFunction;
5655

5756
import static java.util.Arrays.asList;
5857
import static org.assertj.core.api.Assertions.*;
@@ -86,17 +85,6 @@ public static void testVarious()
8685
listCursorTest(Arrays.asList(1, 2), StandardCursor.of(() -> new StandardCursor.RangeSource(1, 2)));
8786
}
8887

89-
public void testReduce()
90-
{
91-
final Double zero = 0.0;
92-
final BiFunction<Double, Integer, Double> operator = (s, v) -> s + ((double)v) / 2.0;
93-
assertSame(zero, StandardCursor.<Integer>emptyCursorable().reduce(zero, operator));
94-
assertEquals(0.0, rangeCursorable(0, 0).reduce(zero, operator));
95-
assertEquals(0.5, rangeCursorable(0, 1).reduce(zero, operator));
96-
assertEquals(1.5, rangeCursorable(0, 2).reduce(zero, operator));
97-
assertEquals(3.0, rangeCursorable(0, 3).reduce(zero, operator));
98-
}
99-
10088
public void testSplitAllowed()
10189
{
10290
assertEquals(false, forRange(1, 0).start().isSplitAllowed());

src/test/java/org/javimmutable/collections/iterators/IndexedIteratorTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
package org.javimmutable.collections.iterators;
3737

3838
import junit.framework.TestCase;
39+
import org.javimmutable.collections.SplitableIterable;
3940
import org.javimmutable.collections.indexed.IndexedHelper;
4041

42+
import java.util.function.BiFunction;
43+
4144
import static java.util.Arrays.asList;
4245
import static org.javimmutable.collections.iterators.StandardIteratorTests.*;
4346

@@ -55,4 +58,25 @@ public void test()
5558
verifyOrderedSplit(true, asList(1), asList(2, 3), IndexedIterator.iterator(IndexedHelper.indexed(1, 2, 3)));
5659
verifyOrderedSplit(true, asList(1, 2), asList(3, 4, 5), IndexedIterator.forRange(1, 5));
5760
}
61+
62+
public void testReduce()
63+
{
64+
final Double zero = 0.0;
65+
final BiFunction<Double, Integer, Double> operator = (s, v) -> s + ((double)v) / 2.0;
66+
assertSame(zero, emptyIterable().reduce(zero, operator));
67+
assertEquals(0.0, rangeIterable(0).reduce(zero, operator));
68+
assertEquals(0.5, rangeIterable(1).reduce(zero, operator));
69+
assertEquals(1.5, rangeIterable(2).reduce(zero, operator));
70+
assertEquals(3.0, rangeIterable(3).reduce(zero, operator));
71+
}
72+
73+
private SplitableIterable<Integer> emptyIterable()
74+
{
75+
return () -> EmptyIterator.of();
76+
}
77+
78+
private SplitableIterable<Integer> rangeIterable(int high)
79+
{
80+
return () -> IndexedIterator.forRange(0, high);
81+
}
5882
}

0 commit comments

Comments
 (0)