From 0d5e5c4f4d4dd1815862743b62c96242946ab98c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sun, 9 Feb 2020 19:29:41 +0100 Subject: [PATCH] (#1242) ListOf is now a concrete in-memory List implementation --- .../java/org/cactoos/collection/Filtered.java | 5 + .../java/org/cactoos/collection/HeadOf.java | 5 + .../java/org/cactoos/collection/Joined.java | 5 + .../java/org/cactoos/collection/Mapped.java | 5 + .../java/org/cactoos/collection/Reversed.java | 5 + .../java/org/cactoos/collection/Shuffled.java | 5 + .../java/org/cactoos/collection/Skipped.java | 5 + .../java/org/cactoos/collection/Sliced.java | 5 + .../java/org/cactoos/collection/Solid.java | 4 + .../java/org/cactoos/collection/Sorted.java | 5 + .../java/org/cactoos/collection/Sticky.java | 4 + .../java/org/cactoos/collection/Synced.java | 4 + .../java/org/cactoos/collection/TailOf.java | 4 + .../org/cactoos/collection/package-info.java | 15 -- src/main/java/org/cactoos/list/ListOf.java | 167 +-------------- src/main/java/org/cactoos/list/Shuffled.java | 31 +-- src/main/java/org/cactoos/list/Solid.java | 60 ------ src/main/java/org/cactoos/list/Sorted.java | 48 +---- src/main/java/org/cactoos/list/Sticky.java | 67 ------ src/main/java/org/cactoos/list/Synced.java | 38 +--- .../java/org/cactoos/list/package-info.java | 5 +- .../java/org/cactoos/set/package-info.java | 3 + .../cactoos/func/ForEachInThreadsTest.java | 8 +- .../org/cactoos/iterator/PartitionedTest.java | 16 +- .../java/org/cactoos/list/NoNullsTest.java | 42 ---- src/test/java/org/cactoos/list/SolidTest.java | 103 ---------- .../java/org/cactoos/list/SortedTest.java | 2 +- .../java/org/cactoos/list/StickyTest.java | 191 ------------------ .../java/org/cactoos/list/SyncedTest.java | 5 +- .../org/cactoos/scalar/AndInThreadsTest.java | 13 +- 30 files changed, 96 insertions(+), 779 deletions(-) delete mode 100644 src/main/java/org/cactoos/list/Solid.java delete mode 100644 src/main/java/org/cactoos/list/Sticky.java delete mode 100644 src/test/java/org/cactoos/list/SolidTest.java delete mode 100644 src/test/java/org/cactoos/list/StickyTest.java diff --git a/src/main/java/org/cactoos/collection/Filtered.java b/src/main/java/org/cactoos/collection/Filtered.java index 49784d6d3f..1e4d500d7e 100644 --- a/src/main/java/org/cactoos/collection/Filtered.java +++ b/src/main/java/org/cactoos/collection/Filtered.java @@ -33,6 +33,11 @@ * @param Type of source item * @since 1.16 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Filtered from iterable + * (composed with ListOf or SetOf in case a copy is needed) + * or any other relevant concrete collection implementation. + * See #1242 for the rationale about this. */ public final class Filtered extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/HeadOf.java b/src/main/java/org/cactoos/collection/HeadOf.java index 45b263739c..24cf8bb22d 100644 --- a/src/main/java/org/cactoos/collection/HeadOf.java +++ b/src/main/java/org/cactoos/collection/HeadOf.java @@ -32,6 +32,11 @@ * * @param Type of source item * @since 0.29 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of HeadOf from iterable + * (composed with ListOf or SetOf in case a copy is needed) + * or any other relevant concrete collection implementation. + * See #1242 for the rationale about this. */ public final class HeadOf extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Joined.java b/src/main/java/org/cactoos/collection/Joined.java index 7ea4193abe..f54580eb8a 100644 --- a/src/main/java/org/cactoos/collection/Joined.java +++ b/src/main/java/org/cactoos/collection/Joined.java @@ -32,6 +32,11 @@ * * @param Type of source item * @since 1.16 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Joined from iterable + * (composed with ListOf or SetOf in case a copy is needed) + * or any other relevant concrete collection implementation. + * See #1242 for the rationale about this. */ public final class Joined extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Mapped.java b/src/main/java/org/cactoos/collection/Mapped.java index b8dd7cba6a..446519cce0 100644 --- a/src/main/java/org/cactoos/collection/Mapped.java +++ b/src/main/java/org/cactoos/collection/Mapped.java @@ -34,6 +34,11 @@ * @param Type of source item * @param Type of target item * @since 0.14 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Mapped from iterable + * (composed with ListOf or SetOf in case a copy is needed) + * or any other relevant concrete collection implementation. + * See #1242 for the rationale about this. */ public final class Mapped extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Reversed.java b/src/main/java/org/cactoos/collection/Reversed.java index bede6b95de..745f09ffc7 100644 --- a/src/main/java/org/cactoos/collection/Reversed.java +++ b/src/main/java/org/cactoos/collection/Reversed.java @@ -36,6 +36,11 @@ * * @param Type of source item * @since 1.16 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Reversed from iterable + * (composed with ListOf or SetOf in case a copy is needed) + * or any other relevant concrete collection implementation. + * See #1242 for the rationale about this. */ public final class Reversed extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Shuffled.java b/src/main/java/org/cactoos/collection/Shuffled.java index 3ad88b57ea..f4678cd9fe 100644 --- a/src/main/java/org/cactoos/collection/Shuffled.java +++ b/src/main/java/org/cactoos/collection/Shuffled.java @@ -36,6 +36,11 @@ * * @param Element type * @since 0.23 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Shuffled from iterable + * (composed with ListOf or SetOf in case a copy is needed) + * or any other relevant concrete collection implementation. + * See #1242 for the rationale about this. */ public final class Shuffled extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Skipped.java b/src/main/java/org/cactoos/collection/Skipped.java index c6390c8f30..c38439c65c 100644 --- a/src/main/java/org/cactoos/collection/Skipped.java +++ b/src/main/java/org/cactoos/collection/Skipped.java @@ -32,6 +32,11 @@ * * @param Type of source item * @since 0.34 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Skipped from iterable + * (composed with ListOf or SetOf in case a copy is needed) + * or any other relevant concrete collection implementation. + * See #1242 for the rationale about this. */ public final class Skipped extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Sliced.java b/src/main/java/org/cactoos/collection/Sliced.java index 22b3a7000f..0e405ecbbc 100644 --- a/src/main/java/org/cactoos/collection/Sliced.java +++ b/src/main/java/org/cactoos/collection/Sliced.java @@ -32,6 +32,11 @@ * * @param Element type * @since 1.0.0 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Sliced from iterable + * (composed with ListOf or SetOf in case a copy is needed) + * or any other relevant concrete collection implementation. + * See #1242 for the rationale about this. */ public final class Sliced extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Solid.java b/src/main/java/org/cactoos/collection/Solid.java index e830c25076..a88e75ab31 100644 --- a/src/main/java/org/cactoos/collection/Solid.java +++ b/src/main/java/org/cactoos/collection/Solid.java @@ -38,6 +38,10 @@ * @param List type * @see Sticky * @since 0.24 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Synced from list, + * set, iterable, iterator or any other relevant concrete + * collection implementation. See #1242 for the rationale about this. */ public final class Solid extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Sorted.java b/src/main/java/org/cactoos/collection/Sorted.java index 19ab1a3ffb..9ed6757898 100644 --- a/src/main/java/org/cactoos/collection/Sorted.java +++ b/src/main/java/org/cactoos/collection/Sorted.java @@ -37,6 +37,11 @@ * * @param Element type * @since 0.19 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Sorted from iterable + * (composed with ListOf or SetOf in case a copy is needed) + * or any other relevant concrete collection implementation. + * See #1242 for the rationale about this. */ public final class Sorted extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Sticky.java b/src/main/java/org/cactoos/collection/Sticky.java index a0fe4ad968..c7e39fd0cd 100644 --- a/src/main/java/org/cactoos/collection/Sticky.java +++ b/src/main/java/org/cactoos/collection/Sticky.java @@ -35,6 +35,10 @@ * * @param Type of item * @since 0.16 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of ListOf, SetOf or + * any other collection concrete implementation. See #1242 + * for the rationale about this. */ public final class Sticky extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/Synced.java b/src/main/java/org/cactoos/collection/Synced.java index 98c58fb9c8..3c0edf8263 100644 --- a/src/main/java/org/cactoos/collection/Synced.java +++ b/src/main/java/org/cactoos/collection/Synced.java @@ -44,6 +44,10 @@ * @param List type * @see Sticky * @since 0.24 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of Synced from list, + * set, iterable, iterator or any other relevant concrete + * collection implementation. See #1242 for the rationale about this. */ public final class Synced extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/TailOf.java b/src/main/java/org/cactoos/collection/TailOf.java index 5de6a426a8..f5e5455c01 100644 --- a/src/main/java/org/cactoos/collection/TailOf.java +++ b/src/main/java/org/cactoos/collection/TailOf.java @@ -32,6 +32,10 @@ * * @param Element type * @since 0.31 + * @todo #1242:30min Remove this class and replace it everywhere + * it was needed by the appropriate usage of TailOf from list, + * set, iterable, iterator or any other relevant concrete + * collection implementation. See #1242 for the rationale about this. */ public final class TailOf extends CollectionEnvelope { diff --git a/src/main/java/org/cactoos/collection/package-info.java b/src/main/java/org/cactoos/collection/package-info.java index cde5b6e943..6f324687fa 100644 --- a/src/main/java/org/cactoos/collection/package-info.java +++ b/src/main/java/org/cactoos/collection/package-info.java @@ -26,20 +26,5 @@ * Collections, tests. * * @since 0.14 - * @todo #1184:30min The behaviours of the classes of this package - * are akward because CollectionOf is based on a Scalar while some - * of the tests of the other classes are expecting mutable collections, - * except for Sticky itself. - * This resulted in the use of Sticky in most of the classes of this package - * during the resolution of #1184 to preserve the actual behaviour. Some - * tests of Sticky were also ignored because of this. - * It is as if CollectionOf was once made to be immutable but - * now there is Immutable for that: ask ARC what direction to take and - * apply it to make this whole package consistent. Keep in mind that - * Collection is particular in the way that it is an Iterable and that - * there are no direct implementation of Collection in Java, but only - * sub-interfaces (Set, List, etc) with their own implementation. - * Also don't forget to unignore or modify the tests of Sticky and improve - * the other tests to be clear about the expected behaviour. */ package org.cactoos.collection; diff --git a/src/main/java/org/cactoos/list/ListOf.java b/src/main/java/org/cactoos/list/ListOf.java index 1c0735a4be..21c9e487e6 100644 --- a/src/main/java/org/cactoos/list/ListOf.java +++ b/src/main/java/org/cactoos/list/ListOf.java @@ -23,38 +23,20 @@ */ 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.Scalar; import org.cactoos.iterable.IterableOf; -import org.cactoos.scalar.Unchecked; /** - * Iterable as {@link List}. - * - *

This class should be used very carefully. You must understand that - * it will fetch the entire content of the encapsulated {@link List} on each - * method call. It doesn't cache the data anyhow. If you don't - * need this {@link List} to re-fresh its content on every call, - * by doing round-trips to the encapsulated iterable, decorate it with - * {@link Sticky}.

+ * Implementation of {@link List}. * *

There is no thread-safety guarantee. * * @param List type - * @see Sticky * @since 0.1 */ -public final class ListOf implements List { - /** - * List. - */ - private final Unchecked> list; - +public final class ListOf extends ListEnvelope { /** * Ctor. * @@ -79,148 +61,7 @@ public ListOf(final Iterator src) { * @param src An {@link Iterable} */ public ListOf(final Iterable src) { - this(() -> { - final List temp = new LinkedList<>(); - src.forEach(temp::add); - return Collections.unmodifiableList(temp); - }); - } - - /** - * Ctor. - * @param slr The scalar - */ - public ListOf(final Scalar> slr) { - this.list = new Unchecked<>(slr); - } - - @Override - public Iterator iterator() { - return this.list.value().iterator(); - } - - @Override - public boolean equals(final Object other) { - return this.list.value().equals(other); - } - - @Override - public int hashCode() { - return this.list.value().hashCode(); - } - - @Override - public String toString() { - return this.list.value().toString(); - } - - @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 Object[] toArray() { - return this.list.value().toArray(); - } - - @Override - public X[] toArray(final X[] array) { - return this.list.value().toArray(array); - } - - @Override - public boolean add(final T item) { - return this.list.value().add(item); - } - - @Override - public boolean remove(final Object object) { - return this.list.value().remove(object); - } - - @Override - public boolean containsAll(final Collection col) { - return this.list.value().containsAll(col); - } - - @Override - public boolean addAll(final Collection col) { - return this.list.value().addAll(col); - } - - @Override - public boolean removeAll(final Collection col) { - return this.list.value().removeAll(col); - } - - @Override - public boolean retainAll(final Collection col) { - return this.list.value().retainAll(col); - } - - @Override - public void clear() { - this.list.value().clear(); - } - - @Override - public boolean addAll(final int index, final Collection col) { - return this.list.value().addAll(index, col); - } - - @Override - public T get(final int index) { - return this.list.value().get(index); - } - - @Override - public T set(final int index, final T element) { - return this.list.value().set(index, element); - } - - @Override - public void add(final int index, final T element) { - this.list.value().add(index, element); - } - - @Override - public T remove(final int index) { - return this.list.value().remove(index); - } - - @Override - public int indexOf(final Object element) { - return this.list.value().indexOf(element); - } - - @Override - public int lastIndexOf(final Object element) { - return this.list.value().lastIndexOf(element); - } - - @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 start, final int end) { - return this.list.value().subList(start, end); + super(new LinkedList<>()); + src.forEach(super::add); } } diff --git a/src/main/java/org/cactoos/list/Shuffled.java b/src/main/java/org/cactoos/list/Shuffled.java index 722c6335c4..e7cef45eab 100644 --- a/src/main/java/org/cactoos/list/Shuffled.java +++ b/src/main/java/org/cactoos/list/Shuffled.java @@ -24,51 +24,24 @@ package org.cactoos.list; import java.util.Collections; -import java.util.LinkedList; import java.util.List; -import org.cactoos.iterable.IterableOf; /** * Shuffled list. * - *

Pay attention that shuffling will happen on each operation - * with the collection. Every time you touch it, it will fetch the - * entire list from the encapsulated object and sort it. If you - * want to avoid that "side-effect", decorate it with - * {@link Sticky}.

- * *

There is no thread-safety guarantee.

* * @param Element type - * @see Sticky * @since 0.23 */ public final class Shuffled extends ListEnvelope { - - /** - * Ctor. - * @param src The underlying collection - */ - @SafeVarargs - public Shuffled(final T... src) { - this(new IterableOf<>(src)); - } - /** * Ctor. * @param src Source */ public Shuffled(final Iterable src) { - super( - new ListOf<>( - () -> { - final List items = new LinkedList<>(); - src.forEach(items::add); - Collections.shuffle(items); - return Collections.unmodifiableList(items); - } - ) - ); + super(new ListOf<>(src)); + Collections.shuffle(this); } } diff --git a/src/main/java/org/cactoos/list/Solid.java b/src/main/java/org/cactoos/list/Solid.java deleted file mode 100644 index 1cf3abc6cf..0000000000 --- a/src/main/java/org/cactoos/list/Solid.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 org.cactoos.iterable.IterableOf; - -/** - * A {@link java.util.List} that is both synchronized and sticky. - * - *

Objects of this class are thread-safe.

- * - * @param Type of item - * @since 0.24 - */ -public final class Solid extends ListEnvelope { - - /** - * Ctor. - * @param items The array - */ - @SafeVarargs - public Solid(final X... items) { - this(new IterableOf<>(items)); - } - - /** - * Ctor. - * @param items The array - */ - public Solid(final Iterable items) { - super( - new ListOf<>( - new org.cactoos.scalar.Solid<>( - () -> new Synced<>(new Sticky<>(items)) - ) - ) - ); - } -} diff --git a/src/main/java/org/cactoos/list/Sorted.java b/src/main/java/org/cactoos/list/Sorted.java index aa606aa008..fa8c844fdd 100644 --- a/src/main/java/org/cactoos/list/Sorted.java +++ b/src/main/java/org/cactoos/list/Sorted.java @@ -23,38 +23,18 @@ */ package org.cactoos.list; -import java.util.Collections; import java.util.Comparator; -import java.util.LinkedList; import java.util.List; -import org.cactoos.iterable.IterableOf; /** * Sorted list. * - *

Pay attention that sorting will happen on each operation - * with the collection. Every time you touch it, it will fetch the - * entire list from the encapsulated object and sort it. If you - * want to avoid that "side-effect", decorate it with - * {@link Sticky}.

- * *

There is no thread-safety guarantee.

* * @param Element type - * @see Sticky * @since 0.19 */ public final class Sorted extends ListEnvelope { - - /** - * Ctor. - * @param src The underlying collection - */ - @SafeVarargs - public Sorted(final T... src) { - this(new IterableOf<>(src)); - } - /** * Ctor. * @@ -62,38 +42,20 @@ public Sorted(final T... src) { * implements {@link Comparable} interface. Otherwise, there will be * a type casting exception in runtime.

* - * @param src The underlying collection + * @param src The source collection */ @SuppressWarnings("unchecked") - public Sorted(final Iterable src) { + public Sorted(final List src) { this((Comparator) Comparator.naturalOrder(), src); } /** * Ctor. * @param cmp The comparator - * @param src The underlying collection - */ - @SafeVarargs - public Sorted(final Comparator cmp, final T... src) { - this(cmp, new IterableOf<>(src)); - } - - /** - * Ctor. - * @param cmp The comparator - * @param src The underlying collection + * @param src The source collection */ public Sorted(final Comparator cmp, final Iterable src) { - super( - new ListOf<>( - () -> { - final List items = new LinkedList<>(); - src.forEach(items::add); - items.sort(cmp); - return Collections.unmodifiableList(items); - } - ) - ); + super(new ListOf<>(src)); + super.sort(cmp); } } diff --git a/src/main/java/org/cactoos/list/Sticky.java b/src/main/java/org/cactoos/list/Sticky.java deleted file mode 100644 index fe5f2d0276..0000000000 --- a/src/main/java/org/cactoos/list/Sticky.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.LinkedList; -import java.util.List; -import org.cactoos.iterable.IterableOf; - -/** - * List decorator that goes through the list only once. - * - *

There is no thread-safety guarantee. - * - * @param Type of item - * @since 0.8 - */ -public final class Sticky extends ListEnvelope { - - /** - * Ctor. - * @param items The array - */ - @SafeVarargs - public Sticky(final X... items) { - this(new IterableOf<>(items)); - } - - /** - * Ctor. - * @param list The iterable - */ - public Sticky(final Iterable list) { - super( - new ListOf<>( - new org.cactoos.scalar.Sticky<>( - () -> { - final List temp = new LinkedList<>(); - list.forEach(temp::add); - return temp; - } - ) - ) - ); - } - -} diff --git a/src/main/java/org/cactoos/list/Synced.java b/src/main/java/org/cactoos/list/Synced.java index 324f37ebf7..c848372e1f 100644 --- a/src/main/java/org/cactoos/list/Synced.java +++ b/src/main/java/org/cactoos/list/Synced.java @@ -24,54 +24,22 @@ package org.cactoos.list; import java.util.Collections; -import java.util.LinkedList; import java.util.List; -import org.cactoos.iterable.IterableOf; /** * Synchronized list. * - *

This class should be used very carefully. You must understand that - * it will fetch the entire content of the encapsulated {@link Iterable} on each - * method call. It doesn't cache the data anyhow. If you don't - * need this {@link java.util.List} to re-fresh - * its content on every call, by doing round-trips to - * the encapsulated iterable, use {@link Sticky}.

- - *

The list is read only.

- * *

Objects of this class are thread-safe.

* * @param Type of item * @since 0.24 */ public final class Synced extends ListEnvelope { - /** * Ctor. - * @param items The array + * @param list The underlying list */ - @SafeVarargs - public Synced(final X... items) { - this(new IterableOf<>(items)); + public Synced(final List list) { + super(Collections.synchronizedList(list)); } - - /** - * Ctor. - * @param list The iterable - */ - public Synced(final Iterable list) { - super( - new ListOf<>( - new org.cactoos.scalar.Synced<>( - () -> { - final List temp = new LinkedList<>(); - list.forEach(temp::add); - return Collections.synchronizedList(temp); - } - ) - ) - ); - } - } diff --git a/src/main/java/org/cactoos/list/package-info.java b/src/main/java/org/cactoos/list/package-info.java index 80ace1e2b8..6aabd5927e 100644 --- a/src/main/java/org/cactoos/list/package-info.java +++ b/src/main/java/org/cactoos/list/package-info.java @@ -29,8 +29,7 @@ * @todo #1230:30min The following list implmenetation classes * {@link org.cactoos.list.Joined}, {@link org.cactoos.list.Mapped}, * {@link org.cactoos.list.NoNulls}, {@link org.cactoos.list.Shuffled}, - * {@link org.cactoos.list.Solid}, {@link org.cactoos.list.Solid}, - * {@link org.cactoos.list.Sorted}, {@link org.cactoos.list.Sticky}, - * {@link org.cactoos.list.Synced} should support mutability. + * {@link org.cactoos.list.Sorted}, {@link org.cactoos.list.Synced} + * should support mutability. */ package org.cactoos.list; diff --git a/src/main/java/org/cactoos/set/package-info.java b/src/main/java/org/cactoos/set/package-info.java index fdc4dfbbcc..9f502ee864 100644 --- a/src/main/java/org/cactoos/set/package-info.java +++ b/src/main/java/org/cactoos/set/package-info.java @@ -26,5 +26,8 @@ * Sets. * * @since 0.49.2 + * @todo #1242:30min The SetOf class should be implemented as an in-memory set, + * for example based on HashSet. See ListOf for an example and #1242 for the + * rationale behind this design. */ package org.cactoos.set; diff --git a/src/test/java/org/cactoos/func/ForEachInThreadsTest.java b/src/test/java/org/cactoos/func/ForEachInThreadsTest.java index fbabf42b29..62c22c8be1 100644 --- a/src/test/java/org/cactoos/func/ForEachInThreadsTest.java +++ b/src/test/java/org/cactoos/func/ForEachInThreadsTest.java @@ -23,10 +23,8 @@ */ package org.cactoos.func; -import java.util.ArrayList; import java.util.List; import org.cactoos.list.ListOf; -import org.cactoos.list.Sticky; import org.cactoos.list.Synced; import org.hamcrest.collection.IsIterableContainingInAnyOrder; import org.junit.Test; @@ -45,10 +43,8 @@ public class ForEachInThreadsTest { @Test @SuppressWarnings("unchecked") public void testProcIterable() throws Exception { - final List list = new Sticky<>( - new Synced<>( - new ArrayList<>(2) - ) + final List list = new Synced<>( + new ListOf<>() ); new ForEachInThreads( new ProcNoNulls<>( diff --git a/src/test/java/org/cactoos/iterator/PartitionedTest.java b/src/test/java/org/cactoos/iterator/PartitionedTest.java index 4e1889e3a7..338b586d40 100644 --- a/src/test/java/org/cactoos/iterator/PartitionedTest.java +++ b/src/test/java/org/cactoos/iterator/PartitionedTest.java @@ -31,7 +31,9 @@ import org.cactoos.scalar.LengthOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.hamcrest.core.IsEqual; import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; /** * Test case for {@link Partitioned}. @@ -93,20 +95,18 @@ public void partitionedEqualSize() { @Test @SuppressWarnings("unchecked") public void partitionedLastPartitionSmaller() { - MatcherAssert.assertThat( + new Assertion<>( "Can't generate a Partitioned of size 2 last partition smaller.", - new ArrayList<>( - new ListOf<>( - new Partitioned<>(2, new ListOf<>(1, 2, 3).iterator()) - ) + new ListOf<>( + new Partitioned<>(2, new ListOf<>(1, 2, 3).iterator()) ), - Matchers.equalTo( + new IsEqual<>( new ListOf<>( new ListOf<>(1, 2), - Collections.singletonList(3) + new ListOf<>(3) ) ) - ); + ).affirm(); } @Test(expected = IllegalArgumentException.class) diff --git a/src/test/java/org/cactoos/list/NoNullsTest.java b/src/test/java/org/cactoos/list/NoNullsTest.java index f5bade9ccb..10d309d74f 100644 --- a/src/test/java/org/cactoos/list/NoNullsTest.java +++ b/src/test/java/org/cactoos/list/NoNullsTest.java @@ -142,46 +142,4 @@ public void getThrowsErrorIfListIteratorPreviousValueIsNullValue() { ) ).affirm(); } - - @Test - public void addThrowsErrorForImmutableListIterator() { - new Assertion<>( - "must throw error if modified with add", - () -> { - new NoNulls<>(new ListOf<>(1, 2, 3)).listIterator().add(4); - return 0; - }, - new Throws<>( - UnsupportedOperationException.class - ) - ).affirm(); - } - - @Test - public void removeThrowsErrorForImmutableListIterator() { - new Assertion<>( - "must throw error if modified with remove", - () -> { - new NoNulls<>(new ListOf<>(1, 2, 3)).listIterator().remove(); - return 0; - }, - new Throws<>( - UnsupportedOperationException.class - ) - ).affirm(); - } - - @Test - public void setThrowsErrorForImmutableListIterator() { - new Assertion<>( - "must throw error if modified if set", - () -> { - new NoNulls<>(new ListOf<>(1, 2, 3)).listIterator().set(4); - return 0; - }, - new Throws<>( - UnsupportedOperationException.class - ) - ).affirm(); - } } diff --git a/src/test/java/org/cactoos/list/SolidTest.java b/src/test/java/org/cactoos/list/SolidTest.java deleted file mode 100644 index f0c093b861..0000000000 --- a/src/test/java/org/cactoos/list/SolidTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.Collections; -import java.util.List; -import org.cactoos.Scalar; -import org.cactoos.iterable.IterableOf; -import org.hamcrest.MatcherAssert; -import org.hamcrest.core.IsEqual; -import org.junit.Test; -import org.llorllale.cactoos.matchers.HasSize; -import org.llorllale.cactoos.matchers.RunsInThreads; - -/** - * Test case for {@link Solid}. - * @since 0.24 - * @checkstyle JavadocMethodCheck (500 lines) - * @checkstyle MagicNumber (500 lines) - */ -public final class SolidTest { - - @Test - public void behavesAsCollection() throws Exception { - MatcherAssert.assertThat( - "Can't behave as a list", - new Solid<>(1, 0, -1, -1, 2), - new BehavesAsList<>(0) - ); - } - - @Test - public void worksInThreads() { - MatcherAssert.assertThat( - list -> !list.iterator().hasNext(), - new RunsInThreads<>(new Solid<>(Collections.emptyList())) - ); - MatcherAssert.assertThat( - list -> { - MatcherAssert.assertThat(list, new BehavesAsList<>(0)); - return true; - }, - new RunsInThreads<>(new Solid<>(1, 0, -1, -1, 2)) - ); - } - - @Test - public void makesListFromMappedIterable() throws Exception { - final List list = new Solid<>( - new Mapped<>( - i -> i + 1, - new IterableOf<>(1, -1, 0, 1) - ) - ); - MatcherAssert.assertThat( - "Can't turn a mapped iterable into a list", - list, - new HasSize(4) - ); - MatcherAssert.assertThat( - "Can't turn a mapped iterable into a list, again", - list, - new HasSize(4) - ); - } - - @Test - public void mapsToSameObjects() throws Exception { - final List> list = new Solid<>( - new Mapped<>( - i -> (Scalar) () -> i, - new IterableOf<>(1, -1, 0, 1) - ) - ); - MatcherAssert.assertThat( - "Can't map only once", - list.get(0), - new IsEqual<>(list.get(0)) - ); - } - -} diff --git a/src/test/java/org/cactoos/list/SortedTest.java b/src/test/java/org/cactoos/list/SortedTest.java index 6dbc0189de..372d738fe5 100644 --- a/src/test/java/org/cactoos/list/SortedTest.java +++ b/src/test/java/org/cactoos/list/SortedTest.java @@ -68,7 +68,7 @@ public void takesItemFromSortedList() throws Exception { "Can't take one element from sorted list", new Sorted<>( Comparator.reverseOrder(), - "alpha", "beta", "gamma", "delta" + new ListOf<>("alpha", "beta", "gamma", "delta") ).get(1), new IsEqual<>("delta") ); diff --git a/src/test/java/org/cactoos/list/StickyTest.java b/src/test/java/org/cactoos/list/StickyTest.java deleted file mode 100644 index f9516648e8..0000000000 --- a/src/test/java/org/cactoos/list/StickyTest.java +++ /dev/null @@ -1,191 +0,0 @@ -/* - * 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.Collections; -import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; -import org.cactoos.Scalar; -import org.cactoos.iterable.IterableOf; -import org.hamcrest.MatcherAssert; -import org.hamcrest.core.IsEqual; -import org.junit.Test; -import org.llorllale.cactoos.matchers.HasSize; -import org.llorllale.cactoos.matchers.IsTrue; - -/** - * Test case for {@link Sticky}. - * @since 0.8 - * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - * @checkstyle JavadocMethodCheck (500 lines) - * @checkstyle MagicNumberCheck (500 lines) - */ -@SuppressWarnings("PMD.TooManyMethods") -public final class StickyTest { - - @Test - public void behavesAsCollection() throws Exception { - MatcherAssert.assertThat( - "Can't behave as a list", - new Sticky<>(1, 0, -1, -1, 2), - new BehavesAsList<>(0) - ); - } - - @Test - public void ignoresChangesInIterable() throws Exception { - final AtomicInteger size = new AtomicInteger(2); - final List list = new Sticky<>( - new ListOf( - () -> Collections.nCopies(size.incrementAndGet(), 0).iterator() - ) - ); - MatcherAssert.assertThat( - "Can't ignore the changes in the underlying iterable", - list.size(), - new IsEqual<>(list.size()) - ); - } - - @Test - public void decoratesArray() throws Exception { - MatcherAssert.assertThat( - "Can't decorate an array of numbers", - new Sticky<>(-1, 0).size(), - new IsEqual<>(2) - ); - } - - @Test - public void testEmpty() { - MatcherAssert.assertThat( - new Sticky<>().isEmpty(), - new IsTrue() - ); - } - - @Test - public void testContains() { - MatcherAssert.assertThat( - new Sticky<>(1, 2).contains(1), - new IsTrue() - ); - } - - @Test - public void testToArray() { - MatcherAssert.assertThat( - new Sticky<>(1, 2).toArray(), - new IsEqual<>(new int[] {1, 2 }) - ); - } - - @Test - public void testToArrayIntoArray() { - final Integer[] arr = new Integer[2]; - MatcherAssert.assertThat( - new Sticky<>(1, 2).toArray(arr), - new IsEqual<>(new int[] {1, 2 }) - ); - } - - @Test - public void testContainsAll() { - MatcherAssert.assertThat( - new Sticky<>(1, 2).containsAll(new ListOf<>(1, 2)), - new IsTrue() - ); - } - - @Test - public void testIndexOf() { - MatcherAssert.assertThat( - new Sticky<>(1, 2).indexOf(1), - new IsEqual<>(0) - ); - } - - @Test - public void testLastIndexOf() { - MatcherAssert.assertThat( - new Sticky<>(1, 2, 2).lastIndexOf(2), - new IsEqual<>(2) - ); - } - - @Test - public void testGet() { - MatcherAssert.assertThat( - new Sticky<>(1, 2).get(1), - new IsEqual<>(2) - ); - } - - @Test - public void testSubList() { - final List list = new Sticky<>( - 1, 2, 0, -1 - ).subList(0, 2); - MatcherAssert.assertThat( - list.size(), - new IsEqual<>(2) - ); - } - - @Test - public void makesListFromMappedIterable() throws Exception { - final List list = new Sticky<>( - new Mapped<>( - i -> i + 1, - new IterableOf<>(1, -1, 0, 1) - ) - ); - MatcherAssert.assertThat( - "Can't turn a mapped iterable into a list", - list, - new HasSize(4) - ); - MatcherAssert.assertThat( - "Can't turn a mapped iterable into a list, again", - list, - new HasSize(4) - ); - } - - @Test - public void mapsToSameObjects() throws Exception { - final List> list = new Sticky<>( - new Mapped<>( - i -> (Scalar) () -> i, - new IterableOf<>(1, -1, 0, 1) - ) - ); - MatcherAssert.assertThat( - "Can't map only once", - list.get(0), - new IsEqual<>(list.get(0)) - ); - } - -} diff --git a/src/test/java/org/cactoos/list/SyncedTest.java b/src/test/java/org/cactoos/list/SyncedTest.java index c333c0ba57..855077c088 100644 --- a/src/test/java/org/cactoos/list/SyncedTest.java +++ b/src/test/java/org/cactoos/list/SyncedTest.java @@ -40,7 +40,7 @@ public final class SyncedTest { public void behavesAsCollection() throws Exception { MatcherAssert.assertThat( "Can't behave as a list", - new Synced<>(1, 0, -1, -1, 2), + new Synced<>(new ListOf<>(1, 0, -1, -1, 2)), new BehavesAsList<>(0) ); } @@ -59,8 +59,7 @@ public void worksInThreads() { ); return true; }, - new RunsInThreads<>(new Synced<>(1, 0, -1, -1, 2)) + new RunsInThreads<>(new Synced<>(new ListOf<>(1, 0, -1, -1, 2))) ); } - } diff --git a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java index 13e5a06282..171e82c053 100644 --- a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java +++ b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java @@ -33,7 +33,6 @@ import org.cactoos.iterable.IterableOf; import org.cactoos.iterable.Mapped; import org.cactoos.list.ListOf; -import org.cactoos.list.Sticky; import org.cactoos.list.Synced; import org.hamcrest.Matcher; import org.hamcrest.MatcherAssert; @@ -100,9 +99,7 @@ public void emptyIterator() throws Exception { @Test public void iteratesList() { - final List list = new Sticky<>( - new Synced<>(new ArrayList<>(2)) - ); + final List list = new Synced<>(new ListOf<>()); MatcherAssert.assertThat( "Can't iterate a list with a procedure", new AndInThreads( @@ -183,9 +180,7 @@ public void worksWithIterableScalarBoolean() throws Exception { @Test public void worksWithExecServiceProcValues() throws Exception { - final List list = new Sticky<>( - new Synced<>(new ArrayList<>(2)) - ); + final List list = new Synced<>(new ListOf<>()); final ExecutorService service = Executors.newSingleThreadExecutor(); new AndInThreads( service, @@ -213,9 +208,7 @@ public void worksWithExecServiceProcValues() throws Exception { @Test public void worksWithExecServiceProcIterable() throws Exception { - final List list = new Sticky<>( - new Synced<>(new ArrayList<>(2)) - ); + final List list = new Synced<>(new ListOf<>()); final ExecutorService service = Executors.newSingleThreadExecutor(); new AndInThreads( service,