Skip to content

Commit

Permalink
(#1247) ListIteratorOf is now ListIteratorEnvelope
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Sep 13, 2020
1 parent a3ed118 commit 07b063e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 158 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/list/ListEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.cactoos.collection.CollectionEnvelope;

/**
* {@link List} envelope that allows mutations.
* {@link List} envelope.
*
* <p>There is no thread-safety guarantee.</p>
*
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/org/cactoos/list/ListIteratorEnvelope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2020 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;

/**
* {@link ListIterator} envelope.
*
* <p>There is no thread-safety guarantee.
*
* @param <T> Items type
* @since 0.47
*/
public abstract class ListIteratorEnvelope<T> implements ListIterator<T> {

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

/**
* Ctor.
* @param iter Original list iterator.
*/
public ListIteratorEnvelope(final ListIterator<T> iter) {
this.origin = iter;
}

@Override
public final boolean hasNext() {
return this.origin.hasNext();
}

@Override
public final T next() {
return this.origin.next();
}

@Override
public final boolean hasPrevious() {
return this.origin.hasPrevious();
}

@Override
public final T previous() {
return this.origin.previous();
}

@Override
public final int nextIndex() {
return this.origin.nextIndex();
}

@Override
public final int previousIndex() {
return this.origin.previousIndex();
}

@Override
public final void remove() {
this.origin.remove();
}

@Override
public final void set(final T item) {
this.origin.set(item);
}

@Override
public final void add(final T item) {
this.origin.add(item);
}
}
4 changes: 3 additions & 1 deletion src/main/java/org/cactoos/list/ListIteratorNoNulls.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
*
* @param <T> Element type
* @since 0.39
* @todo #1247:30min Add also some null checks both to the `set` and
* the `add` methods, then also add some tests to validate this behaviour.
*/
public final class ListIteratorNoNulls<T> implements ListIterator<T> {

Expand All @@ -47,7 +49,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 = src;
}

@Override
Expand Down
125 changes: 0 additions & 125 deletions src/main/java/org/cactoos/list/ListIteratorOf.java

This file was deleted.

23 changes: 6 additions & 17 deletions src/test/java/org/cactoos/list/ListEnvelopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ void getsLastIndexOfValue() {
void mustReturnPreviousIndex() {
new Assertion<>(
"List iterator must return previous index",
new ListIteratorOf<>(
new ListOf<>(1)
).previousIndex(),
new StringList("1").listIterator().previousIndex(),
new IsEqual<>(-1)
).affirm();
}
Expand All @@ -185,21 +183,16 @@ void mustReturnPreviousIndex() {
void mustReturnPreviousElement() {
new Assertion<>(
"List iterator must return previous element",
new ListIteratorOf<>(
new ListOf<>(3, 7),
1
).previous(),
new IsEqual<>(3)
new StringList("3", "7").listIterator(1).previous(),
new IsEqual<>("3")
).affirm();
}

@Test
void mustReturnNextIndex() {
new Assertion<>(
"List iterator must return next index",
new ListIteratorOf<>(
new ListOf<>(1)
).nextIndex(),
new StringList("1").listIterator().nextIndex(),
new IsEqual<>(0)
).affirm();
}
Expand All @@ -208,11 +201,8 @@ void mustReturnNextIndex() {
void mustReturnNextElement() {
new Assertion<>(
"List iterator must return next item",
new ListIteratorOf<>(
new ListOf<>(5, 11, 13),
1
).next(),
new IsEqual<>(11)
new StringList("5", "11", "13").listIterator(1).next(),
new IsEqual<>("11")
).affirm();
}

Expand All @@ -221,5 +211,4 @@ private static final class StringList extends ListEnvelope<String> {
super(new ListOf<>(elements));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@
import org.llorllale.cactoos.matchers.Assertion;

/**
* Test cases for {@link ListIteratorOf}.
* Test cases for {@link ListIteratorEnvelope}.
*
* @since 0.35
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle JavadocTypeCheck (500 lines)
* @checkstyle MagicNumberCheck (500 lines)
*/
final class ListIteratorOfTest {
final class ListIteratorEnvelopeTest {
@Test
void mustReturnPreviousIndex() {
new Assertion<>(
"List Iterator must return previous index",
new ListIteratorOf<>(
new ListOf<>(1)
new StringListIterator(
"1"
).previousIndex(),
new IsEqual<>(-1)
).affirm();
Expand All @@ -50,20 +51,20 @@ void mustReturnPreviousIndex() {
void mustReturnPreviousElement() {
new Assertion<>(
"List Iterator must return previous element",
new ListIteratorOf<>(
new ListOf<>(3, 7),
1
new StringListIterator(
1,
"3", "7"
).previous(),
new IsEqual<>(3)
new IsEqual<>("3")
).affirm();
}

@Test
void mustReturnNextIndex() {
new Assertion<>(
"List iterator must return next index",
new ListIteratorOf<>(
new ListOf<>(1)
new StringListIterator(
"1"
).nextIndex(),
new IsEqual<>(0)
).affirm();
Expand All @@ -73,11 +74,21 @@ void mustReturnNextIndex() {
void mustReturnNextElement() {
new Assertion<>(
"List iterator must return next item",
new ListIteratorOf<>(
new ListOf<>(5, 11, 13),
1
new StringListIterator(
1,
"5", "11", "13"
).next(),
new IsEqual<>(11)
new IsEqual<>("11")
).affirm();
}

private static final class StringListIterator extends ListIteratorEnvelope<String> {
StringListIterator(final int index, final String... elements) {
super(new ListOf<>(elements).listIterator(index));
}

StringListIterator(final String... elements) {
super(new ListOf<>(elements).listIterator());
}
}
}

1 comment on commit 07b063e

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 07b063e Sep 13, 2020

Choose a reason for hiding this comment

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

Puzzle 1247-25d91c9c discovered in src/main/java/org/cactoos/list/ListIteratorNoNulls.java and submitted as #1459. 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.