Skip to content

Commit

Permalink
(yegor256#1219) Introduced immutable list iterator for immutable list
Browse files Browse the repository at this point in the history
  • Loading branch information
fanifieiev committed Nov 5, 2019
1 parent 5e0da30 commit aed8129
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 59 deletions.
12 changes: 8 additions & 4 deletions src/main/java/org/cactoos/list/Immutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
* @param <T> 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.
Expand Down Expand Up @@ -187,12 +187,16 @@ public int lastIndexOf(final Object item) {

@Override
public ListIterator<T> listIterator() {
return this.list.listIterator();
return new ImmutableListIterator<T>(
this.list.listIterator()
);
}

@Override
public ListIterator<T> listIterator(final int index) {
return this.list.listIterator(index);
return new ImmutableListIterator<T>(
this.list.listIterator(index)
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>There is no thread-safety guarantee.
*
* @param <T> Items type
* @since 0.35
*/
public final class ListIteratorOf<T> implements ListIterator<T> {
public final class ImmutableListIterator<T> implements ListIterator<T> {

/**
* Original list iterator.
Expand All @@ -48,7 +48,7 @@ public final class ListIteratorOf<T> implements ListIterator<T> {
* Ctor.
* @param list List that will be called to get a list iterator.
*/
public ListIteratorOf(final List<T> list) {
public ImmutableListIterator(final List<T> list) {
this(list::listIterator);
}

Expand All @@ -57,23 +57,23 @@ public ListIteratorOf(final List<T> 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<T> list, final int index) {
public ImmutableListIterator(final List<T> list, final int index) {
this(() -> list.listIterator(index));
}

/**
* Ctor.
* @param iter Original list iterator.
*/
public ListIteratorOf(final ListIterator<T> iter) {
public ImmutableListIterator(final ListIterator<T> iter) {
this(() -> iter);
}

/**
* Ctor.
* @param orig Original list iterator.
*/
public ListIteratorOf(final Scalar<ListIterator<T>> orig) {
public ImmutableListIterator(final Scalar<ListIterator<T>> orig) {
this.origin = new Unchecked<>(new Sticky<>(orig));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/list/ListIteratorNoNulls.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class ListIteratorNoNulls<T> implements ListIterator<T> {
* @param src List iterator.
*/
public ListIteratorNoNulls(final ListIterator<T> src) {
this.listiterator = new ListIteratorOf<>(src);
this.listiterator = new ImmutableListIterator<>(src);
}

@Override
Expand Down
110 changes: 110 additions & 0 deletions src/test/java/org/cactoos/list/ImmutableListIteratorTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer> iterator = new ImmutableListIterator<>(
new ListOf<>(1, 2)
);
iterator.remove();
}

@Test(expected = UnsupportedOperationException.class)
public void doesNotSupportAdd() {
final ListIterator<Integer> iterator = new ImmutableListIterator<>(
new ListOf<>(1, 2)
);
iterator.add(1);
}

@Test(expected = UnsupportedOperationException.class)
public void doesNotSupportSet() {
final ListIterator<Integer> iterator = new ImmutableListIterator<>(
new ListOf<>(1, 2)
);
iterator.set(1);
}
}
57 changes: 9 additions & 48 deletions src/test/java/org/cactoos/list/ListEnvelopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -57,7 +58,7 @@ public void returnsListIteratorWithUnsupportedRemove() {

@Test()
public void returnsListIteratorWithSupportedSet() {
final ListEnvelope<String> list = new StringList("one", "two");
final ListEnvelope<String> list = new MutableStringList("one", "two");
final ListIterator<String> iterator = list.listIterator(1);
iterator.next();
iterator.set("zero");
Expand Down Expand Up @@ -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<String> {
StringList(final String... elements) {
super(() -> new Immutable<>(Arrays.asList(elements)));
}
}

private static final class MutableStringList extends ListEnvelope<String> {
MutableStringList(final String... elements) {
super(() -> new LinkedList<>(Arrays.asList(elements)));
}
}
}

0 comments on commit aed8129

Please sign in to comment.