diff --git a/src/main/java/org/cactoos/func/SyncFunc.java b/src/main/java/org/cactoos/func/SyncFunc.java index 4a60b73739..2621630940 100644 --- a/src/main/java/org/cactoos/func/SyncFunc.java +++ b/src/main/java/org/cactoos/func/SyncFunc.java @@ -25,6 +25,7 @@ import java.util.concurrent.Callable; import org.cactoos.Func; +import org.cactoos.Proc; /** * Func that is thread-safe. @@ -49,6 +50,16 @@ public final class SyncFunc implements Func { */ private final Object lock; + /** + * Ctor. + * @param runnable Func original + * @param result Result to return + * @since 0.32 + */ + public SyncFunc(final Runnable runnable, final Y result) { + this(new FuncOf<>(runnable, result)); + } + /** * Ctor. * @param callable Func original @@ -58,6 +69,16 @@ public SyncFunc(final Callable callable) { this(new FuncOf<>(callable)); } + /** + * Ctor. + * @param proc Func original + * @param result Result to return + * @since 0.32 + */ + public SyncFunc(final Proc proc, final Y result) { + this(new FuncOf<>(proc, result)); + } + /** * Ctor. * @param fnc Func original diff --git a/src/main/java/org/cactoos/func/SyncProc.java b/src/main/java/org/cactoos/func/SyncProc.java deleted file mode 100644 index d19cafc577..0000000000 --- a/src/main/java/org/cactoos/func/SyncProc.java +++ /dev/null @@ -1,73 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017-2018 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.func; - -import org.cactoos.Proc; - -/** - * Proc that is thread-safe. - * - *

Objects of this class are thread safe.

- * - * @author Alexander Menshikov (sharplermc@gmail.com) - * @version $Id$ - * @param Type of input - * @since 0.32 - */ -public final class SyncProc implements Proc { - /** - * Original proc. - */ - private final Proc origin; - - /** - * Sync lock. - */ - private final Object lock; - - /** - * Ctor. - * @param runnable Runnable original - */ - public SyncProc(final Runnable runnable) { - this(new ProcOf<>(runnable)); - } - - /** - * Ctor. - * @param origin Proc original - */ - public SyncProc(final Proc origin) { - this.origin = origin; - this.lock = new Object(); - } - - @Override - public void exec(final X input) throws Exception { - synchronized (this.lock) { - this.origin.exec(input); - } - } -} diff --git a/src/test/java/org/cactoos/func/SyncFuncTest.java b/src/test/java/org/cactoos/func/SyncFuncTest.java index 41d123f5c8..b3cc7cbc2d 100644 --- a/src/test/java/org/cactoos/func/SyncFuncTest.java +++ b/src/test/java/org/cactoos/func/SyncFuncTest.java @@ -59,6 +59,26 @@ public void funcWorksInThreads() { MatcherAssert.assertThat(list.size(), Matchers.equalTo(threads)); } + @Test + public void procWorksInThreads() { + final int threads = 100; + final int[] counter = new int[]{0}; + MatcherAssert.assertThat( + "Sync func with proc can't work well in multiple threads", + func -> func.apply(1), + new RunsInThreads<>( + new SyncFunc( + new ProcOf<>( + input -> counter[0] = counter[0] + input + ), + true + ), + threads + ) + ); + MatcherAssert.assertThat(counter[0], Matchers.equalTo(threads)); + } + @Test public void callableWorksInThreads() { final int threads = 100; @@ -78,4 +98,22 @@ public void callableWorksInThreads() { ); MatcherAssert.assertThat(counter[0], Matchers.equalTo(threads)); } + + @Test + public void runnableWorksInThreads() { + final int threads = 100; + final int[] counter = new int[]{0}; + MatcherAssert.assertThat( + "Sync func with runnable can't work well in multiple threads", + func -> func.apply(1), + new RunsInThreads<>( + new SyncFunc( + () -> counter[0] = counter[0] + 1, + true + ), + threads + ) + ); + MatcherAssert.assertThat(counter[0], Matchers.equalTo(threads)); + } } diff --git a/src/test/java/org/cactoos/func/SyncProcTest.java b/src/test/java/org/cactoos/func/SyncProcTest.java deleted file mode 100644 index 84f5ad3907..0000000000 --- a/src/test/java/org/cactoos/func/SyncProcTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017-2018 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.func; - -import org.cactoos.matchers.RunsInThreads; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link SyncProc}. - * - * @author Alexander Menshikov (sharplermc@gmail.com) - * @version $Id$ - * @since 0.32 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class SyncProcTest { - @Test - public void procWorksInThreads() { - final int threads = 100; - final int[] counter = new int[]{0}; - MatcherAssert.assertThat( - "Sync func with proc can't work well in multiple threads", - func -> func.apply(1), - new RunsInThreads<>( - new FuncOf<>( - new SyncProc( - new ProcOf<>( - input -> counter[0] = counter[0] + input - ) - ), - true - ), - threads - ) - ); - MatcherAssert.assertThat(counter[0], Matchers.equalTo(threads)); - } - - @Test - public void runnableWorksInThreads() { - final int threads = 100; - final int[] counter = new int[]{0}; - MatcherAssert.assertThat( - "Sync func with runnable can't work well in multiple threads", - func -> func.apply(1), - new RunsInThreads<>( - new FuncOf<>( - new SyncProc( - () -> counter[0] = counter[0] + 1 - ), - true - ), - threads - ) - ); - MatcherAssert.assertThat(counter[0], Matchers.equalTo(threads)); - } -}