diff --git a/src/main/java/org/cactoos/list/ListIteratorOf.java b/src/main/java/org/cactoos/list/ListIteratorOf.java index 84903abc86..67dedff4cc 100644 --- a/src/main/java/org/cactoos/list/ListIteratorOf.java +++ b/src/main/java/org/cactoos/list/ListIteratorOf.java @@ -30,15 +30,13 @@ import org.cactoos.scalar.Unchecked; /** - * Iterator of the list that doesn't allow mutations. + * List Iterator of the list that allows mutations if the original + * list iterator does support. * *

There is no thread-safety guarantee. * * @param Items type * @since 0.35 - * @todo #1219:30min {@link ListIteratorOf} does not have to be immutable. - * Make it possible to use mutable operations like remove/set/add in this - * {@link ListIteratorOf} and change the class javadoc as well. */ public final class ListIteratorOf implements ListIterator { @@ -112,22 +110,16 @@ public int previousIndex() { @Override public void remove() { - throw new UnsupportedOperationException( - "Iterator is read-only and doesn't allow removing items" - ); + this.origin.value().remove(); } @Override public void set(final T item) { - throw new UnsupportedOperationException( - "Iterator is read-only and doesn't allow rewriting items" - ); + this.origin.value().set(item); } @Override public void add(final T item) { - throw new UnsupportedOperationException( - "Iterator is read-only and doesn't allow adding items" - ); + this.origin.value().add(item); } } diff --git a/src/main/java/org/cactoos/list/package-info.java b/src/main/java/org/cactoos/list/package-info.java index f202c9c9ff..9e9f5c7fa8 100644 --- a/src/main/java/org/cactoos/list/package-info.java +++ b/src/main/java/org/cactoos/list/package-info.java @@ -26,5 +26,11 @@ * Lists, tests. * * @since 0.14 + * @todo #1230:30min The following list implmenetation classes + * {@link org.cactoos.list.Joined}, {@link org.cactoos.list.Mapped}, + * {@link org.cactoos.list.NoNulls}, {@link org.cactoos.list.Shuffled}, + * {@link org.cactoos.list.Solid}, {@link org.cactoos.list.Solid}, + * {@link org.cactoos.list.Sorted}, {@link org.cactoos.list.Sticky}, + * {@link org.cactoos.list.Synced} should support mutability. */ package org.cactoos.list; diff --git a/src/test/java/org/cactoos/list/ListEnvelopeTest.java b/src/test/java/org/cactoos/list/ListEnvelopeTest.java index bc780e80b2..37355d455e 100644 --- a/src/test/java/org/cactoos/list/ListEnvelopeTest.java +++ b/src/test/java/org/cactoos/list/ListEnvelopeTest.java @@ -45,7 +45,7 @@ * That's because this test should check the original behavior of ListEnvelope * Now this test checks behavior of the Immutable decorator */ -@SuppressWarnings({ "PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals" }) +@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"}) public final class ListEnvelopeTest { @Test(expected = UnsupportedOperationException.class) @@ -159,9 +159,9 @@ public void returnsSubListWithUnsupportedAdd() { } @Test - public void getsPreviousIndex() { + public void mustReturnPreviousIndex() { new Assertion<>( - "List iterator returns incorrect previous index", + "List Iterator must return previous index", new ListIteratorOf<>( new ListOf<>(1) ).previousIndex(), @@ -170,9 +170,9 @@ public void getsPreviousIndex() { } @Test - public void getsPrevious() { + public void mustReturnPreviousElement() { new Assertion<>( - "List iterator returns incorrect previous item", + "List Iterator must return previous element", new ListIteratorOf<>( new ListOf<>(3, 7), 1 @@ -182,9 +182,9 @@ public void getsPrevious() { } @Test - public void getsNextIndex() { + public void mustReturnNextIndex() { new Assertion<>( - "List iterator returns incorrect next index", + "List iterator must return next index", new ListIteratorOf<>( new ListOf<>(1) ).nextIndex(), @@ -193,9 +193,9 @@ public void getsNextIndex() { } @Test - public void getsNext() { + public void mustReturnNextElement() { new Assertion<>( - "List iterator returns incorrect next item", + "List iterator must return next item", new ListIteratorOf<>( new ListOf<>(5, 11, 13), 1 diff --git a/src/test/java/org/cactoos/list/ListIteratorNoNullsTest.java b/src/test/java/org/cactoos/list/ListIteratorNoNullsTest.java index cb0a47822e..3f91deeef8 100644 --- a/src/test/java/org/cactoos/list/ListIteratorNoNullsTest.java +++ b/src/test/java/org/cactoos/list/ListIteratorNoNullsTest.java @@ -23,9 +23,13 @@ */ package org.cactoos.list; +import java.util.ArrayList; +import java.util.List; import java.util.ListIterator; +import java.util.NoSuchElementException; import org.junit.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.ScalarHasValue; import org.llorllale.cactoos.matchers.Throws; /** @@ -38,12 +42,15 @@ public final class ListIteratorNoNullsTest { @Test - public void getThrowsErrorIfListIteratorNextValueIsNullValue() { + public void mustThrowsErrorIfListIteratorNextValueIsNull() { new Assertion<>( - "must throw error if removed value is null", - () -> new ListIteratorNoNulls<>( - new ListOf<>(null, 2, 3).listIterator() - ).next(), + "must throw error next item is null", + () -> { + new ListIteratorNoNulls<>( + new ListOf<>(null, 2, 3).listIterator() + ).next(); + return 0; + }, new Throws<>( "Next item is NULL", IllegalStateException.class @@ -52,13 +59,17 @@ public void getThrowsErrorIfListIteratorNextValueIsNullValue() { } @Test - public void getThrowsErrorIfListIteratorPreviousValueIsNullValue() { - final ListIterator listiterator = - new ListOf<>(null, 2, 3).listIterator(); - listiterator.next(); + public void mustThrowsErrorIfListIteratorPreviousValueIsNull() { new Assertion<>( "must throw error if previous value is null", - () -> new ListIteratorNoNulls<>(listiterator).previous(), + () -> { + new ListIteratorNoNulls<>( + new ListOf<>( + null, 2, 3 + ).listIterator(1) + ).previous(); + return 0; + }, new Throws<>( "Previous item is NULL", IllegalStateException.class @@ -67,53 +78,61 @@ public void getThrowsErrorIfListIteratorPreviousValueIsNullValue() { } @Test - public void addThrowsErrorForImmutableListIterator() { + public void mustAddToListIterator() { new Assertion<>( - "must throw error if modified with add", + "must add to list iterator", () -> { - new ListIteratorNoNulls<>( - new ListOf<>(1, 2, 3).listIterator() - ).add(4); - return 0; + final List list = new ArrayList<>(2); + list.add(1); + list.add(2); + final ListIterator iterator = new ListIteratorNoNulls<>( + list.listIterator() + ); + iterator.next(); + iterator.add(4); + return iterator.previous(); }, - new Throws<>( - "Iterator is read-only and doesn't allow adding items", - UnsupportedOperationException.class - ) + new ScalarHasValue<>(4) ).affirm(); } @Test - public void removeThrowsErrorForImmutableListIterator() { + public void mustRemoveFromListIterator() { new Assertion<>( - "must throw error if modified with remove", + "must remove element from list iterator", () -> { - new ListIteratorNoNulls<>( - new ListOf<>(1, 2, 3).listIterator() - ).remove(); - return 0; + final List list = new ArrayList<>(2); + list.add(1); + list.add(2); + final ListIterator iterator = new ListIteratorNoNulls<>( + list.listIterator() + ); + iterator.next(); + iterator.remove(); + return iterator.previous(); }, new Throws<>( - "Iterator is read-only and doesn't allow removing items", - UnsupportedOperationException.class + NoSuchElementException.class ) ).affirm(); } @Test - public void setThrowsErrorForImmutableListIterator() { + public void mustSetValueListIterator() { new Assertion<>( - "must throw error if modified with set", + "must set element into list iterator", () -> { - new ListIteratorNoNulls<>( - new ListOf<>(1, 2, 3).listIterator() - ).set(4); - return 0; + final List list = new ArrayList<>(2); + list.add(1); + list.add(2); + final ListIterator iterator = new ListIteratorNoNulls<>( + list.listIterator() + ); + iterator.next(); + iterator.set(4); + return iterator.previous(); }, - new Throws<>( - "Iterator is read-only and doesn't allow rewriting items", - UnsupportedOperationException.class - ) + new ScalarHasValue<>(4) ).affirm(); } } diff --git a/src/test/java/org/cactoos/list/NoNullsTest.java b/src/test/java/org/cactoos/list/NoNullsTest.java index cb0545af7b..5673a01b9f 100644 --- a/src/test/java/org/cactoos/list/NoNullsTest.java +++ b/src/test/java/org/cactoos/list/NoNullsTest.java @@ -152,7 +152,6 @@ public void addThrowsErrorForImmutableListIterator() { return 0; }, new Throws<>( - "Iterator is read-only and doesn't allow adding items", UnsupportedOperationException.class ) ).affirm(); @@ -167,7 +166,6 @@ public void removeThrowsErrorForImmutableListIterator() { return 0; }, new Throws<>( - "Iterator is read-only and doesn't allow removing items", UnsupportedOperationException.class ) ).affirm(); @@ -182,7 +180,6 @@ public void setThrowsErrorForImmutableListIterator() { return 0; }, new Throws<>( - "Iterator is read-only and doesn't allow rewriting items", UnsupportedOperationException.class ) ).affirm();