Skip to content

Commit

Permalink
(yegor256#1445) Cleanup around Scalar and Callable
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Dec 30, 2020
1 parent 8adeb28 commit 026638e
Show file tree
Hide file tree
Showing 24 changed files with 216 additions and 562 deletions.
31 changes: 1 addition & 30 deletions src/main/java/org/cactoos/func/BiFuncOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -48,14 +46,6 @@ public final class BiFuncOf<X, Y, Z> implements BiFunc<X, Y, Z> {
*/
private final BiFunc<X, Y, Z> func;

/**
* Ctor.
* @param result The result
*/
public BiFuncOf(final Z result) {
this((first, second) -> result);
}

/**
* Ctor.
* @param scalar The scalar
Expand Down Expand Up @@ -100,35 +90,16 @@ public BiFuncOf(final BiProc<X, Y> proc, final Z result) {
);
}

/**
* Ctor.
* @param callable The callable
*/
public BiFuncOf(final Callable<Z> 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<X, Y, Z> fnc) {
public BiFuncOf(final BiFunc<X, Y, Z> fnc) {
this.func = fnc;
}

@Override
public Z apply(final X first, final Y second) throws Exception {
return this.func.apply(first, second);
}

}
33 changes: 2 additions & 31 deletions src/main/java/org/cactoos/func/FuncOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,32 +43,6 @@ public final class FuncOf<X, Y> implements Func<X, Y> {
*/
private final Func<X, Y> func;

/**
* Ctor.
* @param result The result
*/
public FuncOf(final Y result) {
this((Func<X, Y>) input -> result);
}

/**
* Ctor.
* @param callable The callable
*/
public FuncOf(final Callable<Y> callable) {
this((Func<X, Y>) 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
Expand All @@ -89,16 +62,14 @@ public FuncOf(final Proc<X> proc, final Y result) {
* @param scalar Origin scalar
*/
public FuncOf(final Scalar<Y> scalar) {
this(input -> {
return scalar.value();
});
this(input -> scalar.value());
}

/**
* Ctor.
* @param fnc Func
*/
private FuncOf(final Func<X, Y> fnc) {
public FuncOf(final Func<X, Y> fnc) {
this.func = fnc;
}

Expand Down
62 changes: 22 additions & 40 deletions src/main/java/org/cactoos/func/RunnableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>There is no thread-safety guarantee.
*
* @param <X> Type of input
* @since 0.12
*/
public final class RunnableOf<X> implements Runnable {
public final class RunnableOf implements Runnable {

/**
* Original proc.
* Runnable.
*/
private final Proc<X> proc;

/**
* The input.
*/
private final X input;

/**
* Ctor.
* @param proc Encapsulated proc
* @since 0.11
*/
public RunnableOf(final Callable<X> proc) {
this(new FuncOf<>(proc));
}

/**
* Ctor.
* @param proc Encapsulated proc
*/
public RunnableOf(final Proc<X> proc) {
this(proc, null);
}
private final Scalar<?> scalar;

/**
* Ctor.
* @param fnc Encapsulated func
* @param ipt Input
* @param <X> Type of input
*/
public RunnableOf(final Func<X, ?> fnc) {
this(fnc, null);
public <X> RunnableOf(final Func<? super X, ?> fnc, final X ipt) {
this(new ProcOf<>(fnc), ipt);
}

/**
* Ctor.
* @param fnc Encapsulated func
* @param proc Encapsulated proc
* @param ipt Input
* @param <X> Type of input
* @since 0.32
*/
public RunnableOf(final Func<X, ?> fnc, final X ipt) {
this(new ProcOf<>(fnc), ipt);
public <X> RunnableOf(final Proc<? super X> 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<X> 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();
}
}
31 changes: 0 additions & 31 deletions src/main/java/org/cactoos/func/SyncFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -48,35 +46,6 @@ public final class SyncFunc<X, Y> implements Func<X, Y> {
*/
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<Y> callable) {
this(new FuncOf<>(callable));
}

/**
* Ctor.
* @param proc Func original
* @param result Result to return
* @since 0.32
*/
public SyncFunc(final Proc<X> proc, final Y result) {
this(new FuncOf<>(proc, result));
}

/**
* Ctor.
* @param fnc Func original
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/cactoos/io/TempFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,9 +132,7 @@ public Path value() throws Exception {
public void close() throws IOException {
new IoCheckedProc<>(
new ForEach<Path>(
new ProcOf<>(
path -> path.toFile().delete()
)
path -> path.toFile().delete()
)
).exec(
new Sorted<>(
Expand Down
19 changes: 1 addition & 18 deletions src/main/java/org/cactoos/proc/ProcOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.proc;

import java.util.concurrent.Callable;
import org.cactoos.Func;
import org.cactoos.Proc;

Expand All @@ -42,22 +41,6 @@ public final class ProcOf<X> implements Proc<X> {
*/
private final Proc<X> proc;

/**
* Ctor.
* @param runnable The runnable
*/
public ProcOf(final Runnable runnable) {
this((Proc<X>) input -> runnable.run());
}

/**
* Ctor.
* @param callable The callable
*/
public ProcOf(final Callable<X> callable) {
this((Proc<X>) input -> callable.call());
}

/**
* Ctor.
* @param fnc The proc
Expand All @@ -70,7 +53,7 @@ public ProcOf(final Func<X, ?> fnc) {
* Ctor.
* @param prc The proc
*/
private ProcOf(final Proc<X> prc) {
public ProcOf(final Proc<X> prc) {
this.proc = prc;
}

Expand Down
21 changes: 3 additions & 18 deletions src/main/java/org/cactoos/scalar/Binary.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
*/
package org.cactoos.scalar;

import org.cactoos.Proc;
import org.cactoos.Scalar;
import org.cactoos.proc.ProcOf;

/**
* Binary operation.
Expand Down Expand Up @@ -58,20 +56,7 @@ public final class Binary implements Scalar<Boolean> {
/**
* Proc executed when condition is true.
*/
private final Proc<Boolean> consequent;

/**
* Ctor.
*
* @param condition Boolean The condition
* @param runnable Runnable executed when condition is true
*/
public Binary(final Scalar<Boolean> condition, final Runnable runnable) {
this(
condition,
new ProcOf<>(runnable)
);
}
private final Runnable consequent;

/**
* Ctor.
Expand All @@ -81,7 +66,7 @@ public Binary(final Scalar<Boolean> condition, final Runnable runnable) {
*/
public Binary(
final Scalar<Boolean> condition,
final Proc<Boolean> consequent
final Runnable consequent
) {
this.condition = condition;
this.consequent = consequent;
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit 026638e

Please sign in to comment.