Skip to content

Commit

Permalink
(yegor256#1226) Replaced AndInThreads(Proc, ...) with ForEachInThreads
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoku committed Jan 2, 2020
1 parent 94d7f5a commit 21e9e86
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 78 deletions.
90 changes: 90 additions & 0 deletions src/main/java/org/cactoos/iterator/ForEachInThreads.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 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.iterator;

import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.func.FuncOf;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.AndInThreads;

/**
* Executes a {@link org.cactoos.Proc} for each element of an
* {@link java.lang.Iterable}
*
* <p>
* This class can be effectively used to iterate through a collection, just like
* {@link java.util.stream.Stream#forEach(java.util.function.Consumer)} works:
* </p>
*
* {@code
* new ForEach(
* new ProcOf<>(input -> System.out.printf("\'%s\' ", input)),
* ).execute(
* new IterableOf<>("Mary", "John", "William", "Napkin")
* ); // will print 'Mary' 'John' 'William' 'Napkin' to standard output
* }
* <p>
* There is no thread-safety guarantee.
*
* @param <X> The type to itetare over
* @since 0.44
*/
public final class ForEachInThreads<X> implements Proc<Iterable<X>> {

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

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

/**
* Ctor.
*
* @param src The iterable
* @exception Exception If fails
*/
@SafeVarargs
public final void exec(final X... src) throws Exception {
this.exec(new IterableOf<>(src));
}

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

}
21 changes: 0 additions & 21 deletions src/main/java/org/cactoos/scalar/AndInThreads.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ public final class AndInThreads implements Scalar<Boolean> {
*/
private final boolean shut;

/**
* Ctor.
* @param proc Proc to map
* @param src The iterable
* @param <X> Type of items in the iterable
*/
@SafeVarargs
public <X> AndInThreads(final Proc<X> proc, final X... src) {
this(new FuncOf<>(proc, true), src);
}

/**
* Ctor.
* @param func Func to map
Expand All @@ -93,16 +82,6 @@ public <X> AndInThreads(final Func<X, Boolean> func, final X... src) {
this(func, new IterableOf<>(src));
}

/**
* Ctor.
* @param proc Proc to use
* @param src The iterable
* @param <X> Type of items in the iterable
*/
public <X> AndInThreads(final Proc<X> proc, final Iterable<X> src) {
this(new FuncOf<>(proc, true), src);
}

/**
* Ctor.
* @param func Func to map
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/org/cactoos/iterator/ForEachInThreadsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 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.iterator;

import java.util.ArrayList;
import java.util.List;
import org.cactoos.Proc;
import org.cactoos.func.ProcNoNulls;
import org.cactoos.list.ListOf;
import org.cactoos.list.Synced;
import org.hamcrest.Matcher;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.MatcherOf;

/**
* Test case for {@link ForEachInThreads}.
*
* @since 0.44
* @checkstyle JavadocMethodCheck (500 lines)
*/
public class ForEachInThreadsTest {

@Test
public void testProcIterable() throws Exception {
final List<Integer> list = new Synced<>(
new ArrayList<>(
2
)
);
new ForEachInThreads<Integer>(
new ProcNoNulls<Integer>(
list::add
)
).exec(
new ListOf<>(
1, 2
)
);
new Assertion<>(
"List does not contain mapped Iterable elements (1)", list,
new IsIterableContainingInAnyOrder<Integer>(
new ListOf<Matcher<? super Integer>>(
new MatcherOf<>(
value -> {
return value.equals(
1
);
}
), new MatcherOf<>(
value -> {
return value.equals(
2
);
}
)
)
)
).affirm();
}

@Test
public void testProcVarargs() throws Exception {
final List<Integer> list = new Synced<>(
new ArrayList<>(
2
)
);
new ForEachInThreads<Integer>(
(Proc<Integer>) list::add
).exec(
1, 1
);
new Assertion<>(
"List does not contain mapped Iterable elements (2)", list,
new IsIterableContainingInAnyOrder<Integer>(
new ListOf<Matcher<? super Integer>>(
new MatcherOf<>(
value -> {
return value.equals(
1
);
}
), new MatcherOf<>(
value -> {
return value.equals(
1
);
}
)
)
)
).affirm();
}

}
57 changes: 0 additions & 57 deletions src/test/java/org/cactoos/scalar/AndInThreadsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.cactoos.Proc;
import org.cactoos.Scalar;
import org.cactoos.func.FuncOf;
import org.cactoos.func.ProcNoNulls;
Expand Down Expand Up @@ -157,34 +156,6 @@ public void iteratesEmptyList() {
);
}

@Test
public void worksWithProc() throws Exception {
final List<Integer> list = new Synced<>(
new ArrayList<>(2)
);
new AndInThreads(
(Proc<Integer>) list::add,
1, 1
).value();
MatcherAssert.assertThat(
list,
new IsIterableContainingInAnyOrder<Integer>(
new ListOf<Matcher<? super Integer>>(
new MatcherOf<>(
value -> {
return value.equals(1);
}
),
new MatcherOf<>(
value -> {
return value.equals(1);
}
)
)
)
);
}

@Test
public void worksWithFunc() throws Exception {
MatcherAssert.assertThat(
Expand All @@ -196,34 +167,6 @@ public void worksWithFunc() throws Exception {
);
}

@Test
public void worksWithProcIterable() throws Exception {
final List<Integer> list = new Synced<>(
new ArrayList<>(2)
);
new AndInThreads(
new ProcNoNulls<Integer>(list::add),
new ListOf<>(1, 2)
).value();
MatcherAssert.assertThat(
list,
new IsIterableContainingInAnyOrder<Integer>(
new ListOf<Matcher<? super Integer>>(
new MatcherOf<>(
value -> {
return value.equals(1);
}
),
new MatcherOf<>(
value -> {
return value.equals(2);
}
)
)
)
);
}

@Test
public void worksWithIterableScalarBoolean() throws Exception {
MatcherAssert.assertThat(
Expand Down

0 comments on commit 21e9e86

Please sign in to comment.