remove isDependentSucceeded in whenComplete

This commit is contained in:
huanghongxun
2019-02-25 23:59:21 +08:00
parent eeef76594f
commit cae32cff4c
9 changed files with 27 additions and 24 deletions

View File

@@ -68,8 +68,8 @@ public class DefaultGameBuilder extends GameBuilder {
libraryTask = libraryTask.thenCompose(dependencyManager.installLibraryAsync(remoteVersion));
return libraryTask;
}).whenComplete((isDependentSucceeded, exception) -> {
if (!isDependentSucceeded)
}).whenComplete(exception -> {
if (exception != null)
dependencyManager.getGameRepository().getVersionRoot(name).delete();
});
}

View File

@@ -19,9 +19,9 @@ package org.jackhuang.hmcl.task;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import org.jackhuang.hmcl.util.function.ExceptionalRunnable;
import org.jetbrains.annotations.NotNull;
/**
*
@@ -29,9 +29,9 @@ import org.jackhuang.hmcl.util.function.ExceptionalRunnable;
*/
class SchedulerImpl extends Scheduler {
private final Consumer<Runnable> executor;
private final Executor executor;
public SchedulerImpl(Consumer<Runnable> executor) {
public SchedulerImpl(Executor executor) {
this.executor = executor;
}
@@ -40,7 +40,7 @@ class SchedulerImpl extends Scheduler {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Exception> wrapper = new AtomicReference<>();
executor.accept(() -> {
executor.execute(() -> {
try {
block.run();
} catch (Exception e) {
@@ -81,7 +81,7 @@ class SchedulerImpl extends Scheduler {
}
@Override
public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
public Void get(long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
if (!latch.await(timeout, unit))
throw new TimeoutException();
return getImpl();

View File

@@ -595,7 +595,10 @@ public abstract class Task<T> {
@Override
public void execute() throws Exception {
action.execute(isDependentsSucceeded(), Task.this.getException());
if (isDependentsSucceeded() != (Task.this.getException() == null))
throw new AssertionError("When dependents succeeded, Task.exception must be nonnull.");
action.execute(Task.this.getException());
if (!isDependentsSucceeded()) {
setSignificance(TaskSignificance.MINOR);
@@ -634,7 +637,7 @@ public abstract class Task<T> {
* @return the new Task
*/
public Task<Void> whenComplete(Scheduler scheduler, FinalizedCallbackWithResult<T> action) {
return whenComplete(scheduler, ((isDependentSucceeded, exception) -> action.execute(getResult(), isDependentSucceeded, exception)));
return whenComplete(scheduler, (exception -> action.execute(getResult(), exception)));
}
/**
@@ -653,8 +656,8 @@ public abstract class Task<T> {
* @return the new Task
*/
public final <E1 extends Exception, E2 extends Exception> Task<Void> whenComplete(Scheduler scheduler, ExceptionalRunnable<E1> success, ExceptionalConsumer<Exception, E2> failure) {
return whenComplete(scheduler, (isDependentSucceeded, exception) -> {
if (isDependentSucceeded) {
return whenComplete(scheduler, exception -> {
if (exception == null) {
if (success != null)
try {
success.run();
@@ -805,11 +808,11 @@ public abstract class Task<T> {
}
public interface FinalizedCallback {
void execute(boolean isDependentSucceeded, Exception exception) throws Exception;
void execute(Exception exception) throws Exception;
}
public interface FinalizedCallbackWithResult<T> {
void execute(T result, boolean isDependentSucceeded, Exception exception) throws Exception;
void execute(T result, Exception exception) throws Exception;
}
private static String getCaller() {

View File

@@ -11,8 +11,8 @@ public class TaskTest {
Task.allOf(Task.runAsync(() -> {
throw new Exception();
}))
)).whenComplete(((isDependentSucceeded, exception) -> {
Assert.assertFalse(isDependentSucceeded);
)).whenComplete((exception -> {
Assert.assertFalse(exception == null);
})).test();
}
}