From f428eafa2583edb57a99b770dbecdd0755c38900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sat, 6 Apr 2019 12:45:08 +0200 Subject: [PATCH] (#996) Make ListEnvelope public and consolidate API --- .../java/org/cactoos/list/ListEnvelope.java | 12 +- .../java/org/cactoos/list/ListIteratorOf.java | 2 +- src/main/java/org/cactoos/list/Mapped.java | 6 - .../org/cactoos/list/ListEnvelopeTest.java | 128 ++++++++++++------ .../java/org/cactoos/list/MappedTest.java | 2 +- 5 files changed, 97 insertions(+), 53 deletions(-) diff --git a/src/main/java/org/cactoos/list/ListEnvelope.java b/src/main/java/org/cactoos/list/ListEnvelope.java index fb43810393..e7f37679a1 100644 --- a/src/main/java/org/cactoos/list/ListEnvelope.java +++ b/src/main/java/org/cactoos/list/ListEnvelope.java @@ -31,7 +31,7 @@ import org.cactoos.scalar.Unchecked; /** - * List envelope. + * {@link List} envelope that doesn't allow mutations. * *

There is no thread-safety guarantee.

* @@ -45,7 +45,7 @@ "PMD.AbstractNaming" } ) -abstract class ListEnvelope extends CollectionEnvelope implements +public abstract class ListEnvelope extends CollectionEnvelope implements List { /** @@ -57,7 +57,7 @@ abstract class ListEnvelope extends CollectionEnvelope implements * Ctor. * @param src Source */ - ListEnvelope(final Scalar> src) { + public ListEnvelope(final Scalar> src) { super(src::value); this.list = new Unchecked<>(src); } @@ -114,11 +114,13 @@ public final ListIterator listIterator(final int index) { @Override public final List subList(final int start, final int end) { - return this.list.value().subList(start, end); + return new ListEnvelope( + () -> this.list.value().subList(start, end) + ) { }; } @Override - public String toString() { + public final String toString() { return this.list.value().toString(); } } diff --git a/src/main/java/org/cactoos/list/ListIteratorOf.java b/src/main/java/org/cactoos/list/ListIteratorOf.java index c7da8fa719..5e53bd64be 100644 --- a/src/main/java/org/cactoos/list/ListIteratorOf.java +++ b/src/main/java/org/cactoos/list/ListIteratorOf.java @@ -30,7 +30,7 @@ import org.cactoos.scalar.Unchecked; /** - * Iterator of the list. + * Iterator of the list that doesn't allow mutations. * *

There is no thread-safety guarantee. * diff --git a/src/main/java/org/cactoos/list/Mapped.java b/src/main/java/org/cactoos/list/Mapped.java index 6b610e397b..c1c8516845 100644 --- a/src/main/java/org/cactoos/list/Mapped.java +++ b/src/main/java/org/cactoos/list/Mapped.java @@ -24,7 +24,6 @@ package org.cactoos.list; import org.cactoos.Func; -import org.cactoos.text.TextOf; /** * Mapped list. @@ -47,9 +46,4 @@ public Mapped(final Func fnc, final Iterable src) { new org.cactoos.iterable.Mapped<>(fnc, src) )); } - - @Override - public String toString() { - return new TextOf(this).toString(); - } } diff --git a/src/test/java/org/cactoos/list/ListEnvelopeTest.java b/src/test/java/org/cactoos/list/ListEnvelopeTest.java index cb9415348e..44e0bb2a1c 100644 --- a/src/test/java/org/cactoos/list/ListEnvelopeTest.java +++ b/src/test/java/org/cactoos/list/ListEnvelopeTest.java @@ -23,33 +23,27 @@ */ package org.cactoos.list; +import java.util.Arrays; import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; import java.util.ListIterator; -import org.hamcrest.MatcherAssert; import org.hamcrest.core.IsEqual; import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; /** * Test case for {@link ListEnvelope}. * * @since 0.32 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle JavadocTypeCheck (500 lines) * @checkstyle MagicNumberCheck (500 lines) */ +@SuppressWarnings({ "PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals" }) public final class ListEnvelopeTest { @Test(expected = UnsupportedOperationException.class) public void returnsListIteratorWithUnsupportedRemove() { - final ListEnvelope list = new ListEnvelope( - () -> { - final List inner = new LinkedList<>(); - inner.add("one"); - return inner; - } - ) { - }; + final ListEnvelope list = new StringList("one"); final Iterator iterator = list.listIterator(); iterator.next(); iterator.remove(); @@ -57,76 +51,130 @@ public void returnsListIteratorWithUnsupportedRemove() { @Test(expected = UnsupportedOperationException.class) public void returnsListIteratorWithUnsupportedSet() { - final ListEnvelope list = new ListEnvelope( - () -> { - final List inner = new LinkedList<>(); - inner.add("three"); - return inner; - } - ) { - }; + final ListEnvelope list = new StringList("one"); final ListIterator iterator = list.listIterator(1); iterator.set("zero"); } @Test(expected = UnsupportedOperationException.class) public void returnsListIteratorWithUnsupportedAdd() { - final ListEnvelope list = new ListEnvelope( - () -> { - final List inner = new LinkedList<>(); - inner.add("ten"); - return inner; - } - ) { - }; + final ListEnvelope list = new StringList("one"); final ListIterator iterator = list.listIterator(); iterator.next(); - iterator.add("twenty"); + iterator.add("two"); + } + + @Test(expected = UnsupportedOperationException.class) + public void subListReturnsListIteratorWithUnsupportedRemove() { + final ListEnvelope list = new StringList("one"); + final Iterator iterator = list.subList(0, 1) + .listIterator(); + iterator.next(); + iterator.remove(); + } + + @Test(expected = UnsupportedOperationException.class) + public void subListReturnsListIteratorWithUnsupportedSet() { + final ListEnvelope list = new StringList("one"); + final ListIterator iterator = list.subList(0, 1) + .listIterator(1); + iterator.set("zero"); + } + + @Test(expected = UnsupportedOperationException.class) + public void subListReturnsListIteratorWithUnsupportedAdd() { + final ListEnvelope list = new StringList("one"); + final ListIterator iterator = list.subList(0, 1) + .listIterator(); + iterator.next(); + iterator.add("two"); + } + + @Test(expected = UnsupportedOperationException.class) + public void unsupportedRemove() { + final ListEnvelope list = new StringList("one"); + list.remove(0); + } + + @Test(expected = UnsupportedOperationException.class) + public void unsupportedSet() { + final ListEnvelope list = new StringList("one"); + list.set(0, "zero"); + } + + @Test(expected = UnsupportedOperationException.class) + public void unsupportedAdd() { + final ListEnvelope list = new StringList("one"); + list.add("two"); + } + + @Test(expected = UnsupportedOperationException.class) + public void returnsSubListWithUnsupportedRemove() { + final ListEnvelope list = new StringList("one"); + list.subList(0, 1).remove(0); + } + + @Test(expected = UnsupportedOperationException.class) + public void returnsSubListWithUnsupportedSet() { + final ListEnvelope list = new StringList("one"); + list.subList(0, 1).set(0, "zero"); + } + + @Test(expected = UnsupportedOperationException.class) + public void returnsSubListWithUnsupportedAdd() { + final ListEnvelope list = new StringList("one"); + list.subList(0, 1).add("two"); } @Test public void getsPreviousIndex() { - MatcherAssert.assertThat( + new Assertion<>( "List iterator returns incorrect previous index", new ListIteratorOf<>( new ListOf<>(1) - ).previousIndex(), + )::previousIndex, new IsEqual<>(-1) - ); + ).affirm(); } @Test public void getsPrevious() { - MatcherAssert.assertThat( + new Assertion<>( "List iterator returns incorrect previous item", new ListIteratorOf<>( new ListOf<>(3, 7), 1 - ).previous(), + )::previous, new IsEqual<>(3) - ); + ).affirm(); } @Test public void getsNextIndex() { - MatcherAssert.assertThat( + new Assertion<>( "List iterator returns incorrect next index", new ListIteratorOf<>( new ListOf<>(1) - ).nextIndex(), + )::nextIndex, new IsEqual<>(0) - ); + ).affirm(); } @Test public void getsNext() { - MatcherAssert.assertThat( + new Assertion<>( "List iterator returns incorrect next item", new ListIteratorOf<>( new ListOf<>(5, 11, 13), 1 - ).next(), + )::next, new IsEqual<>(11) - ); + ).affirm(); + } + + private static final class StringList extends ListEnvelope { + StringList(final String... elements) { + super(() -> Arrays.asList(elements)); + } } } diff --git a/src/test/java/org/cactoos/list/MappedTest.java b/src/test/java/org/cactoos/list/MappedTest.java index 89d7e27c5d..a54c5957f8 100644 --- a/src/test/java/org/cactoos/list/MappedTest.java +++ b/src/test/java/org/cactoos/list/MappedTest.java @@ -84,7 +84,7 @@ public void string() { x -> x * 2, new ListOf<>(1, 2, 3) ).toString(), - Matchers.equalTo("2, 4, 6") + Matchers.equalTo("[2, 4, 6]") ); } }