2
* Copyright 2014 Goldman Sachs.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.gs.collections.impl.utility.internal;
19
import java.util.ArrayList;
20
import java.util.Collection;
21
import java.util.List;
23
import com.gs.collections.api.block.function.Function2;
24
import com.gs.collections.api.block.predicate.Predicate2;
25
import com.gs.collections.api.block.procedure.Procedure2;
26
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
27
import com.gs.collections.api.list.MutableList;
28
import com.gs.collections.api.set.MutableSet;
29
import com.gs.collections.api.tuple.Pair;
30
import com.gs.collections.api.tuple.Twin;
31
import com.gs.collections.impl.block.factory.ObjectIntProcedures;
32
import com.gs.collections.impl.block.factory.Predicates;
33
import com.gs.collections.impl.block.factory.Predicates2;
34
import com.gs.collections.impl.block.factory.Procedures2;
35
import com.gs.collections.impl.block.function.AddFunction;
36
import com.gs.collections.impl.block.function.MaxSizeFunction;
37
import com.gs.collections.impl.block.function.MinSizeFunction;
38
import com.gs.collections.impl.block.procedure.CollectionAddProcedure;
39
import com.gs.collections.impl.block.procedure.DoNothingProcedure;
40
import com.gs.collections.impl.factory.Lists;
41
import com.gs.collections.impl.list.Interval;
42
import com.gs.collections.impl.list.mutable.FastList;
43
import com.gs.collections.impl.set.mutable.UnifiedSet;
44
import com.gs.collections.impl.test.Verify;
45
import com.gs.collections.impl.tuple.Tuples;
46
import com.gs.collections.impl.utility.Iterate;
47
import com.gs.collections.impl.utility.ListIterate;
48
import org.junit.Assert;
49
import org.junit.Test;
51
import static com.gs.collections.impl.factory.Iterables.*;
53
public class RandomAccessListIterateTest
55
@Test(expected = IllegalArgumentException.class)
56
public void forEachWithNegativeFroms()
58
RandomAccessListIterate.forEach(FastList.newList(), -1, 1, DoNothingProcedure.DO_NOTHING);
61
@Test(expected = IllegalArgumentException.class)
62
public void forEachWithNegativeTos()
64
RandomAccessListIterate.forEach(FastList.newList(), 1, -1, DoNothingProcedure.DO_NOTHING);
68
public void forEachInBothWithNull()
70
RandomAccessListIterate.forEachInBoth(null, FastList.newListWith(1, 2, 3), new FailProcedure2());
71
RandomAccessListIterate.forEachInBoth(FastList.newListWith(1, 2, 3), null, new FailProcedure2());
74
@Test(expected = IllegalArgumentException.class)
75
public void forEachInBothThrowsOnMisMatchedLists()
77
RandomAccessListIterate.forEachInBoth(FastList.newListWith("1", 2), FastList.newListWith(1, 2, 3),
78
Procedures2.fromProcedure(DoNothingProcedure.DO_NOTHING));
82
public void removeIf()
84
List<Integer> result = RandomAccessListIterate.removeIf(FastList.newListWith(1, 2, 3), Predicates.greaterThan(1));
85
Verify.assertSize(1, result);
89
public void dropWithLargeCount()
91
Verify.assertEmpty(RandomAccessListIterate.drop(FastList.newListWith(1, 2, 3), 5));
95
public void injectInto()
97
MutableList<Integer> list = Lists.fixedSize.of(1, 2, 3);
98
Assert.assertEquals(Integer.valueOf(7), RandomAccessListIterate.injectInto(1, list, AddFunction.INTEGER));
102
public void injectIntoInt()
104
MutableList<Integer> list = Lists.fixedSize.of(1, 2, 3);
105
Assert.assertEquals(7, RandomAccessListIterate.injectInto(1, list, AddFunction.INTEGER_TO_INT));
109
public void injectIntoLong()
111
MutableList<Integer> list = Lists.fixedSize.of(1, 2, 3);
112
Assert.assertEquals(7, RandomAccessListIterate.injectInto(1, list, AddFunction.INTEGER_TO_LONG));
116
public void injectIntoDouble()
118
MutableList<Double> list = Lists.fixedSize.of(1.0, 2.0, 3.0);
119
Assert.assertEquals(7.0d, RandomAccessListIterate.injectInto(1.0, list, AddFunction.DOUBLE), 0.001);
123
public void injectIntoString()
125
MutableList<String> list = Lists.fixedSize.of("1", "2", "3");
126
Assert.assertEquals("0123", RandomAccessListIterate.injectInto("0", list, AddFunction.STRING));
130
public void injectIntoMaxString()
132
MutableList<String> list = Lists.fixedSize.of("1", "12", "123");
133
Function2<Integer, String, Integer> function = MaxSizeFunction.STRING;
134
Assert.assertEquals(Integer.valueOf(3), RandomAccessListIterate.injectInto(Integer.MIN_VALUE, list, function));
138
public void injectIntoMinString()
140
MutableList<String> list = Lists.fixedSize.of("1", "12", "123");
141
Function2<Integer, String, Integer> function = MinSizeFunction.STRING;
142
Assert.assertEquals(Integer.valueOf(1), RandomAccessListIterate.injectInto(Integer.MAX_VALUE, list, function));
146
public void collect()
149
iList("true", "false", "null"),
150
RandomAccessListIterate.collect(mList(true, false, null), String::valueOf));
154
public void collectReflective()
157
iList("true", "false", "null"),
158
RandomAccessListIterate.collect(mList(true, false, null), String::valueOf));
161
iList("true", "false", "null"),
162
RandomAccessListIterate.collect(mList(true, false, null), String::valueOf, new ArrayList<String>()));
166
public void flattenReflective()
168
MutableList<MutableList<Boolean>> list = Lists.fixedSize.<MutableList<Boolean>>of(
169
Lists.fixedSize.of(true, false),
170
Lists.fixedSize.of(true, null));
171
MutableList<Boolean> newList = RandomAccessListIterate.flatCollect(list, mutableList -> mutableList.toList());
172
Verify.assertListsEqual(
173
FastList.newListWith(true, false, true, null),
176
MutableSet<Boolean> newSet = RandomAccessListIterate.flatCollect(list, mutableList -> mutableList.toSet(), UnifiedSet.<Boolean>newSet());
177
Verify.assertSetsEqual(
178
UnifiedSet.newSetWith(true, false, null),
183
public void getLast()
185
MutableList<Boolean> list = Lists.fixedSize.of(true, null, false);
186
Assert.assertEquals(Boolean.FALSE, RandomAccessListIterate.getLast(list));
190
public void getLastOnEmpty()
192
List<?> list = new ArrayList<Object>();
193
Assert.assertNull(RandomAccessListIterate.getLast(list));
199
MutableList<Integer> list = this.getIntegerList();
200
int result = RandomAccessListIterate.count(list, Predicates.attributeEqual(Number::intValue, 3));
201
Assert.assertEquals(1, result);
202
int result2 = RandomAccessListIterate.count(list, Predicates.attributeEqual(Number::intValue, 6));
203
Assert.assertEquals(0, result2);
206
private MutableList<Integer> getIntegerList()
208
return Interval.toReverseList(1, 5);
212
public void forEachWithIndex()
214
MutableList<Integer> list = this.getIntegerList();
215
Iterate.sortThis(list);
216
RandomAccessListIterate.forEachWithIndex(list, (object, index) -> Assert.assertEquals(index, object - 1));
220
public void forEachUsingFromTo()
222
MutableList<Integer> integers = Interval.oneTo(5).toList();
224
this.assertForEachUsingFromTo(integers);
225
MutableList<Integer> reverseResults = Lists.mutable.of();
226
CollectionAddProcedure<Integer> procedure = CollectionAddProcedure.on(reverseResults);
227
this.assertReverseForEachUsingFromTo(integers, reverseResults, procedure);
230
private void assertForEachUsingFromTo(List<Integer> integers)
232
MutableList<Integer> results = Lists.mutable.of();
233
RandomAccessListIterate.forEach(integers, 0, 4, CollectionAddProcedure.on(results));
234
Assert.assertEquals(integers, results);
235
MutableList<Integer> reverseResults = Lists.mutable.of();
236
CollectionAddProcedure<Integer> procedure = CollectionAddProcedure.on(reverseResults);
238
Verify.assertThrows(IllegalArgumentException.class, () -> RandomAccessListIterate.forEach(integers, 4, -1, procedure));
239
Verify.assertThrows(IllegalArgumentException.class, () -> RandomAccessListIterate.forEach(integers, -1, 4, procedure));
242
private void assertReverseForEachUsingFromTo(List<Integer> integers, MutableList<Integer> reverseResults, CollectionAddProcedure<Integer> procedure)
244
RandomAccessListIterate.forEach(integers, 4, 0, procedure);
245
Assert.assertEquals(ListIterate.reverseThis(integers), reverseResults);
249
public void forEachWithIndexUsingFromTo()
251
MutableList<Integer> integers = Interval.oneTo(5).toList();
252
this.assertForEachWithIndexUsingFromTo(integers);
253
MutableList<Integer> reverseResults = Lists.mutable.of();
254
ObjectIntProcedure<Integer> objectIntProcedure = ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(reverseResults));
255
this.assertReverseForEachIndexUsingFromTo(integers, reverseResults, objectIntProcedure);
258
private void assertForEachWithIndexUsingFromTo(List<Integer> integers)
260
MutableList<Integer> results = Lists.mutable.of();
261
RandomAccessListIterate.forEachWithIndex(integers, 0, 4, ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(results)));
262
Assert.assertEquals(integers, results);
263
MutableList<Integer> reverseResults = Lists.mutable.of();
264
ObjectIntProcedure<Integer> objectIntProcedure = ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(reverseResults));
265
Verify.assertThrows(IllegalArgumentException.class, () -> RandomAccessListIterate.forEachWithIndex(integers, 4, -1, objectIntProcedure));
266
Verify.assertThrows(IllegalArgumentException.class, () -> RandomAccessListIterate.forEachWithIndex(integers, -1, 4, objectIntProcedure));
269
private void assertReverseForEachIndexUsingFromTo(MutableList<Integer> integers, MutableList<Integer> reverseResults, ObjectIntProcedure<Integer> objectIntProcedure)
271
RandomAccessListIterate.forEachWithIndex(integers, 4, 0, objectIntProcedure);
272
Assert.assertEquals(ListIterate.reverseThis(integers), reverseResults);
276
public void forEachInBoth()
278
MutableList<String> list1 = Lists.fixedSize.of("1", "2");
279
MutableList<String> list2 = Lists.fixedSize.of("a", "b");
280
List<Pair<String, String>> list = new ArrayList<Pair<String, String>>();
281
RandomAccessListIterate.forEachInBoth(list1, list2, (argument1, argument2) -> {
282
list.add(Tuples.twin(argument1, argument2));
284
Assert.assertEquals(FastList.newListWith(Tuples.twin("1", "a"), Tuples.twin("2", "b")), list);
288
public void detectWith()
290
MutableList<Integer> list = this.getIntegerList();
291
Assert.assertEquals(Integer.valueOf(1), RandomAccessListIterate.detectWith(list, Object::equals, 1));
292
MutableList<Integer> list2 = Lists.fixedSize.of(1, 2, 2);
293
Assert.assertSame(list2.get(1), RandomAccessListIterate.detectWith(list2, Object::equals, 2));
297
public void selectWith()
299
MutableList<Integer> list = this.getIntegerList();
300
Verify.assertSize(5, RandomAccessListIterate.selectWith(list, Predicates2.instanceOf(), Integer.class));
304
public void rejectWith()
306
MutableList<Integer> list = this.getIntegerList();
307
Verify.assertEmpty(RandomAccessListIterate.rejectWith(list, Predicates2.instanceOf(), Integer.class));
311
public void distinct()
313
MutableList<Integer> list = FastList.newListWith(5, 2, 6, 2, 3, 5, 2);
314
MutableList<Integer> actualList = FastList.newList();
315
RandomAccessListIterate.distinct(list, actualList);
316
Verify.assertListsEqual(FastList.newListWith(5, 2, 6, 3), actualList);
317
Verify.assertSize(7, list);
321
public void selectAndRejectWith()
323
MutableList<Integer> list = this.getIntegerList();
324
Twin<MutableList<Integer>> result = RandomAccessListIterate.selectAndRejectWith(list, Predicates2.in(), Lists.fixedSize.of(1));
325
Verify.assertSize(1, result.getOne());
326
Verify.assertSize(4, result.getTwo());
330
public void anySatisfyWith()
332
MutableList<Integer> list = this.getIntegerList();
333
Assert.assertTrue(RandomAccessListIterate.anySatisfyWith(list, Predicates2.instanceOf(), Integer.class));
334
Assert.assertFalse(RandomAccessListIterate.anySatisfyWith(list, Predicates2.instanceOf(), Double.class));
338
public void allSatisfyWith()
340
MutableList<Integer> list = this.getIntegerList();
341
Assert.assertTrue(RandomAccessListIterate.allSatisfyWith(list, Predicates2.instanceOf(), Integer.class));
342
Predicate2<Integer, Integer> greaterThanPredicate = Predicates2.greaterThan();
343
Assert.assertFalse(RandomAccessListIterate.allSatisfyWith(list, greaterThanPredicate, 2));
347
public void countWith()
349
Assert.assertEquals(5, RandomAccessListIterate.countWith(this.getIntegerList(), Predicates2.instanceOf(), Integer.class));
353
public void collectIf()
355
MutableList<Integer> integers = Lists.fixedSize.of(1, 2, 3);
356
Verify.assertContainsAll(RandomAccessListIterate.collectIf(integers, Integer.class::isInstance, String::valueOf), "1", "2", "3");
357
Verify.assertContainsAll(RandomAccessListIterate.collectIf(integers, Integer.class::isInstance, String::valueOf, new ArrayList<String>()), "1", "2", "3");
363
MutableList<Integer> integers = this.getIntegerList();
364
Collection<Integer> results = RandomAccessListIterate.take(integers, 2);
365
Assert.assertEquals(FastList.newListWith(5, 4), results);
367
Verify.assertSize(0, RandomAccessListIterate.take(integers, 0));
368
Verify.assertSize(5, RandomAccessListIterate.take(integers, 5));
369
Verify.assertSize(5, RandomAccessListIterate.take(integers, 10));
371
MutableList<Integer> integers2 = Lists.fixedSize.of();
372
Verify.assertSize(0, RandomAccessListIterate.take(integers2, 2));
374
Verify.assertSize(0, RandomAccessListIterate.take(Lists.fixedSize.of(), 2));
377
@Test(expected = IllegalArgumentException.class)
378
public void take_throws()
380
RandomAccessListIterate.take(this.getIntegerList(), -1);
386
MutableList<Integer> integers = this.getIntegerList();
387
MutableList<Integer> results = RandomAccessListIterate.drop(integers, 2);
388
Assert.assertEquals(FastList.newListWith(3, 2, 1), results);
390
Verify.assertSize(0, RandomAccessListIterate.drop(integers, 5));
391
Verify.assertSize(0, RandomAccessListIterate.drop(integers, 6));
392
Verify.assertSize(5, RandomAccessListIterate.drop(integers, 0));
394
MutableList<Integer> integers2 = Lists.fixedSize.of();
395
Verify.assertSize(0, RandomAccessListIterate.drop(integers2, 2));
397
Verify.assertSize(0, RandomAccessListIterate.drop(Lists.fixedSize.<Integer>of(), 2));
400
@Test(expected = IllegalArgumentException.class)
401
public void drop_throws()
403
RandomAccessListIterate.drop(this.getIntegerList(), -1);
406
private static class FailProcedure2 implements Procedure2<Object, Integer>
408
private static final long serialVersionUID = 1L;
410
public void value(Object argument1, Integer argument2)
417
public void classIsNonInstantiable()
419
Verify.assertClassNonInstantiable(RandomAccessListIterate.class);