Skip to content

Commit

Permalink
(yegor256#1226) Moved ForEach primitives to func package
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoku committed Jan 9, 2020
1 parent 1b80caf commit d92476a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.iterator;
package org.cactoos.func;

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

/**
Expand All @@ -49,41 +47,30 @@
* There is no thread-safety guarantee.
*
* @param <X> The type to itetare over
* @since 0.44
* @since 1.0
*/
public final class ForEach<X> implements Proc<Iterable<X>> {

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

/**
* Ctor.
*
* @param proc The proc to execute
*/
public ForEach(final Proc<X> proc) {
this.wrapped = new FuncOf<>(
this.func = 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 And(
this.wrapped, input
this.func, input
).value();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.iterator;
package org.cactoos.func;

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

/**
Expand All @@ -44,47 +42,37 @@
* new ProcOf<>(input -> System.out.printf("\'%s\' ", input)),
* ).execute(
* new IterableOf<>("Mary", "John", "William", "Napkin")
* ); // will print 'Mary' 'John' 'William' 'Napkin' to standard output. Sorting is not guaranteed.
* ); // Will print 'Mary' 'John' 'William' 'Napkin' to standard output.
* // Order of printing can be random.
* }
* <p>
* There is no thread-safety guarantee.
*
* @param <X> The type to itetare over
* @since 0.44
* @since 1.0
*/
public final class ForEachInThreads<X> implements Proc<Iterable<X>> {

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

/**
* Ctor.
*
* @param proc The proc to execute
*/
public ForEachInThreads(final Proc<X> proc) {
this.wrapped = new FuncOf<>(
this.func = 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
this.func, input
).value();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.iterator;
package org.cactoos.func;

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;
Expand All @@ -38,7 +36,7 @@
/**
* Test case for {@link ForEachInThreads}.
*
* @since 0.44
* @since 1.0
* @checkstyle JavadocMethodCheck (500 lines)
*/
public class ForEachInThreadsTest {
Expand Down Expand Up @@ -81,38 +79,4 @@ public void testProcIterable() throws Exception {
).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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.iterator;
package org.cactoos.func;

import java.util.LinkedList;
import java.util.List;
Expand All @@ -35,7 +35,7 @@
/**
* Test case for {@link ForEach}.
*
* @since 0.44
* @since 1.0
* @checkstyle JavadocMethodCheck (500 lines)
*/
public class ForEachTest {
Expand All @@ -51,22 +51,9 @@ public void testProcIterable() throws Exception {
)
);
new Assertion<>(
"List does not contain mapped Iterable elements (1)", list, new IsEqual<>(
new ListOf<>(
1, 1
)
)
).affirm();
}

@Test
public void testProcVarargs() throws Exception {
final List<Integer> list = new LinkedList<>();
new ForEach<Integer>(
(Proc<Integer>) list::add
).exec(1, 1);
new Assertion<>(
"List does not contain mapped Iterable elements (2)", list, new IsEqual<>(
"List does not contain mapped Iterable elements (1)",
list,
new IsEqual<>(
new ListOf<>(
1, 1
)
Expand Down

0 comments on commit d92476a

Please sign in to comment.