diff --git a/src/main/java/org/cactoos/collection/CollectionEnvelope.java b/src/main/java/org/cactoos/collection/CollectionEnvelope.java index ff31333150..89306cb3a9 100644 --- a/src/main/java/org/cactoos/collection/CollectionEnvelope.java +++ b/src/main/java/org/cactoos/collection/CollectionEnvelope.java @@ -39,7 +39,7 @@ * @since 0.23 */ @SuppressWarnings("PMD.TooManyMethods") -class CollectionEnvelope implements Collection { +public class CollectionEnvelope implements Collection { /** * Shuffled one. @@ -50,7 +50,7 @@ class CollectionEnvelope implements Collection { * Ctor. * @param slr The scalar */ - CollectionEnvelope(final Scalar> slr) { + public CollectionEnvelope(final Scalar> slr) { this.col = new UncheckedScalar<>(slr); } diff --git a/src/main/java/org/cactoos/list/Joined.java b/src/main/java/org/cactoos/list/Joined.java index 32f7d1b416..e5b279b944 100644 --- a/src/main/java/org/cactoos/list/Joined.java +++ b/src/main/java/org/cactoos/list/Joined.java @@ -23,10 +23,7 @@ */ package org.cactoos.list; -import java.util.Collection; -import java.util.Iterator; import java.util.List; -import java.util.ListIterator; import java.util.stream.Collectors; /** @@ -39,13 +36,7 @@ * @param Type of source item * @since 0.20 */ -@SuppressWarnings("PMD.TooManyMethods") -public final class Joined implements List { - - /** - * The original lists. - */ - private final Iterable> lists; +public final class Joined extends ListEnvelope { /** * Ctor. @@ -62,152 +53,10 @@ public Joined(final List... src) { * @param src Source lists */ public Joined(final Iterable> src) { - this.lists = src; - } - - @Override - public int size() { - int result = 0; - for (final List list : this.lists) { - result += list.size(); - } - return result; - } - - @Override - public boolean isEmpty() { - boolean result = true; - for (final List list : this.lists) { - if (!list.isEmpty()) { - result = false; - break; - } - } - return result; - } - - @Override - public boolean contains(final Object obj) { - boolean result = false; - for (final List list : this.lists) { - if (list.contains(obj)) { - result = true; - break; - } - } - return result; - } - - @Override - public Iterator iterator() { - return this.joined().iterator(); - } - - @Override - public Object[] toArray() { - return this.joined().toArray(); - } - - @Override - @SuppressWarnings("PMD.UseVarargs") - public T[] toArray(final T[] array) { - return this.joined().toArray(array); - } - - @Override - public boolean add(final X item) { - throw new UnsupportedOperationException("#add()"); - } - - @Override - public boolean remove(final Object item) { - throw new UnsupportedOperationException("#remove()"); - } - - @Override - public boolean containsAll(final Collection collection) { - return this.joined().containsAll(collection); - } - - @Override - public boolean addAll(final Collection items) { - throw new UnsupportedOperationException("#addAll()"); - } - - @Override - public boolean addAll(final int index, - final Collection items) { - throw new UnsupportedOperationException("#addAll(index)"); - } - - @Override - public boolean removeAll(final Collection items) { - throw new UnsupportedOperationException("#removeAll()"); - } - - @Override - public boolean retainAll(final Collection items) { - throw new UnsupportedOperationException("#retainAll()"); - } - - @Override - public void clear() { - throw new UnsupportedOperationException("#clear()"); - } - - @Override - public X get(final int index) { - return this.joined().get(index); - } - - @Override - public X set(final int index, final X element) { - throw new UnsupportedOperationException("#set()"); - } - - @Override - public void add(final int index, final X element) { - throw new UnsupportedOperationException("#add(index)"); - } - - @Override - public X remove(final int index) { - throw new UnsupportedOperationException("#remove(index)"); - } - - @Override - public int indexOf(final Object item) { - throw new UnsupportedOperationException("#indexOf()"); - } - - @Override - public int lastIndexOf(final Object item) { - throw new UnsupportedOperationException("#lastIndexOf()"); - } - - @Override - public ListIterator listIterator() { - throw new UnsupportedOperationException("#listIterator()"); - } - - @Override - public ListIterator listIterator(final int index) { - throw new UnsupportedOperationException("#listIterator(index)"); - } - - @Override - public List subList(final int start, final int end) { - return this.joined().subList(start, end); - } - - /** - * Joined list. - * - * @return Result list - */ - private List joined() { - return new ListOf<>(this.lists).stream() + super(() -> new ListOf<>(src).stream() .flatMap(List::stream) - .collect(Collectors.toList()); + .collect(Collectors.toList()) + ); } + } diff --git a/src/main/java/org/cactoos/list/ListEnvelope.java b/src/main/java/org/cactoos/list/ListEnvelope.java new file mode 100644 index 0000000000..de8243b842 --- /dev/null +++ b/src/main/java/org/cactoos/list/ListEnvelope.java @@ -0,0 +1,110 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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.Collection; +import java.util.List; +import java.util.ListIterator; +import org.cactoos.Scalar; +import org.cactoos.collection.CollectionEnvelope; +import org.cactoos.scalar.UncheckedScalar; + +/** + * List envelope. + * + *

There is no thread-safety guarantee.

+ * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Element type + * @since 0.23 + */ +@SuppressWarnings("PMD.TooManyMethods") +class ListEnvelope extends CollectionEnvelope implements List { + + /** + * Encapsulated list. + */ + private final UncheckedScalar> list; + + /** + * Ctor. + * @param src Source + */ + ListEnvelope(final Scalar> src) { + super(src::value); + this.list = new UncheckedScalar<>(src); + } + + @Override + public final boolean addAll(final int index, + final Collection items) { + throw new UnsupportedOperationException("#addAll()"); + } + + @Override + public final T get(final int index) { + return this.list.value().get(index); + } + + @Override + public final T set(final int index, final T element) { + throw new UnsupportedOperationException("#set()"); + } + + @Override + public final void add(final int index, final T element) { + throw new UnsupportedOperationException("#add()"); + } + + @Override + public final T remove(final int index) { + throw new UnsupportedOperationException("#remove()"); + } + + @Override + public final int indexOf(final Object item) { + return this.list.value().indexOf(item); + } + + @Override + public final int lastIndexOf(final Object item) { + return this.list.value().lastIndexOf(item); + } + + @Override + public final ListIterator listIterator() { + return this.list.value().listIterator(); + } + + @Override + public final ListIterator listIterator(final int index) { + return this.list.value().listIterator(index); + } + + @Override + public final List subList(final int start, final int end) { + return this.list.value().subList(start, end); + } +} diff --git a/src/main/java/org/cactoos/list/ListOf.java b/src/main/java/org/cactoos/list/ListOf.java index f616e3e87a..0fdb421396 100644 --- a/src/main/java/org/cactoos/list/ListOf.java +++ b/src/main/java/org/cactoos/list/ListOf.java @@ -23,14 +23,11 @@ */ package org.cactoos.list; -import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.ListIterator; import org.cactoos.iterable.IterableOf; -import org.cactoos.scalar.UncheckedScalar; /** * Iterable as {@link List}. @@ -52,13 +49,7 @@ * @see StickyList * @since 0.1 */ -@SuppressWarnings("PMD.TooManyMethods") -public final class ListOf implements List { - - /** - * The list. - */ - private final UncheckedScalar> list; +public final class ListOf extends ListEnvelope { /** * Ctor. @@ -84,152 +75,13 @@ public ListOf(final Iterator src) { * @param src An {@link Iterable} */ public ListOf(final Iterable src) { - this.list = new UncheckedScalar<>( - () -> { - final List temp = new LinkedList<>(); - for (final T item : src) { - temp.add(item); - } - return Collections.unmodifiableList(temp); + super(() -> { + final List temp = new LinkedList<>(); + for (final T item : src) { + temp.add(item); } - ); - } - - @Override - public int size() { - return this.list.value().size(); - } - - @Override - public boolean isEmpty() { - return this.list.value().isEmpty(); - } - - @Override - public boolean contains(final Object object) { - return this.list.value().contains(object); - } - - @Override - public Iterator iterator() { - return this.list.value().iterator(); - } - - @Override - public Object[] toArray() { - return this.list.value().toArray(); - } - - @Override - @SuppressWarnings("PMD.UseVarargs") - public Y[] toArray(final Y[] array) { - return this.list.value().toArray(array); - } - - @Override - public boolean add(final T element) { - throw new UnsupportedOperationException( - "#add(final T element) is not supported" - ); - } - - @Override - public boolean remove(final Object object) { - throw new UnsupportedOperationException( - "#remove(final Object object) is not supported" - ); - } - - @Override - public boolean containsAll(final Collection collection) { - return this.list.value().containsAll(collection); - } - - @Override - public boolean addAll(final Collection collection) { - throw new UnsupportedOperationException( - "#addAll(final Collection collection) is not supported" - ); - } - - @Override - public boolean addAll(final int index, - final Collection collection) { - throw new UnsupportedOperationException( - "#addAll() is not supported" - ); - } - - @Override - public boolean removeAll(final Collection collection) { - throw new UnsupportedOperationException( - "#removeAll(final Collection collection) is not supported" - ); - } - - @Override - public boolean retainAll(final Collection collection) { - throw new UnsupportedOperationException( - "#retainAll(final Collection collection) is not supported" - ); - } - - @Override - public void clear() { - throw new UnsupportedOperationException( - "#clear() is not supported" - ); - } - - @Override - public T get(final int index) { - return this.list.value().get(index); - } - - @Override - public T set(final int index, final T element) { - throw new UnsupportedOperationException( - "#set() is not supported" - ); - } - - @Override - public void add(final int index, final T element) { - throw new UnsupportedOperationException( - "#add(final int index, final T element) is not supported" - ); - } - - @Override - public T remove(final int index) { - throw new UnsupportedOperationException( - "#remove(final int index) is not supported" - ); - } - - @Override - public int indexOf(final Object object) { - return this.list.value().indexOf(object); - } - - @Override - public int lastIndexOf(final Object object) { - return this.list.value().lastIndexOf(object); - } - - @Override - public ListIterator listIterator() { - return this.list.value().listIterator(); - } - - @Override - public ListIterator listIterator(final int index) { - return this.list.value().listIterator(index); - } - - @Override - public List subList(final int from, final int end) { - return this.list.value().subList(from, end); + return Collections.unmodifiableList(temp); + }); } } diff --git a/src/main/java/org/cactoos/list/Mapped.java b/src/main/java/org/cactoos/list/Mapped.java index 7ca1f30f00..8779b16a6f 100644 --- a/src/main/java/org/cactoos/list/Mapped.java +++ b/src/main/java/org/cactoos/list/Mapped.java @@ -25,10 +25,7 @@ import java.util.Collection; import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; import org.cactoos.Func; -import org.cactoos.func.UncheckedFunc; /** * Mapped list. @@ -41,18 +38,7 @@ * @param Type of target item * @since 0.14 */ -@SuppressWarnings("PMD.TooManyMethods") -public final class Mapped implements List { - - /** - * Original list. - */ - private final List list; - - /** - * Function. - */ - private final Func func; +public final class Mapped extends ListEnvelope { /** * Ctor. @@ -60,8 +46,8 @@ public final class Mapped implements List { * @param fnc Func * @since 0.21 */ - public Mapped(final Iterator src, final Func fnc) { - this(new ListOf<>(src), fnc); + public Mapped(final Func fnc, final Iterator src) { + this(fnc, new ListOf<>(src)); } /** @@ -69,8 +55,8 @@ public Mapped(final Iterator src, final Func fnc) { * @param src Source list * @param fnc Func */ - public Mapped(final Iterable src, final Func fnc) { - this(new ListOf<>(src), fnc); + public Mapped(final Func fnc, final Iterable src) { + this(fnc, new ListOf<>(src)); } /** @@ -78,137 +64,10 @@ public Mapped(final Iterable src, final Func fnc) { * @param src Source list * @param fnc Func */ - public Mapped(final Collection src, final Func fnc) { - this(new ListOf<>(src), fnc); - } - - /** - * Ctor. - * @param src Source list - * @param fnc Func - */ - public Mapped(final List src, final Func fnc) { - this.list = src; - this.func = fnc; - } - - @Override - public int size() { - return this.list.size(); - } - - @Override - public boolean isEmpty() { - return this.list.isEmpty(); - } - - @Override - public boolean contains(final Object object) { - throw new UnsupportedOperationException("#contains()"); - } - - @Override - public Iterator iterator() { - return new org.cactoos.iterator.Mapped<>( - this.func, this.list.iterator() - ); - } - - @Override - public Object[] toArray() { - throw new UnsupportedOperationException("#toArray()"); - } - - @Override - @SuppressWarnings("PMD.UseVarargs") - public T[] toArray(final T[] array) { - throw new UnsupportedOperationException("#toArray(array)"); - } - - @Override - public boolean add(final Y item) { - throw new UnsupportedOperationException("#add()"); - } - - @Override - public boolean remove(final Object item) { - throw new UnsupportedOperationException("#remove()"); - } - - @Override - public boolean containsAll(final Collection items) { - throw new UnsupportedOperationException("#containsAll()"); - } - - @Override - public boolean addAll(final Collection items) { - throw new UnsupportedOperationException("#addAll()"); - } - - @Override - public boolean addAll(final int index, - final Collection items) { - throw new UnsupportedOperationException("#addAll(index)"); - } - - @Override - public boolean removeAll(final Collection items) { - throw new UnsupportedOperationException("#removeAll()"); - } - - @Override - public boolean retainAll(final Collection items) { - throw new UnsupportedOperationException("#retainAll()"); - } - - @Override - public void clear() { - throw new UnsupportedOperationException("#clear()"); - } - - @Override - public Y get(final int index) { - return new UncheckedFunc<>(this.func).apply(this.list.get(index)); - } - - @Override - public Y set(final int index, final Y element) { - throw new UnsupportedOperationException("#set()"); - } - - @Override - public void add(final int index, final Y element) { - throw new UnsupportedOperationException("#add(index)"); - } - - @Override - public Y remove(final int index) { - throw new UnsupportedOperationException("#remove(index)"); - } - - @Override - public int indexOf(final Object item) { - throw new UnsupportedOperationException("#indexOf()"); - } - - @Override - public int lastIndexOf(final Object item) { - throw new UnsupportedOperationException("#lastIndexOf()"); - } - - @Override - public ListIterator listIterator() { - throw new UnsupportedOperationException("#listIterator()"); - } - - @Override - public ListIterator listIterator(final int index) { - throw new UnsupportedOperationException("#listIterator(index)"); - } - - @Override - public List subList(final int start, final int end) { - return new Mapped<>(this.list.subList(start, end), this.func); + public Mapped(final Func fnc, final Collection src) { + super(() -> new ListOf( + new org.cactoos.collection.Mapped<>(fnc, src) + )); } } diff --git a/src/main/java/org/cactoos/list/Shuffled.java b/src/main/java/org/cactoos/list/Shuffled.java index 4527869b10..4045d9c096 100644 --- a/src/main/java/org/cactoos/list/Shuffled.java +++ b/src/main/java/org/cactoos/list/Shuffled.java @@ -28,8 +28,6 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.ListIterator; -import org.cactoos.scalar.UncheckedScalar; /** * Shuffled list. @@ -45,16 +43,10 @@ * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Element type - * @since 0.23 * @see StickyList + * @since 0.23 */ -@SuppressWarnings("PMD.TooManyMethods") -public final class Shuffled implements List { - - /** - * Sorted one. - */ - private final UncheckedScalar> scalar; +public final class Shuffled extends ListEnvelope { /** * Ctor. @@ -87,129 +79,12 @@ public Shuffled(final Iterable src) { * @param src Source */ public Shuffled(final Collection src) { - this.scalar = new UncheckedScalar<>( - () -> { - final List items = new LinkedList<>(); - items.addAll(src); - Collections.shuffle(items); - return Collections.unmodifiableList(items); - } - ); - } - - @Override - public int size() { - return this.scalar.value().size(); - } - - @Override - public boolean isEmpty() { - return this.scalar.value().isEmpty(); - } - - @Override - public boolean contains(final Object item) { - return this.scalar.value().contains(item); - } - - @Override - public Iterator iterator() { - return this.scalar.value().iterator(); - } - - @Override - public Object[] toArray() { - return this.scalar.value().toArray(); - } - - @Override - @SuppressWarnings("PMD.UseVarargs") - public X[] toArray(final X[] array) { - return this.scalar.value().toArray(array); - } - - @Override - public boolean add(final T item) { - throw new UnsupportedOperationException("#add()"); - } - - @Override - public boolean remove(final Object item) { - throw new UnsupportedOperationException("#remove()"); - } - - @Override - public boolean containsAll(final Collection list) { - return this.scalar.value().containsAll(list); - } - - @Override - public boolean addAll(final Collection list) { - throw new UnsupportedOperationException("#addAll()"); + super(() -> { + final List items = new LinkedList<>(); + items.addAll(src); + Collections.shuffle(items); + return Collections.unmodifiableList(items); + }); } - @Override - public boolean addAll(final int index, final Collection item) { - throw new UnsupportedOperationException("#addAll(index)"); - } - - @Override - public boolean removeAll(final Collection list) { - throw new UnsupportedOperationException("#removeAll()"); - } - - @Override - public boolean retainAll(final Collection list) { - throw new UnsupportedOperationException("#retainAll()"); - } - - @Override - public void clear() { - throw new UnsupportedOperationException("#clear()"); - } - - @Override - public T get(final int index) { - return this.scalar.value().get(index); - } - - @Override - public T set(final int index, final T element) { - throw new UnsupportedOperationException("#set()"); - } - - @Override - public void add(final int index, final T element) { - throw new UnsupportedOperationException("#add(index)"); - } - - @Override - public T remove(final int index) { - throw new UnsupportedOperationException("#remove(index)"); - } - - @Override - public int indexOf(final Object item) { - return this.scalar.value().indexOf(item); - } - - @Override - public int lastIndexOf(final Object item) { - return this.scalar.value().lastIndexOf(item); - } - - @Override - public ListIterator listIterator() { - return this.scalar.value().listIterator(); - } - - @Override - public ListIterator listIterator(final int index) { - return this.scalar.value().listIterator(index); - } - - @Override - public List subList(final int start, final int end) { - return this.scalar.value().subList(start, end); - } } diff --git a/src/main/java/org/cactoos/list/Sorted.java b/src/main/java/org/cactoos/list/Sorted.java index 70413e1764..8e1f7591ab 100644 --- a/src/main/java/org/cactoos/list/Sorted.java +++ b/src/main/java/org/cactoos/list/Sorted.java @@ -29,8 +29,6 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.ListIterator; -import org.cactoos.scalar.UncheckedScalar; /** * Sorted list. @@ -46,16 +44,10 @@ * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Element type - * @since 0.19 * @see StickyList + * @since 0.19 */ -@SuppressWarnings("PMD.TooManyMethods") -public final class Sorted implements List { - - /** - * Sorted one. - */ - private final UncheckedScalar> scalar; +public final class Sorted extends ListEnvelope { /** * Ctor. @@ -110,129 +102,13 @@ public Sorted(final Comparator cmp, final T... src) { * @param cmp The comparator */ public Sorted(final Comparator cmp, final Collection src) { - this.scalar = new UncheckedScalar<>( - () -> { - final List items = new LinkedList<>(); - items.addAll(src); - items.sort(cmp); - return Collections.unmodifiableList(items); - } - ); - } - - @Override - public int size() { - return this.scalar.value().size(); - } - - @Override - public boolean isEmpty() { - return this.scalar.value().isEmpty(); - } - - @Override - public boolean contains(final Object item) { - return this.scalar.value().contains(item); - } - - @Override - public Iterator iterator() { - return this.scalar.value().iterator(); - } - - @Override - public Object[] toArray() { - return this.scalar.value().toArray(); - } - - @Override - @SuppressWarnings("PMD.UseVarargs") - public X[] toArray(final X[] array) { - return this.scalar.value().toArray(array); - } - - @Override - public boolean add(final T item) { - throw new UnsupportedOperationException("#add()"); - } - - @Override - public boolean remove(final Object item) { - throw new UnsupportedOperationException("#remove()"); - } - - @Override - public boolean containsAll(final Collection list) { - return this.scalar.value().containsAll(list); - } - - @Override - public boolean addAll(final Collection list) { - throw new UnsupportedOperationException("#addAll()"); + super(() -> { + final List items = new LinkedList<>(); + items.addAll(src); + items.sort(cmp); + return Collections.unmodifiableList(items); + } +); } - @Override - public boolean addAll(final int index, final Collection item) { - throw new UnsupportedOperationException("#addAll(index)"); - } - - @Override - public boolean removeAll(final Collection list) { - throw new UnsupportedOperationException("#removeAll()"); - } - - @Override - public boolean retainAll(final Collection list) { - throw new UnsupportedOperationException("#retainAll()"); - } - - @Override - public void clear() { - throw new UnsupportedOperationException("#clear()"); - } - - @Override - public T get(final int index) { - return this.scalar.value().get(index); - } - - @Override - public T set(final int index, final T element) { - throw new UnsupportedOperationException("#set()"); - } - - @Override - public void add(final int index, final T element) { - throw new UnsupportedOperationException("#add(index)"); - } - - @Override - public T remove(final int index) { - throw new UnsupportedOperationException("#remove(index)"); - } - - @Override - public int indexOf(final Object item) { - return this.scalar.value().indexOf(item); - } - - @Override - public int lastIndexOf(final Object item) { - return this.scalar.value().lastIndexOf(item); - } - - @Override - public ListIterator listIterator() { - return this.scalar.value().listIterator(); - } - - @Override - public ListIterator listIterator(final int index) { - return this.scalar.value().listIterator(index); - } - - @Override - public List subList(final int start, final int end) { - return this.scalar.value().subList(start, end); - } } diff --git a/src/test/java/org/cactoos/list/BehavesAsList.java b/src/test/java/org/cactoos/list/BehavesAsList.java new file mode 100644 index 0000000000..d296d7c983 --- /dev/null +++ b/src/test/java/org/cactoos/list/BehavesAsList.java @@ -0,0 +1,71 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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.List; +import org.cactoos.collection.BehavesAsCollection; +import org.hamcrest.Description; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.hamcrest.TypeSafeMatcher; + +/** + * Matcher for collection. + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of source item + * @since 0.23 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class BehavesAsList extends TypeSafeMatcher> { + + /** + * Sample item. + */ + private final E sample; + + /** + * Ctor. + * @param item Sample item + */ + public BehavesAsList(final E item) { + super(); + this.sample = item; + } + + @Override + public boolean matchesSafely(final List list) { + MatcherAssert.assertThat(list.get(0), Matchers.notNullValue()); + MatcherAssert.assertThat( + list.indexOf(this.sample), + Matchers.greaterThanOrEqualTo(0) + ); + return new BehavesAsCollection(this.sample).matchesSafely(list); + } + + @Override + public void describeTo(final Description desc) { + desc.appendText("not a valid list"); + } +} diff --git a/src/test/java/org/cactoos/list/JoinedTest.java b/src/test/java/org/cactoos/list/JoinedTest.java index 29250a059e..b427743f5e 100644 --- a/src/test/java/org/cactoos/list/JoinedTest.java +++ b/src/test/java/org/cactoos/list/JoinedTest.java @@ -39,6 +39,18 @@ @SuppressWarnings("PMD.TooManyMethods") public final class JoinedTest { + @Test + public void behavesAsCollection() throws Exception { + MatcherAssert.assertThat( + "Can't behave as a list", + new Joined( + new ListOf<>(1, 2), + new ListOf<>(3, 4) + ), + new BehavesAsList<>(2) + ); + } + @Test public void size() throws Exception { MatcherAssert.assertThat( @@ -54,7 +66,7 @@ public void size() throws Exception { public void isEmpty() throws Exception { MatcherAssert.assertThat( new Joined( - new ListOf<>(5, 6) + new ListOf(5, 6) ).isEmpty(), Matchers.equalTo(false) ); @@ -97,12 +109,12 @@ public void toArray() throws Exception { @Test(expected = UnsupportedOperationException.class) public void add() throws Exception { - new Joined(new ListOf<>("add0")).add("new add0"); + new Joined(new ListOf("add0")).add("new add0"); } @Test(expected = UnsupportedOperationException.class) public void remove() throws Exception { - new Joined(new ListOf<>("remove")).remove("new remove"); + new Joined(new ListOf("remove")).remove("new remove"); } @Test @@ -121,21 +133,21 @@ public void containsAll() throws Exception { @Test(expected = UnsupportedOperationException.class) public void addAll() throws Exception { new Joined( - new ListOf<>("addAll") + new ListOf("addAll") ).addAll(new ListOf<>("new addAll")); } @Test(expected = UnsupportedOperationException.class) public void addAllSecond() throws Exception { new Joined( - new ListOf<>("addAll1") + new ListOf("addAll1") ).addAll(22, new ListOf<>("new addAll1")); } @Test(expected = UnsupportedOperationException.class) public void removeAll() throws Exception { new Joined( - new ListOf<>("removeAll") + new ListOf("removeAll") ).removeAll(new ListOf<>("new removeAll")); } @@ -146,7 +158,7 @@ public void retainAll() throws Exception { @Test(expected = UnsupportedOperationException.class) public void clear() throws Exception { - new Joined(new ListOf<>("clear")).clear(); + new Joined(new ListOf("clear")).clear(); } @Test @@ -163,12 +175,12 @@ public void get() throws Exception { @Test(expected = UnsupportedOperationException.class) public void set() throws Exception { - new Joined(new ListOf<>("set")).set(33, "new set"); + new Joined(new ListOf("set")).set(33, "new set"); } @Test(expected = UnsupportedOperationException.class) public void addSecond() throws Exception { - new Joined(new ListOf<>("add")).add(44, "new add"); + new Joined(new ListOf("add")).add(44, "new add"); } @Test(expected = UnsupportedOperationException.class) @@ -176,22 +188,7 @@ public void removeSecond() throws Exception { new Joined().remove(55); } - @Test(expected = UnsupportedOperationException.class) - public void indexOf() throws Exception { - new Joined().indexOf("indexOf"); - } - - @Test(expected = UnsupportedOperationException.class) - public void lastIndexOf() throws Exception { - new Joined().lastIndexOf("lastIndexOf"); - } - - @Test(expected = UnsupportedOperationException.class) - public void listIterator() throws Exception { - new Joined().listIterator(); - } - - @Test(expected = UnsupportedOperationException.class) + @Test(expected = IndexOutOfBoundsException.class) public void listIteratorSecond() throws Exception { new Joined().listIterator(66); } diff --git a/src/test/java/org/cactoos/list/ListOfTest.java b/src/test/java/org/cactoos/list/ListOfTest.java index 1cb6ed4c69..7a792a3c9d 100644 --- a/src/test/java/org/cactoos/list/ListOfTest.java +++ b/src/test/java/org/cactoos/list/ListOfTest.java @@ -40,6 +40,15 @@ */ public final class ListOfTest { + @Test + public void behavesAsCollection() throws Exception { + MatcherAssert.assertThat( + "Can't behave as a list", + new ListOf<>(1, 2), + new BehavesAsList<>(2) + ); + } + @Test public void elementAtIndexTest() throws Exception { final int num = 345; diff --git a/src/test/java/org/cactoos/list/MappedTest.java b/src/test/java/org/cactoos/list/MappedTest.java index a782353bd6..1afd8fedba 100644 --- a/src/test/java/org/cactoos/list/MappedTest.java +++ b/src/test/java/org/cactoos/list/MappedTest.java @@ -42,15 +42,25 @@ */ public final class MappedTest { + @Test + public void behavesAsCollection() throws Exception { + MatcherAssert.assertThat( + "Can't behave as a list", + new Mapped( + i -> i + 1, + new IterableOf<>(-1, 1, 1) + ), + new BehavesAsList<>(0) + ); + } + @Test public void transformsList() throws IOException { MatcherAssert.assertThat( "Can't transform an iterable", new Mapped( - new IterableOf<>( - "hello", "world", "друг" - ), - input -> new UpperText(new TextOf(input)) + input -> new UpperText(new TextOf(input)), + new IterableOf<>("hello", "world", "друг") ).iterator().next().asString(), Matchers.equalTo("HELLO") ); @@ -61,8 +71,8 @@ public void transformsEmptyList() { MatcherAssert.assertThat( "Can't transform an empty iterable", new Mapped( - Collections.emptyList(), - input -> new UpperText(new TextOf(input)) + input -> new UpperText(new TextOf(input)), + Collections.emptyList() ), Matchers.emptyIterable() ); diff --git a/src/test/java/org/cactoos/list/ShuffledTest.java b/src/test/java/org/cactoos/list/ShuffledTest.java index 8af93821dd..5f5fb7a9cc 100644 --- a/src/test/java/org/cactoos/list/ShuffledTest.java +++ b/src/test/java/org/cactoos/list/ShuffledTest.java @@ -37,6 +37,17 @@ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class ShuffledTest { + @Test + public void behavesAsCollection() throws Exception { + MatcherAssert.assertThat( + "Can't behave as a list", + new Shuffled<>( + new ListOf<>(1, 0, -1, -1, 2) + ), + new BehavesAsList<>(0) + ); + } + @Test public void shufflesList() throws Exception { MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/list/SortedTest.java b/src/test/java/org/cactoos/list/SortedTest.java index 6450c30bbd..561c2854e5 100644 --- a/src/test/java/org/cactoos/list/SortedTest.java +++ b/src/test/java/org/cactoos/list/SortedTest.java @@ -38,6 +38,17 @@ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class SortedTest { + @Test + public void behavesAsCollection() throws Exception { + MatcherAssert.assertThat( + "Can't behave as a list", + new Sorted<>( + new ListOf<>(1, 0, -1, -1, 2) + ), + new BehavesAsList<>(0) + ); + } + @Test public void sortsCollection() throws Exception { MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/list/StickyListTest.java b/src/test/java/org/cactoos/list/StickyListTest.java index 5fd45d005d..39b5f6a2f3 100644 --- a/src/test/java/org/cactoos/list/StickyListTest.java +++ b/src/test/java/org/cactoos/list/StickyListTest.java @@ -43,11 +43,20 @@ @SuppressWarnings("PMD.TooManyMethods") public final class StickyListTest { + @Test + public void behavesAsCollection() throws Exception { + MatcherAssert.assertThat( + "Can't behave as a list", + new StickyList<>(1, 0, -1, -1, 2), + new BehavesAsList<>(0) + ); + } + @Test public void ignoresChangesInIterable() throws Exception { final AtomicInteger size = new AtomicInteger(2); final List list = new StickyList<>( - new ListOf<>( + new ListOf( () -> Collections.nCopies(size.incrementAndGet(), 0).iterator() ) );