Skip to content

Commit

Permalink
(#1445) Cleanup around Proc and co
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Jan 16, 2021
1 parent ecb0cf4 commit 74dc2a3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 27 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/cactoos/BiProc.java
Expand Up @@ -39,6 +39,10 @@
* @param <X> Type of input
* @param <Y> Type of input
* @since 0.20
* @todo #1445:30min Introduce BiProcOf on the model of ProcOf, FuncOf, etc
* with constructors taking Func, BiFunc, Proc and BiProc as primary.
* Add tests similar to those of ProcOfTest, FuncOfTest, etc to fully cover
* the implementation. Use it where needed, for example in AndWithIndexTest.
*/
public interface BiProc<X, Y> {

Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/cactoos/proc/ForEach.java
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.proc;

import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.func.FuncOf;
import org.cactoos.scalar.And;
Expand All @@ -47,31 +46,29 @@
* <p>
* There is no thread-safety guarantee.
*
* @param <X> The type to itetare over
* @param <X> The type to iterate over
* @since 1.0
*/
public final class ForEach<X> implements Proc<Iterable<X>> {

/**
* The proc.
*/
private final Func<X, Boolean> func;
private final Proc<X> proc;

/**
* Ctor.
*
* @param proc The proc to execute
*/
public ForEach(final Proc<X> proc) {
this.func = new FuncOf<>(
proc, true
);
this.proc = proc;
}

@Override
public void exec(final Iterable<X> input) throws Exception {
new And(
this.func, input
new FuncOf<>(this.proc, true), input
).value();
}

Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/cactoos/proc/ForEachInThreads.java
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.proc;

import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.func.FuncOf;
import org.cactoos.scalar.AndInThreads;
Expand All @@ -49,31 +48,29 @@
* <p>
* There is no thread-safety guarantee.
*
* @param <X> The type to itetare over
* @param <X> The type to iterate over
* @since 1.0
*/
public final class ForEachInThreads<X> implements Proc<Iterable<X>> {

/**
* The proc.
*/
private final Func<X, Boolean> func;
private final Proc<X> proc;

/**
* Ctor.
*
* @param proc The proc to execute
*/
public ForEachInThreads(final Proc<X> proc) {
this.func = new FuncOf<>(
proc, true
);
this.proc = proc;
}

@Override
public void exec(final Iterable<X> input) throws Exception {
new AndInThreads(
this.func, input
new FuncOf<>(this.proc, true), input
).value();
}

Expand Down
10 changes: 3 additions & 7 deletions src/main/java/org/cactoos/proc/ForEachWithIndex.java
Expand Up @@ -23,10 +23,8 @@
*/
package org.cactoos.proc;

import org.cactoos.BiFunc;
import org.cactoos.BiProc;
import org.cactoos.Proc;
import org.cactoos.func.BiFuncOf;
import org.cactoos.scalar.AndWithIndex;

/**
Expand Down Expand Up @@ -57,23 +55,21 @@ public final class ForEachWithIndex<X> implements Proc<Iterable<X>> {
/**
* The proc.
*/
private final BiFunc<X, Integer, Boolean> func;
private final BiProc<X, Integer> proc;

/**
* Ctor.
*
* @param proc The proc to execute
*/
public ForEachWithIndex(final BiProc<X, Integer> proc) {
this.func = new BiFuncOf<>(
proc, true
);
this.proc = proc;
}

@Override
public void exec(final Iterable<X> input) throws Exception {
new AndWithIndex(
this.func, input
this.proc, input
).value();
}
}
6 changes: 5 additions & 1 deletion src/main/java/org/cactoos/proc/ProcOf.java
Expand Up @@ -46,7 +46,11 @@ public final class ProcOf<X> implements Proc<X> {
* @param fnc The proc
*/
public ProcOf(final Func<X, ?> fnc) {
this((Proc<X>) fnc::apply);
this(
input -> {
fnc.apply(input);
}
);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/org/cactoos/scalar/And.java
Expand Up @@ -105,10 +105,21 @@ public <X> And(final Func<X, Boolean> func, final Iterable<X> src) {
*/
@SafeVarargs
public <X> And(final X subject, final Func<X, Boolean>... conditions) {
this(subject, new IterableOf<>(conditions));
}

/**
* Ctor.
* @param subject The subject
* @param conditions Funcs to map
* @param <X> Type of items in the iterable
* @since 0.49
*/
public <X> And(final X subject, final Iterable<Func<X, Boolean>> conditions) {
this(
new Mapped<>(
item -> (Scalar<Boolean>) () -> item.apply(subject),
new IterableOf<>(conditions)
conditions
)
);
}
Expand Down
4 changes: 0 additions & 4 deletions src/test/java/org/cactoos/io/TempFolderTest.java
Expand Up @@ -87,15 +87,13 @@ void deletesNonEmptyDirectory() throws Exception {
filename,
""
).value();
return true;
}
)
).exec(
new IterableOf<>(
"file1.txt", "file2.txt", "file3.txt"
)
);
return true;
}
)
).exec(
Expand Down Expand Up @@ -129,15 +127,13 @@ void createDirectoryWithDirectoriesAndFiles() throws Exception {
filename,
""
).value();
return true;
}
)
).exec(
new IterableOf<>(
"1.txt", "2.txt", "3.txt"
)
);
return true;
}
)
).exec(
Expand Down

0 comments on commit 74dc2a3

Please sign in to comment.