From 13947f959b14e1f4386c995db45ff0b52d0a246c Mon Sep 17 00:00:00 2001 From: Alexander Menshikov Date: Thu, 10 May 2018 20:53:31 +0300 Subject: [PATCH] #551 CR --- src/main/java/org/cactoos/func/AsyncFunc.java | 21 ++--- src/main/java/org/cactoos/func/FuncOf.java | 16 +++- .../java/org/cactoos/func/IoCheckedProc.java | 2 +- .../java/org/cactoos/func/RepeatedFunc.java | 7 +- src/main/java/org/cactoos/func/RetryFunc.java | 22 ++--- src/main/java/org/cactoos/func/SyncFunc.java | 21 +++++ src/main/java/org/cactoos/func/SyncProc.java | 73 ----------------- src/main/java/org/cactoos/func/TimedFunc.java | 7 +- .../java/org/cactoos/func/UncheckedProc.java | 2 +- .../java/org/cactoos/func/AsyncFuncTest.java | 9 +- .../java/org/cactoos/func/FuncOfTest.java | 13 +++ .../java/org/cactoos/func/RetryFuncTest.java | 11 +-- .../java/org/cactoos/func/SyncFuncTest.java | 38 +++++++++ .../java/org/cactoos/func/SyncProcTest.java | 82 ------------------- .../java/org/cactoos/func/TimedFuncTest.java | 1 - .../java/org/cactoos/map/MapEnvelopeTest.java | 12 +-- 16 files changed, 119 insertions(+), 218 deletions(-) delete mode 100644 src/main/java/org/cactoos/func/SyncProc.java delete mode 100644 src/test/java/org/cactoos/func/SyncProcTest.java diff --git a/src/main/java/org/cactoos/func/AsyncFunc.java b/src/main/java/org/cactoos/func/AsyncFunc.java index b685e3cc05..6303b5f3cf 100644 --- a/src/main/java/org/cactoos/func/AsyncFunc.java +++ b/src/main/java/org/cactoos/func/AsyncFunc.java @@ -63,11 +63,9 @@ public final class AsyncFunc implements Func>, Proc { /** * Ctor. * @param proc The proc - * @param result Result to return - * @since 0.32 */ - public AsyncFunc(final Proc proc, final Y result) { - this(new FuncOf<>(proc, result)); + public AsyncFunc(final Proc proc) { + this(new FuncOf<>(proc)); } /** @@ -81,13 +79,10 @@ public AsyncFunc(final Func fnc) { /** * Ctor. * @param proc The proc - * @param result Result to return * @param fct Factory - * @since 0.32 */ - public AsyncFunc(final Proc proc, final Y result, - final ThreadFactory fct) { - this(new FuncOf<>(proc, result), fct); + public AsyncFunc(final Proc proc, final ThreadFactory fct) { + this(new FuncOf<>(proc), fct); } /** @@ -103,12 +98,10 @@ public AsyncFunc(final Func fnc, final ThreadFactory fct) { * Ctor. * @param proc The proc * @param exec Executor Service - * @param result Result to return - * @since 0.32 + * @since 0.17 */ - public AsyncFunc(final Proc proc, final Y result, - final ExecutorService exec) { - this(new FuncOf<>(proc, result), exec); + public AsyncFunc(final Proc proc, final ExecutorService exec) { + this(new FuncOf<>(proc), exec); } /** diff --git a/src/main/java/org/cactoos/func/FuncOf.java b/src/main/java/org/cactoos/func/FuncOf.java index e731a21644..8ff2de3245 100644 --- a/src/main/java/org/cactoos/func/FuncOf.java +++ b/src/main/java/org/cactoos/func/FuncOf.java @@ -37,6 +37,10 @@ * @param Type of input * @param Type of output * @since 0.12 + * @todo #551:30min Remove ctor FuncOf(Proc) by replacing with FuncOf(Proc, Y) + * FuncOf(Proc) force using of null value which is against design principles. + * It's affect a lot of classes other classes. + * Please take a look on #551 and #843 for more details. */ public final class FuncOf implements Func { @@ -50,7 +54,7 @@ public final class FuncOf implements Func { * @param result The result */ public FuncOf(final Y result) { - this(input -> result); + this((Func) input -> result); } /** @@ -58,7 +62,7 @@ public FuncOf(final Y result) { * @param callable The callable */ public FuncOf(final Callable callable) { - this(input -> callable.call()); + this((Func) input -> callable.call()); } /** @@ -71,6 +75,14 @@ public FuncOf(final Runnable runnable, final Y result) { this(input -> runnable.run(), result); } + /** + * Ctor. + * @param proc The proc + */ + public FuncOf(final Proc proc) { + this(proc, null); + } + /** * Ctor. * @param proc The proc diff --git a/src/main/java/org/cactoos/func/IoCheckedProc.java b/src/main/java/org/cactoos/func/IoCheckedProc.java index bf7b834b1b..7643ed8888 100644 --- a/src/main/java/org/cactoos/func/IoCheckedProc.java +++ b/src/main/java/org/cactoos/func/IoCheckedProc.java @@ -54,7 +54,7 @@ public IoCheckedProc(final Proc prc) { @Override public void exec(final X input) throws IOException { - new IoCheckedFunc<>(new FuncOf<>(this.proc, null)).apply(input); + new IoCheckedFunc<>(new FuncOf<>(this.proc)).apply(input); } } diff --git a/src/main/java/org/cactoos/func/RepeatedFunc.java b/src/main/java/org/cactoos/func/RepeatedFunc.java index e70bcaa8e5..1d1e6d1d18 100644 --- a/src/main/java/org/cactoos/func/RepeatedFunc.java +++ b/src/main/java/org/cactoos/func/RepeatedFunc.java @@ -51,12 +51,11 @@ public final class RepeatedFunc implements Func { /** * Ctor. * @param proc Proc - * @param result Result to return * @param max How many times - * @since 0.32 + * @since 0.12 */ - public RepeatedFunc(final Proc proc, final Y result, final int max) { - this(new FuncOf<>(proc, result), max); + public RepeatedFunc(final Proc proc, final int max) { + this(new FuncOf<>(proc), max); } /** diff --git a/src/main/java/org/cactoos/func/RetryFunc.java b/src/main/java/org/cactoos/func/RetryFunc.java index 08ae06d8be..146b2590fc 100644 --- a/src/main/java/org/cactoos/func/RetryFunc.java +++ b/src/main/java/org/cactoos/func/RetryFunc.java @@ -52,34 +52,30 @@ public final class RetryFunc implements Func { /** * Ctor. * @param proc Func original - * @param result Result to return - * @since 0.32 + * @since 0.12 */ - public RetryFunc(final Proc proc, final Y result) { - this(new FuncOf<>(proc, result)); + public RetryFunc(final Proc proc) { + this(new FuncOf<>(proc)); } /** * Ctor. * @param proc Func original - * @param result Result to return * @param attempts Maximum number of attempts - * @since 0.32 + * @since 0.12 */ - public RetryFunc(final Proc proc, final Y result, final int attempts) { - this(new FuncOf<>(proc, result), attempts); + public RetryFunc(final Proc proc, final int attempts) { + this(new FuncOf<>(proc), attempts); } /** * Ctor. * @param proc Func original - * @param result Result to return * @param ext Exit condition, returns TRUE if there is no more reason to try - * @since 0.32 + * @since 0.12 */ - public RetryFunc(final Proc proc, final Y result, - final Func ext) { - this(new FuncOf<>(proc, result), ext); + public RetryFunc(final Proc proc, final Func ext) { + this(new FuncOf<>(proc), ext); } /** 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/main/java/org/cactoos/func/TimedFunc.java b/src/main/java/org/cactoos/func/TimedFunc.java index bfc3cf542e..42f330d379 100644 --- a/src/main/java/org/cactoos/func/TimedFunc.java +++ b/src/main/java/org/cactoos/func/TimedFunc.java @@ -53,13 +53,10 @@ public final class TimedFunc implements Func { /** * Ctor. * @param proc Proc - * @param result Result to return * @param milliseconds Milliseconds - * @since 0.32 */ - public TimedFunc(final Proc proc, final Y result, - final long milliseconds) { - this(new FuncOf<>(proc, result), milliseconds); + public TimedFunc(final Proc proc, final long milliseconds) { + this(new FuncOf<>(proc), milliseconds); } /** diff --git a/src/main/java/org/cactoos/func/UncheckedProc.java b/src/main/java/org/cactoos/func/UncheckedProc.java index d590d7c11c..a5e3d80a3b 100644 --- a/src/main/java/org/cactoos/func/UncheckedProc.java +++ b/src/main/java/org/cactoos/func/UncheckedProc.java @@ -52,7 +52,7 @@ public UncheckedProc(final Proc prc) { @Override public void exec(final X input) { - new UncheckedFunc<>(new FuncOf<>(this.proc, null)).apply(input); + new UncheckedFunc<>(new FuncOf<>(this.proc)).apply(input); } } diff --git a/src/test/java/org/cactoos/func/AsyncFuncTest.java b/src/test/java/org/cactoos/func/AsyncFuncTest.java index 5fdd379c50..8d4b2213f7 100644 --- a/src/test/java/org/cactoos/func/AsyncFuncTest.java +++ b/src/test/java/org/cactoos/func/AsyncFuncTest.java @@ -43,7 +43,6 @@ * @since 0.10 * @checkstyle JavadocMethodCheck (500 lines) */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class AsyncFuncTest { @Test @@ -72,8 +71,7 @@ public void runsAsProcInBackground() { input -> { final CountDownLatch latch = new CountDownLatch(1); new AsyncFunc<>( - (Proc) ipt -> latch.countDown(), - true + (Proc) ipt -> latch.countDown() ).exec(input); latch.await(); return true; @@ -92,8 +90,7 @@ public void runsInBackgroundWithoutFuture() { new AsyncFunc<>( input -> { latch.countDown(); - }, - "done!" + } ), new FuncApplies<>( true, @@ -122,7 +119,6 @@ public void runsInBackgroundWithThreadFactory() { } latch.countDown(); }, - null, factory ), new FuncApplies<>( @@ -153,7 +149,6 @@ public void runsInBackgroundWithExecutorService() { } latch.countDown(); }, - null, Executors.newSingleThreadExecutor(factory) ), new FuncApplies<>( diff --git a/src/test/java/org/cactoos/func/FuncOfTest.java b/src/test/java/org/cactoos/func/FuncOfTest.java index f0fef9ecc6..db4cb1ed54 100644 --- a/src/test/java/org/cactoos/func/FuncOfTest.java +++ b/src/test/java/org/cactoos/func/FuncOfTest.java @@ -50,6 +50,19 @@ public void convertsProcIntoFunc() throws Exception { ); } + @Test + public void convertsProcWithNoResultIntoFunc() throws Exception { + final AtomicBoolean done = new AtomicBoolean(false); + MatcherAssert.assertThat( + new FuncOf( + input -> { + done.set(true); + } + ).apply("hello you"), + Matchers.nullValue() + ); + } + @Test public void convertsValueIntoFunc() throws Exception { MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/func/RetryFuncTest.java b/src/test/java/org/cactoos/func/RetryFuncTest.java index 76ae9e2a52..6d7a47a2ce 100644 --- a/src/test/java/org/cactoos/func/RetryFuncTest.java +++ b/src/test/java/org/cactoos/func/RetryFuncTest.java @@ -66,10 +66,9 @@ public void runsProcMultipleTimes() throws Exception { throw new IllegalArgumentException("May happen"); } }, - true, Integer.MAX_VALUE ).apply(true), - Matchers.equalTo(true) + Matchers.nullValue() ); } @@ -82,10 +81,9 @@ public void runsProcDefaultMultipleTimes() throws Exception { if (fail.getAndSet(false)) { throw new IllegalArgumentException("May happen"); } - }, - true + } ).apply(true), - Matchers.equalTo(true) + Matchers.nullValue() ); } @@ -98,10 +96,9 @@ public void runsProcConditionMultipleTimes() throws Exception { throw new IllegalArgumentException("May happen"); } }, - true, count -> count == Integer.MAX_VALUE ).apply(true), - Matchers.equalTo(true) + Matchers.nullValue() ); } } 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)); - } -} diff --git a/src/test/java/org/cactoos/func/TimedFuncTest.java b/src/test/java/org/cactoos/func/TimedFuncTest.java index 26008f90ed..82e71ecff7 100644 --- a/src/test/java/org/cactoos/func/TimedFuncTest.java +++ b/src/test/java/org/cactoos/func/TimedFuncTest.java @@ -63,7 +63,6 @@ public void procGetsInterrupted() throws Exception { new Endless<>(() -> input) ).value(); }, - true, period ).apply(true); } diff --git a/src/test/java/org/cactoos/map/MapEnvelopeTest.java b/src/test/java/org/cactoos/map/MapEnvelopeTest.java index 05d0acda63..c48ea3e571 100644 --- a/src/test/java/org/cactoos/map/MapEnvelopeTest.java +++ b/src/test/java/org/cactoos/map/MapEnvelopeTest.java @@ -64,8 +64,7 @@ public void putThrowsException() { ), new MatcherOf<>( new FuncOf<>( - (map) -> map.put(2, 2), - true + (map) -> map.put(2, 2) )) ); } @@ -85,8 +84,7 @@ public void removeThrowsException() { ), new MatcherOf<>( new FuncOf<>( - (map) -> map.remove(0), - true + (map) -> map.remove(0) )) ); } @@ -106,8 +104,7 @@ public void putAllThrowsException() { ), new MatcherOf<>( new FuncOf<>( - (map) -> map.putAll(new MapOf()), - true + (map) -> map.putAll(new MapOf()) )) ); } @@ -127,8 +124,7 @@ public void clearThrowsException() { ), new MatcherOf<>( new FuncOf<>( - Map::clear, - true + Map::clear )) ); }