Skip to content

Commit

Permalink
Merge pull request #911 from vmzakharov/master
Browse files Browse the repository at this point in the history
Implement swap() method on mutable primitive lists
  • Loading branch information
nikhilnanivadekar committed Jun 11, 2020
2 parents 03126bd + ce127db commit c2f479b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public interface Mutable<name>List extends Mutable<name>Collection, <name>List

<type> set(int index, <type> element);

default void swap(int index1, int index2)
{
<type> value = this.get(index1);
this.set(index1, this.get(index2));
this.set(index2, value);
}

<sharedAPI(fileName(primitive), name)>

<mutableAPI(fileName(primitive), type, name)>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ public class <name>ArrayList extends Abstract<name>Iterable
return previous;
}

@Override
public void swap(int index1, int index2)
{
<type> value = this.get(index1);
this.items[index1] = this.items[index2];
this.items[index2] = value;
}

@Override
public <name>ArrayList with(<type> element)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,16 @@ public abstract class Abstract<name>ListTestCase extends AbstractMutable<name>Co
Assert.assertEquals(this.newMutableCollectionWith(<["1", "4", "3"]:(literal.(type))(); separator=", ">), list);
}

@Test
public void swap()
{
Mutable<name>List list = this.classUnderTest();
list.swap(1, 2);
Assert.assertEquals(this.newMutableCollectionWith(<["1", "3", "2"]:(literal.(type))(); separator=", ">), list);
list.swap(1, 1);
Assert.assertEquals(this.newMutableCollectionWith(<["1", "3", "2"]:(literal.(type))(); separator=", ">), list);
}

@Test(expected = UnsupportedOperationException.class)
public void subList()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ public class Unmodifiable<name>ListTest extends Abstract<name>ListTestCase
this.list.set(1, <(literal.(type))("4")>);
}

@Override
@Test(expected = UnsupportedOperationException.class)
public void swap()
{
this.list.swap(0, 1);
}

@Override
@Test(expected = UnsupportedOperationException.class)
public void clear()
Expand Down

0 comments on commit c2f479b

Please sign in to comment.