diff --git a/eclipse-collections-api/src/main/java/org/eclipse/collections/api/factory/list/MutableListFactory.java b/eclipse-collections-api/src/main/java/org/eclipse/collections/api/factory/list/MutableListFactory.java index 6dbac48a8f..b185439636 100644 --- a/eclipse-collections-api/src/main/java/org/eclipse/collections/api/factory/list/MutableListFactory.java +++ b/eclipse-collections-api/src/main/java/org/eclipse/collections/api/factory/list/MutableListFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Goldman Sachs and others. + * Copyright (c) 2020 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -46,8 +46,24 @@ default MutableList of(T... items) return this.with(items); } + /** + * Creates a new list using the passed {@code items} argument as the backing store. + *

+ * !!! WARNING: This method uses the passed in array, so can be very unsafe if the original + * array is held onto anywhere else. !!! + */ MutableList with(T... items); + /** + * Creates a new list by first copying the array passed in. + */ + default MutableList wrapCopy(T... array) + { + T[] newArray = (T[]) new Object[array.length]; + System.arraycopy(array, 0, newArray, 0, array.length); + return this.with(newArray); + } + /** * Same as {@link #empty()}. but takes in initial capacity. */ diff --git a/eclipse-collections-code-generator/src/main/resources/api/factory/list/mutablePrimitiveListFactory.stg b/eclipse-collections-code-generator/src/main/resources/api/factory/list/mutablePrimitiveListFactory.stg index 9fb01ea1c5..15b0a062ef 100644 --- a/eclipse-collections-code-generator/src/main/resources/api/factory/list/mutablePrimitiveListFactory.stg +++ b/eclipse-collections-code-generator/src/main/resources/api/factory/list/mutablePrimitiveListFactory.stg @@ -42,8 +42,24 @@ public interface MutableListFactory */ MutableList of(... items); + /** + * Creates a new list using the passed {@code items} argument as the backing store. + * \

+ * !!! WARNING: This method uses the passed in array, so can be very unsafe if the original + * array is held onto anywhere else. !!! + */ MutableList with(... items); + /** + * Creates a new list by first copying the array passed in. + */ + default MutableList wrapCopy(... array) + { + [] newArray = new [array.length]; + System.arraycopy(array, 0, newArray, 0, array.length); + return this.with(newArray); + } + /** * Same as {@link #withAll(Iterable)}. */ diff --git a/eclipse-collections-code-generator/src/main/resources/copyrightAndOthers.stg b/eclipse-collections-code-generator/src/main/resources/copyrightAndOthers.stg index 3ad90a6377..35eb84e536 100644 --- a/eclipse-collections-code-generator/src/main/resources/copyrightAndOthers.stg +++ b/eclipse-collections-code-generator/src/main/resources/copyrightAndOthers.stg @@ -1,6 +1,6 @@ copyrightAndOthers() ::= << /* - * Copyright (c) 2018 Goldman Sachs and others. + * Copyright (c) 2020 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. diff --git a/eclipse-collections-code-generator/src/main/resources/impl/list/mutable/mutablePrimitiveListFactoryImpl.stg b/eclipse-collections-code-generator/src/main/resources/impl/list/mutable/mutablePrimitiveListFactoryImpl.stg index fddffd0806..d352cb5b7e 100644 --- a/eclipse-collections-code-generator/src/main/resources/impl/list/mutable/mutablePrimitiveListFactoryImpl.stg +++ b/eclipse-collections-code-generator/src/main/resources/impl/list/mutable/mutablePrimitiveListFactoryImpl.stg @@ -54,6 +54,12 @@ public enum MutableListFactoryImpl implements MutableListFactory return this.with(items); } + /** + * Creates a new list using the passed {@code items} argument as the backing store. + * \

+ * !!! WARNING: This method uses the passed in array, so can be very unsafe if the original + * array is held onto anywhere else. !!! + */ @Override public MutableList with(... items) { diff --git a/eclipse-collections-code-generator/src/main/resources/impl/list/mutable/primitiveArrayList.stg b/eclipse-collections-code-generator/src/main/resources/impl/list/mutable/primitiveArrayList.stg index 20aba82e21..b12de43a86 100644 --- a/eclipse-collections-code-generator/src/main/resources/impl/list/mutable/primitiveArrayList.stg +++ b/eclipse-collections-code-generator/src/main/resources/impl/list/mutable/primitiveArrayList.stg @@ -114,6 +114,16 @@ public class ArrayList extends AbstractIterable return newList; } + /** + * Creates a new list by first copying the array passed in. + */ + public static ArrayList wrapCopy(... array) + { + [] newArray = new [array.length]; + System.arraycopy(array, 0, newArray, 0, array.length); + return new ArrayList(newArray); + } + @Override public int size() { diff --git a/eclipse-collections-code-generator/src/main/resources/test/factory/primitiveListsTest.stg b/eclipse-collections-code-generator/src/main/resources/test/factory/primitiveListsTest.stg index 290cb6e777..d8796f0f91 100644 --- a/eclipse-collections-code-generator/src/main/resources/test/factory/primitiveListsTest.stg +++ b/eclipse-collections-code-generator/src/main/resources/test/factory/primitiveListsTest.stg @@ -1,4 +1,4 @@ -import "copyright.stg" +import "copyrightAndOthers.stg" import "primitiveHashCode.stg" import "primitiveLiteral.stg" @@ -13,7 +13,7 @@ class(primitive) ::= << >> body(type, wrapperName, name) ::= << - + package org.eclipse.collections.impl.factory.primitive; @@ -138,6 +138,16 @@ public class ListsTest Assert.assertEquals(list, Lists.immutable.of(new []{<["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]:(literal.(type))(); separator=", ">})); } + @Test + public void wrapCopy() + { + [] array = {<["0", "1"]:(literal.(type))(); separator=", ">}; + MutableList actual = Lists.mutable.wrapCopy(array); + MutableList expected = ArrayList.newListWith(<["0", "1"]:(literal.(type))(); separator=", ">); + array[0] = <(literal.(type))("1")>; + Assert.assertEquals(expected, actual); + } + @Test public void newListWithList() { diff --git a/eclipse-collections-code-generator/src/main/resources/test/list/mutable/primitiveArrayListTest.stg b/eclipse-collections-code-generator/src/main/resources/test/list/mutable/primitiveArrayListTest.stg index b4b3fa9a04..18d2c28143 100644 --- a/eclipse-collections-code-generator/src/main/resources/test/list/mutable/primitiveArrayListTest.stg +++ b/eclipse-collections-code-generator/src/main/resources/test/list/mutable/primitiveArrayListTest.stg @@ -73,6 +73,15 @@ public class ArrayListTest extends AbstractListTestCase ArrayList newList = ArrayList.newWithNValues(-5, <(literal.(type))("42")>); } + @Test + public void wrapCopy() + { + [] array = {<(literal.(type))("0")>, <(literal.(type))("1")>}; + ArrayList list = ArrayList.wrapCopy(array); + array[0] = <(literal.(type))("1")>; + Assert.assertTrue(list.get(0) \< <(literal.(type))("1")>); + } + @Test public void addAtIndexAtCapacity() throws Exception { diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/mutable/MutableListFactoryImpl.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/mutable/MutableListFactoryImpl.java index 4e20ae8752..61695deafd 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/mutable/MutableListFactoryImpl.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/mutable/MutableListFactoryImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Goldman Sachs and others. + * Copyright (c) 2020 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -27,6 +27,12 @@ public MutableList empty() return FastList.newList(); } + /** + * Creates a new list using the passed {@code items} argument as the backing store. + *

+ * !!! WARNING: This method uses the passed in array, so can be very unsafe if the original + * array is held onto anywhere else. !!! + */ @Override public MutableList with(T... items) { diff --git a/unit-tests/src/test/java/org/eclipse/collections/impl/factory/ListsTest.java b/unit-tests/src/test/java/org/eclipse/collections/impl/factory/ListsTest.java index a19b64d2c0..35947f2f08 100644 --- a/unit-tests/src/test/java/org/eclipse/collections/impl/factory/ListsTest.java +++ b/unit-tests/src/test/java/org/eclipse/collections/impl/factory/ListsTest.java @@ -105,6 +105,16 @@ public void mutables() Verify.assertInstanceOf(MutableList.class, listFactory.fromStream(Stream.of(1, 2, 3))); } + @Test + public void wrapCopy() + { + Integer[] integers = {1, 2, 3, 4}; + MutableList actual = Lists.mutable.wrapCopy(integers); + MutableList expected = Lists.mutable.with(1, 2, 3, 4); + integers[0] = Integer.valueOf(4); + Assert.assertEquals(expected, actual); + } + @Test public void immutableWithListTest() { diff --git a/unit-tests/src/test/java/org/eclipse/collections/impl/list/mutable/FastListTest.java b/unit-tests/src/test/java/org/eclipse/collections/impl/list/mutable/FastListTest.java index 96b0906c92..add85c51d5 100644 --- a/unit-tests/src/test/java/org/eclipse/collections/impl/list/mutable/FastListTest.java +++ b/unit-tests/src/test/java/org/eclipse/collections/impl/list/mutable/FastListTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Goldman Sachs. + * Copyright (c) 2020 Goldman Sachs and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompany this distribution. @@ -116,9 +116,13 @@ public void testAddWithZeroBasedConstructor() } @Test - public void testWrapCopy() + public void wrapCopy() { - Assert.assertEquals(this.newWith(1, 2, 3, 4), FastList.wrapCopy(1, 2, 3, 4)); + Integer[] integers = {1, 2, 3, 4}; + FastList actual = FastList.wrapCopy(integers); + FastList expected = this.newWith(1, 2, 3, 4); + integers[0] = Integer.valueOf(4); + Assert.assertEquals(expected, actual); } @Override