From 026638e2291ed51498d57b3a46fae9786a3e8dd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Wed, 30 Dec 2020 17:17:37 +0100 Subject: [PATCH] (#1445) Cleanup around Scalar and Callable --- src/main/java/org/cactoos/func/BiFuncOf.java | 31 +------- src/main/java/org/cactoos/func/FuncOf.java | 33 +-------- .../java/org/cactoos/func/RunnableOf.java | 62 ++++++---------- src/main/java/org/cactoos/func/SyncFunc.java | 31 -------- src/main/java/org/cactoos/io/TempFolder.java | 5 +- src/main/java/org/cactoos/proc/ProcOf.java | 19 +---- src/main/java/org/cactoos/scalar/Binary.java | 21 +----- .../java/org/cactoos/scalar/CallableOf.java | 40 +--------- src/main/java/org/cactoos/scalar/FirstOf.java | 4 +- src/main/java/org/cactoos/scalar/ItemAt.java | 2 +- .../{RepeatedCallable.java => Repeated.java} | 30 ++++---- .../java/org/cactoos/scalar/ScalarOf.java | 50 ++++++++++--- .../org/cactoos/scalar/ScalarOfCallable.java | 44 ----------- .../org/cactoos/experimental/TimedTest.java | 18 ++--- .../java/org/cactoos/func/BiFuncOfTest.java | 39 +++------- .../java/org/cactoos/func/FuncOfTest.java | 24 ++---- .../java/org/cactoos/func/RunnableOfTest.java | 43 ++++++----- .../java/org/cactoos/func/SyncFuncTest.java | 73 ------------------- .../java/org/cactoos/iterable/PagedTest.java | 4 +- src/test/java/org/cactoos/map/MapOfTest.java | 20 ++--- .../java/org/cactoos/proc/ProcOfTest.java | 64 ++-------------- .../org/cactoos/scalar/CallableOfTest.java | 47 +----------- ...tedCallableTest.java => RepeatedTest.java} | 31 ++++---- ...rOfCallableTest.java => ScalarOfTest.java} | 43 ++++++++++- 24 files changed, 216 insertions(+), 562 deletions(-) rename src/main/java/org/cactoos/scalar/{RepeatedCallable.java => Repeated.java} (69%) delete mode 100644 src/main/java/org/cactoos/scalar/ScalarOfCallable.java rename src/test/java/org/cactoos/scalar/{RepeatedCallableTest.java => RepeatedTest.java} (77%) rename src/test/java/org/cactoos/scalar/{ScalarOfCallableTest.java => ScalarOfTest.java} (55%) diff --git a/src/main/java/org/cactoos/func/BiFuncOf.java b/src/main/java/org/cactoos/func/BiFuncOf.java index 98d3047ff4..971fc517c4 100644 --- a/src/main/java/org/cactoos/func/BiFuncOf.java +++ b/src/main/java/org/cactoos/func/BiFuncOf.java @@ -23,13 +23,11 @@ */ package org.cactoos.func; -import java.util.concurrent.Callable; import org.cactoos.BiFunc; import org.cactoos.BiProc; import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.Scalar; -import org.cactoos.scalar.CallableOf; /** * Represents many possible inputs as {@link BiFunc}. @@ -48,14 +46,6 @@ public final class BiFuncOf implements BiFunc { */ private final BiFunc func; - /** - * Ctor. - * @param result The result - */ - public BiFuncOf(final Z result) { - this((first, second) -> result); - } - /** * Ctor. * @param scalar The scalar @@ -100,29 +90,11 @@ public BiFuncOf(final BiProc proc, final Z result) { ); } - /** - * Ctor. - * @param callable The callable - */ - public BiFuncOf(final Callable callable) { - this((first, second) -> callable.call()); - } - - /** - * Ctor. - * @param runnable The runnable - * @param result Result to return - * @since 0.32 - */ - public BiFuncOf(final Runnable runnable, final Z result) { - this(new CallableOf<>(runnable, result)); - } - /** * Ctor. * @param fnc Func */ - private BiFuncOf(final BiFunc fnc) { + public BiFuncOf(final BiFunc fnc) { this.func = fnc; } @@ -130,5 +102,4 @@ private BiFuncOf(final BiFunc fnc) { public Z apply(final X first, final Y second) throws Exception { return this.func.apply(first, second); } - } diff --git a/src/main/java/org/cactoos/func/FuncOf.java b/src/main/java/org/cactoos/func/FuncOf.java index 7e464ba71c..6b673fad58 100644 --- a/src/main/java/org/cactoos/func/FuncOf.java +++ b/src/main/java/org/cactoos/func/FuncOf.java @@ -23,7 +23,6 @@ */ package org.cactoos.func; -import java.util.concurrent.Callable; import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.Scalar; @@ -44,32 +43,6 @@ public final class FuncOf implements Func { */ private final Func func; - /** - * Ctor. - * @param result The result - */ - public FuncOf(final Y result) { - this((Func) input -> result); - } - - /** - * Ctor. - * @param callable The callable - */ - public FuncOf(final Callable callable) { - this((Func) input -> callable.call()); - } - - /** - * Ctor. - * @param runnable The runnable - * @param result Result to return - * @since 0.32 - */ - public FuncOf(final Runnable runnable, final Y result) { - this(input -> runnable.run(), result); - } - /** * Ctor. * @param proc The proc @@ -89,16 +62,14 @@ public FuncOf(final Proc proc, final Y result) { * @param scalar Origin scalar */ public FuncOf(final Scalar scalar) { - this(input -> { - return scalar.value(); - }); + this(input -> scalar.value()); } /** * Ctor. * @param fnc Func */ - private FuncOf(final Func fnc) { + public FuncOf(final Func fnc) { this.func = fnc; } diff --git a/src/main/java/org/cactoos/func/RunnableOf.java b/src/main/java/org/cactoos/func/RunnableOf.java index 4eccfd4669..fe9e2d2092 100644 --- a/src/main/java/org/cactoos/func/RunnableOf.java +++ b/src/main/java/org/cactoos/func/RunnableOf.java @@ -23,79 +23,61 @@ */ package org.cactoos.func; -import java.util.concurrent.Callable; import org.cactoos.Func; import org.cactoos.Proc; +import org.cactoos.Scalar; import org.cactoos.proc.ProcOf; -import org.cactoos.proc.UncheckedProc; +import org.cactoos.scalar.Unchecked; /** * Func as Runnable. * *

There is no thread-safety guarantee. * - * @param Type of input * @since 0.12 */ -public final class RunnableOf implements Runnable { +public final class RunnableOf implements Runnable { /** - * Original proc. + * Runnable. */ - private final Proc proc; - - /** - * The input. - */ - private final X input; - - /** - * Ctor. - * @param proc Encapsulated proc - * @since 0.11 - */ - public RunnableOf(final Callable proc) { - this(new FuncOf<>(proc)); - } - - /** - * Ctor. - * @param proc Encapsulated proc - */ - public RunnableOf(final Proc proc) { - this(proc, null); - } + private final Scalar scalar; /** * Ctor. * @param fnc Encapsulated func + * @param ipt Input + * @param Type of input */ - public RunnableOf(final Func fnc) { - this(fnc, null); + public RunnableOf(final Func fnc, final X ipt) { + this(new ProcOf<>(fnc), ipt); } /** * Ctor. - * @param fnc Encapsulated func + * @param proc Encapsulated proc * @param ipt Input + * @param Type of input + * @since 0.32 */ - public RunnableOf(final Func fnc, final X ipt) { - this(new ProcOf<>(fnc), ipt); + public RunnableOf(final Proc proc, final X ipt) { + this(() -> { + proc.exec(ipt); + return true; + }); } /** * Ctor. - * @param proc Encapsulated proc - * @param ipt Input - * @since 0.32 + * @param runnable Encapsulated scalar + * @since 0.11 */ - public RunnableOf(final Proc proc, final X ipt) { - this.proc = proc; - this.input = ipt; + public RunnableOf(final Scalar runnable) { + this.scalar = runnable; } @Override public void run() { - new UncheckedProc<>(this.proc).exec(this.input); + new Unchecked<>(this.scalar).value(); } } diff --git a/src/main/java/org/cactoos/func/SyncFunc.java b/src/main/java/org/cactoos/func/SyncFunc.java index 944451fa16..f3f107af78 100644 --- a/src/main/java/org/cactoos/func/SyncFunc.java +++ b/src/main/java/org/cactoos/func/SyncFunc.java @@ -23,9 +23,7 @@ */ package org.cactoos.func; -import java.util.concurrent.Callable; import org.cactoos.Func; -import org.cactoos.Proc; /** * Func that is thread-safe. @@ -48,35 +46,6 @@ 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 - * @since 0.12 - */ - 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/io/TempFolder.java b/src/main/java/org/cactoos/io/TempFolder.java index 39137ed685..2ba56f4f3a 100644 --- a/src/main/java/org/cactoos/io/TempFolder.java +++ b/src/main/java/org/cactoos/io/TempFolder.java @@ -35,7 +35,6 @@ import org.cactoos.iterable.Sorted; import org.cactoos.proc.ForEach; import org.cactoos.proc.IoCheckedProc; -import org.cactoos.proc.ProcOf; import org.cactoos.scalar.IoChecked; import org.cactoos.scalar.Sticky; import org.cactoos.text.Joined; @@ -133,9 +132,7 @@ public Path value() throws Exception { public void close() throws IOException { new IoCheckedProc<>( new ForEach( - new ProcOf<>( - path -> path.toFile().delete() - ) + path -> path.toFile().delete() ) ).exec( new Sorted<>( diff --git a/src/main/java/org/cactoos/proc/ProcOf.java b/src/main/java/org/cactoos/proc/ProcOf.java index a01b580c7f..8fe9fdca5d 100644 --- a/src/main/java/org/cactoos/proc/ProcOf.java +++ b/src/main/java/org/cactoos/proc/ProcOf.java @@ -23,7 +23,6 @@ */ package org.cactoos.proc; -import java.util.concurrent.Callable; import org.cactoos.Func; import org.cactoos.Proc; @@ -42,22 +41,6 @@ public final class ProcOf implements Proc { */ private final Proc proc; - /** - * 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 @@ -70,7 +53,7 @@ public ProcOf(final Func fnc) { * Ctor. * @param prc The proc */ - private ProcOf(final Proc prc) { + public ProcOf(final Proc prc) { this.proc = prc; } diff --git a/src/main/java/org/cactoos/scalar/Binary.java b/src/main/java/org/cactoos/scalar/Binary.java index e54584583e..e324ebccfd 100644 --- a/src/main/java/org/cactoos/scalar/Binary.java +++ b/src/main/java/org/cactoos/scalar/Binary.java @@ -23,9 +23,7 @@ */ package org.cactoos.scalar; -import org.cactoos.Proc; import org.cactoos.Scalar; -import org.cactoos.proc.ProcOf; /** * Binary operation. @@ -58,20 +56,7 @@ public final class Binary implements Scalar { /** * Proc executed when condition is true. */ - private final Proc consequent; - - /** - * Ctor. - * - * @param condition Boolean The condition - * @param runnable Runnable executed when condition is true - */ - public Binary(final Scalar condition, final Runnable runnable) { - this( - condition, - new ProcOf<>(runnable) - ); - } + private final Runnable consequent; /** * Ctor. @@ -81,7 +66,7 @@ public Binary(final Scalar condition, final Runnable runnable) { */ public Binary( final Scalar condition, - final Proc consequent + final Runnable consequent ) { this.condition = condition; this.consequent = consequent; @@ -91,7 +76,7 @@ public Binary( public Boolean value() throws Exception { final Boolean result = this.condition.value(); if (result) { - this.consequent.exec(true); + this.consequent.run(); } return result; } diff --git a/src/main/java/org/cactoos/scalar/CallableOf.java b/src/main/java/org/cactoos/scalar/CallableOf.java index 2b9fd5dceb..6916528b7b 100644 --- a/src/main/java/org/cactoos/scalar/CallableOf.java +++ b/src/main/java/org/cactoos/scalar/CallableOf.java @@ -24,10 +24,7 @@ package org.cactoos.scalar; import java.util.concurrent.Callable; -import org.cactoos.Func; -import org.cactoos.Proc; import org.cactoos.Scalar; -import org.cactoos.func.FuncOf; /** * Func as {@link Callable}. @@ -42,51 +39,16 @@ * *

There is no thread-safety guarantee. * - * @param Type of input * @param Type of output * @since 0.12 */ -public final class CallableOf implements Callable { +public final class CallableOf implements Callable { /** * Original callable. */ private final Scalar scalar; - /** - * Ctor. - * @param runnable Encapsulated proc - * @param result Result to return - * @since 0.32 - */ - public CallableOf(final Runnable runnable, final T result) { - this(() -> { - runnable.run(); - return result; - }); - } - - /** - * Ctor. - * @param proc Encapsulated proc - * @param ipt Input - * @param result Result to return - * @since 0.41 - */ - public CallableOf(final Proc proc, final X ipt, final T result) { - this(new FuncOf<>(proc, result), ipt); - } - - /** - * Ctor. - * @param fnc Encapsulated func - * @param ipt Input - * @since 0.41 - */ - public CallableOf(final Func fnc, final X ipt) { - this(() -> fnc.apply(ipt)); - } - /** * Ctor. * @param slr Encapsulated scalar diff --git a/src/main/java/org/cactoos/scalar/FirstOf.java b/src/main/java/org/cactoos/scalar/FirstOf.java index 28f54c08a2..23cb45e1c6 100644 --- a/src/main/java/org/cactoos/scalar/FirstOf.java +++ b/src/main/java/org/cactoos/scalar/FirstOf.java @@ -60,7 +60,7 @@ public final class FirstOf implements Scalar { */ public FirstOf(final Iterable src, final T fbck) { this( - new FuncOf<>(true), + new FuncOf<>(new True()), src, () -> fbck ); @@ -73,7 +73,7 @@ public FirstOf(final Iterable src, final T fbck) { */ public FirstOf(final Iterable src, final Scalar fbck) { this( - new FuncOf<>(true), + new FuncOf<>(new True()), src, fbck ); diff --git a/src/main/java/org/cactoos/scalar/ItemAt.java b/src/main/java/org/cactoos/scalar/ItemAt.java index 487f2c3c4c..94b78670cf 100644 --- a/src/main/java/org/cactoos/scalar/ItemAt.java +++ b/src/main/java/org/cactoos/scalar/ItemAt.java @@ -80,7 +80,7 @@ public ItemAt( final T fallback, final Iterable iterable ) { - this(position, new FuncOf<>(fallback), iterable); + this(position, new FuncOf<>(new Constant<>(fallback)), iterable); } /** diff --git a/src/main/java/org/cactoos/scalar/RepeatedCallable.java b/src/main/java/org/cactoos/scalar/Repeated.java similarity index 69% rename from src/main/java/org/cactoos/scalar/RepeatedCallable.java rename to src/main/java/org/cactoos/scalar/Repeated.java index f161a2d8c7..492ec88469 100644 --- a/src/main/java/org/cactoos/scalar/RepeatedCallable.java +++ b/src/main/java/org/cactoos/scalar/Repeated.java @@ -23,22 +23,21 @@ */ package org.cactoos.scalar; -import java.util.concurrent.Callable; +import org.cactoos.Scalar; import org.cactoos.func.FuncOf; -import org.cactoos.func.Repeated; /** - * Callable that runs repeatedly for a number of times. + * Scalar that runs repeatedly for a number of times. * * @param Type of output * @since 0.49.2 */ -public final class RepeatedCallable implements Callable { +public final class Repeated implements Scalar { /** - * Callable. + * Scalar. */ - private final Callable callable; + private final Scalar scalar; /** * Times to repeat. @@ -48,23 +47,22 @@ public final class RepeatedCallable implements Callable { /** * Ctor. * - *

If {@code max} is equal or less than zero {@link #call()} will return + *

If {@code max} is equal or less than zero {@link #value()} will return * an exception.

* - * @param cllbl Callable to repeat. - * @param count How many times. + * @param scalar Scalar to repeat. + * @param times How many times. */ - public RepeatedCallable(final Callable cllbl, final int count) { - this.callable = cllbl; - this.times = count; + public Repeated(final Scalar scalar, final int times) { + this.scalar = scalar; + this.times = times; } @Override - public X call() throws Exception { - return new Repeated<>( - new FuncOf<>(this.callable), + public X value() throws Exception { + return new org.cactoos.func.Repeated<>( + new FuncOf<>(this.scalar), this.times ).apply(true); } - } diff --git a/src/main/java/org/cactoos/scalar/ScalarOf.java b/src/main/java/org/cactoos/scalar/ScalarOf.java index 1d6d350eb7..924e86afc9 100644 --- a/src/main/java/org/cactoos/scalar/ScalarOf.java +++ b/src/main/java/org/cactoos/scalar/ScalarOf.java @@ -23,7 +23,10 @@ */ package org.cactoos.scalar; -import org.cactoos.Scalar; +import java.util.concurrent.Callable; +import org.cactoos.Func; +import org.cactoos.Proc; +import org.cactoos.func.FuncOf; /** * ScalarOf. @@ -31,24 +34,49 @@ * @param Element type * @since 0.4 */ -public final class ScalarOf implements Scalar { +public final class ScalarOf extends ScalarEnvelope { + /** + * Ctor. + * @param runnable Encapsulated proc + * @param result Result to return + * @since 0.48 + */ + public ScalarOf(final Runnable runnable, final T result) { + this(() -> { + runnable.run(); + return result; + }); + } /** - * The scalar. + * Ctor. + * @param proc Encapsulated proc + * @param ipt Input + * @param result Result to return + * @param Type of input + * @since 0.48 */ - private final Scalar origin; + public ScalarOf(final Proc proc, final X ipt, final T result) { + this(new FuncOf<>(proc, result), ipt); + } /** * Ctor. - * - * @param origin The scalar + * @param fnc Encapsulated func + * @param ipt Input + * @param Type of input + * @since 0.41 */ - public ScalarOf(final Scalar origin) { - this.origin = origin; + public ScalarOf(final Func fnc, final X ipt) { + this(() -> fnc.apply(ipt)); } - @Override - public T value() throws Exception { - return this.origin.value(); + /** + * Ctor. + * + * @param origin The scalar + */ + public ScalarOf(final Callable origin) { + super(origin::call); } } diff --git a/src/main/java/org/cactoos/scalar/ScalarOfCallable.java b/src/main/java/org/cactoos/scalar/ScalarOfCallable.java deleted file mode 100644 index 4b95056dee..0000000000 --- a/src/main/java/org/cactoos/scalar/ScalarOfCallable.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2017-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.cactoos.scalar; - -import java.util.concurrent.Callable; - -/** - * ScalarOfCallable. - * - * @param Element type. - * @since 0.47 - */ -public final class ScalarOfCallable extends ScalarEnvelope { - - /** - * Ctor. - * - * @param callable The callable - */ - public ScalarOfCallable(final Callable callable) { - super(callable::call); - } -} diff --git a/src/test/java/org/cactoos/experimental/TimedTest.java b/src/test/java/org/cactoos/experimental/TimedTest.java index 21b1e64e4c..06f518ddb9 100644 --- a/src/test/java/org/cactoos/experimental/TimedTest.java +++ b/src/test/java/org/cactoos/experimental/TimedTest.java @@ -30,7 +30,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import org.cactoos.scalar.RepeatedCallable; +import org.cactoos.scalar.Repeated; import org.hamcrest.core.IsNull; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; @@ -84,7 +84,7 @@ final class TimedTest { "PMD.AvoidThrowingRawExceptionTypes", "PMD.DoNotThrowExceptionInFinally" }) void containsResults() throws Exception { - new RepeatedCallable<>( + new Repeated<>( () -> { final ExecutorService extor = Executors.newFixedThreadPool( TimedTest.THREADS @@ -129,7 +129,7 @@ void containsResults() throws Exception { return true; }, TimedTest.REPETITIONS_COUNT - ).call(); + ).value(); } /** @@ -142,7 +142,7 @@ void containsResults() throws Exception { }) void failsDueToTimeoutWithExternalExecutorService() throws Exception { - new RepeatedCallable<>( + new Repeated<>( () -> { final ExecutorService extor = Executors.newFixedThreadPool( TimedTest.THREADS @@ -187,7 +187,7 @@ void failsDueToTimeoutWithExternalExecutorService() return true; }, TimedTest.REPETITIONS_COUNT - ).call(); + ).value(); } /** @@ -221,7 +221,7 @@ void failsDueToException() { */ @Test void containsValuesWithInlineExecutorService() throws Exception { - new RepeatedCallable<>( + new Repeated<>( () -> { new Assertion<>( // @checkstyle LineLengthCheck (1 line) @@ -252,7 +252,7 @@ void containsValuesWithInlineExecutorService() throws Exception { return true; }, TimedTest.REPETITIONS_COUNT - ).call(); + ).value(); } /** @@ -261,7 +261,7 @@ void containsValuesWithInlineExecutorService() throws Exception { */ @Test void failsDueToTimeoutWithInlineExecutorService() throws Exception { - new RepeatedCallable<>( + new Repeated<>( () -> { new Assertion<>( // @checkstyle LineLengthCheck (1 line) @@ -291,7 +291,7 @@ void failsDueToTimeoutWithInlineExecutorService() throws Exception { return true; }, TimedTest.REPETITIONS_COUNT - ).call(); + ).value(); } /** diff --git a/src/test/java/org/cactoos/func/BiFuncOfTest.java b/src/test/java/org/cactoos/func/BiFuncOfTest.java index e375926720..566db12587 100644 --- a/src/test/java/org/cactoos/func/BiFuncOfTest.java +++ b/src/test/java/org/cactoos/func/BiFuncOfTest.java @@ -24,7 +24,8 @@ package org.cactoos.func; import java.util.concurrent.atomic.AtomicBoolean; -import org.cactoos.Scalar; +import org.cactoos.proc.ProcOf; +import org.cactoos.scalar.True; import org.hamcrest.core.IsEqual; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; @@ -35,6 +36,7 @@ * * @since 0.20 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ final class BiFuncOfTest { @@ -43,7 +45,7 @@ void convertsFuncIntoBiFunc() throws Exception { new Assertion<>( "Must convert function into bi-function", new BiFuncOf<>( - input -> 1 + new FuncOf<>(input -> 1) ).apply(1, 2), new IsEqual<>(1) ).affirm(); @@ -55,43 +57,22 @@ void convertsProcIntoBiFunc() throws Exception { new Assertion<>( "Must convert procedure into bi-function", new BiFuncOf( - input -> done.set(true), + new ProcOf<>( + input -> { + done.set(true); + } + ), true ).apply("hello world", 1), new IsEqual<>(done.get()) ).affirm(); } - @Test - void convertsRunnableIntoBiFunc() throws Exception { - final AtomicBoolean done = new AtomicBoolean(false); - new Assertion<>( - "Must convert runnable into bi-function", - new BiFuncOf( - () -> done.set(true), - true - ).apply("hello, world", 1), - new IsTrue() - ).affirm(); - } - - @Test - void convertsValueIntoBiFunc() throws Exception { - new Assertion<>( - "Must convert value into bi-function", - new BiFuncOf( - true - ).apply("hello, dude!", 1), - new IsTrue() - ).affirm(); - } - @Test void convertsScalarIntoBiFunc() throws Exception { - final Scalar scalar = () -> true; new Assertion<>( "Must convert scalar into bi-function", - new BiFuncOf(scalar).apply(false, false), + new BiFuncOf(new True()).apply(false, false), new IsTrue() ).affirm(); } diff --git a/src/test/java/org/cactoos/func/FuncOfTest.java b/src/test/java/org/cactoos/func/FuncOfTest.java index a7c727db71..180668e320 100644 --- a/src/test/java/org/cactoos/func/FuncOfTest.java +++ b/src/test/java/org/cactoos/func/FuncOfTest.java @@ -24,11 +24,11 @@ package org.cactoos.func; import java.util.concurrent.atomic.AtomicBoolean; +import org.cactoos.proc.ProcOf; import org.cactoos.scalar.Constant; import org.hamcrest.core.IsEqual; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; -import org.llorllale.cactoos.matchers.IsTrue; /** * Test case for {@link FuncOf}. @@ -44,31 +44,23 @@ void convertsProcIntoFunc() throws Exception { new Assertion<>( "Must convert procedure into function", new FuncOf( - input -> done.set(true), + new ProcOf<>( + input -> { + done.set(true); + } + ), true ).apply("hello world"), new IsEqual<>(done.get()) ).affirm(); } - @Test - void convertsValueIntoFunc() throws Exception { - new Assertion<>( - "Must convert value into function", - new FuncOf( - true - ).apply("hello, dude!"), - new IsTrue() - ).affirm(); - } - @Test void convertsScalarIntoFunc() throws Exception { - final Constant scalar = new Constant<>(1); new Assertion<>( "Result of func must be equal to the original value", - new FuncOf<>(scalar).apply(new Object()), - new IsEqual<>(scalar.value()) + new FuncOf<>(new Constant<>(1)).apply(new Object()), + new IsEqual<>(1) ).affirm(); } } diff --git a/src/test/java/org/cactoos/func/RunnableOfTest.java b/src/test/java/org/cactoos/func/RunnableOfTest.java index 6f8a37ee55..439d0c8288 100644 --- a/src/test/java/org/cactoos/func/RunnableOfTest.java +++ b/src/test/java/org/cactoos/func/RunnableOfTest.java @@ -25,6 +25,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.cactoos.proc.ProcOf; +import org.cactoos.scalar.ScalarOf; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.MatcherOf; @@ -35,18 +36,22 @@ * @since 0.2 * @checkstyle JavadocMethodCheck (500 lines) */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RunnableOfTest { @Test void convertsFuncIntoRunnable() { final AtomicBoolean done = new AtomicBoolean(); new Assertion<>( - "Can't execute Runnable with Func", - new RunnableOf<>( - input -> { - done.set(true); - return 1; - } + "Must execute Runnable with Func", + new RunnableOf( + new FuncOf<>( + ignored -> { + done.set(true); + return "ignored"; + } + ), + "ignored" ), new MatcherOf( input -> { @@ -61,14 +66,14 @@ void convertsFuncIntoRunnable() { void convertsProcIntoRunnable() { final AtomicBoolean done = new AtomicBoolean(); new Assertion<>( - "Can't execute Runnable with ProcOf", - new RunnableOf<>( + "Must execute Runnable with Proc", + new RunnableOf( new ProcOf<>( - input -> { + ignored -> { done.set(true); - return 1; } - ) + ), + "ignored" ), new MatcherOf( input -> { @@ -80,15 +85,17 @@ void convertsProcIntoRunnable() { } @Test - void convertsCallableIntoRunnable() { + void convertsScalarIntoRunnable() { final AtomicBoolean done = new AtomicBoolean(); new Assertion<>( - "Can't execute Runnable with Callable", - new RunnableOf<>( - () -> { - done.set(true); - return null; - } + "Must execute Runnable with Scalar", + new RunnableOf( + new ScalarOf<>( + () -> { + done.set(true); + return null; + } + ) ), new MatcherOf( input -> { diff --git a/src/test/java/org/cactoos/func/SyncFuncTest.java b/src/test/java/org/cactoos/func/SyncFuncTest.java index 07bfa1d2b2..1f85322b47 100644 --- a/src/test/java/org/cactoos/func/SyncFuncTest.java +++ b/src/test/java/org/cactoos/func/SyncFuncTest.java @@ -25,7 +25,6 @@ import java.util.LinkedList; import java.util.List; -import org.cactoos.proc.ProcOf; import org.hamcrest.core.IsEqual; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; @@ -37,9 +36,7 @@ * @since 0.24 * @checkstyle JavadocMethodCheck (500 lines) */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class SyncFuncTest { - @Test void funcWorksInThreads() { final List list = new LinkedList<>(); @@ -60,74 +57,4 @@ void funcWorksInThreads() { new IsEqual<>(threads) ).affirm(); } - - @Test - void procWorksInThreads() { - final int threads = 100; - final int[] counter = new int[]{0}; - new Assertion<>( - "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 - ) - ).affirm(); - new Assertion<>( - "Must run the expected amount of threads", - counter[0], - new IsEqual<>(threads) - ).affirm(); - } - - @Test - void callableWorksInThreads() { - final int threads = 100; - final int[] counter = new int[]{0}; - new Assertion<>( - "Sync func with callable can't work well in multiple threads", - func -> func.apply(1), - new RunsInThreads<>( - new SyncFunc( - () -> { - counter[0] = counter[0] + 1; - return true; - } - ), - threads - ) - ).affirm(); - new Assertion<>( - "Must run the expected amount of threads", - counter[0], - new IsEqual<>(threads) - ).affirm(); - } - - @Test - void runnableWorksInThreads() { - final int threads = 100; - final int[] counter = new int[]{0}; - new Assertion<>( - "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 - ) - ).affirm(); - new Assertion<>( - "Must run the expected amount of threads", - counter[0], - new IsEqual<>(threads) - ).affirm(); - } } diff --git a/src/test/java/org/cactoos/iterable/PagedTest.java b/src/test/java/org/cactoos/iterable/PagedTest.java index a91beea2ae..2766b3ae2e 100644 --- a/src/test/java/org/cactoos/iterable/PagedTest.java +++ b/src/test/java/org/cactoos/iterable/PagedTest.java @@ -28,7 +28,7 @@ import org.cactoos.Scalar; import org.cactoos.iterator.IteratorOf; import org.cactoos.scalar.LengthOf; -import org.cactoos.scalar.ScalarOfCallable; +import org.cactoos.scalar.ScalarOf; import org.cactoos.scalar.Ternary; import org.hamcrest.collection.IsIterableWithSize; import org.hamcrest.core.IsEqual; @@ -103,7 +103,7 @@ void throwsNoSuchElement() { final Iterator> pages = service.iterator(); new Assertion>( "must throw an exception when first iterator is empty", - new ScalarOfCallable( + new ScalarOf( () -> new Paged<>( () -> pages.next().iterator(), page -> new Ternary<>( diff --git a/src/test/java/org/cactoos/map/MapOfTest.java b/src/test/java/org/cactoos/map/MapOfTest.java index 71a0323384..d5986e637e 100644 --- a/src/test/java/org/cactoos/map/MapOfTest.java +++ b/src/test/java/org/cactoos/map/MapOfTest.java @@ -28,6 +28,7 @@ import org.cactoos.Scalar; import org.cactoos.func.FuncOf; import org.cactoos.iterable.IterableOf; +import org.cactoos.scalar.Constant; import org.hamcrest.MatcherAssert; import org.hamcrest.collection.IsMapContaining; import org.hamcrest.core.AllOf; @@ -35,6 +36,7 @@ import org.hamcrest.core.IsEqual; import org.hamcrest.core.StringStartsWith; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; /** * Test case for {@link MapOf}. @@ -158,25 +160,25 @@ void createsMapFromMapAndMapEntries() { @Test void createsMapFromFunctionsAndIterable() { - MatcherAssert.assertThat( - "Can't create a map from functions and iterable.", + new Assertion<>( + "Must create a map from functions and iterable.", new MapOf( - new FuncOf(0), - new FuncOf(0), + new FuncOf(new Constant<>(0)), + new FuncOf(new Constant<>(0)), new IterableOf(0) ), new IsMapContaining<>(new IsEqual<>(0), new IsEqual<>(0)) - ); + ).affirm(); } @Test @SuppressWarnings("unchecked") void createsMapFromMapFunctionsAndIterable() { - MatcherAssert.assertThat( + new Assertion<>( "Can't create a map from map, functions and iterable.", new MapOf( - new FuncOf(0), - new FuncOf(0), + new FuncOf(new Constant<>(0)), + new FuncOf(new Constant<>(0)), new MapOf( new MapEntry(1, 1) ), @@ -188,7 +190,7 @@ void createsMapFromMapFunctionsAndIterable() { new IsMapContaining<>(new IsEqual<>(1), new IsEqual<>(1)) ) ) - ); + ).affirm(); } } diff --git a/src/test/java/org/cactoos/proc/ProcOfTest.java b/src/test/java/org/cactoos/proc/ProcOfTest.java index e2cc0e9d71..ffd5bceadf 100644 --- a/src/test/java/org/cactoos/proc/ProcOfTest.java +++ b/src/test/java/org/cactoos/proc/ProcOfTest.java @@ -25,8 +25,7 @@ import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Callable; -import org.cactoos.text.FormattedText; +import org.cactoos.func.FuncOf; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.HasValues; @@ -38,67 +37,20 @@ * @checkstyle JavadocMethodCheck (500 lines) */ final class ProcOfTest { - - @Test - void worksWithRunnable() throws Exception { - final String str = "test string"; - final List list = new ArrayList<>(1); - new ProcOf( - new Runnable() { - @Override - public void run() { - list.add(str); - } - } - ).exec("You can use any input in a Runnable."); - new Assertion<>( - new FormattedText( - "Wrong result when created with a Runnable. Expected %s", - str - ).asString(), - list, - new HasValues<>(str) - ).affirm(); - } - - @Test - void worksWithCallable() throws Exception { - final String str = "test str"; - final List list = new ArrayList<>(1); - new ProcOf( - new Callable() { - @Override - public String call() throws Exception { - list.add(str); - return "Any return value can be used."; - } - } - ).exec("You can use any input in a Callable"); - new Assertion<>( - new FormattedText( - "Wrong result when created with a Callable. Expected %s", - str - ).asString(), - list, - new HasValues<>(str) - ).affirm(); - } - @Test void worksWithFunc() throws Exception { final String str = "test input"; final List list = new ArrayList<>(1); new ProcOf( - input -> { - list.add(input); - return list.size(); - } + new FuncOf<>( + input -> { + list.add(input); + return list.size(); + } + ) ).exec(str); new Assertion<>( - new FormattedText( - "Wrong result when created with a Func. Expected %s", - str - ).asString(), + "Must contains the expected value from func", list, new HasValues<>(str) ).affirm(); diff --git a/src/test/java/org/cactoos/scalar/CallableOfTest.java b/src/test/java/org/cactoos/scalar/CallableOfTest.java index 99e5d98a16..005344bc1a 100644 --- a/src/test/java/org/cactoos/scalar/CallableOfTest.java +++ b/src/test/java/org/cactoos/scalar/CallableOfTest.java @@ -23,7 +23,6 @@ */ package org.cactoos.scalar; -import java.util.concurrent.atomic.AtomicBoolean; import org.hamcrest.core.IsEqual; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; @@ -34,53 +33,15 @@ * @since 0.2 * @checkstyle JavadocMethodCheck (500 lines) */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class CallableOfTest { - - @Test - void convertsRunnableIntoCallable() throws Exception { - final AtomicBoolean flag = new AtomicBoolean(false); - new CallableOf<>( - () -> flag.set(true), - true - ).call(); - new Assertion<>( - "must have been set by callable", - flag.get(), - new IsEqual<>(true) - ).affirm(); - } - - @Test - void convertsProcIntoCallable() throws Exception { - final AtomicBoolean flag = new AtomicBoolean(false); - new Assertion<>( - "must return predefined result", - new CallableOf<>( - bool -> { - flag.set(bool); - }, - true, - false - ).call(), - new IsEqual<>(false) - ).affirm(); - new Assertion<>( - "must have been set by callable", - flag.get(), - new IsEqual<>(true) - ).affirm(); - } - @Test - void convertsFuncIntoCallable() throws Exception { + void convertsScalar() throws Exception { new Assertion<>( - "must return the application of func", + "must return the value of scalar", new CallableOf<>( - num -> num + 1, - 1 + new Constant<>(1) ).call(), - new IsEqual<>(2) + new IsEqual<>(1) ).affirm(); } diff --git a/src/test/java/org/cactoos/scalar/RepeatedCallableTest.java b/src/test/java/org/cactoos/scalar/RepeatedTest.java similarity index 77% rename from src/test/java/org/cactoos/scalar/RepeatedCallableTest.java rename to src/test/java/org/cactoos/scalar/RepeatedTest.java index a4815a2b5a..89f11250c5 100644 --- a/src/test/java/org/cactoos/scalar/RepeatedCallableTest.java +++ b/src/test/java/org/cactoos/scalar/RepeatedTest.java @@ -23,37 +23,32 @@ */ package org.cactoos.scalar; -import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicInteger; import org.hamcrest.core.IsEqual; -import org.junit.Assert; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.Throws; /** - * Test case for {@link RepeatedCallable}. + * Test case for {@link Repeated}. * * @since 0.49.2 * @checkstyle MagicNumberCheck (100 line) * @checkstyle JavadocMethodCheck (100 lines) */ -final class RepeatedCallableTest { +final class RepeatedTest { @Test - void runsCallableMultipleTimes() throws Exception { + void runsMultipleTimes() throws Exception { final AtomicInteger atom = new AtomicInteger(); - final Callable callable = new RepeatedCallable<>( - new CallableOf<>( - () -> atom.getAndIncrement() - ), - 3 - ); new Assertion<>( - "Must run callable 3 times", - callable.call(), + "Must run scalar 3 times", + new Repeated<>( + () -> atom.incrementAndGet(), + 3 + ).value(), new IsEqual<>(3) - ); + ).affirm(); } @Test @@ -61,15 +56,15 @@ void throwsIfZero() { new Assertion<>( // @checkstyle LineLengthCheck (1 line) "Must throws an exception if number of repetitions not be at least 1", - () -> new RepeatedCallable<>( - new CallableOf<>( + () -> new Repeated<>( + new ScalarOf<>( () -> { - Assert.fail("intended to fail"); + throw new IllegalStateException("intended to fail"); }, true ), 0 - ).call(), + ).value(), new Throws<>( "The number of repetitions must be at least 1", IllegalArgumentException.class diff --git a/src/test/java/org/cactoos/scalar/ScalarOfCallableTest.java b/src/test/java/org/cactoos/scalar/ScalarOfTest.java similarity index 55% rename from src/test/java/org/cactoos/scalar/ScalarOfCallableTest.java rename to src/test/java/org/cactoos/scalar/ScalarOfTest.java index 1e92e81df3..f13c3b162a 100644 --- a/src/test/java/org/cactoos/scalar/ScalarOfCallableTest.java +++ b/src/test/java/org/cactoos/scalar/ScalarOfTest.java @@ -23,22 +23,57 @@ */ package org.cactoos.scalar; +import org.cactoos.func.FuncOf; +import org.cactoos.func.RunnableOf; +import org.cactoos.proc.ProcOf; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.ScalarHasValue; /** - * Test case for {@link ScalarOfCallable}. + * Test case for {@link ScalarOf}. * - * @since 0.47 + * @since 0.48 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ -final class ScalarOfCallableTest { +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +final class ScalarOfTest { @Test void worksWithCallable() { final Object obj = new Object(); new Assertion<>( "must hold the same value as given by callable", - new ScalarOfCallable<>(new CallableOf<>(new Constant<>(obj))), + new ScalarOf<>(new CallableOf<>(new Constant<>(obj))), + new ScalarHasValue<>(obj) + ).affirm(); + } + + @Test + void worksWithRunnable() { + final Object obj = new Object(); + new Assertion<>( + "must hold the same value as given", + new ScalarOf<>(new RunnableOf(ignored -> { }, "ignored"), obj), + new ScalarHasValue<>(obj) + ).affirm(); + } + + @Test + void worksWithFunc() { + final Object obj = new Object(); + new Assertion<>( + "must hold the same value as given by func", + new ScalarOf<>(new FuncOf<>(new Constant<>(obj)), "ignored"), + new ScalarHasValue<>(obj) + ).affirm(); + } + + @Test + void worksWithProc() { + final Object obj = new Object(); + new Assertion<>( + "must hold the expected value", + new ScalarOf<>(new ProcOf<>(ignored -> { }), "ignored", obj), new ScalarHasValue<>(obj) ).affirm(); }