From aed81292e07338fe553a333ccd34b44f08480dfd Mon Sep 17 00:00:00 2001 From: Fevzi Anifieiev Date: Tue, 5 Nov 2019 17:04:07 -0500 Subject: [PATCH 1/5] (#1219) Introduced immutable list iterator for immutable list --- src/main/java/org/cactoos/list/Immutable.java | 12 +- ...atorOf.java => ImmutableListIterator.java} | 12 +- .../org/cactoos/list/ListIteratorNoNulls.java | 2 +- .../list/ImmutableListIteratorTest.java | 110 ++++++++++++++++++ .../org/cactoos/list/ListEnvelopeTest.java | 57 ++------- 5 files changed, 134 insertions(+), 59 deletions(-) rename src/main/java/org/cactoos/list/{ListIteratorOf.java => ImmutableListIterator.java} (89%) create mode 100644 src/test/java/org/cactoos/list/ImmutableListIteratorTest.java diff --git a/src/main/java/org/cactoos/list/Immutable.java b/src/main/java/org/cactoos/list/Immutable.java index d46d68de4e..36f2888d17 100644 --- a/src/main/java/org/cactoos/list/Immutable.java +++ b/src/main/java/org/cactoos/list/Immutable.java @@ -36,8 +36,8 @@ * @param Element type * @since 1.16 * @todo #898:30min Introduce an Immutable wrapper for {@link ListIterator} - * and use it in listIterator() methods instead of {@link ListIteratorOf}. - * One another option is renaming of {@link ListIteratorOf}. + * and use it in listIterator() methods instead of {@link ImmutableListIterator}. + * One another option is renaming of {@link ImmutableListIterator}. * @todo #898:30min Replace all the Collections.unmodifiableList * with the {@link org.cactoos.list.Immutable} from the cactoos codebase. * That should be done because Elegant Object principles are against static methods. @@ -187,12 +187,16 @@ public int lastIndexOf(final Object item) { @Override public ListIterator listIterator() { - return this.list.listIterator(); + return new ImmutableListIterator( + this.list.listIterator() + ); } @Override public ListIterator listIterator(final int index) { - return this.list.listIterator(index); + return new ImmutableListIterator( + this.list.listIterator(index) + ); } @Override diff --git a/src/main/java/org/cactoos/list/ListIteratorOf.java b/src/main/java/org/cactoos/list/ImmutableListIterator.java similarity index 89% rename from src/main/java/org/cactoos/list/ListIteratorOf.java rename to src/main/java/org/cactoos/list/ImmutableListIterator.java index cbac3cfdc1..37716483d9 100644 --- a/src/main/java/org/cactoos/list/ListIteratorOf.java +++ b/src/main/java/org/cactoos/list/ImmutableListIterator.java @@ -30,14 +30,14 @@ import org.cactoos.scalar.Unchecked; /** - * Iterator of the list that doesn't allow mutations. + * List Iterator that doesn't allow mutations. * *

There is no thread-safety guarantee. * * @param Items type * @since 0.35 */ -public final class ListIteratorOf implements ListIterator { +public final class ImmutableListIterator implements ListIterator { /** * Original list iterator. @@ -48,7 +48,7 @@ public final class ListIteratorOf implements ListIterator { * Ctor. * @param list List that will be called to get a list iterator. */ - public ListIteratorOf(final List list) { + public ImmutableListIterator(final List list) { this(list::listIterator); } @@ -57,7 +57,7 @@ public ListIteratorOf(final List list) { * @param list List that will be called to get a list iterator. * @param index Start index for a newly created list iterator. */ - public ListIteratorOf(final List list, final int index) { + public ImmutableListIterator(final List list, final int index) { this(() -> list.listIterator(index)); } @@ -65,7 +65,7 @@ public ListIteratorOf(final List list, final int index) { * Ctor. * @param iter Original list iterator. */ - public ListIteratorOf(final ListIterator iter) { + public ImmutableListIterator(final ListIterator iter) { this(() -> iter); } @@ -73,7 +73,7 @@ public ListIteratorOf(final ListIterator iter) { * Ctor. * @param orig Original list iterator. */ - public ListIteratorOf(final Scalar> orig) { + public ImmutableListIterator(final Scalar> orig) { this.origin = new Unchecked<>(new Sticky<>(orig)); } diff --git a/src/main/java/org/cactoos/list/ListIteratorNoNulls.java b/src/main/java/org/cactoos/list/ListIteratorNoNulls.java index 12e77740e3..4c7ac4f601 100644 --- a/src/main/java/org/cactoos/list/ListIteratorNoNulls.java +++ b/src/main/java/org/cactoos/list/ListIteratorNoNulls.java @@ -47,7 +47,7 @@ public final class ListIteratorNoNulls implements ListIterator { * @param src List iterator. */ public ListIteratorNoNulls(final ListIterator src) { - this.listiterator = new ListIteratorOf<>(src); + this.listiterator = new ImmutableListIterator<>(src); } @Override diff --git a/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java b/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java new file mode 100644 index 0000000000..d230488c92 --- /dev/null +++ b/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java @@ -0,0 +1,110 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2019 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.list; + +import java.util.ListIterator; +import org.hamcrest.core.IsEqual; +import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; + +/** + * Test case for {@link ImmutableListIterator}. + * @since 0.32 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle JavadocTypeCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"}) +public final class ImmutableListIteratorTest { + + @Test + public void getsPreviousIndex() { + new Assertion<>( + "List iterator returns incorrect previous index", + new ImmutableListIterator<>( + new ListOf<>(1) + ).previousIndex(), + new IsEqual<>(-1) + ).affirm(); + } + + @Test + public void getsPrevious() { + new Assertion<>( + "List iterator returns incorrect previous item", + new ImmutableListIterator<>( + new ListOf<>(3, 7), + 1 + ).previous(), + new IsEqual<>(3) + ).affirm(); + } + + @Test + public void getsNextIndex() { + new Assertion<>( + "List iterator returns incorrect next index", + new ImmutableListIterator<>( + new ListOf<>(1) + ).nextIndex(), + new IsEqual<>(0) + ).affirm(); + } + + @Test + public void getsNext() { + new Assertion<>( + "List iterator returns incorrect next item", + new ImmutableListIterator<>( + new ListOf<>(5, 11, 13), + 1 + ).next(), + new IsEqual<>(11) + ).affirm(); + } + + @Test(expected = UnsupportedOperationException.class) + public void doesNotSupportRemove() { + final ListIterator iterator = new ImmutableListIterator<>( + new ListOf<>(1, 2) + ); + iterator.remove(); + } + + @Test(expected = UnsupportedOperationException.class) + public void doesNotSupportAdd() { + final ListIterator iterator = new ImmutableListIterator<>( + new ListOf<>(1, 2) + ); + iterator.add(1); + } + + @Test(expected = UnsupportedOperationException.class) + public void doesNotSupportSet() { + final ListIterator iterator = new ImmutableListIterator<>( + new ListOf<>(1, 2) + ); + iterator.set(1); + } +} diff --git a/src/test/java/org/cactoos/list/ListEnvelopeTest.java b/src/test/java/org/cactoos/list/ListEnvelopeTest.java index e6301e496b..702a79c6a0 100644 --- a/src/test/java/org/cactoos/list/ListEnvelopeTest.java +++ b/src/test/java/org/cactoos/list/ListEnvelopeTest.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import org.hamcrest.MatcherAssert; @@ -44,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) @@ -57,7 +58,7 @@ public void returnsListIteratorWithUnsupportedRemove() { @Test() public void returnsListIteratorWithSupportedSet() { - final ListEnvelope list = new StringList("one", "two"); + final ListEnvelope list = new MutableStringList("one", "two"); final ListIterator iterator = list.listIterator(1); iterator.next(); iterator.set("zero"); @@ -157,55 +158,15 @@ public void returnsSubListWithUnsupportedAdd() { list.subList(0, 1).add("two"); } - @Test - public void getsPreviousIndex() { - new Assertion<>( - "List iterator returns incorrect previous index", - new ListIteratorOf<>( - new ListOf<>(1) - ).previousIndex(), - new IsEqual<>(-1) - ).affirm(); - } - - @Test - public void getsPrevious() { - new Assertion<>( - "List iterator returns incorrect previous item", - new ListIteratorOf<>( - new ListOf<>(3, 7), - 1 - ).previous(), - new IsEqual<>(3) - ).affirm(); - } - - @Test - public void getsNextIndex() { - new Assertion<>( - "List iterator returns incorrect next index", - new ListIteratorOf<>( - new ListOf<>(1) - ).nextIndex(), - new IsEqual<>(0) - ).affirm(); - } - - @Test - public void getsNext() { - new Assertion<>( - "List iterator returns incorrect next item", - new ListIteratorOf<>( - new ListOf<>(5, 11, 13), - 1 - ).next(), - new IsEqual<>(11) - ).affirm(); - } - private static final class StringList extends ListEnvelope { StringList(final String... elements) { super(() -> new Immutable<>(Arrays.asList(elements))); } } + + private static final class MutableStringList extends ListEnvelope { + MutableStringList(final String... elements) { + super(() -> new LinkedList<>(Arrays.asList(elements))); + } + } } From ac3184a488e527311bd93071e66c967955634879 Mon Sep 17 00:00:00 2001 From: Fevzi Anifieiev Date: Tue, 5 Nov 2019 17:45:28 -0500 Subject: [PATCH 2/5] (#1219) Removed todo description as the changes have been done --- src/main/java/org/cactoos/list/Immutable.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/cactoos/list/Immutable.java b/src/main/java/org/cactoos/list/Immutable.java index 36f2888d17..0e14bd4f67 100644 --- a/src/main/java/org/cactoos/list/Immutable.java +++ b/src/main/java/org/cactoos/list/Immutable.java @@ -35,9 +35,6 @@ * * @param Element type * @since 1.16 - * @todo #898:30min Introduce an Immutable wrapper for {@link ListIterator} - * and use it in listIterator() methods instead of {@link ImmutableListIterator}. - * One another option is renaming of {@link ImmutableListIterator}. * @todo #898:30min Replace all the Collections.unmodifiableList * with the {@link org.cactoos.list.Immutable} from the cactoos codebase. * That should be done because Elegant Object principles are against static methods. From 4a6b4f993cdf92954c93fd260062e9396a35c7f9 Mon Sep 17 00:00:00 2001 From: Fevzi Anifieiev Date: Wed, 6 Nov 2019 16:12:16 -0500 Subject: [PATCH 3/5] (#1219) Reverted two previous commits --- src/main/java/org/cactoos/list/Immutable.java | 11 +- .../org/cactoos/list/ListIteratorNoNulls.java | 2 +- ...eListIterator.java => ListIteratorOf.java} | 12 +- .../list/ImmutableListIteratorTest.java | 110 ------------------ .../org/cactoos/list/ListEnvelopeTest.java | 57 +++++++-- 5 files changed, 60 insertions(+), 132 deletions(-) rename src/main/java/org/cactoos/list/{ImmutableListIterator.java => ListIteratorOf.java} (89%) delete mode 100644 src/test/java/org/cactoos/list/ImmutableListIteratorTest.java diff --git a/src/main/java/org/cactoos/list/Immutable.java b/src/main/java/org/cactoos/list/Immutable.java index 0e14bd4f67..d46d68de4e 100644 --- a/src/main/java/org/cactoos/list/Immutable.java +++ b/src/main/java/org/cactoos/list/Immutable.java @@ -35,6 +35,9 @@ * * @param Element type * @since 1.16 + * @todo #898:30min Introduce an Immutable wrapper for {@link ListIterator} + * and use it in listIterator() methods instead of {@link ListIteratorOf}. + * One another option is renaming of {@link ListIteratorOf}. * @todo #898:30min Replace all the Collections.unmodifiableList * with the {@link org.cactoos.list.Immutable} from the cactoos codebase. * That should be done because Elegant Object principles are against static methods. @@ -184,16 +187,12 @@ public int lastIndexOf(final Object item) { @Override public ListIterator listIterator() { - return new ImmutableListIterator( - this.list.listIterator() - ); + return this.list.listIterator(); } @Override public ListIterator listIterator(final int index) { - return new ImmutableListIterator( - this.list.listIterator(index) - ); + return this.list.listIterator(index); } @Override diff --git a/src/main/java/org/cactoos/list/ListIteratorNoNulls.java b/src/main/java/org/cactoos/list/ListIteratorNoNulls.java index 4c7ac4f601..12e77740e3 100644 --- a/src/main/java/org/cactoos/list/ListIteratorNoNulls.java +++ b/src/main/java/org/cactoos/list/ListIteratorNoNulls.java @@ -47,7 +47,7 @@ public final class ListIteratorNoNulls implements ListIterator { * @param src List iterator. */ public ListIteratorNoNulls(final ListIterator src) { - this.listiterator = new ImmutableListIterator<>(src); + this.listiterator = new ListIteratorOf<>(src); } @Override diff --git a/src/main/java/org/cactoos/list/ImmutableListIterator.java b/src/main/java/org/cactoos/list/ListIteratorOf.java similarity index 89% rename from src/main/java/org/cactoos/list/ImmutableListIterator.java rename to src/main/java/org/cactoos/list/ListIteratorOf.java index 37716483d9..cbac3cfdc1 100644 --- a/src/main/java/org/cactoos/list/ImmutableListIterator.java +++ b/src/main/java/org/cactoos/list/ListIteratorOf.java @@ -30,14 +30,14 @@ import org.cactoos.scalar.Unchecked; /** - * List Iterator that doesn't allow mutations. + * Iterator of the list that doesn't allow mutations. * *

There is no thread-safety guarantee. * * @param Items type * @since 0.35 */ -public final class ImmutableListIterator implements ListIterator { +public final class ListIteratorOf implements ListIterator { /** * Original list iterator. @@ -48,7 +48,7 @@ public final class ImmutableListIterator implements ListIterator { * Ctor. * @param list List that will be called to get a list iterator. */ - public ImmutableListIterator(final List list) { + public ListIteratorOf(final List list) { this(list::listIterator); } @@ -57,7 +57,7 @@ public ImmutableListIterator(final List list) { * @param list List that will be called to get a list iterator. * @param index Start index for a newly created list iterator. */ - public ImmutableListIterator(final List list, final int index) { + public ListIteratorOf(final List list, final int index) { this(() -> list.listIterator(index)); } @@ -65,7 +65,7 @@ public ImmutableListIterator(final List list, final int index) { * Ctor. * @param iter Original list iterator. */ - public ImmutableListIterator(final ListIterator iter) { + public ListIteratorOf(final ListIterator iter) { this(() -> iter); } @@ -73,7 +73,7 @@ public ImmutableListIterator(final ListIterator iter) { * Ctor. * @param orig Original list iterator. */ - public ImmutableListIterator(final Scalar> orig) { + public ListIteratorOf(final Scalar> orig) { this.origin = new Unchecked<>(new Sticky<>(orig)); } diff --git a/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java b/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java deleted file mode 100644 index d230488c92..0000000000 --- a/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2017-2019 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.cactoos.list; - -import java.util.ListIterator; -import org.hamcrest.core.IsEqual; -import org.junit.Test; -import org.llorllale.cactoos.matchers.Assertion; - -/** - * Test case for {@link ImmutableListIterator}. - * @since 0.32 - * @checkstyle JavadocMethodCheck (500 lines) - * @checkstyle JavadocTypeCheck (500 lines) - * @checkstyle MagicNumberCheck (500 lines) - */ -@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"}) -public final class ImmutableListIteratorTest { - - @Test - public void getsPreviousIndex() { - new Assertion<>( - "List iterator returns incorrect previous index", - new ImmutableListIterator<>( - new ListOf<>(1) - ).previousIndex(), - new IsEqual<>(-1) - ).affirm(); - } - - @Test - public void getsPrevious() { - new Assertion<>( - "List iterator returns incorrect previous item", - new ImmutableListIterator<>( - new ListOf<>(3, 7), - 1 - ).previous(), - new IsEqual<>(3) - ).affirm(); - } - - @Test - public void getsNextIndex() { - new Assertion<>( - "List iterator returns incorrect next index", - new ImmutableListIterator<>( - new ListOf<>(1) - ).nextIndex(), - new IsEqual<>(0) - ).affirm(); - } - - @Test - public void getsNext() { - new Assertion<>( - "List iterator returns incorrect next item", - new ImmutableListIterator<>( - new ListOf<>(5, 11, 13), - 1 - ).next(), - new IsEqual<>(11) - ).affirm(); - } - - @Test(expected = UnsupportedOperationException.class) - public void doesNotSupportRemove() { - final ListIterator iterator = new ImmutableListIterator<>( - new ListOf<>(1, 2) - ); - iterator.remove(); - } - - @Test(expected = UnsupportedOperationException.class) - public void doesNotSupportAdd() { - final ListIterator iterator = new ImmutableListIterator<>( - new ListOf<>(1, 2) - ); - iterator.add(1); - } - - @Test(expected = UnsupportedOperationException.class) - public void doesNotSupportSet() { - final ListIterator iterator = new ImmutableListIterator<>( - new ListOf<>(1, 2) - ); - iterator.set(1); - } -} diff --git a/src/test/java/org/cactoos/list/ListEnvelopeTest.java b/src/test/java/org/cactoos/list/ListEnvelopeTest.java index 702a79c6a0..e6301e496b 100644 --- a/src/test/java/org/cactoos/list/ListEnvelopeTest.java +++ b/src/test/java/org/cactoos/list/ListEnvelopeTest.java @@ -25,7 +25,6 @@ import java.util.Arrays; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import org.hamcrest.MatcherAssert; @@ -45,7 +44,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) @@ -58,7 +57,7 @@ public void returnsListIteratorWithUnsupportedRemove() { @Test() public void returnsListIteratorWithSupportedSet() { - final ListEnvelope list = new MutableStringList("one", "two"); + final ListEnvelope list = new StringList("one", "two"); final ListIterator iterator = list.listIterator(1); iterator.next(); iterator.set("zero"); @@ -158,15 +157,55 @@ public void returnsSubListWithUnsupportedAdd() { list.subList(0, 1).add("two"); } + @Test + public void getsPreviousIndex() { + new Assertion<>( + "List iterator returns incorrect previous index", + new ListIteratorOf<>( + new ListOf<>(1) + ).previousIndex(), + new IsEqual<>(-1) + ).affirm(); + } + + @Test + public void getsPrevious() { + new Assertion<>( + "List iterator returns incorrect previous item", + new ListIteratorOf<>( + new ListOf<>(3, 7), + 1 + ).previous(), + new IsEqual<>(3) + ).affirm(); + } + + @Test + public void getsNextIndex() { + new Assertion<>( + "List iterator returns incorrect next index", + new ListIteratorOf<>( + new ListOf<>(1) + ).nextIndex(), + new IsEqual<>(0) + ).affirm(); + } + + @Test + public void getsNext() { + new Assertion<>( + "List iterator returns incorrect next item", + new ListIteratorOf<>( + new ListOf<>(5, 11, 13), + 1 + ).next(), + new IsEqual<>(11) + ).affirm(); + } + private static final class StringList extends ListEnvelope { StringList(final String... elements) { super(() -> new Immutable<>(Arrays.asList(elements))); } } - - private static final class MutableStringList extends ListEnvelope { - MutableStringList(final String... elements) { - super(() -> new LinkedList<>(Arrays.asList(elements))); - } - } } From fd65977847f283da313a68afd02b6eec91e54719 Mon Sep 17 00:00:00 2001 From: Fevzi Anifieiev Date: Wed, 6 Nov 2019 17:27:47 -0500 Subject: [PATCH 4/5] (#1219) Introduced immutable list iterator --- src/main/java/org/cactoos/list/Immutable.java | 11 +- .../cactoos/list/ImmutableListIterator.java | 101 ++++++++++++++++ .../org/cactoos/list/ListIteratorNoNulls.java | 2 +- .../java/org/cactoos/list/ListIteratorOf.java | 3 + .../list/ImmutableListIteratorTest.java | 108 ++++++++++++++++++ .../org/cactoos/list/ListEnvelopeTest.java | 9 +- 6 files changed, 227 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/cactoos/list/ImmutableListIterator.java create mode 100644 src/test/java/org/cactoos/list/ImmutableListIteratorTest.java diff --git a/src/main/java/org/cactoos/list/Immutable.java b/src/main/java/org/cactoos/list/Immutable.java index d46d68de4e..39dc9bdd5b 100644 --- a/src/main/java/org/cactoos/list/Immutable.java +++ b/src/main/java/org/cactoos/list/Immutable.java @@ -35,9 +35,6 @@ * * @param Element type * @since 1.16 - * @todo #898:30min Introduce an Immutable wrapper for {@link ListIterator} - * and use it in listIterator() methods instead of {@link ListIteratorOf}. - * One another option is renaming of {@link ListIteratorOf}. * @todo #898:30min Replace all the Collections.unmodifiableList * with the {@link org.cactoos.list.Immutable} from the cactoos codebase. * That should be done because Elegant Object principles are against static methods. @@ -187,12 +184,16 @@ public int lastIndexOf(final Object item) { @Override public ListIterator listIterator() { - return this.list.listIterator(); + return new ImmutableListIterator<>( + this.list.listIterator() + ); } @Override public ListIterator listIterator(final int index) { - return this.list.listIterator(index); + return new ImmutableListIterator<>( + this.list.listIterator(index) + ); } @Override diff --git a/src/main/java/org/cactoos/list/ImmutableListIterator.java b/src/main/java/org/cactoos/list/ImmutableListIterator.java new file mode 100644 index 0000000000..a3286bfe78 --- /dev/null +++ b/src/main/java/org/cactoos/list/ImmutableListIterator.java @@ -0,0 +1,101 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2019 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.list; + +import java.util.ListIterator; + +/** + * Immutable {@link ListIterator} that doesn't allow mutations. + * + *

There is no thread-safety guarantee. + * + * @param Items type + * @since 1.0 + */ +public final class ImmutableListIterator implements ListIterator { + + /** + * Original list iterator. + */ + private final ListIterator origin; + + /** + * Ctor. + * @param iter Original list iterator. + */ + public ImmutableListIterator(final ListIterator iter) { + this.origin = new ListIteratorOf<>(iter); + } + + @Override + public boolean hasNext() { + return this.origin.hasNext(); + } + + @Override + public T next() { + return this.origin.next(); + } + + @Override + public boolean hasPrevious() { + return this.origin.hasPrevious(); + } + + @Override + public T previous() { + return this.origin.previous(); + } + + @Override + public int nextIndex() { + return this.origin.nextIndex(); + } + + @Override + public int previousIndex() { + return this.origin.previousIndex(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException( + "Iterator is read-only and doesn't allow removing items" + ); + } + + @Override + public void set(final T item) { + throw new UnsupportedOperationException( + "Iterator is read-only and doesn't allow rewriting items" + ); + } + + @Override + public void add(final T item) { + throw new UnsupportedOperationException( + "Iterator is read-only and doesn't allow adding items" + ); + } +} diff --git a/src/main/java/org/cactoos/list/ListIteratorNoNulls.java b/src/main/java/org/cactoos/list/ListIteratorNoNulls.java index 12e77740e3..65376d0ddf 100644 --- a/src/main/java/org/cactoos/list/ListIteratorNoNulls.java +++ b/src/main/java/org/cactoos/list/ListIteratorNoNulls.java @@ -27,7 +27,7 @@ import java.util.ListIterator; /** - * A decorator of {@link ListIterator} that is immutable and tolerates no NULLs. + * A decorator of {@link ListIterator} that tolerates no NULLs. * *

There is no thread-safety guarantee.

* diff --git a/src/main/java/org/cactoos/list/ListIteratorOf.java b/src/main/java/org/cactoos/list/ListIteratorOf.java index cbac3cfdc1..84903abc86 100644 --- a/src/main/java/org/cactoos/list/ListIteratorOf.java +++ b/src/main/java/org/cactoos/list/ListIteratorOf.java @@ -36,6 +36,9 @@ * * @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 { diff --git a/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java b/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java new file mode 100644 index 0000000000..9aa8e8f9c1 --- /dev/null +++ b/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java @@ -0,0 +1,108 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2019 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.list; + +import java.util.ListIterator; +import org.hamcrest.core.IsEqual; +import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; + +/** + * Test case for {@link ImmutableListIterator}. + * @since 1.0 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle JavadocTypeCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"}) +public final class ImmutableListIteratorTest { + + @Test + public void getsPreviousIndex() { + new Assertion<>( + "List iterator returns incorrect previous index", + new ImmutableListIterator<>( + new ListOf<>(1).listIterator() + ).previousIndex(), + new IsEqual<>(-1) + ).affirm(); + } + + @Test + public void getsPrevious() { + new Assertion<>( + "List iterator returns incorrect previous item", + new ImmutableListIterator<>( + new ListOf<>(3, 7).listIterator(1) + ).previous(), + new IsEqual<>(3) + ).affirm(); + } + + @Test + public void getsNextIndex() { + new Assertion<>( + "List iterator returns incorrect next index", + new ImmutableListIterator<>( + new ListOf<>(1).listIterator() + ).nextIndex(), + new IsEqual<>(0) + ).affirm(); + } + + @Test + public void getsNext() { + new Assertion<>( + "List iterator returns incorrect next item", + new ImmutableListIterator<>( + new ListOf<>(5, 11, 13).listIterator(1) + ).next(), + new IsEqual<>(11) + ).affirm(); + } + + @Test(expected = UnsupportedOperationException.class) + public void doesNotSupportRemove() { + final ListIterator iterator = new ImmutableListIterator<>( + new ListOf<>(1, 2).listIterator() + ); + iterator.remove(); + } + + @Test(expected = UnsupportedOperationException.class) + public void doesNotSupportAdd() { + final ListIterator iterator = new ImmutableListIterator<>( + new ListOf<>(1, 2).listIterator() + ); + iterator.add(1); + } + + @Test(expected = UnsupportedOperationException.class) + public void doesNotSupportSet() { + final ListIterator iterator = new ImmutableListIterator<>( + new ListOf<>(1, 2).listIterator() + ); + iterator.set(1); + } +} diff --git a/src/test/java/org/cactoos/list/ListEnvelopeTest.java b/src/test/java/org/cactoos/list/ListEnvelopeTest.java index e6301e496b..bc780e80b2 100644 --- a/src/test/java/org/cactoos/list/ListEnvelopeTest.java +++ b/src/test/java/org/cactoos/list/ListEnvelopeTest.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import org.hamcrest.MatcherAssert; @@ -57,7 +58,7 @@ public void returnsListIteratorWithUnsupportedRemove() { @Test() public void returnsListIteratorWithSupportedSet() { - final ListEnvelope list = new StringList("one", "two"); + final ListEnvelope list = new MutableStringList("one", "two"); final ListIterator iterator = list.listIterator(1); iterator.next(); iterator.set("zero"); @@ -208,4 +209,10 @@ private static final class StringList extends ListEnvelope { super(() -> new Immutable<>(Arrays.asList(elements))); } } + + private static final class MutableStringList extends ListEnvelope { + MutableStringList(final String... elements) { + super(() -> new LinkedList<>(Arrays.asList(elements))); + } + } } From 40851be4c73ae0eeda4fea7ef57c5ecd5a47e4f1 Mon Sep 17 00:00:00 2001 From: Fevzi Anifieiev Date: Fri, 8 Nov 2019 11:44:58 -0500 Subject: [PATCH 5/5] (#1219) After code review fixes --- .../cactoos/list/ImmutableListIterator.java | 8 +- .../list/ImmutableListIteratorTest.java | 81 ++++++++++++------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/cactoos/list/ImmutableListIterator.java b/src/main/java/org/cactoos/list/ImmutableListIterator.java index a3286bfe78..ffae9352a7 100644 --- a/src/main/java/org/cactoos/list/ImmutableListIterator.java +++ b/src/main/java/org/cactoos/list/ImmutableListIterator.java @@ -45,7 +45,7 @@ public final class ImmutableListIterator implements ListIterator { * @param iter Original list iterator. */ public ImmutableListIterator(final ListIterator iter) { - this.origin = new ListIteratorOf<>(iter); + this.origin = iter; } @Override @@ -81,21 +81,21 @@ public int previousIndex() { @Override public void remove() { throw new UnsupportedOperationException( - "Iterator is read-only and doesn't allow removing items" + "List Iterator is read-only and doesn't allow removing items" ); } @Override public void set(final T item) { throw new UnsupportedOperationException( - "Iterator is read-only and doesn't allow rewriting items" + "List Iterator is read-only and doesn't allow rewriting items" ); } @Override public void add(final T item) { throw new UnsupportedOperationException( - "Iterator is read-only and doesn't allow adding items" + "List Iterator is read-only and doesn't allow adding items" ); } } diff --git a/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java b/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java index 9aa8e8f9c1..9a4fea2955 100644 --- a/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java +++ b/src/test/java/org/cactoos/list/ImmutableListIteratorTest.java @@ -23,10 +23,10 @@ */ package org.cactoos.list; -import java.util.ListIterator; import org.hamcrest.core.IsEqual; import org.junit.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.Throws; /** * Test case for {@link ImmutableListIterator}. @@ -39,9 +39,9 @@ public final class ImmutableListIteratorTest { @Test - public void getsPreviousIndex() { + public void mustReturnPreviousIndex() { new Assertion<>( - "List iterator returns incorrect previous index", + "Must return next previous index", new ImmutableListIterator<>( new ListOf<>(1).listIterator() ).previousIndex(), @@ -50,9 +50,9 @@ public void getsPreviousIndex() { } @Test - public void getsPrevious() { + public void mustReturnPreviousElement() { new Assertion<>( - "List iterator returns incorrect previous item", + "Must return previous element", new ImmutableListIterator<>( new ListOf<>(3, 7).listIterator(1) ).previous(), @@ -61,9 +61,9 @@ public void getsPrevious() { } @Test - public void getsNextIndex() { + public void mustReturnNextIndex() { new Assertion<>( - "List iterator returns incorrect next index", + "Must return next index", new ImmutableListIterator<>( new ListOf<>(1).listIterator() ).nextIndex(), @@ -72,9 +72,9 @@ public void getsNextIndex() { } @Test - public void getsNext() { + public void mustReturnNextElement() { new Assertion<>( - "List iterator returns incorrect next item", + "Must return next element", new ImmutableListIterator<>( new ListOf<>(5, 11, 13).listIterator(1) ).next(), @@ -82,27 +82,54 @@ public void getsNext() { ).affirm(); } - @Test(expected = UnsupportedOperationException.class) - public void doesNotSupportRemove() { - final ListIterator iterator = new ImmutableListIterator<>( - new ListOf<>(1, 2).listIterator() - ); - iterator.remove(); + @Test + public void mustRaiseErrorOnListIteratorRemove() { + new Assertion<>( + "Must throw error if modified with remove operation", + () -> { + new ImmutableListIterator<>( + new ListOf<>(1, 2).listIterator() + ).remove(); + return 0; + }, + new Throws<>( + "List Iterator is read-only and doesn't allow removing items", + UnsupportedOperationException.class + ) + ).affirm(); } - @Test(expected = UnsupportedOperationException.class) - public void doesNotSupportAdd() { - final ListIterator iterator = new ImmutableListIterator<>( - new ListOf<>(1, 2).listIterator() - ); - iterator.add(1); + @Test + public void mustRaiseErrorOnListIteratorAdd() { + new Assertion<>( + "Must throw error if modified with add operation", + () -> { + new ImmutableListIterator<>( + new ListOf<>(1, 2).listIterator() + ).add(1); + return 0; + }, + new Throws<>( + "List Iterator is read-only and doesn't allow adding items", + UnsupportedOperationException.class + ) + ).affirm(); } - @Test(expected = UnsupportedOperationException.class) - public void doesNotSupportSet() { - final ListIterator iterator = new ImmutableListIterator<>( - new ListOf<>(1, 2).listIterator() - ); - iterator.set(1); + @Test + public void mustRaiseErrorOnListIteratorSet() { + new Assertion<>( + "Must throw error if modified with set operation", + () -> { + new ImmutableListIterator<>( + new ListOf<>(1, 2).listIterator() + ).set(1); + return 0; + }, + new Throws<>( + "List Iterator is read-only and doesn't allow rewriting items", + UnsupportedOperationException.class + ) + ).affirm(); } }