Skip to content

Commit

Permalink
(yegor256#996) Make ListEnvelope public and consolidate API
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Apr 14, 2019
1 parent a760506 commit f428eaf
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 53 deletions.
12 changes: 7 additions & 5 deletions src/main/java/org/cactoos/list/ListEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.cactoos.scalar.Unchecked;

/**
* List envelope.
* {@link List} envelope that doesn't allow mutations.
*
* <p>There is no thread-safety guarantee.</p>
*
Expand All @@ -45,7 +45,7 @@
"PMD.AbstractNaming"
}
)
abstract class ListEnvelope<T> extends CollectionEnvelope<T> implements
public abstract class ListEnvelope<T> extends CollectionEnvelope<T> implements
List<T> {

/**
Expand All @@ -57,7 +57,7 @@ abstract class ListEnvelope<T> extends CollectionEnvelope<T> implements
* Ctor.
* @param src Source
*/
ListEnvelope(final Scalar<List<T>> src) {
public ListEnvelope(final Scalar<List<T>> src) {
super(src::value);
this.list = new Unchecked<>(src);
}
Expand Down Expand Up @@ -114,11 +114,13 @@ public final ListIterator<T> listIterator(final int index) {

@Override
public final List<T> subList(final int start, final int end) {
return this.list.value().subList(start, end);
return new ListEnvelope<T>(
() -> this.list.value().subList(start, end)
) { };
}

@Override
public String toString() {
public final String toString() {
return this.list.value().toString();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/list/ListIteratorOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.cactoos.scalar.Unchecked;

/**
* Iterator of the list.
* Iterator of the list that doesn't allow mutations.
*
* <p>There is no thread-safety guarantee.
*
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/org/cactoos/list/Mapped.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.cactoos.list;

import org.cactoos.Func;
import org.cactoos.text.TextOf;

/**
* Mapped list.
Expand All @@ -47,9 +46,4 @@ public Mapped(final Func<X, Y> fnc, final Iterable<X> src) {
new org.cactoos.iterable.Mapped<>(fnc, src)
));
}

@Override
public String toString() {
return new TextOf(this).toString();
}
}
128 changes: 88 additions & 40 deletions src/test/java/org/cactoos/list/ListEnvelopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,110 +23,158 @@
*/
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<String> list = new ListEnvelope<String>(
() -> {
final List<String> inner = new LinkedList<>();
inner.add("one");
return inner;
}
) {
};
final ListEnvelope<String> list = new StringList("one");
final Iterator<String> iterator = list.listIterator();
iterator.next();
iterator.remove();
}

@Test(expected = UnsupportedOperationException.class)
public void returnsListIteratorWithUnsupportedSet() {
final ListEnvelope<String> list = new ListEnvelope<String>(
() -> {
final List<String> inner = new LinkedList<>();
inner.add("three");
return inner;
}
) {
};
final ListEnvelope<String> list = new StringList("one");
final ListIterator<String> iterator = list.listIterator(1);
iterator.set("zero");
}

@Test(expected = UnsupportedOperationException.class)
public void returnsListIteratorWithUnsupportedAdd() {
final ListEnvelope<String> list = new ListEnvelope<String>(
() -> {
final List<String> inner = new LinkedList<>();
inner.add("ten");
return inner;
}
) {
};
final ListEnvelope<String> list = new StringList("one");
final ListIterator<String> iterator = list.listIterator();
iterator.next();
iterator.add("twenty");
iterator.add("two");
}

@Test(expected = UnsupportedOperationException.class)
public void subListReturnsListIteratorWithUnsupportedRemove() {
final ListEnvelope<String> list = new StringList("one");
final Iterator<String> iterator = list.subList(0, 1)
.listIterator();
iterator.next();
iterator.remove();
}

@Test(expected = UnsupportedOperationException.class)
public void subListReturnsListIteratorWithUnsupportedSet() {
final ListEnvelope<String> list = new StringList("one");
final ListIterator<String> iterator = list.subList(0, 1)
.listIterator(1);
iterator.set("zero");
}

@Test(expected = UnsupportedOperationException.class)
public void subListReturnsListIteratorWithUnsupportedAdd() {
final ListEnvelope<String> list = new StringList("one");
final ListIterator<String> iterator = list.subList(0, 1)
.listIterator();
iterator.next();
iterator.add("two");
}

@Test(expected = UnsupportedOperationException.class)
public void unsupportedRemove() {
final ListEnvelope<String> list = new StringList("one");
list.remove(0);
}

@Test(expected = UnsupportedOperationException.class)
public void unsupportedSet() {
final ListEnvelope<String> list = new StringList("one");
list.set(0, "zero");
}

@Test(expected = UnsupportedOperationException.class)
public void unsupportedAdd() {
final ListEnvelope<String> list = new StringList("one");
list.add("two");
}

@Test(expected = UnsupportedOperationException.class)
public void returnsSubListWithUnsupportedRemove() {
final ListEnvelope<String> list = new StringList("one");
list.subList(0, 1).remove(0);
}

@Test(expected = UnsupportedOperationException.class)
public void returnsSubListWithUnsupportedSet() {
final ListEnvelope<String> list = new StringList("one");
list.subList(0, 1).set(0, "zero");
}

@Test(expected = UnsupportedOperationException.class)
public void returnsSubListWithUnsupportedAdd() {
final ListEnvelope<String> 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<String> {
StringList(final String... elements) {
super(() -> Arrays.asList(elements));
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/cactoos/list/MappedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
);
}
}

0 comments on commit f428eaf

Please sign in to comment.