From ba94b95c311a3f31d0bf703015fdbba42bcd184b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 19 Jul 2017 11:19:26 +0300 Subject: [PATCH 1/3] #274: FuncOf, ProcOf, RunnableOf, CallableOf --- src/main/java/org/cactoos/func/And.java | 4 +- src/main/java/org/cactoos/func/AsyncFunc.java | 4 +- .../java/org/cactoos/func/CallableAsFunc.java | 62 ------ .../{FuncAsCallable.java => CallableOf.java} | 25 ++- .../cactoos/func/CallableWithFallback.java | 71 ------- .../java/org/cactoos/func/ChainedFunc.java | 27 ++- src/main/java/org/cactoos/func/ConstFunc.java | 59 ------ .../java/org/cactoos/func/FuncAsProc.java | 58 ------ src/main/java/org/cactoos/func/FuncOf.java | 124 ++++++++++++ .../org/cactoos/func/FuncWithFallback.java | 182 ++++++++++++++++++ .../func/{ProcAsFunc.java => ProcOf.java} | 57 ++++-- .../java/org/cactoos/func/RepeatedFunc.java | 11 ++ src/main/java/org/cactoos/func/RetryFunc.java | 30 +++ .../java/org/cactoos/func/RunnableAsFunc.java | 62 ------ .../{FuncAsRunnable.java => RunnableOf.java} | 19 +- .../cactoos/func/RunnableWithFallback.java | 70 ------- src/main/java/org/cactoos/func/SyncFunc.java | 29 +++ src/main/java/org/cactoos/func/SyncProc.java | 58 ------ .../java/org/cactoos/func/UncheckedProc.java | 2 +- src/test/java/org/cactoos/func/AndTest.java | 6 +- ...sCallableTest.java => CallableOfTest.java} | 6 +- .../func/CallableWithFallbackTest.java | 73 ------- .../java/org/cactoos/func/ConstFuncTest.java | 49 ----- .../org/cactoos/func/RepeatedFuncTest.java | 4 +- ...sRunnableTest.java => RunnableOfTest.java} | 6 +- .../func/RunnableWithFallbackTest.java | 76 -------- 26 files changed, 481 insertions(+), 693 deletions(-) delete mode 100644 src/main/java/org/cactoos/func/CallableAsFunc.java rename src/main/java/org/cactoos/func/{FuncAsCallable.java => CallableOf.java} (79%) delete mode 100644 src/main/java/org/cactoos/func/CallableWithFallback.java delete mode 100644 src/main/java/org/cactoos/func/ConstFunc.java delete mode 100644 src/main/java/org/cactoos/func/FuncAsProc.java create mode 100644 src/main/java/org/cactoos/func/FuncOf.java rename src/main/java/org/cactoos/func/{ProcAsFunc.java => ProcOf.java} (61%) delete mode 100644 src/main/java/org/cactoos/func/RunnableAsFunc.java rename src/main/java/org/cactoos/func/{FuncAsRunnable.java => RunnableOf.java} (81%) delete mode 100644 src/main/java/org/cactoos/func/RunnableWithFallback.java delete mode 100644 src/main/java/org/cactoos/func/SyncProc.java rename src/test/java/org/cactoos/func/{FuncAsCallableTest.java => CallableOfTest.java} (93%) delete mode 100644 src/test/java/org/cactoos/func/CallableWithFallbackTest.java delete mode 100644 src/test/java/org/cactoos/func/ConstFuncTest.java rename src/test/java/org/cactoos/func/{FuncAsRunnableTest.java => RunnableOfTest.java} (94%) delete mode 100644 src/test/java/org/cactoos/func/RunnableWithFallbackTest.java diff --git a/src/main/java/org/cactoos/func/And.java b/src/main/java/org/cactoos/func/And.java index 54a6f287c9..6fc63761a2 100644 --- a/src/main/java/org/cactoos/func/And.java +++ b/src/main/java/org/cactoos/func/And.java @@ -53,7 +53,7 @@ public final class And implements Scalar { */ @SafeVarargs public And(final Proc proc, final X... src) { - this(new ProcAsFunc<>(proc, true), src); + this(new FuncOf<>(proc, true), src); } /** @@ -74,7 +74,7 @@ public And(final Func func, final X... src) { * @param Type of items in the iterable */ public And(final Iterable src, final Proc proc) { - this(src, new ProcAsFunc<>(proc, true)); + this(src, new FuncOf<>(proc, true)); } /** diff --git a/src/main/java/org/cactoos/func/AsyncFunc.java b/src/main/java/org/cactoos/func/AsyncFunc.java index 3922c2b786..210931a202 100644 --- a/src/main/java/org/cactoos/func/AsyncFunc.java +++ b/src/main/java/org/cactoos/func/AsyncFunc.java @@ -64,7 +64,7 @@ public final class AsyncFunc implements Func> { * @param proc The proc */ public AsyncFunc(final Proc proc) { - this(new ProcAsFunc<>(proc)); + this(new FuncOf<>(proc)); } /** @@ -81,7 +81,7 @@ public AsyncFunc(final Func fnc) { * @param fct Factory */ public AsyncFunc(final Proc proc, final ThreadFactory fct) { - this(new ProcAsFunc<>(proc), fct); + this(new FuncOf<>(proc), fct); } /** diff --git a/src/main/java/org/cactoos/func/CallableAsFunc.java b/src/main/java/org/cactoos/func/CallableAsFunc.java deleted file mode 100644 index e5c3c7a6a7..0000000000 --- a/src/main/java/org/cactoos/func/CallableAsFunc.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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 java.util.concurrent.Callable; -import org.cactoos.Func; - -/** - * Callable as Func. - * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @param Type of input - * @param Type of output - * @see FuncAsCallable - * @see FuncAsRunnable - * @see RunnableAsFunc - * @since 0.2 - */ -public final class CallableAsFunc implements Func { - - /** - * The callable. - */ - private final Callable callable; - - /** - * Ctor. - * @param clbl The callable - */ - public CallableAsFunc(final Callable clbl) { - this.callable = clbl; - } - - @Override - public Y apply(final X input) throws Exception { - return this.callable.call(); - } -} diff --git a/src/main/java/org/cactoos/func/FuncAsCallable.java b/src/main/java/org/cactoos/func/CallableOf.java similarity index 79% rename from src/main/java/org/cactoos/func/FuncAsCallable.java rename to src/main/java/org/cactoos/func/CallableOf.java index 2862f574d8..3aef3a2602 100644 --- a/src/main/java/org/cactoos/func/FuncAsCallable.java +++ b/src/main/java/org/cactoos/func/CallableOf.java @@ -25,6 +25,7 @@ import java.util.concurrent.Callable; import org.cactoos.Func; +import org.cactoos.Proc; /** * Func as {@link Callable}. @@ -42,20 +43,38 @@ * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of input - * @since 0.2 + * @since 0.12 */ -public final class FuncAsCallable implements Callable { +public final class CallableOf implements Callable { /** * Original func. */ private final Func func; + /** + * Ctor. + * @param runnable Encapsulated proc + * @since 0.12 + */ + public CallableOf(final Runnable runnable) { + this(new FuncOf<>(runnable)); + } + + /** + * Ctor. + * @param proc Encapsulated proc + * @since 0.12 + */ + public CallableOf(final Proc proc) { + this(new FuncOf<>(proc)); + } + /** * Ctor. * @param fnc Encapsulated func */ - public FuncAsCallable(final Func fnc) { + public CallableOf(final Func fnc) { this.func = fnc; } diff --git a/src/main/java/org/cactoos/func/CallableWithFallback.java b/src/main/java/org/cactoos/func/CallableWithFallback.java deleted file mode 100644 index f251b5b693..0000000000 --- a/src/main/java/org/cactoos/func/CallableWithFallback.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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 java.util.concurrent.Callable; -import org.cactoos.Func; - -/** - * Callable with a fallback plan. - * - *

