diff --git a/src/main/java/org/cactoos/experimental/Threads.java b/src/main/java/org/cactoos/experimental/Threads.java index 3a9c74aefd..068394a20b 100644 --- a/src/main/java/org/cactoos/experimental/Threads.java +++ b/src/main/java/org/cactoos/experimental/Threads.java @@ -34,7 +34,6 @@ import org.cactoos.iterable.IterableOf; import org.cactoos.iterable.Mapped; import org.cactoos.list.ListOf; -import org.cactoos.scalar.CallableOf; /** * Allows to execute the tasks concurrently. @@ -110,7 +109,12 @@ private Threads( super( () -> new Mapped<>( Future::get, - new UncheckedFunc<>(fnc).apply(new Mapped<>(CallableOf::new, tasks)) + new UncheckedFunc<>(fnc).apply( + new Mapped<>( + task -> () -> task.value(), + tasks + ) + ) ).iterator() ); } diff --git a/src/main/java/org/cactoos/experimental/Timed.java b/src/main/java/org/cactoos/experimental/Timed.java index bef5f9b5a2..bf2a1be755 100644 --- a/src/main/java/org/cactoos/experimental/Timed.java +++ b/src/main/java/org/cactoos/experimental/Timed.java @@ -35,7 +35,6 @@ import org.cactoos.iterable.IterableOf; import org.cactoos.iterable.Mapped; import org.cactoos.list.ListOf; -import org.cactoos.scalar.CallableOf; /** * Allows to execute the tasks concurrently within given timeout. @@ -125,7 +124,11 @@ public Timed( threads ); try { - return executor.invokeAll(new ListOf<>(todo), timeout, unit); + return executor.invokeAll( + new ListOf<>(todo), + timeout, + unit + ); } finally { executor.shutdown(); } @@ -146,7 +149,12 @@ private Timed( super( () -> new Mapped<>( Future::get, - new UncheckedFunc<>(fnc).apply(new Mapped<>(CallableOf::new, tasks)) + new UncheckedFunc<>(fnc).apply( + new Mapped<>( + task -> () -> task.value(), + tasks + ) + ) ).iterator() ); } diff --git a/src/main/java/org/cactoos/func/BiFuncOf.java b/src/main/java/org/cactoos/func/BiFuncOf.java index 98d3047ff4..396eac410e 100644 --- a/src/main/java/org/cactoos/func/BiFuncOf.java +++ b/src/main/java/org/cactoos/func/BiFuncOf.java @@ -29,7 +29,7 @@ import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.Scalar; -import org.cactoos.scalar.CallableOf; +import org.cactoos.scalar.ScalarOf; /** * Represents many possible inputs as {@link BiFunc}. @@ -115,7 +115,7 @@ public BiFuncOf(final Callable callable) { * @since 0.32 */ public BiFuncOf(final Runnable runnable, final Z result) { - this(new CallableOf<>(runnable, result)); + this(new ScalarOf<>(runnable, result)); } /** diff --git a/src/main/java/org/cactoos/scalar/CallableOf.java b/src/main/java/org/cactoos/scalar/CallableOf.java deleted file mode 100644 index 2b9fd5dceb..0000000000 --- a/src/main/java/org/cactoos/scalar/CallableOf.java +++ /dev/null @@ -1,103 +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; -import org.cactoos.Func; -import org.cactoos.Proc; -import org.cactoos.Scalar; -import org.cactoos.func.FuncOf; - -/** - * Func as {@link Callable}. - * - *

You may want to use this decorator where - * {@link Callable} is required, but you just have a function:

- * - *
 Callable<String> callable = new CallableOf<>(
- *   i -> "Hello, world!"
- * );
- * 
- * - *

There is no thread-safety guarantee. - * - * @param Type of input - * @param Type of output - * @since 0.12 - */ -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 - * @since 0.41 - */ - public CallableOf(final Scalar slr) { - this.scalar = slr; - } - - @Override - public T call() throws Exception { - return this.scalar.value(); - } -} diff --git a/src/main/java/org/cactoos/scalar/ScalarOf.java b/src/main/java/org/cactoos/scalar/ScalarOf.java index 9264cb47b4..1201f64659 100644 --- a/src/main/java/org/cactoos/scalar/ScalarOf.java +++ b/src/main/java/org/cactoos/scalar/ScalarOf.java @@ -23,15 +23,55 @@ */ package org.cactoos.scalar; +import org.cactoos.Func; +import org.cactoos.Proc; import org.cactoos.Scalar; +import org.cactoos.func.FuncOf; /** * ScalarOf. * + * @param Element type * @param Element type * @since 0.4 */ -public final class ScalarOf extends ScalarEnvelope { +public final class ScalarOf extends ScalarEnvelope { + /** + * Ctor. + * @param runnable Encapsulated proc + * @param result Result to return + * @since 0.32 + */ + public ScalarOf(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 ScalarOf(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 ScalarOf(final Func fnc, final X ipt) { + this(() -> fnc.apply(ipt)); + } + /** * Ctor. * diff --git a/src/test/java/org/cactoos/scalar/CallableOfTest.java b/src/test/java/org/cactoos/scalar/ScalarOfTest.java similarity index 92% rename from src/test/java/org/cactoos/scalar/CallableOfTest.java rename to src/test/java/org/cactoos/scalar/ScalarOfTest.java index 99e5d98a16..420a254b04 100644 --- a/src/test/java/org/cactoos/scalar/CallableOfTest.java +++ b/src/test/java/org/cactoos/scalar/ScalarOfTest.java @@ -29,21 +29,21 @@ import org.llorllale.cactoos.matchers.Assertion; /** - * Test case for {@link CallableOf}. + * Test case for {@link ScalarOf}. * * @since 0.2 * @checkstyle JavadocMethodCheck (500 lines) */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") -final class CallableOfTest { +final class ScalarOfTest { @Test void convertsRunnableIntoCallable() throws Exception { final AtomicBoolean flag = new AtomicBoolean(false); - new CallableOf<>( + new ScalarOf<>( () -> flag.set(true), true - ).call(); + ).value(); new Assertion<>( "must have been set by callable", flag.get(), @@ -56,13 +56,13 @@ void convertsProcIntoCallable() throws Exception { final AtomicBoolean flag = new AtomicBoolean(false); new Assertion<>( "must return predefined result", - new CallableOf<>( + new ScalarOf<>( bool -> { flag.set(bool); }, true, false - ).call(), + ).value(), new IsEqual<>(false) ).affirm(); new Assertion<>( @@ -76,10 +76,10 @@ void convertsProcIntoCallable() throws Exception { void convertsFuncIntoCallable() throws Exception { new Assertion<>( "must return the application of func", - new CallableOf<>( + new ScalarOf<>( num -> num + 1, 1 - ).call(), + ).value(), new IsEqual<>(2) ).affirm(); }