Skip to content

Commit

Permalink
yegor256#1445: Remove CallableOf
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Nov 1, 2020
1 parent 0b637b6 commit af197d8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 119 deletions.
8 changes: 6 additions & 2 deletions src/main/java/org/cactoos/experimental/Threads.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
);
}
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/cactoos/experimental/Timed.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
Expand All @@ -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()
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/func/BiFuncOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -115,7 +115,7 @@ public BiFuncOf(final Callable<Z> callable) {
* @since 0.32
*/
public BiFuncOf(final Runnable runnable, final Z result) {
this(new CallableOf<>(runnable, result));
this(new ScalarOf<>(runnable, result));
}

/**
Expand Down
103 changes: 0 additions & 103 deletions src/main/java/org/cactoos/scalar/CallableOf.java

This file was deleted.

42 changes: 41 additions & 1 deletion src/main/java/org/cactoos/scalar/ScalarOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <X> Element type
* @param <T> Element type
* @since 0.4
*/
public final class ScalarOf<T> extends ScalarEnvelope<T> {
public final class ScalarOf<X, T> extends ScalarEnvelope<T> {
/**
* 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<X> 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<X, T> fnc, final X ipt) {
this(() -> fnc.apply(ipt));
}

/**
* Ctor.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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<>(
Expand All @@ -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();
}
Expand Down

0 comments on commit af197d8

Please sign in to comment.