From cbe468cae8456061780a34871d810409d07af326 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sat, 7 Oct 2017 10:01:38 +0200 Subject: [PATCH] add toArray(int[]) --- .../java/org/lucares/collections/IntList.java | 14 ++++- .../org/lucares/collections/IntListTest.java | 53 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java index 1d6d9d3..feb113d 100644 --- a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java +++ b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java @@ -20,12 +20,13 @@ public final class IntList implements Serializable, Cloneable { // TODO support sublists // TODO add retainAll // TODO add removeAll + // TODO add retainAll for sorted lists + // TODO add removeAll for sorted lists // TODO add removeIf // TODO add removeRange // TODO add replace // TODO add lastIndexOf // TODO add indexOf - // TODO add toArray(int[] a) // TODO remove bounds checks that are handled by java, e.g. negative indices // TODO toString @@ -152,6 +153,7 @@ public final class IntList implements Serializable, Cloneable { ensureCapacity(values.length); + // TODO do not copy the array twice final int[] newData = new int[data.length]; System.arraycopy(data, 0, newData, 0, pos); System.arraycopy(values, 0, newData, pos, values.length); @@ -282,6 +284,15 @@ public final class IntList implements Serializable, Cloneable { return get(0, index); } + public int[] toArray(final int[] input) { + + if (input.length < index) { + return toArray(); + } + System.arraycopy(data, 0, input, 0, index); + return input; + } + /** * Sorts the list into ascending order. */ @@ -451,4 +462,5 @@ public final class IntList implements Serializable, Cloneable { throw new IllegalStateException(e); } } + } diff --git a/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java index 6bb2359..5be1a76 100644 --- a/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java +++ b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java @@ -285,6 +285,59 @@ public class IntListTest { Assert.assertArrayEquals(actual, new int[0]); } + @Test + public void testToArray() { + final IntList list = new IntList(); + list.addAll(1, 2, 3, 4, 5, 6); + + { + final int[] input = new int[1]; + final int[] actual = list.toArray(input); + // input is too short -> new array returned + Assert.assertNotSame(input, actual); + Assert.assertArrayEquals(list.toArray(), actual); + } + + { + final int[] input = new int[list.size()]; + final int[] actual = list.toArray(input); + // input fits exactly -> input returned + Assert.assertSame(input, actual); + Assert.assertArrayEquals(list.toArray(), actual); + } + + { + final int[] input = new int[list.size() + 1]; + final int[] expected = { 1, 2, 3, 4, 5, 6, 0 }; + final int[] actual = list.toArray(input); + // input too big -> input returned + Assert.assertSame(input, actual); + Assert.assertArrayEquals(expected, actual); + } + } + + @Test + public void testToArrayWithEmptyList() { + final IntList list = new IntList(); + + { + final int[] input = new int[0]; + final int[] actual = list.toArray(input); + // input fits exactly -> input returned + Assert.assertSame(input, actual); + Assert.assertArrayEquals(list.toArray(), actual); + } + + { + final int[] input = new int[list.size() + 1]; + final int[] expected = { 0 }; + final int[] actual = list.toArray(input); + // input too big -> input returned + Assert.assertSame(input, actual); + Assert.assertArrayEquals(expected, actual); + } + } + @Test public void testRemove() { final IntList list = new IntList();