From 4d824991ee45db581107ba8a5e45656945015f96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sat, 29 Feb 2020 17:45:45 +0100 Subject: [PATCH] (#1299) Removed collection's Filtered --- .../java/org/cactoos/collection/Filtered.java | 69 ------------ .../org/cactoos/collection/FilteredTest.java | 100 ------------------ .../org/cactoos/iterable/FilteredTest.java | 56 ++++++++++ 3 files changed, 56 insertions(+), 169 deletions(-) delete mode 100644 src/main/java/org/cactoos/collection/Filtered.java delete mode 100644 src/test/java/org/cactoos/collection/FilteredTest.java diff --git a/src/main/java/org/cactoos/collection/Filtered.java b/src/main/java/org/cactoos/collection/Filtered.java deleted file mode 100644 index 1e4d500d7e..0000000000 --- a/src/main/java/org/cactoos/collection/Filtered.java +++ /dev/null @@ -1,69 +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.collection; - -import org.cactoos.Func; -import org.cactoos.iterable.IterableOf; - -/** - * Filtered collection. - * - *

There is no thread-safety guarantee. - - * @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 { - - /** - * Ctor. - * @param func Filter function - * @param src Source collection - * @since 0.23 - */ - @SafeVarargs - public Filtered(final Func func, final X... src) { - this(func, new IterableOf<>(src)); - } - - /** - * Ctor. - * @param func Filter function - * @param src Source collection - */ - public Filtered(final Func func, final Iterable src) { - super( - new Sticky<>( - new org.cactoos.iterable.Filtered<>( - func, src - ) - ) - ); - } -} diff --git a/src/test/java/org/cactoos/collection/FilteredTest.java b/src/test/java/org/cactoos/collection/FilteredTest.java deleted file mode 100644 index da2e0f5f94..0000000000 --- a/src/test/java/org/cactoos/collection/FilteredTest.java +++ /dev/null @@ -1,100 +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.collection; - -import org.cactoos.iterable.IterableOf; -import org.cactoos.list.ListOf; -import org.hamcrest.collection.IsEmptyCollection; -import org.hamcrest.core.IsEqual; -import org.hamcrest.core.IsNot; -import org.junit.Test; -import org.llorllale.cactoos.matchers.Assertion; - -/** - * Test case for {@link org.cactoos.collection.Filtered}. - * - * @since 0.16 - * @checkstyle JavadocMethodCheck (500 lines) - * @checkstyle MagicNumber (500 line) - * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - */ -public final class FilteredTest { - - @Test - public void behavesAsCollection() { - new Assertion<>( - "Can't behave as a collection", - new Filtered(i -> i < 2, 1, 2, 0, -1), - new BehavesAsCollection<>(-1) - ).affirm(); - } - - @Test - public void filterEmptyList() { - new Assertion<>( - "Filter must work on empty collection", - new Filtered( - input -> input.length() > 4, - new ListOf<>() - ), - new IsEmptyCollection<>() - ).affirm(); - } - - @Test - public void size() { - new Assertion<>( - "Size must be equal to number of items matching the filter", - new Filtered( - input -> input.length() >= 4, - new IterableOf<>("some", "text", "yes") - ).size(), - new IsEqual<>(2) - ).affirm(); - } - - @Test - public void withItemsNotEmpty() { - new Assertion<>( - "Must not be empty with items", - new Filtered( - input -> input.length() > 4, - new IterableOf<>("first", "second") - ), - new IsNot<>(new IsEmptyCollection<>()) - ).affirm(); - } - - @Test - public void withoutItemsIsEmpty() { - new Assertion<>( - "Must be empty without items", - new Filtered( - input -> input.length() > 16, - new IterableOf<>("third", "fourth") - ), - new IsEmptyCollection<>() - ).affirm(); - } -} diff --git a/src/test/java/org/cactoos/iterable/FilteredTest.java b/src/test/java/org/cactoos/iterable/FilteredTest.java index 4524f10a28..d127da37ad 100644 --- a/src/test/java/org/cactoos/iterable/FilteredTest.java +++ b/src/test/java/org/cactoos/iterable/FilteredTest.java @@ -23,16 +23,22 @@ */ package org.cactoos.iterable; +import org.cactoos.list.ListOf; import org.cactoos.scalar.LengthOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.hamcrest.collection.IsEmptyIterable; +import org.hamcrest.core.IsNot; import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.ScalarHasValue; /** * Test case for {@link Filtered}. * @since 0.1 * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle MagicNumberCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class FilteredTest { @@ -82,4 +88,54 @@ public void filtersIterablesWithSize() { ); } + @Test + public void filterEmptyList() { + new Assertion<>( + "Filter must work on empty collection", + new Filtered( + input -> input.length() > 4, + new ListOf<>() + ), + new IsEmptyIterable<>() + ).affirm(); + } + + @Test + public void length() { + new Assertion<>( + "Size must be equal to number of items matching the filter", + new LengthOf( + new Filtered<>( + input -> input.length() >= 4, + new IterableOf<>("some", "text", "yes") + ) + ), + new ScalarHasValue<>(2.) + ).affirm(); + } + + @Test + public void withItemsNotEmpty() { + new Assertion<>( + "Must not be empty with items", + new Filtered( + input -> input.length() > 4, + new IterableOf<>("first", "second") + ), + new IsNot<>(new IsEmptyIterable<>()) + ).affirm(); + } + + @Test + public void withoutItemsIsEmpty() { + new Assertion<>( + "Must be empty without items", + new Filtered( + input -> input.length() > 16, + new IterableOf<>("third", "fourth") + ), + new IsEmptyIterable<>() + ).affirm(); + } + }