complete Studio core and JavaFX boundary migration
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 64.01% (20482/31999)
* Branch Coverage: 54.22% (8240/15196)
* Lines of Code: 31999
* Cyclomatic Complexity: 13164
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 715
Intrepid/Prometeu/Studio/pipeline/head This commit looks good
Intrepid/Prometeu/Studio/pipeline/pr-master Build started...
JaCoCo Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 64.01% (20482/31999)
* Branch Coverage: 54.22% (8240/15196)
* Lines of Code: 31999
* Cyclomatic Complexity: 13164
#### Quality Gates Summary
Output truncated.
Test / Build skipped: 15, passed: 715
Intrepid/Prometeu/Studio/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
package p.studio.app.operation;
|
||||
|
||||
import p.studio.api.operation.OperationExecutor;
|
||||
import p.studio.api.operation.OperationHandle;
|
||||
import p.studio.api.operation.OperationId;
|
||||
import p.studio.api.operation.OperationSnapshot;
|
||||
import p.studio.api.operation.OperationState;
|
||||
import p.studio.api.operation.OperationSubscription;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class ExecutorOperationExecutor implements OperationExecutor {
|
||||
private final ExecutorService executor;
|
||||
|
||||
public ExecutorOperationExecutor(ExecutorService executor) {
|
||||
this.executor = Objects.requireNonNull(executor, "executor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> OperationHandle<R> submit(String summary, Callable<R> operation) {
|
||||
final ExecutorOperationHandle<R> handle = new ExecutorOperationHandle<>(summary, operation);
|
||||
handle.start(executor);
|
||||
return handle;
|
||||
}
|
||||
|
||||
private static final class ExecutorOperationHandle<R> implements OperationHandle<R> {
|
||||
private final OperationId id = new OperationId(UUID.randomUUID().toString());
|
||||
private final String summary;
|
||||
private final Callable<R> operation;
|
||||
private final CompletableFuture<R> completion = new CompletableFuture<>();
|
||||
private final CopyOnWriteArrayList<Consumer<OperationSnapshot>> subscribers = new CopyOnWriteArrayList<>();
|
||||
private final AtomicReference<OperationSnapshot> snapshot;
|
||||
private volatile Future<?> task;
|
||||
|
||||
private ExecutorOperationHandle(String summary, Callable<R> operation) {
|
||||
this.summary = Objects.requireNonNullElse(summary, "");
|
||||
this.operation = Objects.requireNonNull(operation, "operation");
|
||||
this.snapshot = new AtomicReference<>(new OperationSnapshot(id, OperationState.PENDING, Instant.now(), this.summary));
|
||||
}
|
||||
|
||||
private void start(ExecutorService executor) {
|
||||
final FutureTask<Void> submitted = new FutureTask<>(() -> {
|
||||
try {
|
||||
final R result = operation.call();
|
||||
if (transitionTerminal(OperationState.SUCCEEDED, summary)) {
|
||||
completion.complete(result);
|
||||
}
|
||||
} catch (Throwable failure) {
|
||||
if (transitionTerminal(OperationState.FAILED, failureSummary(failure))) {
|
||||
completion.completeExceptionally(failure);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
task = submitted;
|
||||
try {
|
||||
transition(OperationState.RUNNING, summary);
|
||||
executor.execute(submitted);
|
||||
} catch (RuntimeException failure) {
|
||||
if (transitionTerminal(OperationState.FAILED, failureSummary(failure))) {
|
||||
completion.completeExceptionally(failure);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OperationId id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OperationSnapshot snapshot() {
|
||||
return snapshot.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<R> completion() {
|
||||
return completion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cancel() {
|
||||
while (true) {
|
||||
final OperationSnapshot current = snapshot.get();
|
||||
if (terminal(current.state())) {
|
||||
return false;
|
||||
}
|
||||
final OperationSnapshot cancelled = new OperationSnapshot(id, OperationState.CANCELLED, Instant.now(), summary);
|
||||
if (snapshot.compareAndSet(current, cancelled)) {
|
||||
final Future<?> submitted = task;
|
||||
if (submitted != null) {
|
||||
submitted.cancel(true);
|
||||
}
|
||||
completion.cancel(false);
|
||||
publish(cancelled);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OperationSubscription subscribe(Consumer<OperationSnapshot> subscriber) {
|
||||
final Consumer<OperationSnapshot> listener = Objects.requireNonNull(subscriber, "subscriber");
|
||||
subscribers.add(listener);
|
||||
notifySubscriber(listener, snapshot.get());
|
||||
return () -> subscribers.remove(listener);
|
||||
}
|
||||
|
||||
private void transition(OperationState state, String message) {
|
||||
final OperationSnapshot next = new OperationSnapshot(id, state, Instant.now(), message);
|
||||
snapshot.set(next);
|
||||
publish(next);
|
||||
}
|
||||
|
||||
private boolean transitionTerminal(OperationState state, String message) {
|
||||
while (true) {
|
||||
final OperationSnapshot current = snapshot.get();
|
||||
if (terminal(current.state())) {
|
||||
return false;
|
||||
}
|
||||
final OperationSnapshot next = new OperationSnapshot(id, state, Instant.now(), message);
|
||||
if (snapshot.compareAndSet(current, next)) {
|
||||
publish(next);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void publish(OperationSnapshot value) {
|
||||
subscribers.forEach(subscriber -> notifySubscriber(subscriber, value));
|
||||
}
|
||||
|
||||
private static void notifySubscriber(Consumer<OperationSnapshot> subscriber, OperationSnapshot value) {
|
||||
try {
|
||||
subscriber.accept(value);
|
||||
} catch (RuntimeException ignored) {
|
||||
// Observation must not change operation completion.
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean terminal(OperationState state) {
|
||||
return state == OperationState.SUCCEEDED || state == OperationState.FAILED || state == OperationState.CANCELLED;
|
||||
}
|
||||
|
||||
private String failureSummary(Throwable failure) {
|
||||
final String message = failure.getMessage();
|
||||
return message == null || message.isBlank() ? summary : message;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user