Skip to content

Commit

Permalink
(yegor256#1293) Remove collection's Mapped
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Feb 29, 2020
1 parent 671155e commit 4b28a23
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 231 deletions.
68 changes: 0 additions & 68 deletions src/main/java/org/cactoos/collection/Mapped.java

This file was deleted.

31 changes: 11 additions & 20 deletions src/main/java/org/cactoos/experimental/Threads.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@
*/
package org.cactoos.experimental;

import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.cactoos.Func;
import org.cactoos.Scalar;
import org.cactoos.collection.Mapped;
import org.cactoos.func.CallableOf;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.iterable.IterableEnvelope;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.list.ListOf;

/**
* Allows to execute the tasks concurrently.
Expand All @@ -59,7 +60,7 @@ public Threads(final ExecutorService exc, final Scalar<T>... tasks) {
* @param tasks The tasks to be executed concurrently.
*/
public Threads(final ExecutorService exc, final Iterable<Scalar<T>> tasks) {
this(exc::invokeAll, tasks);
this(input -> exc.invokeAll(new ListOf<>(input)), tasks);
}

/**
Expand Down Expand Up @@ -88,7 +89,7 @@ public Threads(final int threads, final Iterable<Scalar<T>> tasks) {
threads
);
try {
return executor.invokeAll(todo);
return executor.invokeAll(new ListOf<>(todo));
} finally {
executor.shutdown();
}
Expand All @@ -101,26 +102,16 @@ public Threads(final int threads, final Iterable<Scalar<T>> tasks) {
* Ctor.
* @param fnc The function to map each task into {@link Future}.
* @param tasks The tasks to be executed concurrently.
* @checkstyle IllegalCatchCheck (20 lines)
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private Threads(
final Func<Collection<Callable<T>>, Collection<Future<T>>> fnc,
final Func<Iterable<Callable<T>>, Iterable<Future<T>>> fnc,
final Iterable<Scalar<T>> tasks
) {
super(
new IterableOf<>(
() -> {
try {
return new Mapped<>(
Future::get,
fnc.apply(new Mapped<>(task -> task::value, tasks))
).iterator();
} catch (final Exception exp) {
throw new CompletionException(exp);
}
}
)
() -> new Mapped<>(
Future::get,
new UncheckedFunc<>(fnc).apply(new Mapped<>(CallableOf::new, tasks))
).iterator()
);
}
}
31 changes: 11 additions & 20 deletions src/main/java/org/cactoos/experimental/Timed.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@
*/
package org.cactoos.experimental;

import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.cactoos.Func;
import org.cactoos.Scalar;
import org.cactoos.collection.Mapped;
import org.cactoos.func.CallableOf;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.iterable.IterableEnvelope;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.list.ListOf;

/**
* Allows to execute the tasks concurrently within given timeout.
Expand Down Expand Up @@ -77,7 +78,7 @@ public Timed(
final TimeUnit unit
) {
this(
input -> exc.invokeAll(input, timeout, unit),
input -> exc.invokeAll(new ListOf<>(input), timeout, unit),
tasks
);
}
Expand Down Expand Up @@ -124,7 +125,7 @@ public Timed(
threads
);
try {
return executor.invokeAll(todo, timeout, unit);
return executor.invokeAll(new ListOf<>(todo), timeout, unit);
} finally {
executor.shutdown();
}
Expand All @@ -137,26 +138,16 @@ public Timed(
* Ctor.
* @param fnc The function to map each task into {@link Future}.
* @param tasks The tasks to be executed concurrently.
* @checkstyle IllegalCatchCheck (20 lines)
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private Timed(
final Func<Collection<Callable<T>>, Collection<Future<T>>> fnc,
final Func<Iterable<Callable<T>>, Iterable<Future<T>>> fnc,
final Iterable<Scalar<T>> tasks
) {
super(
new IterableOf<>(
() -> {
try {
return new Mapped<>(
Future::get,
fnc.apply(new Mapped<>(task -> task::value, tasks))
).iterator();
} catch (final Exception exp) {
throw new CompletionException(exp);
}
}
)
() -> new Mapped<>(
Future::get,
new UncheckedFunc<>(fnc).apply(new Mapped<>(CallableOf::new, tasks))
).iterator()
);
}
}
11 changes: 6 additions & 5 deletions src/main/java/org/cactoos/func/CallableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.Callable;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.Scalar;

/**
* Func as {@link Callable}.
Expand All @@ -49,7 +50,7 @@ public final class CallableOf<X, T> implements Callable<T> {
/**
* Original callable.
*/
private final Callable<T> callable;
private final Scalar<T> scalar;

/**
* Ctor.
Expand Down Expand Up @@ -87,15 +88,15 @@ public CallableOf(final Func<X, T> fnc, final X ipt) {

/**
* Ctor.
* @param cal Encapsulated callable
* @param slr Encapsulated scalar
* @since 0.41
*/
public CallableOf(final Callable<T> cal) {
this.callable = cal;
public CallableOf(final Scalar<T> slr) {
this.scalar = slr;
}

@Override
public T call() throws Exception {
return this.callable.call();
return this.scalar.value();
}
}
104 changes: 0 additions & 104 deletions src/test/java/org/cactoos/collection/MappedTest.java

This file was deleted.

10 changes: 6 additions & 4 deletions src/test/java/org/cactoos/experimental/ThreadsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@

package org.cactoos.experimental;

import java.util.concurrent.CompletionException;
import java.io.UncheckedIOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.cactoos.Proc;
import org.cactoos.Scalar;
import org.cactoos.func.Repeated;
import org.cactoos.func.UncheckedFunc;
import org.junit.Test;
Expand Down Expand Up @@ -95,13 +96,14 @@ public void failsDueToException() {
"wraps error into CompletionException",
() -> new Threads<String>(
Executors.newSingleThreadExecutor(),
() -> {
(Scalar<String>) () -> {
throw new IllegalStateException("Something went wrong");
}
).iterator().next(),
new Throws<>(
"java.lang.IllegalStateException: Something went wrong",
CompletionException.class
// @checkstyle LineLengthCheck (1 line)
"java.io.IOException: java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Something went wrong",
UncheckedIOException.class
)
).affirm();
}
Expand Down
Loading

0 comments on commit 4b28a23

Please sign in to comment.