From 21e9e861fc5b4a7625f8bf7e6d10dfc93dc31bcc Mon Sep 17 00:00:00 2001 From: Riccardo Ianniello Date: Thu, 2 Jan 2020 01:30:28 +0100 Subject: [PATCH] (#1226) Replaced AndInThreads(Proc, ...) with ForEachInThreads --- .../cactoos/iterator/ForEachInThreads.java | 90 +++++++++++++ .../java/org/cactoos/scalar/AndInThreads.java | 21 ---- .../iterator/ForEachInThreadsTest.java | 118 ++++++++++++++++++ .../org/cactoos/scalar/AndInThreadsTest.java | 57 --------- 4 files changed, 208 insertions(+), 78 deletions(-) create mode 100644 src/main/java/org/cactoos/iterator/ForEachInThreads.java create mode 100644 src/test/java/org/cactoos/iterator/ForEachInThreadsTest.java diff --git a/src/main/java/org/cactoos/iterator/ForEachInThreads.java b/src/main/java/org/cactoos/iterator/ForEachInThreads.java new file mode 100644 index 0000000000..a48eb15779 --- /dev/null +++ b/src/main/java/org/cactoos/iterator/ForEachInThreads.java @@ -0,0 +1,90 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2019 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.iterator; + +import org.cactoos.Func; +import org.cactoos.Proc; +import org.cactoos.func.FuncOf; +import org.cactoos.iterable.IterableOf; +import org.cactoos.scalar.AndInThreads; + +/** + * Executes a {@link org.cactoos.Proc} for each element of an + * {@link java.lang.Iterable} + * + *

+ * This class can be effectively used to iterate through a collection, just like + * {@link java.util.stream.Stream#forEach(java.util.function.Consumer)} works: + *

+ * + * {@code + * new ForEach( + * new ProcOf<>(input -> System.out.printf("\'%s\' ", input)), + * ).execute( + * new IterableOf<>("Mary", "John", "William", "Napkin") + * ); // will print 'Mary' 'John' 'William' 'Napkin' to standard output + * } + *

+ * There is no thread-safety guarantee. + * + * @param The type to itetare over + * @since 0.44 + */ +public final class ForEachInThreads implements Proc> { + + /** + * The proc. + */ + private final Func wrapped; + + /** + * Ctor. + * + * @param proc The proc to execute + */ + public ForEachInThreads(final Proc proc) { + this.wrapped = new FuncOf<>( + proc, true + ); + } + + /** + * Ctor. + * + * @param src The iterable + * @exception Exception If fails + */ + @SafeVarargs + public final void exec(final X... src) throws Exception { + this.exec(new IterableOf<>(src)); + } + + @Override + public void exec(final Iterable input) throws Exception { + new AndInThreads( + this.wrapped, input + ).value(); + } + +} diff --git a/src/main/java/org/cactoos/scalar/AndInThreads.java b/src/main/java/org/cactoos/scalar/AndInThreads.java index 235d5fbbe6..2f95a7ea70 100644 --- a/src/main/java/org/cactoos/scalar/AndInThreads.java +++ b/src/main/java/org/cactoos/scalar/AndInThreads.java @@ -71,17 +71,6 @@ public final class AndInThreads implements Scalar { */ private final boolean shut; - /** - * Ctor. - * @param proc Proc to map - * @param src The iterable - * @param Type of items in the iterable - */ - @SafeVarargs - public AndInThreads(final Proc proc, final X... src) { - this(new FuncOf<>(proc, true), src); - } - /** * Ctor. * @param func Func to map @@ -93,16 +82,6 @@ public AndInThreads(final Func func, final X... src) { this(func, new IterableOf<>(src)); } - /** - * Ctor. - * @param proc Proc to use - * @param src The iterable - * @param Type of items in the iterable - */ - public AndInThreads(final Proc proc, final Iterable src) { - this(new FuncOf<>(proc, true), src); - } - /** * Ctor. * @param func Func to map diff --git a/src/test/java/org/cactoos/iterator/ForEachInThreadsTest.java b/src/test/java/org/cactoos/iterator/ForEachInThreadsTest.java new file mode 100644 index 0000000000..ff2476eb13 --- /dev/null +++ b/src/test/java/org/cactoos/iterator/ForEachInThreadsTest.java @@ -0,0 +1,118 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2019 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.iterator; + +import java.util.ArrayList; +import java.util.List; +import org.cactoos.Proc; +import org.cactoos.func.ProcNoNulls; +import org.cactoos.list.ListOf; +import org.cactoos.list.Synced; +import org.hamcrest.Matcher; +import org.hamcrest.collection.IsIterableContainingInAnyOrder; +import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.MatcherOf; + +/** + * Test case for {@link ForEachInThreads}. + * + * @since 0.44 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public class ForEachInThreadsTest { + + @Test + public void testProcIterable() throws Exception { + final List list = new Synced<>( + new ArrayList<>( + 2 + ) + ); + new ForEachInThreads( + new ProcNoNulls( + list::add + ) + ).exec( + new ListOf<>( + 1, 2 + ) + ); + new Assertion<>( + "List does not contain mapped Iterable elements (1)", list, + new IsIterableContainingInAnyOrder( + new ListOf>( + new MatcherOf<>( + value -> { + return value.equals( + 1 + ); + } + ), new MatcherOf<>( + value -> { + return value.equals( + 2 + ); + } + ) + ) + ) + ).affirm(); + } + + @Test + public void testProcVarargs() throws Exception { + final List list = new Synced<>( + new ArrayList<>( + 2 + ) + ); + new ForEachInThreads( + (Proc) list::add + ).exec( + 1, 1 + ); + new Assertion<>( + "List does not contain mapped Iterable elements (2)", list, + new IsIterableContainingInAnyOrder( + new ListOf>( + new MatcherOf<>( + value -> { + return value.equals( + 1 + ); + } + ), new MatcherOf<>( + value -> { + return value.equals( + 1 + ); + } + ) + ) + ) + ).affirm(); + } + +} diff --git a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java index 2d5e984d98..e3483f80fc 100644 --- a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java +++ b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java @@ -27,7 +27,6 @@ import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import org.cactoos.Proc; import org.cactoos.Scalar; import org.cactoos.func.FuncOf; import org.cactoos.func.ProcNoNulls; @@ -157,34 +156,6 @@ public void iteratesEmptyList() { ); } - @Test - public void worksWithProc() throws Exception { - final List list = new Synced<>( - new ArrayList<>(2) - ); - new AndInThreads( - (Proc) list::add, - 1, 1 - ).value(); - MatcherAssert.assertThat( - list, - new IsIterableContainingInAnyOrder( - new ListOf>( - new MatcherOf<>( - value -> { - return value.equals(1); - } - ), - new MatcherOf<>( - value -> { - return value.equals(1); - } - ) - ) - ) - ); - } - @Test public void worksWithFunc() throws Exception { MatcherAssert.assertThat( @@ -196,34 +167,6 @@ public void worksWithFunc() throws Exception { ); } - @Test - public void worksWithProcIterable() throws Exception { - final List list = new Synced<>( - new ArrayList<>(2) - ); - new AndInThreads( - new ProcNoNulls(list::add), - new ListOf<>(1, 2) - ).value(); - MatcherAssert.assertThat( - list, - new IsIterableContainingInAnyOrder( - new ListOf>( - new MatcherOf<>( - value -> { - return value.equals(1); - } - ), - new MatcherOf<>( - value -> { - return value.equals(2); - } - ) - ) - ) - ); - } - @Test public void worksWithIterableScalarBoolean() throws Exception { MatcherAssert.assertThat(