Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 16, 2021
2 parents 5074b24 + 1cb1b88 commit 865f249
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 140 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/cactoos/BiProc.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
18 changes: 15 additions & 3 deletions src/main/java/org/cactoos/scalar/And.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public <X> And(final Func<X, Boolean> func, final X... src) {
public <X> And(final Func<X, Boolean> func, final Iterable<X> src) {
this(
new Mapped<>(
item -> (Scalar<Boolean>) () -> func.apply(item), src
item -> new ScalarOf<>(() -> func.apply(item)),
src
)
);
}
Expand All @@ -105,10 +106,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)
item -> new ScalarOf<>(() -> item.apply(subject)),
conditions
)
);
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/cactoos/scalar/AndInThreads.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public <X> AndInThreads(final Func<X, Boolean> func,
final Iterable<X> src) {
this(
new Mapped<>(
item -> (Scalar<Boolean>) () -> func.apply(item), src
item -> new ScalarOf<>(() -> func.apply(item)),
src
)
);
}
Expand Down Expand Up @@ -164,7 +165,8 @@ public <X> AndInThreads(final ExecutorService svc,
this(
svc,
new Mapped<>(
item -> (Scalar<Boolean>) () -> func.apply(item), src
item -> new ScalarOf<>(() -> func.apply(item)),
src
)
);
}
Expand Down Expand Up @@ -210,7 +212,7 @@ public Boolean value() throws Exception {
futures.add(this.service.submit(item::value));
}
final boolean result = new And(
(Func<Future<Boolean>, Boolean>) Future::get,
Future::get,
futures
).value();
if (this.shut) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/cactoos/scalar/AndWithIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.cactoos.Proc;
import org.cactoos.Scalar;
import org.cactoos.func.BiFuncOf;
import org.cactoos.func.FuncOf;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;

Expand Down Expand Up @@ -115,8 +116,8 @@ public <X> AndWithIndex(final BiFunc<X, Integer, Boolean> func,
final Iterable<X> src) {
this(
new Mapped<>(
item -> (Func<Integer, Boolean>) input
-> func.apply(item, input), src
item -> new FuncOf<>(input -> func.apply(item, input)),
src
)
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/cactoos/scalar/Or.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public <X> Or(final Proc<X> proc, final Iterable<X> src) {
public <X> Or(final Func<X, Boolean> func, final Iterable<X> src) {
this(
new Mapped<>(
item -> (Scalar<Boolean>) () -> func.apply(item), src
item -> new ScalarOf<>(() -> func.apply(item)),
src
)
);
}
Expand All @@ -140,7 +141,7 @@ public <X> Or(final Func<X, Boolean> func, final Iterable<X> src) {
public <X> Or(final X subject, final Func<X, Boolean>... conditions) {
this(
new Mapped<>(
item -> (Scalar<Boolean>) () -> item.apply(subject),
item -> new ScalarOf<>(() -> item.apply(subject)),
new IterableOf<>(conditions)
)
);
Expand Down
63 changes: 47 additions & 16 deletions src/test/java/org/cactoos/func/BiFuncOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,88 @@
*/
package org.cactoos.func;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.cactoos.BiFunc;
import org.cactoos.proc.ProcOf;
import org.cactoos.scalar.True;
import org.hamcrest.core.IsEqual;
import org.cactoos.scalar.Constant;
import org.hamcrest.core.IsSame;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsTrue;
import org.llorllale.cactoos.matchers.MatcherOf;

/**
* Test case for {@link BiFuncOf}.
*
* @since 0.20
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class BiFuncOfTest {

@Test
void convertsFuncIntoBiFunc() throws Exception {
new Assertion<>(
"Must convert function into bi-function",
new BiFuncOf<>(
new FuncOf<>(input -> 1)
).apply(1, 2),
new IsEqual<>(1)
new FuncOf<>(input -> input)
),
new MatcherOf<>(
func -> {
final Object first = new Object();
final Object res = func.apply(first, "discarded");
return res.equals(first);
}
)
).affirm();
}

@Test
void convertsProcIntoBiFunc() throws Exception {
final AtomicBoolean done = new AtomicBoolean(false);
final AtomicReference<Object> done = new AtomicReference<>();
final Object result = new Object();
new Assertion<>(
"Must convert procedure into bi-function",
new BiFuncOf<String, Integer, Boolean>(
new BiFuncOf<>(
new ProcOf<>(
input -> {
done.set(true);
done.set(input);
}
),
true
).apply("hello world", 1),
new IsEqual<>(done.get())
result
),
new MatcherOf<>(
func -> {
final Object first = new Object();
final Object res = func.apply(first, "discarded");
return res.equals(result) && done.get().equals(first);
}
)
).affirm();
}

@Test
void convertsScalarIntoBiFunc() throws Exception {
final Object obj = new Object();
new Assertion<>(
"Must convert scalar into bi-function",
new BiFuncOf<Boolean, Boolean, Boolean>(new True()).apply(false, false),
new IsTrue()
new BiFuncOf<>(new Constant<>(obj)).apply("discarded", "discarded"),
new IsSame<>(obj)
).affirm();
}

@Test
void convertsLambdaIntoBiFunc() throws Exception {
new Assertion<>(
"Must convert lambda into bi-function",
new BiFuncOf<>((first, second) -> new Object[] {first, second}),
new MatcherOf<BiFunc<Object, Object, Object[]>>(
func -> {
final Object first = new Object();
final Object second = new Object();
final Object[] res = func.apply(first, second);
return res.length == 2 && res[0].equals(first) && res[1].equals(second);
}
)
).affirm();
}
}
Loading

1 comment on commit 865f249

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 865f249 Jan 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1445-c6a75ced discovered in src/main/java/org/cactoos/BiProc.java and submitted as #1532. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.