Skip to content

Commit 4768e42

Browse files
committed
Fixes inherited methods downgrading ralist into plain list.
1 parent 487fbf0 commit 4768e42

File tree

2 files changed

+86
-73
lines changed

2 files changed

+86
-73
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ JImmutableRandomAccessList<T> assign(int index,
7272
@Override
7373
JImmutableRandomAccessList<T> insert(@Nullable T value);
7474

75+
@Nonnull
76+
@Override
77+
default JImmutableRandomAccessList<T> insert(@Nonnull Iterable<? extends T> values)
78+
{
79+
return insertAll(values);
80+
}
81+
7582
/**
7683
* Insert value at index (which must be within 0 to size).
7784
* Shifts all values at and after index one position to the right and adds 1
@@ -255,6 +262,27 @@ JImmutableRandomAccessList<T> insertAll(int index,
255262
@Override
256263
JImmutableRandomAccessList<T> deleteAll();
257264

265+
/**
266+
* Returns a list of the same type as this containing only those elements for which
267+
* predicate returns true. Implementations are optimized assuming predicate will
268+
* return false more often than true.
269+
*
270+
* @param predicate decides whether to include an element
271+
* @return list of same type as this containing only those elements for which predicate returns true
272+
*/
273+
@Nonnull
274+
@Override
275+
default JImmutableRandomAccessList<T> select(@Nonnull Predicate<T> predicate)
276+
{
277+
JImmutableRandomAccessList<T> answer = deleteAll();
278+
for (T value : this) {
279+
if (predicate.test(value)) {
280+
answer = answer.insert(value);
281+
}
282+
}
283+
return answer.size() == size() ? this : answer;
284+
}
285+
258286
/**
259287
* Returns a list of the same type as this containing all those elements for which
260288
* predicate returns false. Implementation optimized assuming predicate will

src/test/java/org/javimmutable/collections/btree_list/JImmutableBtreeListTest.java

Lines changed: 58 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.javimmutable.collections.Cursor;
4040
import org.javimmutable.collections.Func0;
4141
import org.javimmutable.collections.Func2;
42-
import org.javimmutable.collections.JImmutableList;
4342
import org.javimmutable.collections.JImmutableRandomAccessList;
4443
import org.javimmutable.collections.MutableBuilder;
4544
import org.javimmutable.collections.common.StandardIterableStreamableTests;
@@ -149,7 +148,7 @@ public void test()
149148

150149
public void testInsertIterable()
151150
{
152-
JImmutableList<Integer> list = JImmutableBtreeList.of();
151+
JImmutableRandomAccessList<Integer> list = JImmutableBtreeList.of();
153152
StandardCursorTest.emptyCursorTest(list.cursor());
154153

155154
list = list.insert(Arrays.asList(1, 2, 3));
@@ -170,19 +169,19 @@ public void testInsertAllAtIndex()
170169
//empty into empty
171170
JImmutableBtreeList<Integer> list = JImmutableBtreeList.of();
172171
try {
173-
list.insertAll(-1, plainIterable(Collections.<Integer>emptyList()));
172+
list.insertAll(-1, plainIterable(Collections.emptyList()));
174173
fail();
175174
} catch (IndexOutOfBoundsException ignored) {
176175
//expected
177176
}
178177
try {
179-
list.insertAll(-1, Collections.<Integer>emptyList());
178+
list.insertAll(-1, Collections.emptyList());
180179
fail();
181180
} catch (IndexOutOfBoundsException ignored) {
182181
//expected
183182
}
184183
try {
185-
list.insertAll(-1, getCursor(Collections.<Integer>emptyList()));
184+
list.insertAll(-1, getCursor(Collections.emptyList()));
186185
} catch (IndexOutOfBoundsException ignored) {
187186
//expected
188187
}
@@ -219,19 +218,19 @@ public void testInsertAllAtIndex()
219218
//empty into values
220219
list = list.insert(4).insert(5);
221220
try {
222-
list.insertAll(-1, plainIterable(Collections.<Integer>emptyList()));
221+
list.insertAll(-1, plainIterable(Collections.emptyList()));
223222
fail();
224223
} catch (IndexOutOfBoundsException ignored) {
225224
//expected
226225
}
227226
try {
228-
list.insertAll(-1, Collections.<Integer>emptyList());
227+
list.insertAll(-1, Collections.emptyList());
229228
fail();
230229
} catch (IndexOutOfBoundsException ignored) {
231230
//expected
232231
}
233232
try {
234-
list.insertAll(-1, getCursor(Collections.<Integer>emptyList()));
233+
list.insertAll(-1, getCursor(Collections.emptyList()));
235234
} catch (IndexOutOfBoundsException ignored) {
236235
//expected
237236
}
@@ -269,9 +268,9 @@ public void testInsertAllAtIndex()
269268
//empty into empty
270269
list = JImmutableBtreeList.of();
271270
JImmutableBtreeList<Integer> expected = list;
272-
checkCursorable = list.insertAll(0, plainIterable(Collections.<Integer>emptyList()));
273-
checkCollection = list.insertAll(0, Collections.<Integer>emptyList());
274-
checkCursor = list.insertAll(0, getCursor(Collections.<Integer>emptyList()));
271+
checkCursorable = list.insertAll(0, plainIterable(Collections.emptyList()));
272+
checkCollection = list.insertAll(0, Collections.emptyList());
273+
checkCursor = list.insertAll(0, getCursor(Collections.emptyList()));
275274
checkIterator = list.insertAll(0, Collections.<Integer>emptyList().iterator());
276275
assertEquals(expected, checkCursorable);
277276
assertEquals(expected, checkCollection);
@@ -291,9 +290,9 @@ public void testInsertAllAtIndex()
291290

292291
//empty into values
293292
list = list.insert(4).insert(5);
294-
checkCursorable = list.insertAll(0, plainIterable(Collections.<Integer>emptyList()));
295-
checkCollection = list.insertAll(0, Collections.<Integer>emptyList());
296-
checkCursor = list.insertAll(0, getCursor(Collections.<Integer>emptyList()));
293+
checkCursorable = list.insertAll(0, plainIterable(Collections.emptyList()));
294+
checkCollection = list.insertAll(0, Collections.emptyList());
295+
checkCursor = list.insertAll(0, getCursor(Collections.emptyList()));
297296
checkIterator = list.insertAll(0, Collections.<Integer>emptyList().iterator());
298297
assertEquals(expected, checkCursorable);
299298
assertEquals(expected, checkCollection);
@@ -316,19 +315,19 @@ public void testInsertAllAtIndex()
316315
//empty into empty -- should fail
317316
list = JImmutableBtreeList.of();
318317
try {
319-
list.insertAll(1, plainIterable(Collections.<Integer>emptyList()));
318+
list.insertAll(1, plainIterable(Collections.emptyList()));
320319
fail();
321320
} catch (IndexOutOfBoundsException ignored) {
322321
//expected
323322
}
324323
try {
325-
list.insertAll(1, Collections.<Integer>emptyList());
324+
list.insertAll(1, Collections.emptyList());
326325
fail();
327326
} catch (IndexOutOfBoundsException ignored) {
328327
//expected
329328
}
330329
try {
331-
list.insertAll(1, getCursor(Collections.<Integer>emptyList()));
330+
list.insertAll(1, getCursor(Collections.emptyList()));
332331
} catch (IndexOutOfBoundsException ignored) {
333332
//expected
334333
}
@@ -365,9 +364,9 @@ public void testInsertAllAtIndex()
365364
//empty into values -- should work
366365
list = list.insert(0).insert(3);
367366
expected = list;
368-
checkCursorable = list.insertAll(0, plainIterable(Collections.<Integer>emptyList()));
369-
checkCollection = list.insertAll(0, Collections.<Integer>emptyList());
370-
checkCursor = list.insertAll(0, getCursor(Collections.<Integer>emptyList()));
367+
checkCursorable = list.insertAll(0, plainIterable(Collections.emptyList()));
368+
checkCollection = list.insertAll(0, Collections.emptyList());
369+
checkCursor = list.insertAll(0, getCursor(Collections.emptyList()));
371370
checkIterator = list.insertAll(0, Collections.<Integer>emptyList().iterator());
372371
assertEquals(expected, checkCursorable);
373372
assertEquals(expected, checkCollection);
@@ -390,9 +389,9 @@ public void testInsertAllAtIndex()
390389
//empty into empty
391390
list = JImmutableBtreeList.of();
392391
expected = list;
393-
checkCursorable = list.insertAll(list.size(), plainIterable(Collections.<Integer>emptyList()));
394-
checkCollection = list.insertAll(list.size(), Collections.<Integer>emptyList());
395-
checkCursor = list.insertAll(list.size(), getCursor(Collections.<Integer>emptyList()));
392+
checkCursorable = list.insertAll(list.size(), plainIterable(Collections.emptyList()));
393+
checkCollection = list.insertAll(list.size(), Collections.emptyList());
394+
checkCursor = list.insertAll(list.size(), getCursor(Collections.emptyList()));
396395
checkIterator = list.insertAll(list.size(), Collections.<Integer>emptyList().iterator());
397396
assertEquals(expected, checkCursorable);
398397
assertEquals(expected, checkCollection);
@@ -412,9 +411,9 @@ public void testInsertAllAtIndex()
412411

413412
//empty into values
414413
list = list.insert(0).insert(1).insert(2).insert(3);
415-
checkCursorable = list.insertAll(list.size(), plainIterable(Collections.<Integer>emptyList()));
416-
checkCollection = list.insertAll(list.size(), Collections.<Integer>emptyList());
417-
checkCursor = list.insertAll(list.size(), getCursor(Collections.<Integer>emptyList()));
414+
checkCursorable = list.insertAll(list.size(), plainIterable(Collections.emptyList()));
415+
checkCollection = list.insertAll(list.size(), Collections.emptyList());
416+
checkCursor = list.insertAll(list.size(), getCursor(Collections.emptyList()));
418417
checkIterator = list.insertAll(list.size(), Collections.<Integer>emptyList().iterator());
419418
assertEquals(expected, checkCursorable);
420419
assertEquals(expected, checkCollection);
@@ -436,19 +435,19 @@ public void testInsertAllAtIndex()
436435
// empty into empty
437436
list = JImmutableBtreeList.of();
438437
try {
439-
list.insertAll(list.size() + 1, plainIterable(Collections.<Integer>emptyList()));
438+
list.insertAll(list.size() + 1, plainIterable(Collections.emptyList()));
440439
fail();
441440
} catch (IndexOutOfBoundsException ignored) {
442441
//expected
443442
}
444443
try {
445-
list.insertAll(list.size() + 1, Collections.<Integer>emptyList());
444+
list.insertAll(list.size() + 1, Collections.emptyList());
446445
fail();
447446
} catch (IndexOutOfBoundsException ignored) {
448447
//expected
449448
}
450449
try {
451-
list.insertAll(list.size() + 1, getCursor(Collections.<Integer>emptyList()));
450+
list.insertAll(list.size() + 1, getCursor(Collections.emptyList()));
452451
} catch (IndexOutOfBoundsException ignored) {
453452
//expected
454453
}
@@ -485,19 +484,19 @@ public void testInsertAllAtIndex()
485484
//empty into values
486485
list = list.insert(4).insert(5);
487486
try {
488-
list.insertAll(list.size() + 1, plainIterable(Collections.<Integer>emptyList()));
487+
list.insertAll(list.size() + 1, plainIterable(Collections.emptyList()));
489488
fail();
490489
} catch (IndexOutOfBoundsException ignored) {
491490
//expected
492491
}
493492
try {
494-
list.insertAll(list.size() + 1, Collections.<Integer>emptyList());
493+
list.insertAll(list.size() + 1, Collections.emptyList());
495494
fail();
496495
} catch (IndexOutOfBoundsException ignored) {
497496
//expected
498497
}
499498
try {
500-
list.insertAll(list.size() + 1, getCursor(Collections.<Integer>emptyList()));
499+
list.insertAll(list.size() + 1, getCursor(Collections.emptyList()));
501500
} catch (IndexOutOfBoundsException ignored) {
502501
//expected
503502
}
@@ -537,9 +536,9 @@ public void testInsertAllFirst()
537536
//empty into empty
538537
JImmutableBtreeList<Integer> list = JImmutableBtreeList.of();
539538
JImmutableBtreeList<Integer> expected = list;
540-
JImmutableBtreeList<Integer> checkCursorable = list.insertAllFirst(plainIterable(Collections.<Integer>emptyList()));
541-
JImmutableBtreeList<Integer> checkCollection = list.insertAllFirst(Collections.<Integer>emptyList());
542-
JImmutableBtreeList<Integer> checkCursor = list.insertAllFirst(getCursor(Collections.<Integer>emptyList()));
539+
JImmutableBtreeList<Integer> checkCursorable = list.insertAllFirst(plainIterable(Collections.emptyList()));
540+
JImmutableBtreeList<Integer> checkCollection = list.insertAllFirst(Collections.emptyList());
541+
JImmutableBtreeList<Integer> checkCursor = list.insertAllFirst(getCursor(Collections.emptyList()));
543542
JImmutableBtreeList<Integer> checkIterator = list.insertAllFirst(Collections.<Integer>emptyList().iterator());
544543
assertEquals(expected, checkCursorable);
545544
assertEquals(expected, checkCollection);
@@ -560,9 +559,9 @@ public void testInsertAllFirst()
560559
//empty into values
561560
list = list.insert(4).insert(5);
562561
expected = list;
563-
checkCursorable = list.insertAllFirst(plainIterable(Collections.<Integer>emptyList()));
564-
checkCollection = list.insertAllFirst(Collections.<Integer>emptyList());
565-
checkCursor = list.insertAllFirst(getCursor(Collections.<Integer>emptyList()));
562+
checkCursorable = list.insertAllFirst(plainIterable(Collections.emptyList()));
563+
checkCollection = list.insertAllFirst(Collections.emptyList());
564+
checkCursor = list.insertAllFirst(getCursor(Collections.emptyList()));
566565
checkIterator = list.insertAllFirst(Collections.<Integer>emptyList().iterator());
567566
assertEquals(expected, checkCursorable);
568567
assertEquals(expected, checkCollection);
@@ -588,9 +587,9 @@ public void testInsertAllLast()
588587
//empty into empty
589588
JImmutableBtreeList<Integer> list = JImmutableBtreeList.of();
590589
JImmutableBtreeList<Integer> expected = list;
591-
JImmutableBtreeList<Integer> checkCursorable = list.insertAll(plainIterable(Collections.<Integer>emptyList()));
592-
JImmutableBtreeList<Integer> checkCollection = list.insertAll(Collections.<Integer>emptyList());
593-
JImmutableBtreeList<Integer> checkCursor = list.insertAll(getCursor(Collections.<Integer>emptyList()));
590+
JImmutableBtreeList<Integer> checkCursorable = list.insertAll(plainIterable(Collections.emptyList()));
591+
JImmutableBtreeList<Integer> checkCollection = list.insertAll(Collections.emptyList());
592+
JImmutableBtreeList<Integer> checkCursor = list.insertAll(getCursor(Collections.emptyList()));
594593
JImmutableBtreeList<Integer> checkIterator = list.insertAll(Collections.<Integer>emptyList().iterator());
595594
assertEquals(expected, checkCursorable);
596595
assertEquals(expected, checkCollection);
@@ -611,9 +610,9 @@ public void testInsertAllLast()
611610
//empty into values
612611
list = list.insert(0);
613612
expected = list;
614-
checkCursorable = list.insertAll(plainIterable(Collections.<Integer>emptyList()));
615-
checkCollection = list.insertAll(Collections.<Integer>emptyList());
616-
checkCursor = list.insertAll(getCursor(Collections.<Integer>emptyList()));
613+
checkCursorable = list.insertAll(plainIterable(Collections.emptyList()));
614+
checkCollection = list.insertAll(Collections.emptyList());
615+
checkCursor = list.insertAll(getCursor(Collections.emptyList()));
617616
checkIterator = list.insertAll(Collections.<Integer>emptyList().iterator());
618617
assertEquals(expected, checkCursorable);
619618
assertEquals(expected, checkCollection);
@@ -635,9 +634,9 @@ public void testInsertAllLast()
635634
//empty into empty
636635
list = JImmutableBtreeList.of();
637636
expected = list;
638-
checkCursorable = list.insertAllLast(plainIterable(Collections.<Integer>emptyList()));
639-
checkCollection = list.insertAllLast(Collections.<Integer>emptyList());
640-
checkCursor = list.insertAllLast(getCursor(Collections.<Integer>emptyList()));
637+
checkCursorable = list.insertAllLast(plainIterable(Collections.emptyList()));
638+
checkCollection = list.insertAllLast(Collections.emptyList());
639+
checkCursor = list.insertAllLast(getCursor(Collections.emptyList()));
641640
checkIterator = list.insertAllLast(Collections.<Integer>emptyList().iterator());
642641
assertEquals(expected, checkCursorable);
643642
assertEquals(expected, checkCollection);
@@ -658,9 +657,9 @@ public void testInsertAllLast()
658657
//empty into values
659658
list = list.insert(0).insert(1).insert(2).insert(3);
660659
expected = list;
661-
checkCursorable = list.insertAllLast(plainIterable(Collections.<Integer>emptyList()));
662-
checkCollection = list.insertAllLast(Collections.<Integer>emptyList());
663-
checkCursor = list.insertAllLast(getCursor(Collections.<Integer>emptyList()));
660+
checkCursorable = list.insertAllLast(plainIterable(Collections.emptyList()));
661+
checkCollection = list.insertAllLast(Collections.emptyList());
662+
checkCursor = list.insertAllLast(getCursor(Collections.emptyList()));
664663
checkIterator = list.insertAllLast(Collections.<Integer>emptyList().iterator());
665664
assertEquals(expected, checkCursorable);
666665
assertEquals(expected, checkCollection);
@@ -1090,7 +1089,6 @@ public void testRandom4()
10901089
}
10911090
}
10921091

1093-
10941092
public void testCursor()
10951093
{
10961094
JImmutableBtreeList<Integer> list = JImmutableBtreeList.of();
@@ -1226,7 +1224,7 @@ public void testReject()
12261224

12271225
public void testStreams()
12281226
{
1229-
JImmutableList<Integer> list = JImmutableBtreeList.<Integer>builder().add(1, 2, 3, 4, 5, 6, 7).build();
1227+
JImmutableRandomAccessList<Integer> list = JImmutableBtreeList.<Integer>builder().add(1, 2, 3, 4, 5, 6, 7).build();
12301228
assertEquals(asList(1, 2, 3, 4), list.stream().filter(x -> x < 5).collect(toList()));
12311229
assertEquals(asList(1, 2, 3, 4), list.parallelStream().filter(x -> x < 5).collect(toList()));
12321230

@@ -1241,7 +1239,7 @@ public void testStreams()
12411239

12421240
public void testParallelStreams()
12431241
{
1244-
final JImmutableList<Integer> original = JImmutableBtreeList.of(IndexedList.retained(StandardCursor.makeList(StandardCursor.forRange(1, 10000))));
1242+
final JImmutableRandomAccessList<Integer> original = JImmutableBtreeList.of(IndexedList.retained(StandardCursor.makeList(StandardCursor.forRange(1, 10000))));
12451243
final List<Integer> collected = original.stream().parallel().collect(toList());
12461244
assertEquals(original.getList(), collected);
12471245
}
@@ -1256,30 +1254,17 @@ public void testBuilder()
12561254
list.checkInvariants();
12571255
}
12581256

1259-
Func0<? extends MutableBuilder<Integer, JImmutableRandomAccessList<Integer>>> factory = new Func0<JImmutableBtreeList.Builder<Integer>>()
1260-
{
1261-
@Override
1262-
public JImmutableBtreeList.Builder<Integer> apply()
1263-
{
1264-
return JImmutableBtreeList.builder();
1265-
}
1266-
};
1257+
Func0<? extends MutableBuilder<Integer, JImmutableRandomAccessList<Integer>>> factory = (Func0<JImmutableBtreeList.Builder<Integer>>)() -> JImmutableBtreeList.builder();
12671258

1268-
Func2<List<Integer>, JImmutableRandomAccessList<Integer>, Boolean> comparator = new Func2<List<Integer>, JImmutableRandomAccessList<Integer>, Boolean>()
1269-
{
1270-
@Override
1271-
public Boolean apply(List<Integer> list,
1272-
JImmutableRandomAccessList<Integer> tree)
1273-
{
1274-
((JImmutableBtreeList<Integer>)tree).checkInvariants();
1275-
for (int i = 0; i < list.size(); ++i) {
1276-
assertEquals(list.get(i), tree.get(i));
1277-
}
1278-
return true;
1259+
Func2<List<Integer>, JImmutableRandomAccessList<Integer>, Boolean> comparator = (list, tree) -> {
1260+
tree.checkInvariants();
1261+
for (int i = 0; i < list.size(); ++i) {
1262+
assertEquals(list.get(i), tree.get(i));
12791263
}
1264+
return true;
12801265
};
12811266

1282-
StandardMutableBuilderTests.verifyBuilder(source, factory, (comparator));
1267+
StandardMutableBuilderTests.verifyBuilder(source, factory, comparator);
12831268
}
12841269

12851270
private JImmutableRandomAccessList<Integer> ralist(Integer... values)

0 commit comments

Comments
 (0)