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;
19
import java.io.IOException;
20
import java.util.ArrayList;
21
import java.util.Collections;
22
import java.util.List;
24
import com.gs.collections.api.RichIterable;
25
import com.gs.collections.api.block.function.Function;
26
import com.gs.collections.api.list.MutableList;
27
import com.gs.collections.api.list.primitive.MutableBooleanList;
28
import com.gs.collections.api.map.MutableMap;
29
import com.gs.collections.api.multimap.Multimap;
30
import com.gs.collections.api.multimap.MutableMultimap;
31
import com.gs.collections.api.partition.PartitionIterable;
32
import com.gs.collections.api.tuple.Pair;
33
import com.gs.collections.api.tuple.Twin;
34
import com.gs.collections.impl.block.factory.Functions;
35
import com.gs.collections.impl.block.factory.Functions2;
36
import com.gs.collections.impl.block.factory.IntegerPredicates;
37
import com.gs.collections.impl.block.factory.Predicates;
38
import com.gs.collections.impl.block.factory.Predicates2;
39
import com.gs.collections.impl.block.factory.PrimitiveFunctions;
40
import com.gs.collections.impl.block.function.AddFunction;
41
import com.gs.collections.impl.block.function.MaxSizeFunction;
42
import com.gs.collections.impl.block.function.MinSizeFunction;
43
import com.gs.collections.impl.block.function.NegativeIntervalFunction;
44
import com.gs.collections.impl.block.procedure.MapPutProcedure;
45
import com.gs.collections.impl.factory.Lists;
46
import com.gs.collections.impl.list.Interval;
47
import com.gs.collections.impl.list.mutable.FastList;
48
import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList;
49
import com.gs.collections.impl.list.mutable.primitive.ByteArrayList;
50
import com.gs.collections.impl.list.mutable.primitive.CharArrayList;
51
import com.gs.collections.impl.list.mutable.primitive.DoubleArrayList;
52
import com.gs.collections.impl.list.mutable.primitive.FloatArrayList;
53
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
54
import com.gs.collections.impl.list.mutable.primitive.LongArrayList;
55
import com.gs.collections.impl.list.mutable.primitive.ShortArrayList;
56
import com.gs.collections.impl.map.mutable.UnifiedMap;
57
import com.gs.collections.impl.multimap.list.FastListMultimap;
58
import com.gs.collections.impl.test.Verify;
59
import org.junit.Assert;
60
import org.junit.Test;
62
import static com.gs.collections.impl.factory.Iterables.*;
64
public class ArrayIterateTest
66
private static final Integer[] INTEGER_ARRAY = {5, 4, 3, 2, 1};
69
public void injectInto()
71
Integer[] objectArray = this.threeIntegerArray2();
74
ArrayIterate.injectInto(1, objectArray, AddFunction.INTEGER));
77
private Integer[] threeIntegerArray2()
79
return new Integer[]{1, 2, 3};
83
public void injectIntoDouble()
85
Double[] objectArray = {(double) 1, (double) 2, (double) 3};
87
new Double(1 + 1 + 2 + 3),
88
ArrayIterate.injectInto((double) 1, objectArray, AddFunction.DOUBLE));
92
public void injectIntoPrimitives()
94
double doubleActual = ArrayIterate.injectInto(1.0d, new Double[]{1.0d, 2.0d, 3.0d}, (doubleParameter, objectParameter) -> doubleParameter + objectParameter);
95
Assert.assertEquals(7.0, doubleActual, 0.000001);
96
long longActual = ArrayIterate.injectInto(1L, new Long[]{1L, 2L, 3L}, (long longParameter, Long objectParameter) -> longParameter + objectParameter);
97
Assert.assertEquals(7L, longActual);
98
int intActual = ArrayIterate.injectInto(1, new Integer[]{1, 2, 3}, (int intParameter, Integer objectParameter) -> intParameter + objectParameter);
99
Assert.assertEquals(7, intActual);
103
public void injectIntoThrowsOnNullArgument()
105
Verify.assertThrows(IllegalArgumentException.class, () -> {
106
ArrayIterate.injectInto(0, null, (int intParameter, Object objectParameter) -> 0);
108
Verify.assertThrows(IllegalArgumentException.class, () -> {
109
ArrayIterate.injectInto(0L, null, (long longParameter, Object objectParameter) -> 0);
111
Verify.assertThrows(IllegalArgumentException.class, () -> {
112
ArrayIterate.injectInto((double) 0, null, (doubleParameter, objectParameter) -> 0.0);
114
Verify.assertThrows(IllegalArgumentException.class, () -> {
115
ArrayIterate.injectInto(null, null, null);
119
@Test(expected = IllegalArgumentException.class)
120
public void allSatisfyThrowsOnNullArgument()
122
ArrayIterate.allSatisfy(null, null);
125
@Test(expected = IllegalArgumentException.class)
126
public void allSatisfyWithThrowsOnNullArgument()
128
ArrayIterate.allSatisfyWith(null, null, null);
131
@Test(expected = IllegalArgumentException.class)
132
public void anySatisfyThrowsOnNullArgument()
134
ArrayIterate.anySatisfy(null, null);
137
@Test(expected = IllegalArgumentException.class)
138
public void anySatisfyWithThrowsOnNullArgument()
140
ArrayIterate.anySatisfyWith(null, null, null);
143
@Test(expected = IllegalArgumentException.class)
144
public void noneSatisfyThrowsOnNullArgument()
146
ArrayIterate.noneSatisfy(null, null);
149
@Test(expected = IllegalArgumentException.class)
150
public void noneSatisfyWithThrowsOnNullArgument()
152
ArrayIterate.noneSatisfyWith(null, null, null);
155
@Test(expected = IllegalArgumentException.class)
156
public void selectThrowsOnNullArgument()
158
ArrayIterate.select(null, null);
161
@Test(expected = IllegalArgumentException.class)
162
public void selectWithTargetThrowsOnNullArgument()
164
ArrayIterate.select(null, null, null);
167
@Test(expected = IllegalArgumentException.class)
168
public void selectWithThrowsOnNullArgument()
170
ArrayIterate.selectWith(null, null, null);
173
@Test(expected = IllegalArgumentException.class)
174
public void selectWithWithTargetThrowsOnNullArgument()
176
ArrayIterate.selectWith(null, null, null, null);
179
@Test(expected = IllegalArgumentException.class)
180
public void rejectThrowsOnNullArgument()
182
ArrayIterate.reject(null, null);
185
@Test(expected = IllegalArgumentException.class)
186
public void rejectWithTargetThrowsOnNullArgument()
188
ArrayIterate.reject(null, null, null);
191
@Test(expected = IllegalArgumentException.class)
192
public void rejectWithThrowsOnNullArgument()
194
ArrayIterate.rejectWith(null, null, null);
197
@Test(expected = IllegalArgumentException.class)
198
public void rejectWithWithTargetThrowsOnNullArgument()
200
ArrayIterate.rejectWith(null, null, null, null);
203
@Test(expected = IllegalArgumentException.class)
204
public void collectThrowsOnNullArgument()
206
ArrayIterate.collect(null, null);
209
@Test(expected = IllegalArgumentException.class)
210
public void collectIfThrowsOnNullArgument()
212
ArrayIterate.collectIf(null, null, null);
215
@Test(expected = IllegalArgumentException.class)
216
public void collectWithTargetThrowsOnNullArgument()
218
ArrayIterate.collect(null, null, null);
221
@Test(expected = IllegalArgumentException.class)
222
public void collectIfWithTargetThrowsOnNullArgument()
224
ArrayIterate.collectIf(null, null, null, null);
227
@Test(expected = IllegalArgumentException.class)
228
public void collectWithThrowsOnNullArgument()
230
ArrayIterate.collectWith(null, null, null);
233
@Test(expected = IllegalArgumentException.class)
234
public void collectWithWithTargetThrowsOnNullArgument()
236
ArrayIterate.collectWith(null, null, null, null);
239
@Test(expected = IllegalArgumentException.class)
240
public void flatCollectThrowsOnNullArgument()
242
ArrayIterate.flatCollect(null, null);
245
@Test(expected = IllegalArgumentException.class)
246
public void flatCollectWithTargetThrowsOnNullArgument()
248
ArrayIterate.flatCollect(null, null, null);
251
@Test(expected = IllegalArgumentException.class)
252
public void forEachThrowsOnNullArgument()
254
ArrayIterate.forEach(null, null);
257
@Test(expected = IllegalArgumentException.class)
258
public void forEachWithFromToThrowsOnNullArgument()
260
ArrayIterate.forEach(null, 0, 0, null);
263
@Test(expected = IllegalArgumentException.class)
264
public void forEachWithIndexThrowsOnNullArgument()
266
ArrayIterate.forEachWithIndex(null, null);
269
@Test(expected = IllegalArgumentException.class)
270
public void forEachWithIndexWithFromToThrowsOnNullArgument()
272
ArrayIterate.forEachWithIndex(null, 0, 0, null);
276
public void partition()
278
PartitionIterable<Integer> result =
279
ArrayIterate.partition(new Integer[]{1, 2, 3, 4, 5}, Predicates.greaterThan(3));
280
Assert.assertEquals(iBag(4, 5), result.getSelected().toBag());
281
Assert.assertEquals(iBag(1, 2, 3), result.getRejected().toBag());
285
public void injectIntoString()
287
String[] objectArray = {"1", "2", "3"};
288
Assert.assertEquals("0123", ArrayIterate.injectInto("0", objectArray, AddFunction.STRING));
293
public void injectIntoMaxString()
295
String[] objectArray = this.threeStringArray();
296
Assert.assertEquals(Integer.valueOf(3),
297
ArrayIterate.injectInto(Integer.MIN_VALUE, objectArray, MaxSizeFunction.STRING));
300
private String[] threeStringArray()
302
return new String[]{"1", "12", "123"};
307
public void injectIntoMinString()
309
String[] objectArray = this.threeStringArray();
312
ArrayIterate.injectInto(Integer.MAX_VALUE, objectArray, MinSizeFunction.STRING));
316
public void collect()
318
Boolean[] objectArray = {true, false, null};
320
iList("true", "false", "null"),
321
ArrayIterate.collect(objectArray, String::valueOf));
325
public void collectBoolean()
327
Integer[] objectArray = {-1, 0, 42};
328
Assert.assertEquals(this.getExpectedBooleanResults(),
329
ArrayIterate.collectBoolean(objectArray, PrimitiveFunctions.integerIsPositive()));
333
public void collectBooleanWithTarget()
335
Integer[] objectArray = {-1, 0, 42};
336
BooleanArrayList target = new BooleanArrayList();
337
MutableBooleanList result = ArrayIterate.collectBoolean(objectArray, PrimitiveFunctions.integerIsPositive(), target);
338
Assert.assertEquals(this.getExpectedBooleanResults(), result);
339
Assert.assertSame("Target List not returned as result", target, result);
342
private BooleanArrayList getExpectedBooleanResults()
344
return BooleanArrayList.newListWith(false, false, true);
348
public void collectByte()
350
Integer[] objectArray = {-1, 0, 42};
351
Assert.assertEquals(this.getExpectedByteResults(),
352
ArrayIterate.collectByte(objectArray, PrimitiveFunctions.unboxIntegerToByte()));
356
public void collectByteWithTarget()
358
Integer[] objectArray = {-1, 0, 42};
359
ByteArrayList target = new ByteArrayList();
360
ByteArrayList result = ArrayIterate.collectByte(objectArray, PrimitiveFunctions.unboxIntegerToByte(), target);
361
Assert.assertEquals(this.getExpectedByteResults(), result);
362
Assert.assertSame("Target List not returned as result", target, result);
365
private ByteArrayList getExpectedByteResults()
367
return ByteArrayList.newListWith((byte) -1, (byte) 0, (byte) 42);
371
public void collectChar()
373
Integer[] objectArray = {-1, 0, 42};
374
Assert.assertEquals(this.getExpectedCharResults(),
375
ArrayIterate.collectChar(objectArray, PrimitiveFunctions.unboxIntegerToChar()));
379
public void collectCharWithTarget()
381
Integer[] objectArray = {-1, 0, 42};
382
CharArrayList target = new CharArrayList();
383
CharArrayList result = ArrayIterate.collectChar(objectArray, PrimitiveFunctions.unboxIntegerToChar(), target);
384
Assert.assertEquals(this.getExpectedCharResults(), result);
385
Assert.assertSame("Target List not returned as result", target, result);
388
private CharArrayList getExpectedCharResults()
390
return CharArrayList.newListWith((char) -1, (char) 0, (char) 42);
394
public void collectDouble()
396
Integer[] objectArray = {-1, 0, 42};
397
Assert.assertEquals(this.getExpectedDoubleResults(),
398
ArrayIterate.collectDouble(objectArray, PrimitiveFunctions.unboxIntegerToDouble()));
402
public void collectDoubleWithTarget()
404
Integer[] objectArray = {-1, 0, 42};
405
DoubleArrayList target = new DoubleArrayList();
406
DoubleArrayList result = ArrayIterate.collectDouble(objectArray, PrimitiveFunctions.unboxIntegerToDouble(), target);
407
Assert.assertEquals(this.getExpectedDoubleResults(), result);
408
Assert.assertSame("Target List not returned as result", target, result);
411
private DoubleArrayList getExpectedDoubleResults()
413
return DoubleArrayList.newListWith(-1.0d, 0.0d, 42.0d);
417
public void collectFloat()
419
Integer[] objectArray = {-1, 0, 42};
420
Assert.assertEquals(this.getExpectedFloatResults(),
421
ArrayIterate.collectFloat(objectArray, PrimitiveFunctions.unboxIntegerToFloat()));
425
public void collectFloatWithTarget()
427
Integer[] objectArray = {-1, 0, 42};
428
FloatArrayList target = new FloatArrayList();
429
FloatArrayList result = ArrayIterate.collectFloat(objectArray, PrimitiveFunctions.unboxIntegerToFloat(), target);
430
Assert.assertEquals(this.getExpectedFloatResults(), result);
431
Assert.assertSame("Target List not returned as result", target, result);
434
private FloatArrayList getExpectedFloatResults()
436
return FloatArrayList.newListWith(-1.0f, 0.0f, 42.0f);
440
public void collectInt()
442
Integer[] objectArray = {-1, 0, 42};
443
Assert.assertEquals(this.getExpectedIntResults(),
444
ArrayIterate.collectInt(objectArray, PrimitiveFunctions.unboxIntegerToInt()));
448
public void collectIntWithTarget()
450
Integer[] objectArray = {-1, 0, 42};
451
IntArrayList target = new IntArrayList();
452
IntArrayList result = ArrayIterate.collectInt(objectArray, PrimitiveFunctions.unboxIntegerToInt(), target);
453
Assert.assertEquals(this.getExpectedIntResults(), result);
454
Assert.assertSame("Target List not returned as result", target, result);
457
private IntArrayList getExpectedIntResults()
459
return IntArrayList.newListWith(-1, 0, 42);
463
public void collectLong()
465
Integer[] objectArray = {-1, 0, 42};
466
Assert.assertEquals(this.getExpectedLongResults(),
467
ArrayIterate.collectLong(objectArray, PrimitiveFunctions.unboxIntegerToLong()));
471
public void collectLongWithTarget()
473
Integer[] objectArray = {-1, 0, 42};
474
LongArrayList target = new LongArrayList();
475
LongArrayList result = ArrayIterate.collectLong(objectArray, PrimitiveFunctions.unboxIntegerToLong(), target);
476
Assert.assertEquals(this.getExpectedLongResults(), result);
477
Assert.assertSame("Target List not returned as result", target, result);
480
private LongArrayList getExpectedLongResults()
482
return LongArrayList.newListWith(-1L, 0L, 42L);
486
public void collectShort()
488
Integer[] objectArray = {-1, 0, 42};
489
Assert.assertEquals(this.getExpectedShortResults(),
490
ArrayIterate.collectShort(objectArray, PrimitiveFunctions.unboxIntegerToShort()));
494
public void collectShortWithTarget()
496
Integer[] objectArray = {-1, 0, 42};
497
ShortArrayList target = new ShortArrayList();
498
ShortArrayList result = ArrayIterate.collectShort(objectArray, PrimitiveFunctions.unboxIntegerToShort(), target);
499
Assert.assertEquals(this.getExpectedShortResults(), result);
500
Assert.assertSame("Target List not returned as result", target, result);
503
private ShortArrayList getExpectedShortResults()
505
return ShortArrayList.newListWith((short) -1, (short) 0, (short) 42);
509
public void collectWith()
511
Boolean[] objectArray = {true, false, null};
513
iList("true", "false", "null"),
514
ArrayIterate.collectWith(objectArray, Functions2.fromFunction(String::valueOf), null));
518
public void addAllTo()
520
MutableList<Integer> result = Lists.mutable.of();
521
ArrayIterate.addAllTo(new Integer[]{1, 2, 3}, result);
522
Assert.assertEquals(FastList.newListWith(1, 2, 3), result);
526
public void getFirstAndLast()
528
List<Boolean> list = new ArrayList<Boolean>();
529
list.add(Boolean.TRUE);
531
list.add(Boolean.FALSE);
532
Object[] objectArray = list.toArray();
533
Assert.assertEquals(Boolean.TRUE, ArrayIterate.getFirst(objectArray));
534
Assert.assertEquals(Boolean.FALSE, ArrayIterate.getLast(objectArray));
538
public void getFirstAndLastOnEmpty()
540
Object[] objectArray = {};
541
Assert.assertNull(ArrayIterate.getFirst(objectArray));
542
Assert.assertNull(ArrayIterate.getLast(objectArray));
549
FastList.newListWith(5, 4, 3, 2, 1),
550
ArrayIterate.select(INTEGER_ARRAY, Integer.class::isInstance));
557
FastList.newListWith(5, 4, 3, 2, 1),
558
ArrayIterate.reject(INTEGER_ARRAY, String.class::isInstance));
562
public void distinct()
564
List<Integer> result = FastList.newList();
565
ArrayIterate.distinct(new Integer[]{5, 3, 1, 5, 7, 1}, result);
566
Assert.assertEquals(FastList.newListWith(5, 3, 1, 7), result);
568
ArrayIterate.distinct(INTEGER_ARRAY, result);
569
Assert.assertEquals(FastList.newListWith(INTEGER_ARRAY), result);
573
public void selectWith()
576
FastList.newListWith(5, 4, 3, 2, 1),
577
ArrayIterate.selectWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
581
public void selectInstancesOf()
585
ArrayIterate.selectInstancesOf(new Number[]{1, 2.0, 3, 4.0, 5}, Integer.class));
589
public void countOnNullOrEmptyArray()
591
Assert.assertEquals(0, ArrayIterate.count(null, ignored1 -> true));
592
Assert.assertEquals(0, ArrayIterate.count(new Object[]{}, ignored -> true));
598
Assert.assertEquals(3, ArrayIterate.count(INTEGER_ARRAY, Predicates.lessThan(4)));
602
public void countWith()
604
Assert.assertEquals(5, ArrayIterate.countWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
605
Assert.assertEquals(0, ArrayIterate.countWith(new Integer[]{}, Predicates2.instanceOf(), Integer.class));
606
Assert.assertEquals(1, ArrayIterate.countWith(new Object[]{"test", null, Integer.valueOf(2)}, Predicates2.instanceOf(), Integer.class));
610
public void selectAndRejectWith()
612
Twin<MutableList<Integer>> result =
613
ArrayIterate.selectAndRejectWith(INTEGER_ARRAY, Predicates2.<Integer>lessThan(), 3);
614
MutableList<Integer> positive = result.getOne();
615
MutableList<Integer> negative = result.getTwo();
616
Verify.assertSize(2, positive);
617
Verify.assertContains(2, positive);
618
Verify.assertNotContains(3, positive);
619
Verify.assertSize(3, negative);
620
Verify.assertNotContains(2, negative);
621
Verify.assertContains(3, negative);
625
public void selectWithDifferentTargetCollection()
628
FastList.newListWith(5, 4, 3, 2, 1),
629
ArrayIterate.select(INTEGER_ARRAY, Integer.class::isInstance, new ArrayList<Integer>()));
633
public void rejectDifferentTargetCollection()
635
Verify.assertEmpty(ArrayIterate.reject(INTEGER_ARRAY, Integer.class::isInstance, new ArrayList<Integer>()));
639
public void rejectWith()
641
Verify.assertEmpty(ArrayIterate.rejectWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
642
Verify.assertEmpty(ArrayIterate.rejectWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class, new ArrayList<Integer>()));
646
public void collectIf()
648
Object[] integers = Lists.fixedSize.of(1, 2, 3).toArray();
649
Verify.assertContainsAll(ArrayIterate.collectIf(integers, Integer.class::isInstance, String::valueOf), "1", "2", "3");
650
Verify.assertContainsAll(ArrayIterate.collectIf(integers, Integer.class::isInstance, String::valueOf, FastList.<String>newList()), "1", "2", "3");
656
MutableMap<String, Integer> map = ArrayIterate.toMap(INTEGER_ARRAY, String::valueOf);
657
Verify.assertSize(5, map);
658
Verify.assertContainsKeyValue("1", 1, map);
659
Verify.assertContainsKeyValue("2", 2, map);
660
Verify.assertContainsKeyValue("3", 3, map);
661
Verify.assertContainsKeyValue("4", 4, map);
662
Verify.assertContainsKeyValue("5", 5, map);
668
MutableMap<String, Integer> map = ArrayIterate.toMap(INTEGER_ARRAY, String::valueOf, Functions.squaredInteger());
669
Verify.assertSize(5, map);
670
Verify.assertContainsKeyValue("1", 1, map);
671
Verify.assertContainsKeyValue("2", 4, map);
672
Verify.assertContainsKeyValue("3", 9, map);
673
Verify.assertContainsKeyValue("4", 16, map);
674
Verify.assertContainsKeyValue("5", 25, map);
678
public void contains()
680
Assert.assertTrue(ArrayIterate.contains(INTEGER_ARRAY, 5));
681
Assert.assertFalse(ArrayIterate.contains(INTEGER_ARRAY, 6));
682
Assert.assertFalse(ArrayIterate.contains(INTEGER_ARRAY, null));
683
Assert.assertTrue(ArrayIterate.contains(new Object[]{null}, null));
687
public void containsInt()
689
int[] array = {1, 2, 3, 4, 5};
690
Assert.assertTrue(ArrayIterate.contains(array, 5));
691
Assert.assertFalse(ArrayIterate.contains(array, 6));
695
public void containsLong()
697
long[] array = {1, 2, 3, 4, 5};
698
Assert.assertTrue(ArrayIterate.contains(array, 5));
699
Assert.assertFalse(ArrayIterate.contains(array, 6));
703
public void containsDouble()
705
double[] array = {1, 2, 3, 4, 5};
706
Assert.assertTrue(ArrayIterate.contains(array, 5));
707
Assert.assertFalse(ArrayIterate.contains(array, 6));
711
public void anySatisfy()
713
Assert.assertTrue(ArrayIterate.anySatisfy(INTEGER_ARRAY, Integer.class::isInstance));
714
Assert.assertFalse(ArrayIterate.anySatisfy(INTEGER_ARRAY, Predicates.isNull()));
718
public void anySatisfyWith()
720
Assert.assertTrue(ArrayIterate.anySatisfyWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
724
public void allSatisfy()
726
Assert.assertTrue(ArrayIterate.allSatisfy(INTEGER_ARRAY, Integer.class::isInstance));
727
Assert.assertFalse(ArrayIterate.allSatisfy(INTEGER_ARRAY, Predicates.isNull()));
731
public void allSatisfyWith()
733
Assert.assertTrue(ArrayIterate.allSatisfyWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
737
public void noneSatisfy()
739
Assert.assertTrue(ArrayIterate.noneSatisfy(INTEGER_ARRAY, String.class::isInstance));
740
Assert.assertFalse(ArrayIterate.noneSatisfy(INTEGER_ARRAY, Predicates.notNull()));
744
public void noneSatisfyWith()
746
Assert.assertTrue(ArrayIterate.noneSatisfyWith(INTEGER_ARRAY, Predicates2.instanceOf(), String.class));
750
public void isEmpty()
752
Assert.assertFalse(ArrayIterate.isEmpty(INTEGER_ARRAY));
753
Assert.assertTrue(ArrayIterate.isEmpty(new Object[]{}));
754
Assert.assertTrue(ArrayIterate.isEmpty(null));
758
public void notEmpty()
760
Assert.assertTrue(ArrayIterate.notEmpty(new Integer[]{5, 4, 3, 2, 1}));
761
Assert.assertFalse(ArrayIterate.notEmpty(new Object[]{}));
762
Assert.assertFalse(ArrayIterate.notEmpty(null));
768
Assert.assertEquals(5, ArrayIterate.size(new Integer[]{5, 4, 3, 2, 1}));
769
Assert.assertEquals(0, ArrayIterate.size(null));
775
Assert.assertEquals(Integer.valueOf(1), new Integer[]{5, 4, 3, 2, 1}[4]);
779
public void forEachInBoth()
781
MutableMap<String, String> map = UnifiedMap.newMap();
782
ArrayIterate.forEachInBoth(
783
new String[]{"1", "2", "3"},
784
new String[]{"a", "b", "c"},
785
new MapPutProcedure<String, String>(map));
787
UnifiedMap.newWithKeysValues(
792
ArrayIterate.forEachInBoth(null, null, (argument1, argument2) -> Assert.fail());
795
@Test(expected = RuntimeException.class)
796
public void forEachInBothThrowsOnDifferentLengthArrays()
798
ArrayIterate.forEachInBoth(new Integer[]{1, 2, 3}, new Integer[]{1, 2}, (argument1, argument2) -> Assert.fail());
802
public void forEachWithIndex()
804
Integer[] objectArray = {1, 2, 3, 4};
805
ArrayIterate.forEachWithIndex(objectArray, (i, index) -> Assert.assertEquals(index, i - 1));
808
private Integer[] createIntegerArray(int size)
810
Integer[] array = new Integer[size];
811
for (int i = 0; i < size; i++)
821
Integer[] array = this.createIntegerArray(1);
822
Assert.assertEquals(Integer.valueOf(1), ArrayIterate.detect(array, integer -> integer == 1));
826
public void detectWith()
828
Integer[] array = this.createIntegerArray(1);
831
ArrayIterate.detectWith(array, Predicates2.<Integer>lessThan(), 2));
833
ArrayIterate.detectWith(new Integer[0], Predicates2.<Integer>lessThan(), 2));
837
public void detectIfNone()
839
Integer[] array = this.createIntegerArray(1);
840
Assert.assertEquals(Integer.valueOf(7), ArrayIterate.detectIfNone(array, Integer.valueOf(2)::equals, 7));
841
Assert.assertEquals(Integer.valueOf(1), ArrayIterate.detectIfNone(array, Integer.valueOf(1)::equals, 7));
845
public void detectWithIfNone()
847
Integer[] array = this.createIntegerArray(1);
848
Assert.assertEquals(Integer.valueOf(7),
849
ArrayIterate.detectWithIfNone(array, Object::equals, 2, 7));
853
public void indexOf()
855
String[] array = {"1", "2", "3", null};
856
Assert.assertEquals(0, ArrayIterate.indexOf(array, "1"));
857
Assert.assertEquals(1, ArrayIterate.indexOf(array, "2"));
858
Assert.assertEquals(2, ArrayIterate.indexOf(array, "3"));
859
Assert.assertEquals(3, ArrayIterate.indexOf(array, null));
860
Assert.assertEquals(-1, ArrayIterate.indexOf(array, "4"));
864
public void indexOfPredicates()
866
String[] array = {"1", "2", "3", null};
867
Assert.assertEquals(0, ArrayIterate.detectIndex(array, String.class::isInstance));
868
Assert.assertEquals(3, ArrayIterate.detectIndex(array, Predicates.isNull()));
869
Assert.assertEquals(0, ArrayIterate.detectIndexWith(array, Predicates2.instanceOf(), String.class));
875
Assert.assertEquals(ListIterate.take(Interval.zeroTo(0), 5), ArrayIterate.take(Interval.zeroTo(0).toArray(), 5));
876
Assert.assertEquals(ListIterate.take(Interval.zeroTo(5), 5), ArrayIterate.take(Interval.zeroTo(5).toArray(), 5));
877
Assert.assertEquals(ListIterate.take(Interval.zeroTo(10), 5), ArrayIterate.take(Interval.zeroTo(10).toArray(), 5));
880
@Test(expected = IllegalArgumentException.class)
881
public void take_negative_throws()
883
ArrayIterate.take(Interval.zeroTo(0).toArray(), -1);
889
Assert.assertEquals(ListIterate.drop(Interval.zeroTo(0).toList(), 5), ArrayIterate.drop(Interval.zeroTo(0).toList().toArray(), 5));
890
Assert.assertEquals(ListIterate.drop(Interval.zeroTo(5), 5), ArrayIterate.drop(Interval.zeroTo(5).toArray(), 5));
891
Assert.assertEquals(ListIterate.drop(Interval.zeroTo(10), 5), ArrayIterate.drop(Interval.zeroTo(10).toArray(), 5));
894
@Test(expected = IllegalArgumentException.class)
895
public void drop_negative_throws()
897
ArrayIterate.drop(Interval.zeroTo(0).toArray(), -1);
901
public void groupBy()
903
Integer[] array = {1, 2, 3, 4, 5, 6, 7};
904
Function<Integer, Boolean> isOddFunction = object -> IntegerPredicates.isOdd().accept(object);
906
MutableMap<Boolean, RichIterable<Integer>> expected =
907
UnifiedMap.<Boolean, RichIterable<Integer>>newWithKeysValues(
908
Boolean.TRUE, FastList.newListWith(1, 3, 5, 7),
909
Boolean.FALSE, FastList.newListWith(2, 4, 6));
911
Multimap<Boolean, Integer> multimap = ArrayIterate.groupBy(array, isOddFunction);
912
Assert.assertEquals(expected, multimap.toMap());
916
public void groupByEach()
918
MutableMultimap<Integer, Integer> expected = FastListMultimap.newMultimap();
919
for (int i = 1; i < 8; i++)
921
expected.putAll(-i, Interval.fromTo(i, 7));
924
Multimap<Integer, Integer> actual = ArrayIterate.groupByEach(new Integer[]{1, 2, 3, 4, 5, 6, 7}, new NegativeIntervalFunction());
925
Assert.assertEquals(expected, actual);
931
String[] array = {"1", "2", "3", "4", "5", "6", "7"};
932
Object[] nulls = Collections.nCopies(array.length, null).toArray();
933
Object[] nullsPlusOne = Collections.nCopies(array.length + 1, null).toArray();
934
Object[] nullsMinusOne = Collections.nCopies(array.length - 1, null).toArray();
936
MutableList<Pair<String, Object>> pairs = ArrayIterate.zip(array, nulls);
938
FastList.newListWith(array),
939
pairs.collect((Function<Pair<String, ?>, String>) Pair::getOne));
941
FastList.newListWith(nulls),
942
pairs.collect((Function<Pair<?, Object>, Object>) Pair::getTwo, Lists.mutable.of()));
944
MutableList<Pair<String, Object>> pairsPlusOne = ArrayIterate.zip(array, nullsPlusOne);
946
FastList.newListWith(array),
947
pairsPlusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne));
948
Assert.assertEquals(FastList.newListWith(nulls), pairsPlusOne.collect((Function<Pair<?, Object>, Object>) Pair::getTwo, Lists.mutable.of()));
950
MutableList<Pair<String, Object>> pairsMinusOne = ArrayIterate.zip(array, nullsMinusOne);
951
Assert.assertEquals(array.length - 1, pairsMinusOne.size());
952
Assert.assertTrue(FastList.newListWith(array).containsAll(pairsMinusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne)));
955
ArrayIterate.zip(array, nulls),
956
ArrayIterate.zip(array, nulls, FastList.<Pair<String, Object>>newList()));
960
public void zipWithIndex()
962
String[] array = {"1", "2", "3", "4", "5", "6", "7"};
963
MutableList<Pair<String, Integer>> pairs = ArrayIterate.zipWithIndex(array);
966
FastList.newListWith(array),
967
pairs.collect((Function<Pair<String, ?>, String>) Pair::getOne));
969
Interval.zeroTo(array.length - 1).toList(),
970
pairs.collect((Function<Pair<?, Integer>, Integer>) Pair::getTwo, FastList.<Integer>newList()));
973
ArrayIterate.zipWithIndex(array),
974
ArrayIterate.zipWithIndex(array, FastList.<Pair<String, Integer>>newList()));
980
String[] array = {"1", "2", "3", "4", "5", "6", "7"};
981
RichIterable<RichIterable<String>> groups = ArrayIterate.chunk(array, 2);
982
RichIterable<Integer> sizes = groups.collect(RichIterable::size);
983
Assert.assertEquals(FastList.newListWith(2, 2, 2, 1), sizes);
986
@Test(expected = IllegalArgumentException.class)
987
public void chunk_zero_throws()
989
String[] array = {"1", "2", "3", "4", "5", "6", "7"};
990
ArrayIterate.chunk(array, 0);
994
public void chunk_large_size()
996
String[] array = {"1", "2", "3", "4", "5", "6", "7"};
997
Assert.assertEquals(FastList.newListWith(array), ArrayIterate.chunk(array, 10).getFirst());
1001
public void makeString()
1003
String[] array = {"1", "2", "3", "4", "5"};
1004
Assert.assertEquals("1, 2, 3, 4, 5", ArrayIterate.makeString(array));
1008
public void appendString()
1010
String[] array = {"1", "2", "3", "4", "5"};
1011
StringBuilder stringBuilder = new StringBuilder();
1012
ArrayIterate.appendString(array, stringBuilder);
1013
Assert.assertEquals("1, 2, 3, 4, 5", stringBuilder.toString());
1016
@Test(expected = RuntimeException.class)
1017
public void appendStringThrowsIOException()
1019
ArrayIterate.appendString(new String[]{"1", "2", "3"}, new Appendable()
1021
public Appendable append(CharSequence csq) throws IOException
1023
throw new IOException();
1026
public Appendable append(CharSequence csq, int start, int end) throws IOException
1028
throw new IOException();
1031
public Appendable append(char c) throws IOException
1033
throw new IOException();
1039
public void classIsNonInstantiable()
1041
Verify.assertClassNonInstantiable(ArrayIterate.class);