Skip to content

Commit

Permalink
Add method wrapCopy to primitive lists to mirror functionality in Fas…
Browse files Browse the repository at this point in the history
…tList. Fixes #742.

Signed-off-by: Donald Raab <Donald.Raab@bnymellon.com>
  • Loading branch information
donraab committed Apr 2, 2020
1 parent b6a7eba commit 57fb520
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -46,8 +46,24 @@ default <T> MutableList<T> of(T... items)
return this.with(items);
}

/**
* Creates a new list using the passed {@code items} argument as the backing store.
* <p>
* !!! WARNING: This method uses the passed in array, so can be very unsafe if the original
* array is held onto anywhere else. !!!
*/
<T> MutableList<T> with(T... items);

/**
* Creates a new list by first copying the array passed in.
*/
default <T> MutableList<T> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,24 @@ public interface Mutable<name>ListFactory
*/
Mutable<name>List of(<type>... items);

/**
* Creates a new list using the passed {@code items} argument as the backing store.
* \<p>
* !!! WARNING: This method uses the passed in array, so can be very unsafe if the original
* array is held onto anywhere else. !!!
*/
Mutable<name>List with(<type>... items);

/**
* Creates a new list by first copying the array passed in.
*/
default Mutable<name>List wrapCopy(<type>... array)
{
<type>[] newArray = new <type>[array.length];
System.arraycopy(array, 0, newArray, 0, array.length);
return this.with(newArray);
}

/**
* Same as {@link #withAll(<name>Iterable)}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public enum Mutable<name>ListFactoryImpl implements Mutable<name>ListFactory
return this.with(items);
}

/**
* Creates a new list using the passed {@code items} argument as the backing store.
* \<p>
* !!! WARNING: This method uses the passed in array, so can be very unsafe if the original
* array is held onto anywhere else. !!!
*/
@Override
public Mutable<name>List with(<type>... items)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ public class <name>ArrayList extends Abstract<name>Iterable
return newList;
}

/**
* Creates a new list by first copying the array passed in.
*/
public static <name>ArrayList wrapCopy(<type>... array)
{
<type>[] newArray = new <type>[array.length];
System.arraycopy(array, 0, newArray, 0, array.length);
return new <name>ArrayList(newArray);
}

@Override
public int size()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "copyright.stg"
import "copyrightAndOthers.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

Expand All @@ -13,7 +13,7 @@ class(primitive) ::= <<
>>

body(type, wrapperName, name) ::= <<
<copyright()>
<copyrightAndOthers()>

package org.eclipse.collections.impl.factory.primitive;

Expand Down Expand Up @@ -138,6 +138,16 @@ public class <name>ListsTest
Assert.assertEquals(list, <name>Lists.immutable.of(new <type>[]{<["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]:(literal.(type))(); separator=", ">}));
}

@Test
public void wrapCopy()
{
<type>[] array = {<["0", "1"]:(literal.(type))(); separator=", ">};
Mutable<name>List actual = <name>Lists.mutable.wrapCopy(array);
Mutable<name>List expected = <name>ArrayList.newListWith(<["0", "1"]:(literal.(type))(); separator=", ">);
array[0] = <(literal.(type))("1")>;
Assert.assertEquals(expected, actual);
}

@Test
public void newListWithList()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public class <name>ArrayListTest extends Abstract<name>ListTestCase
<name>ArrayList newList = <name>ArrayList.newWithNValues(-5, <(literal.(type))("42")>);
}

@Test
public void wrapCopy()
{
<type>[] array = {<(literal.(type))("0")>, <(literal.(type))("1")>};
<name>ArrayList list = <name>ArrayList.wrapCopy(array);
array[0] = <(literal.(type))("1")>;
Assert.assertTrue(list.get(0) \< <(literal.(type))("1")>);
}

@Test
public void addAtIndexAtCapacity() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -27,6 +27,12 @@ public <T> MutableList<T> empty()
return FastList.newList();
}

/**
* Creates a new list using the passed {@code items} argument as the backing store.
* <p>
* !!! WARNING: This method uses the passed in array, so can be very unsafe if the original
* array is held onto anywhere else. !!!
*/
@Override
public <T> MutableList<T> with(T... items)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> actual = Lists.mutable.wrapCopy(integers);
MutableList<Integer> expected = Lists.mutable.with(1, 2, 3, 4);
integers[0] = Integer.valueOf(4);
Assert.assertEquals(expected, actual);
}

@Test
public void immutableWithListTest()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Integer> actual = FastList.wrapCopy(integers);
FastList<Integer> expected = this.newWith(1, 2, 3, 4);
integers[0] = Integer.valueOf(4);
Assert.assertEquals(expected, actual);
}

@Override
Expand Down

0 comments on commit 57fb520

Please sign in to comment.