Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Nov 8, 2019
2 parents eee3713 + 40851be commit 2136d9d
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/cactoos/list/Immutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
*
* @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}.
* @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 +184,16 @@ public int lastIndexOf(final Object item) {

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

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

@Override
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/org/cactoos/list/ImmutableListIterator.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>There is no thread-safety guarantee.
*
* @param <T> Items type
* @since 1.0
*/
public final class ImmutableListIterator<T> implements ListIterator<T> {

/**
* Original list iterator.
*/
private final ListIterator<T> origin;

/**
* Ctor.
* @param iter Original list iterator.
*/
public ImmutableListIterator(final ListIterator<T> iter) {
this.origin = 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(
"List Iterator is read-only and doesn't allow removing items"
);
}

@Override
public void set(final T item) {
throw new UnsupportedOperationException(
"List Iterator is read-only and doesn't allow rewriting items"
);
}

@Override
public void add(final T item) {
throw new UnsupportedOperationException(
"List Iterator is read-only and doesn't allow adding items"
);
}
}
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 @@ -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.
*
* <p>There is no thread-safety guarantee.</p>
*
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/cactoos/list/ListIteratorOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
*
* @param <T> 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<T> implements ListIterator<T> {

Expand Down
135 changes: 135 additions & 0 deletions src/test/java/org/cactoos/list/ImmutableListIteratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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 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}.
* @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 mustReturnPreviousIndex() {
new Assertion<>(
"Must return next previous index",
new ImmutableListIterator<>(
new ListOf<>(1).listIterator()
).previousIndex(),
new IsEqual<>(-1)
).affirm();
}

@Test
public void mustReturnPreviousElement() {
new Assertion<>(
"Must return previous element",
new ImmutableListIterator<>(
new ListOf<>(3, 7).listIterator(1)
).previous(),
new IsEqual<>(3)
).affirm();
}

@Test
public void mustReturnNextIndex() {
new Assertion<>(
"Must return next index",
new ImmutableListIterator<>(
new ListOf<>(1).listIterator()
).nextIndex(),
new IsEqual<>(0)
).affirm();
}

@Test
public void mustReturnNextElement() {
new Assertion<>(
"Must return next element",
new ImmutableListIterator<>(
new ListOf<>(5, 11, 13).listIterator(1)
).next(),
new IsEqual<>(11)
).affirm();
}

@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
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
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();
}
}
9 changes: 8 additions & 1 deletion 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 Down Expand Up @@ -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 @@ -208,4 +209,10 @@ private static final class StringList extends ListEnvelope<String> {
super(() -> new Immutable<>(Arrays.asList(elements)));
}
}

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

2 comments on commit 2136d9d

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 2136d9d Nov 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 898-fb66ac0b disappeared from src/main/java/org/cactoos/list/Immutable.java, that's why I closed #1219. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 2136d9d Nov 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1219-fd8dadee discovered in src/main/java/org/cactoos/list/ListIteratorOf.java and submitted as #1230. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.