diff --git a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java index bafd368..d2d7674 100644 --- a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java +++ b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java @@ -3,6 +3,9 @@ package org.lucares.collections; import java.io.Serializable; import java.util.Arrays; import java.util.List; +import java.util.Spliterator.OfInt; +import java.util.stream.IntStream; +import java.util.stream.StreamSupport; /** * A list for primitive ints. @@ -15,7 +18,6 @@ public class IntList implements Serializable, Cloneable { // TODO support Iterator // TODO add mod counts // TODO support sublists - // TODO support Stream-API // TODO add retainAll // TODO add removeAll // TODO add removeIf @@ -25,6 +27,7 @@ public class IntList implements Serializable, Cloneable { // TODO add indexOf // TODO add toArray(int[] a) // TODO remove bounds checks that are handled by java, e.g. negative indices + // TODO toString private static final long serialVersionUID = -6823520157007564746L; @@ -318,6 +321,46 @@ public class IntList implements Serializable, Cloneable { } } + /** + * Returns a sequential {@link IntStream} with this collection as its source. + * + * @return a sequential {@link IntStream} + */ + public IntStream stream() { + + return Arrays.stream(data, 0, index); + } + + /** + * Returns a parallel {@link IntStream} with this collection as its source. + * + * @return a parallel {@link IntStream} + */ + public IntStream parallelStream() { + final OfInt spliterator = Arrays.spliterator(data, 0, index); + return StreamSupport.intStream(spliterator, true); + } + + @Override + public String toString() { + + if (data == null) + return "null"; + final int iMax = data.length - 1; + if (iMax == -1) + return "[]"; + + final StringBuilder result = new StringBuilder(); + result.append('['); + for (int i = 0; i < index; i++) { + if (i > 0) { + result.append(", "); + } + result.append(data[i]); + } + return result.append(']').toString(); + } + @Override public int hashCode() { if (index == 0) { diff --git a/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java index d703bff..6bb2359 100644 --- a/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java +++ b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java @@ -7,8 +7,10 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; +import java.util.stream.IntStream; import org.junit.Assert; import org.junit.Test; @@ -474,4 +476,58 @@ public class IntListTest { list.set(1, 0); Assert.assertNotEquals(list, clone); } + + @Test + public void testToString() { + Assert.assertEquals("[]", new IntList().toString()); + + final IntList list = new IntList(); + list.addAll(-2, -1, 0, 1, 2); + Assert.assertEquals("[-2, -1, 0, 1, 2]", list.toString()); + Assert.assertEquals("same result as Arrays.toString()", Arrays.toString(list.toArray()), list.toString()); + } + + @Test + public void testSequentialStream() { + + { + final IntList list = new IntList(); + list.addAll(0, 1, 2, 3, 4, 5, 6); + final IntStream stream = list.stream(); + Assert.assertEquals(list.size(), stream.count()); + } + { + final IntList list = new IntList(); + list.addAll(0, 1, 2, 3, 4, 5); + final IntStream stream = list.stream(); + Assert.assertEquals(15, stream.sum()); + } + { + final IntList emptyList = new IntList(); + Assert.assertEquals(0, emptyList.stream().count()); + } + } + + @Test + public void testParallelStream() { + final IntList list = new IntList(); + + final int size = 1000; + final int[] ints = new int[size]; + for (int i = 0; i < size; i++) { + ints[i] = i; + } + + list.addAll(ints); + + final ConcurrentLinkedQueue processingOrder = new ConcurrentLinkedQueue<>(); + final List actualList = list.parallelStream()// + .peek(e -> processingOrder.add(e))// + .boxed()// + .collect(Collectors.toList()); + + Assert.assertEquals("should be sequential, when using collect", actualList.toString(), list.toString()); + Assert.assertNotEquals("should use parallelism during computation", processingOrder.toString(), + list.toString()); + } }