There is no thread-safety guarantee. - * - * @author Vladislav Cheparin (ivladislav969@gmail.com) - * @version $Id$ - * @param Type of output - * @since 0.11 - */ -public final class CallableWithFallback implements Callable { - - /** - * The callable. - */ - private final Callable callable; - - /** - * The fallback. - */ - private final Func fallback; - - /** - * Ctor. - * @param clbl The callable - * @param fbk The fallback - */ - public CallableWithFallback(final Callable clbl, - final Func fbk) { - this.callable = clbl; - this.fallback = fbk; - } - - @Override - public Y call() throws Exception { - return new UncheckedFunc<>( - new FuncWithFallback( - new CallableAsFunc<>(this.callable), - this.fallback - ) - ).apply(true); - } -} diff --git a/src/main/java/org/cactoos/func/ChainedFunc.java b/src/main/java/org/cactoos/func/ChainedFunc.java index c41c41163f..96c997488f 100644 --- a/src/main/java/org/cactoos/func/ChainedFunc.java +++ b/src/main/java/org/cactoos/func/ChainedFunc.java @@ -55,27 +55,24 @@ public final class ChainedFunc implements Func { /** * Ctor. - * @param before Before function - * @param funcs Functions - * @param after After function + * @param bfr Before function + * @param list Functions + * @param atr After function */ - public ChainedFunc( - final Func before, - final Iterable> funcs, - final Func after - ) { - this.before = before; - this.funcs = funcs; - this.after = after; + public ChainedFunc(final Func bfr, final Iterable> list, + final Func atr) { + this.before = bfr; + this.funcs = list; + this.after = atr; } /** * Ctor. - * @param before Before function - * @param after After function + * @param bfr Before function + * @param atr After function */ - public ChainedFunc(final Func before, final Func after) { - this(before, Collections.emptyList(), after); + public ChainedFunc(final Func bfr, final Func atr) { + this(bfr, Collections.emptyList(), atr); } @Override diff --git a/src/main/java/org/cactoos/func/ConstFunc.java b/src/main/java/org/cactoos/func/ConstFunc.java deleted file mode 100644 index 3f2c8dfbf6..0000000000 --- a/src/main/java/org/cactoos/func/ConstFunc.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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.Func; - -/** - * Func that always returns the same result. - * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @param Type of input - * @param Type of output - * @since 0.1 - */ -public final class ConstFunc implements Func { - - /** - * The result to return. - */ - private final Y result; - - /** - * Ctor. - * @param rslt What to return - */ - public ConstFunc(final Y rslt) { - this.result = rslt; - } - - @Override - public Y apply(final X input) { - return this.result; - } - -} diff --git a/src/main/java/org/cactoos/func/FuncAsProc.java b/src/main/java/org/cactoos/func/FuncAsProc.java deleted file mode 100644 index 13e50948bd..0000000000 --- a/src/main/java/org/cactoos/func/FuncAsProc.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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.Func; -import org.cactoos.Proc; - -/** - * Func as Proc. - * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @param Type of input - * @since 0.6 - */ -public final class FuncAsProc implements Proc { - - /** - * The func. - */ - private final Func func; - - /** - * Ctor. - * @param fnc The proc - */ - public FuncAsProc(final Func fnc) { - this.func = fnc; - } - - @Override - public void exec(final X input) throws Exception { - this.func.apply(input); - } -} diff --git a/src/main/java/org/cactoos/func/FuncOf.java b/src/main/java/org/cactoos/func/FuncOf.java new file mode 100644 index 0000000000..dbd395add3 --- /dev/null +++ b/src/main/java/org/cactoos/func/FuncOf.java @@ -0,0 +1,124 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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 java.util.concurrent.Callable; +import java.util.function.Consumer; +import java.util.function.Function; +import org.cactoos.Func; +import org.cactoos.Proc; + +/** + * Represents many possible inputs as {@link Func}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of input + * @param Type of output + * @since 0.12 + */ +public final class FuncOf implements Func { + + /** + * The func. + */ + private final Func func; + + /** + * Ctor. + * @param result The result + */ + public FuncOf(final Y result) { + this((Func) input -> result); + } + + /** + * Ctor. + * @param function The function + */ + public FuncOf(final Function function) { + this((Func) function::apply); + } + + /** + * Ctor. + * @param consumer The consumer + */ + public FuncOf(final Consumer consumer) { + this((Proc) consumer::accept); + } + + /** + * Ctor. + * @param callable The callable + */ + public FuncOf(final Callable callable) { + this((Func) input -> callable.call()); + } + + /** + * Ctor. + * @param runnable The runnable + */ + public FuncOf(final Runnable runnable) { + this((Proc) input -> runnable.run()); + } + + /** + * Ctor. + * @param proc The proc + */ + public FuncOf(final Proc proc) { + this(proc, null); + } + + /** + * Ctor. + * @param proc The proc + * @param result Result to return + */ + public FuncOf(final Proc proc, final Y result) { + this( + (Func) input -> { + proc.exec(input); + return result; + } + ); + } + + /** + * Ctor. + * @param fnc Func + */ + public FuncOf(final Func fnc) { + this.func = fnc; + } + + @Override + public Y apply(final X input) throws Exception { + return this.func.apply(input); + } +} diff --git a/src/main/java/org/cactoos/func/FuncWithFallback.java b/src/main/java/org/cactoos/func/FuncWithFallback.java index 9667a3d91b..567de92f8d 100644 --- a/src/main/java/org/cactoos/func/FuncWithFallback.java +++ b/src/main/java/org/cactoos/func/FuncWithFallback.java @@ -23,7 +23,9 @@ */ package org.cactoos.func; +import java.util.concurrent.Callable; import org.cactoos.Func; +import org.cactoos.Proc; /** * Func with a fallback plan. @@ -63,6 +65,186 @@ public FuncWithFallback(final Func fnc, this(fnc, fbk, input -> input); } + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Func fnc, + final Proc fbk, final Func flw) { + this(fnc, new FuncOf<>(fbk), flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Func fnc, + final Proc fbk, final Proc flw) { + this(fnc, new FuncOf<>(fbk), new FuncOf<>(flw)); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Func fnc, + final Func fbk, final Proc flw) { + this(fnc, fbk, new FuncOf<>(flw)); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Proc fnc, + final Func fbk, final Func flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Proc fnc, + final Proc fbk, final Func flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Proc fnc, + final Proc fbk, final Proc flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Proc fnc, + final Func fbk, final Proc flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Callable fnc, + final Func fbk, final Func flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Callable fnc, + final Proc fbk, final Func flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Callable fnc, + final Proc fbk, final Proc flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Callable fnc, + final Func fbk, final Proc flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Runnable fnc, + final Func fbk, final Proc flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Runnable fnc, + final Func fbk, final Func flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Runnable fnc, + final Proc fbk, final Func flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + + /** + * Ctor. + * @param fnc The func + * @param fbk The fallback + * @param flw The follow up func + * @since 0.12 + */ + public FuncWithFallback(final Runnable fnc, + final Proc fbk, final Proc flw) { + this(new FuncOf<>(fnc), fbk, flw); + } + /** * Ctor. * @param fnc The func diff --git a/src/main/java/org/cactoos/func/ProcAsFunc.java b/src/main/java/org/cactoos/func/ProcOf.java similarity index 61% rename from src/main/java/org/cactoos/func/ProcAsFunc.java rename to src/main/java/org/cactoos/func/ProcOf.java index ac41c7f061..11836a4b82 100644 --- a/src/main/java/org/cactoos/func/ProcAsFunc.java +++ b/src/main/java/org/cactoos/func/ProcOf.java @@ -23,23 +23,23 @@ */ package org.cactoos.func; +import java.util.concurrent.Callable; +import java.util.function.Consumer; +import java.util.function.Function; import org.cactoos.Func; import org.cactoos.Proc; /** - * Proc as a Func. - * - *

Be careful, this function will always return {@code null}.

+ * Func as Proc. * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of input - * @param Type of output - * @since 0.2 + * @since 0.12 */ -public final class ProcAsFunc implements Func { +public final class ProcOf implements Proc { /** * The proc. @@ -47,32 +47,55 @@ public final class ProcAsFunc implements Func { private final Proc proc; /** - * The result. + * Ctor. + * @param function The function */ - private final Y result; + public ProcOf(final Function function) { + this((Proc) function::apply); + } /** * Ctor. - * @param prc The proc + * @param consumer The consumer + */ + public ProcOf(final Consumer consumer) { + this((Proc) consumer::accept); + } + + /** + * Ctor. + * @param runnable The runnable + */ + public ProcOf(final Runnable runnable) { + this((Proc) input -> runnable.run()); + } + + /** + * Ctor. + * @param callable The callable + */ + public ProcOf(final Callable callable) { + this((Proc) input -> callable.call()); + } + + /** + * Ctor. + * @param fnc The proc */ - public ProcAsFunc(final Proc prc) { - this(prc, null); + public ProcOf(final Func fnc) { + this((Proc) fnc::apply); } /** * Ctor. * @param prc The proc - * @param rslt Result to return - * @since 0.7 */ - public ProcAsFunc(final Proc prc, final Y rslt) { + public ProcOf(final Proc prc) { this.proc = prc; - this.result = rslt; } @Override - public Y apply(final X input) throws Exception { + public void exec(final X input) throws Exception { this.proc.exec(input); - return this.result; } } diff --git a/src/main/java/org/cactoos/func/RepeatedFunc.java b/src/main/java/org/cactoos/func/RepeatedFunc.java index 5b2d55a4ab..6745144847 100644 --- a/src/main/java/org/cactoos/func/RepeatedFunc.java +++ b/src/main/java/org/cactoos/func/RepeatedFunc.java @@ -24,6 +24,7 @@ package org.cactoos.func; import org.cactoos.Func; +import org.cactoos.Proc; import org.cactoos.text.FormattedText; /** @@ -48,6 +49,16 @@ public final class RepeatedFunc implements Func { */ private final int times; + /** + * Ctor. + * @param proc Proc + * @param max How many times + * @since 0.12 + */ + public RepeatedFunc(final Proc proc, final int max) { + this(new FuncOf<>(proc), max); + } + /** * Ctor. * diff --git a/src/main/java/org/cactoos/func/RetryFunc.java b/src/main/java/org/cactoos/func/RetryFunc.java index 463a375e77..c1ebcd55e2 100644 --- a/src/main/java/org/cactoos/func/RetryFunc.java +++ b/src/main/java/org/cactoos/func/RetryFunc.java @@ -24,6 +24,7 @@ package org.cactoos.func; import org.cactoos.Func; +import org.cactoos.Proc; /** * Func that will try a few times before throwing an exception. @@ -48,6 +49,35 @@ public final class RetryFunc implements Func { */ private final Func exit; + /** + * Ctor. + * @param proc Func original + * @since 0.12 + */ + public RetryFunc(final Proc proc) { + this(new FuncOf<>(proc)); + } + + /** + * Ctor. + * @param proc Func original + * @param attempts Maximum number of attempts + * @since 0.12 + */ + public RetryFunc(final Proc proc, final int attempts) { + this(new FuncOf<>(proc), attempts); + } + + /** + * Ctor. + * @param proc Func original + * @param ext Exit condition, returns TRUE if there is no more reason to try + * @since 0.12 + */ + public RetryFunc(final Proc proc, final Func ext) { + this(new FuncOf<>(proc), ext); + } + /** * Ctor. * @param fnc Func original diff --git a/src/main/java/org/cactoos/func/RunnableAsFunc.java b/src/main/java/org/cactoos/func/RunnableAsFunc.java deleted file mode 100644 index 5cb6a1709e..0000000000 --- a/src/main/java/org/cactoos/func/RunnableAsFunc.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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.Func; - -/** - * Runnable as a Func. - * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @param Type of input - * @param Type of output - * @see FuncAsCallable - * @see FuncAsRunnable - * @see CallableAsFunc - * @since 0.2 - */ -public final class RunnableAsFunc implements Func { - - /** - * The runnable. - */ - private final Runnable runnable; - - /** - * Ctor. - * @param rnbl The runnable - */ - public RunnableAsFunc(final Runnable rnbl) { - this.runnable = rnbl; - } - - @Override - public Y apply(final X input) { - this.runnable.run(); - return null; - } -} diff --git a/src/main/java/org/cactoos/func/FuncAsRunnable.java b/src/main/java/org/cactoos/func/RunnableOf.java similarity index 81% rename from src/main/java/org/cactoos/func/FuncAsRunnable.java rename to src/main/java/org/cactoos/func/RunnableOf.java index 16da75be14..e077c17e8a 100644 --- a/src/main/java/org/cactoos/func/FuncAsRunnable.java +++ b/src/main/java/org/cactoos/func/RunnableOf.java @@ -23,6 +23,7 @@ */ package org.cactoos.func; +import java.util.concurrent.Callable; import org.cactoos.Func; import org.cactoos.Proc; @@ -33,9 +34,9 @@ * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.12 */ -public final class FuncAsRunnable implements Runnable { +public final class RunnableOf implements Runnable { /** * Original func. @@ -47,15 +48,23 @@ public final class FuncAsRunnable implements Runnable { * @param proc Encapsulated proc * @since 0.11 */ - public FuncAsRunnable(final Proc proc) { - this(new ProcAsFunc<>(proc)); + public RunnableOf(final Callable proc) { + this(new FuncOf<>(proc)); + } + + /** + * Ctor. + * @param proc Encapsulated proc + */ + public RunnableOf(final Proc proc) { + this(new FuncOf<>(proc)); } /** * Ctor. * @param fnc Encapsulated func */ - public FuncAsRunnable(final Func fnc) { + public RunnableOf(final Func fnc) { this.func = fnc; } diff --git a/src/main/java/org/cactoos/func/RunnableWithFallback.java b/src/main/java/org/cactoos/func/RunnableWithFallback.java deleted file mode 100644 index c616b0e047..0000000000 --- a/src/main/java/org/cactoos/func/RunnableWithFallback.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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; - -/** - * Runnable with a fallback plan. - * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.6 - */ -public final class RunnableWithFallback implements Runnable { - - /** - * The runnable. - */ - private final Runnable runnable; - - /** - * The fallback. - */ - private final Proc fallback; - - /** - * Ctor. - * @param rnb Runnable - * @param fbk The fallback - */ - public RunnableWithFallback(final Runnable rnb, - final Proc fbk) { - this.runnable = rnb; - this.fallback = fbk; - } - - @Override - public void run() { - new UncheckedFunc<>( - new FuncWithFallback( - new RunnableAsFunc<>(this.runnable), - new ProcAsFunc<>(this.fallback) - ) - ).apply(true); - } - -} diff --git a/src/main/java/org/cactoos/func/SyncFunc.java b/src/main/java/org/cactoos/func/SyncFunc.java index ccc8d80f41..a12d13802c 100644 --- a/src/main/java/org/cactoos/func/SyncFunc.java +++ b/src/main/java/org/cactoos/func/SyncFunc.java @@ -23,7 +23,9 @@ */ package org.cactoos.func; +import java.util.concurrent.Callable; import org.cactoos.Func; +import org.cactoos.Proc; /** * Func that is thread-safe. @@ -41,6 +43,33 @@ public final class SyncFunc implements Func { */ private final Func func; + /** + * Ctor. + * @param runnable Func original + * @since 0.12 + */ + public SyncFunc(final Runnable runnable) { + this(new FuncOf<>(runnable)); + } + + /** + * Ctor. + * @param callable Func original + * @since 0.12 + */ + public SyncFunc(final Callable callable) { + this(new FuncOf<>(callable)); + } + + /** + * Ctor. + * @param proc Func original + * @since 0.12 + */ + public SyncFunc(final Proc proc) { + this(new FuncOf<>(proc)); + } + /** * 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 ed9de046ef..0000000000 --- a/src/main/java/org/cactoos/func/SyncProc.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @param Type of input - * @since 0.4 - */ -public final class SyncProc implements Proc { - - /** - * Original proc. - */ - private final Proc proc; - - /** - * Ctor. - * @param prc Func original - */ - public SyncProc(final Proc prc) { - this.proc = prc; - } - - @Override - public void exec(final X input) throws Exception { - synchronized (this.proc) { - this.proc.exec(input); - } - } - -} diff --git a/src/main/java/org/cactoos/func/UncheckedProc.java b/src/main/java/org/cactoos/func/UncheckedProc.java index 5999d2d3e2..1897676bcd 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 ProcAsFunc<>(this.proc)).apply(input); + new UncheckedFunc<>(new FuncOf<>(this.proc)).apply(input); } } diff --git a/src/test/java/org/cactoos/func/AndTest.java b/src/test/java/org/cactoos/func/AndTest.java index b3bf841ad9..ed93c7f1bd 100644 --- a/src/test/java/org/cactoos/func/AndTest.java +++ b/src/test/java/org/cactoos/func/AndTest.java @@ -97,9 +97,9 @@ public void iteratesList() { MatcherAssert.assertThat( "Can't iterate a list with a procedure", new And( - new MappedIterable>( + new MappedIterable<>( new ArrayAsIterable<>("hello", "world"), - new ProcAsFunc<>(list::add, () -> true) + new FuncOf<>(list::add, () -> true) ) ), new ScalarHasValue<>( @@ -121,7 +121,7 @@ public void iteratesEmptyList() { new And( new MappedIterable>( Collections.emptyList(), - new ProcAsFunc<>(list::add, () -> true) + new FuncOf<>(list::add, () -> true) ) ), new ScalarHasValue<>( diff --git a/src/test/java/org/cactoos/func/FuncAsCallableTest.java b/src/test/java/org/cactoos/func/CallableOfTest.java similarity index 93% rename from src/test/java/org/cactoos/func/FuncAsCallableTest.java rename to src/test/java/org/cactoos/func/CallableOfTest.java index 23807c3544..2268917b2e 100644 --- a/src/test/java/org/cactoos/func/FuncAsCallableTest.java +++ b/src/test/java/org/cactoos/func/CallableOfTest.java @@ -28,19 +28,19 @@ import org.junit.Test; /** - * Test case for {@link FuncAsCallable}. + * Test case for {@link CallableOf}. * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @since 0.2 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class FuncAsCallableTest { +public final class CallableOfTest { @Test public void convertsFuncIntoCallable() throws Exception { MatcherAssert.assertThat( - new FuncAsCallable<>( + new CallableOf<>( input -> 1 ).call(), Matchers.equalTo(1) diff --git a/src/test/java/org/cactoos/func/CallableWithFallbackTest.java b/src/test/java/org/cactoos/func/CallableWithFallbackTest.java deleted file mode 100644 index 6876b7820f..0000000000 --- a/src/test/java/org/cactoos/func/CallableWithFallbackTest.java +++ /dev/null @@ -1,73 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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 java.io.IOException; -import org.cactoos.FuncApplies; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link CallableWithFallback}. - * - * @author Vladislav Cheparin (ivladislav969@gmail.com) - * @version $Id$ - * @since 0.11 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class CallableWithFallbackTest { - - @Test - public void usesMainFunc() throws Exception { - MatcherAssert.assertThat( - "Can't use the main function if no exception", - new CallableAsFunc<>( - new CallableWithFallback<>( - () -> 1, - input -> { - throw new IOException(input); - } - ) - ), - new FuncApplies<>(true, Matchers.equalTo(1)) - ); - } - - @Test - public void usesFallback() throws Exception { - MatcherAssert.assertThat( - "Can't use the fallback function if there is exception", - new CallableAsFunc<>( - new CallableWithFallback<>( - () -> { - throw new IllegalStateException("intended"); - }, - input -> 1 - ) - ), - new FuncApplies<>(true, Matchers.equalTo(1)) - ); - } -} diff --git a/src/test/java/org/cactoos/func/ConstFuncTest.java b/src/test/java/org/cactoos/func/ConstFuncTest.java deleted file mode 100644 index 53823302e1..0000000000 --- a/src/test/java/org/cactoos/func/ConstFuncTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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.FuncApplies; -import org.hamcrest.MatcherAssert; -import org.junit.Test; - -/** - * Test case for {@link ConstFunc}. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.2 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class ConstFuncTest { - - @Test - public void returnsConstant() { - MatcherAssert.assertThat( - "Can't return a constant", - new ConstFunc<>(1), - new FuncApplies<>(-1, 1) - ); - } - -} diff --git a/src/test/java/org/cactoos/func/RepeatedFuncTest.java b/src/test/java/org/cactoos/func/RepeatedFuncTest.java index 6542189169..056ec0ac2d 100644 --- a/src/test/java/org/cactoos/func/RepeatedFuncTest.java +++ b/src/test/java/org/cactoos/func/RepeatedFuncTest.java @@ -42,7 +42,9 @@ public final class RepeatedFuncTest { @Test public void runsFuncMultipleTimes() throws Exception { final Func func = new RepeatedFunc<>( - input -> new SecureRandom().nextInt(), + input -> { + return new SecureRandom().nextInt(); + }, 2 ); MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/func/FuncAsRunnableTest.java b/src/test/java/org/cactoos/func/RunnableOfTest.java similarity index 94% rename from src/test/java/org/cactoos/func/FuncAsRunnableTest.java rename to src/test/java/org/cactoos/func/RunnableOfTest.java index 8d3571f9cd..9654261ec0 100644 --- a/src/test/java/org/cactoos/func/FuncAsRunnableTest.java +++ b/src/test/java/org/cactoos/func/RunnableOfTest.java @@ -28,21 +28,21 @@ import org.junit.Test; /** - * Test case for {@link FuncAsRunnable}. + * Test case for {@link RunnableOf}. * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @since 0.2 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class FuncAsRunnableTest { +public final class RunnableOfTest { @Test public void convertsFuncIntoRunnable() throws Exception { final AtomicBoolean done = new AtomicBoolean(); MatcherAssert.assertThat( "Can't execute Runnable", - new FuncAsRunnable( + new RunnableOf( input -> { done.set(true); return 1; diff --git a/src/test/java/org/cactoos/func/RunnableWithFallbackTest.java b/src/test/java/org/cactoos/func/RunnableWithFallbackTest.java deleted file mode 100644 index 0d3b4ecb6f..0000000000 --- a/src/test/java/org/cactoos/func/RunnableWithFallbackTest.java +++ /dev/null @@ -1,76 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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 java.io.IOException; -import org.cactoos.FuncApplies; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link RunnableWithFallback}. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.6 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class RunnableWithFallbackTest { - - @Test - public void usesMainFunc() throws Exception { - MatcherAssert.assertThat( - "Can't use the main function if no exception", - new RunnableAsFunc<>( - new RunnableWithFallback( - () -> { - }, - input -> { - throw new IOException(input); - } - ) - ), - new FuncApplies<>(true, Matchers.nullValue()) - ); - } - - @Test - public void usesFallback() throws Exception { - MatcherAssert.assertThat( - "Can't use the fallback function if there is exception", - new RunnableAsFunc<>( - new RunnableWithFallback( - () -> { - throw new IllegalStateException("intended"); - }, - input -> { - } - ) - ), - new FuncApplies<>(true, Matchers.nullValue()) - ); - } - -} From 7678a34808387a7ac90b53c14994f299b81f7979 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 19 Jul 2017 12:45:21 +0300 Subject: [PATCH 2/3] #274: ctors fixed --- .../java/org/cactoos/func/CallableOf.java | 28 +++++++++++++++---- src/main/java/org/cactoos/func/FuncOf.java | 20 +------------ .../{FuncAsMatcher.java => MatcherOf.java} | 15 ++++++++-- src/main/java/org/cactoos/func/ProcOf.java | 18 ------------ .../java/org/cactoos/func/RunnableOf.java | 28 +++++++++++++++---- src/test/java/org/cactoos/func/AndTest.java | 8 ++++-- .../java/org/cactoos/func/AsyncFuncTest.java | 8 ++++-- .../java/org/cactoos/func/RunnableOfTest.java | 4 +-- .../java/org/cactoos/io/InputAsBytesTest.java | 8 ++++-- .../org/cactoos/io/InputAsPropertiesTest.java | 8 ++++-- .../java/org/cactoos/io/StickyInputTest.java | 4 +-- .../java/org/cactoos/io/TeeInputTest.java | 28 +++++++++++-------- .../org/cactoos/list/MapAsPropertiesTest.java | 8 ++++-- 13 files changed, 102 insertions(+), 83 deletions(-) rename src/main/java/org/cactoos/func/{FuncAsMatcher.java => MatcherOf.java} (86%) diff --git a/src/main/java/org/cactoos/func/CallableOf.java b/src/main/java/org/cactoos/func/CallableOf.java index 3aef3a2602..a03e674386 100644 --- a/src/main/java/org/cactoos/func/CallableOf.java +++ b/src/main/java/org/cactoos/func/CallableOf.java @@ -42,15 +42,21 @@ * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ - * @param Type of input + * @param Type of input + * @param Type of output * @since 0.12 */ -public final class CallableOf implements Callable { +public final class CallableOf implements Callable { /** * Original func. */ - private final Func func; + private final Func func; + + /** + * The input. + */ + private final X input; /** * Ctor. @@ -66,7 +72,7 @@ public CallableOf(final Runnable runnable) { * @param proc Encapsulated proc * @since 0.12 */ - public CallableOf(final Proc proc) { + public CallableOf(final Proc proc) { this(new FuncOf<>(proc)); } @@ -74,12 +80,22 @@ public CallableOf(final Proc proc) { * Ctor. * @param fnc Encapsulated func */ - public CallableOf(final Func fnc) { + public CallableOf(final Func fnc) { + this(fnc, null); + } + + /** + * Ctor. + * @param fnc Encapsulated func + * @param ipt Input + */ + public CallableOf(final Func fnc, final X ipt) { this.func = fnc; + this.input = ipt; } @Override public T call() throws Exception { - return this.func.apply(null); + return this.func.apply(this.input); } } diff --git a/src/main/java/org/cactoos/func/FuncOf.java b/src/main/java/org/cactoos/func/FuncOf.java index dbd395add3..40e3f9689b 100644 --- a/src/main/java/org/cactoos/func/FuncOf.java +++ b/src/main/java/org/cactoos/func/FuncOf.java @@ -24,8 +24,6 @@ package org.cactoos.func; import java.util.concurrent.Callable; -import java.util.function.Consumer; -import java.util.function.Function; import org.cactoos.Func; import org.cactoos.Proc; @@ -55,22 +53,6 @@ public FuncOf(final Y result) { this((Func) input -> result); } - /** - * Ctor. - * @param function The function - */ - public FuncOf(final Function function) { - this((Func) function::apply); - } - - /** - * Ctor. - * @param consumer The consumer - */ - public FuncOf(final Consumer consumer) { - this((Proc) consumer::accept); - } - /** * Ctor. * @param callable The callable @@ -102,7 +84,7 @@ public FuncOf(final Proc proc) { */ public FuncOf(final Proc proc, final Y result) { this( - (Func) input -> { + input -> { proc.exec(input); return result; } diff --git a/src/main/java/org/cactoos/func/FuncAsMatcher.java b/src/main/java/org/cactoos/func/MatcherOf.java similarity index 86% rename from src/main/java/org/cactoos/func/FuncAsMatcher.java rename to src/main/java/org/cactoos/func/MatcherOf.java index b7f31ebdaa..0855d73a44 100644 --- a/src/main/java/org/cactoos/func/FuncAsMatcher.java +++ b/src/main/java/org/cactoos/func/MatcherOf.java @@ -24,6 +24,7 @@ package org.cactoos.func; import org.cactoos.Func; +import org.cactoos.Proc; import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher; @@ -35,20 +36,28 @@ * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of object to match - * @since 0.2 + * @since 0.12 */ -public final class FuncAsMatcher extends TypeSafeMatcher { +public final class MatcherOf extends TypeSafeMatcher { /** * The func. */ private final Func func; + /** + * Ctor. + * @param proc The func + */ + public MatcherOf(final Proc proc) { + this(new FuncOf<>(proc)); + } + /** * Ctor. * @param fnc The func */ - public FuncAsMatcher(final Func fnc) { + public MatcherOf(final Func fnc) { super(); this.func = fnc; } diff --git a/src/main/java/org/cactoos/func/ProcOf.java b/src/main/java/org/cactoos/func/ProcOf.java index 11836a4b82..5ea783ea87 100644 --- a/src/main/java/org/cactoos/func/ProcOf.java +++ b/src/main/java/org/cactoos/func/ProcOf.java @@ -24,8 +24,6 @@ package org.cactoos.func; import java.util.concurrent.Callable; -import java.util.function.Consumer; -import java.util.function.Function; import org.cactoos.Func; import org.cactoos.Proc; @@ -46,22 +44,6 @@ public final class ProcOf implements Proc { */ private final Proc proc; - /** - * Ctor. - * @param function The function - */ - public ProcOf(final Function function) { - this((Proc) function::apply); - } - - /** - * Ctor. - * @param consumer The consumer - */ - public ProcOf(final Consumer consumer) { - this((Proc) consumer::accept); - } - /** * Ctor. * @param runnable The runnable diff --git a/src/main/java/org/cactoos/func/RunnableOf.java b/src/main/java/org/cactoos/func/RunnableOf.java index e077c17e8a..28e2fe139b 100644 --- a/src/main/java/org/cactoos/func/RunnableOf.java +++ b/src/main/java/org/cactoos/func/RunnableOf.java @@ -34,21 +34,27 @@ * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ + * @param Type of input * @since 0.12 */ -public final class RunnableOf implements Runnable { +public final class RunnableOf implements Runnable { /** * Original func. */ - private final Func func; + private final Func func; + + /** + * The input. + */ + private final X input; /** * Ctor. * @param proc Encapsulated proc * @since 0.11 */ - public RunnableOf(final Callable proc) { + public RunnableOf(final Callable proc) { this(new FuncOf<>(proc)); } @@ -56,7 +62,7 @@ public RunnableOf(final Callable proc) { * Ctor. * @param proc Encapsulated proc */ - public RunnableOf(final Proc proc) { + public RunnableOf(final Proc proc) { this(new FuncOf<>(proc)); } @@ -64,12 +70,22 @@ public RunnableOf(final Proc proc) { * Ctor. * @param fnc Encapsulated func */ - public RunnableOf(final Func fnc) { + public RunnableOf(final Func fnc) { + this(fnc, null); + } + + /** + * Ctor. + * @param fnc Encapsulated func + * @param ipt Input + */ + public RunnableOf(final Func fnc, final X ipt) { this.func = fnc; + this.input = ipt; } @Override public void run() { - new UncheckedFunc<>(this.func).apply(null); + new UncheckedFunc<>(this.func).apply(this.input); } } diff --git a/src/test/java/org/cactoos/func/AndTest.java b/src/test/java/org/cactoos/func/AndTest.java index ed93c7f1bd..42bbea2f6b 100644 --- a/src/test/java/org/cactoos/func/AndTest.java +++ b/src/test/java/org/cactoos/func/AndTest.java @@ -105,7 +105,7 @@ public void iteratesList() { new ScalarHasValue<>( Matchers.allOf( Matchers.equalTo(true), - new FuncAsMatcher<>( + new MatcherOf<>( value -> list.size() == 2 ) ) @@ -127,8 +127,10 @@ public void iteratesEmptyList() { new ScalarHasValue<>( Matchers.allOf( Matchers.equalTo(true), - new FuncAsMatcher<>( - value -> list.isEmpty() + new MatcherOf<>( + value -> { + return list.isEmpty(); + } ) ) ) diff --git a/src/test/java/org/cactoos/func/AsyncFuncTest.java b/src/test/java/org/cactoos/func/AsyncFuncTest.java index 22d26d0411..86e5e7e584 100644 --- a/src/test/java/org/cactoos/func/AsyncFuncTest.java +++ b/src/test/java/org/cactoos/func/AsyncFuncTest.java @@ -52,7 +52,7 @@ public void runsInBackground() { ), new FuncApplies<>( true, - new FuncAsMatcher>( + new MatcherOf>( future -> !future.isDone() ) ) @@ -71,8 +71,10 @@ public void runsInBackgroundWithoutFuture() { ), new FuncApplies<>( true, - new FuncAsMatcher>( - future -> latch.await(1L, TimeUnit.SECONDS) + new MatcherOf>( + future -> { + return latch.await(1L, TimeUnit.SECONDS); + } ) ) ); diff --git a/src/test/java/org/cactoos/func/RunnableOfTest.java b/src/test/java/org/cactoos/func/RunnableOfTest.java index 9654261ec0..527d44aa54 100644 --- a/src/test/java/org/cactoos/func/RunnableOfTest.java +++ b/src/test/java/org/cactoos/func/RunnableOfTest.java @@ -42,13 +42,13 @@ public void convertsFuncIntoRunnable() throws Exception { final AtomicBoolean done = new AtomicBoolean(); MatcherAssert.assertThat( "Can't execute Runnable", - new RunnableOf( + new RunnableOf<>( input -> { done.set(true); return 1; } ), - new FuncAsMatcher( + new MatcherOf( input -> { input.run(); return done.get(); diff --git a/src/test/java/org/cactoos/io/InputAsBytesTest.java b/src/test/java/org/cactoos/io/InputAsBytesTest.java index fc248fa1ce..9844fee99c 100644 --- a/src/test/java/org/cactoos/io/InputAsBytesTest.java +++ b/src/test/java/org/cactoos/io/InputAsBytesTest.java @@ -28,7 +28,7 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicBoolean; -import org.cactoos.func.FuncAsMatcher; +import org.cactoos.func.MatcherOf; import org.cactoos.list.EndlessIterable; import org.cactoos.list.LimitedIterable; import org.cactoos.text.BytesAsText; @@ -154,8 +154,10 @@ public void close() throws IOException { ).asBytes(), StandardCharsets.UTF_8 ).asString(), - new FuncAsMatcher<>( - text -> closed.get() + new MatcherOf<>( + text -> { + return closed.get(); + } ) ); } diff --git a/src/test/java/org/cactoos/io/InputAsPropertiesTest.java b/src/test/java/org/cactoos/io/InputAsPropertiesTest.java index b4938f1445..389dc870eb 100644 --- a/src/test/java/org/cactoos/io/InputAsPropertiesTest.java +++ b/src/test/java/org/cactoos/io/InputAsPropertiesTest.java @@ -25,7 +25,7 @@ import java.util.Properties; import org.cactoos.ScalarHasValue; -import org.cactoos.func.FuncAsMatcher; +import org.cactoos.func.MatcherOf; import org.hamcrest.MatcherAssert; import org.junit.Test; @@ -46,8 +46,10 @@ public void readsInputContent() { "foo=Hello, world!\nbar=works fine?\n" ), new ScalarHasValue<>( - new FuncAsMatcher( - props -> "Hello, world!".equals(props.getProperty("foo")) + new MatcherOf( + props -> { + return "Hello, world!".equals(props.getProperty("foo")); + } ) ) ); diff --git a/src/test/java/org/cactoos/io/StickyInputTest.java b/src/test/java/org/cactoos/io/StickyInputTest.java index f63605f80f..753e3ed7c7 100644 --- a/src/test/java/org/cactoos/io/StickyInputTest.java +++ b/src/test/java/org/cactoos/io/StickyInputTest.java @@ -27,7 +27,7 @@ import org.cactoos.Input; import org.cactoos.ScalarHasValue; import org.cactoos.TextHasString; -import org.cactoos.func.FuncAsMatcher; +import org.cactoos.func.MatcherOf; import org.cactoos.func.RepeatedFunc; import org.cactoos.text.BytesAsText; import org.hamcrest.MatcherAssert; @@ -53,7 +53,7 @@ public void readsFileContent() { "org/cactoos/large-text.txt" ) ), - new FuncAsMatcher<>( + new MatcherOf<>( new RepeatedFunc( input -> new InputAsBytes( new TeeInput(input, new DeadOutput()) diff --git a/src/test/java/org/cactoos/io/TeeInputTest.java b/src/test/java/org/cactoos/io/TeeInputTest.java index 93914a8e30..9bf54d3ff8 100644 --- a/src/test/java/org/cactoos/io/TeeInputTest.java +++ b/src/test/java/org/cactoos/io/TeeInputTest.java @@ -29,7 +29,7 @@ import java.nio.file.Files; import java.nio.file.Path; import org.cactoos.TextHasString; -import org.cactoos.func.FuncAsMatcher; +import org.cactoos.func.MatcherOf; import org.cactoos.text.BytesAsText; import org.cactoos.text.TextAsBytes; import org.hamcrest.MatcherAssert; @@ -62,10 +62,12 @@ public void copiesContent() { ) ), new TextHasString( - new FuncAsMatcher<>( - str -> new String( - baos.toByteArray(), StandardCharsets.UTF_8 - ).equals(str) + new MatcherOf<>( + str -> { + return new String( + baos.toByteArray(), StandardCharsets.UTF_8 + ).equals(str); + } ) ) ); @@ -82,13 +84,15 @@ public void copiesToFile() throws IOException { ) ), new TextHasString( - new FuncAsMatcher<>( - str -> str.equals( - new String( - Files.readAllBytes(temp), - StandardCharsets.UTF_8 - ) - ) + new MatcherOf<>( + str -> { + return str.equals( + new String( + Files.readAllBytes(temp), + StandardCharsets.UTF_8 + ) + ); + } ) ) ); diff --git a/src/test/java/org/cactoos/list/MapAsPropertiesTest.java b/src/test/java/org/cactoos/list/MapAsPropertiesTest.java index 2da2625492..d41bb07e7d 100644 --- a/src/test/java/org/cactoos/list/MapAsPropertiesTest.java +++ b/src/test/java/org/cactoos/list/MapAsPropertiesTest.java @@ -27,7 +27,7 @@ import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; import org.cactoos.ScalarHasValue; -import org.cactoos.func.FuncAsMatcher; +import org.cactoos.func.MatcherOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -56,8 +56,10 @@ public void convertsMapToProperties() { ) ), new ScalarHasValue<>( - new FuncAsMatcher( - props -> props.getProperty("0").endsWith(", world") + new MatcherOf( + props -> { + return props.getProperty("0").endsWith(", world"); + } ) ) ); From d46fb6fb63391e16afe271b8cca94005e830f52f Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Wed, 19 Jul 2017 09:28:32 -0300 Subject: [PATCH 3/3] #327: Eclipse cannot infer type arguments for AndTest --- src/test/java/org/cactoos/func/AndTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/func/AndTest.java b/src/test/java/org/cactoos/func/AndTest.java index 42bbea2f6b..87d9e6a2bb 100644 --- a/src/test/java/org/cactoos/func/AndTest.java +++ b/src/test/java/org/cactoos/func/AndTest.java @@ -97,7 +97,7 @@ public void iteratesList() { MatcherAssert.assertThat( "Can't iterate a list with a procedure", new And( - new MappedIterable<>( + new MappedIterable>( new ArrayAsIterable<>("hello", "world"), new FuncOf<>(list::add, () -> true) )