[Feature] 让Java下载页面可以同时下载多个Java (#5394)
Co-authored-by: Glavo <zjx001202@gmail.com>
This commit is contained in:
committed by
GitHub
parent
25fef6b5c2
commit
80ba1ec1a7
@@ -22,6 +22,7 @@ import javafx.beans.property.DoubleProperty;
|
||||
import javafx.beans.property.ReadOnlyDoubleProperty;
|
||||
import javafx.beans.property.SimpleDoubleProperty;
|
||||
import org.jackhuang.hmcl.event.EventManager;
|
||||
import org.jackhuang.hmcl.util.Result;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalConsumer;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalFunction;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalRunnable;
|
||||
@@ -784,6 +785,37 @@ public abstract class Task<T> {
|
||||
return whenComplete(executor, (exception -> action.execute(getResult(), exception)));
|
||||
}
|
||||
|
||||
public Task<Result<T>> wrapResult() {
|
||||
return new Task<Result<T>>() {
|
||||
{
|
||||
setSignificance(TaskSignificance.MODERATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() throws Exception {
|
||||
if (isDependentsSucceeded() != (Task.this.getException() == null))
|
||||
throw new AssertionError("When whenComplete succeeded, Task.exception must be null.", Task.this.getException());
|
||||
|
||||
if (isDependentsSucceeded()) {
|
||||
setResult(Result.success(Task.this.getResult()));
|
||||
} else {
|
||||
setSignificance(TaskSignificance.MINOR);
|
||||
setResult(Result.failure(Task.this.getException()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Task<?>> getDependents() {
|
||||
return Collections.singleton(Task.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRelyingOnDependents() {
|
||||
return false;
|
||||
}
|
||||
}.setExecutor(executor).setName(getCaller()).setSignificance(TaskSignificance.MODERATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Task with the same exception as this task, that executes
|
||||
* the given actions when this task completes.
|
||||
|
||||
78
HMCLCore/src/main/java/org/jackhuang/hmcl/util/Result.java
Normal file
78
HMCLCore/src/main/java/org/jackhuang/hmcl/util/Result.java
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher
|
||||
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.jackhuang.hmcl.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/// @author Glavo
|
||||
public final class Result<T> {
|
||||
public static <T> Result<T> success(T value) {
|
||||
return new Result<>(value, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failure(@NotNull Throwable exception) {
|
||||
Objects.requireNonNull(exception);
|
||||
return new Result<>(null, exception);
|
||||
}
|
||||
|
||||
private final T value;
|
||||
private final Throwable exception;
|
||||
|
||||
private Result(T value, Throwable exception) {
|
||||
this.value = value;
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return exception == null;
|
||||
}
|
||||
|
||||
public boolean isFailure() {
|
||||
return exception != null;
|
||||
}
|
||||
|
||||
public T get() throws Throwable {
|
||||
if (exception != null)
|
||||
throw exception;
|
||||
else
|
||||
return value;
|
||||
}
|
||||
|
||||
public @Nullable T getOrNull() {
|
||||
return exception == null ? value : null;
|
||||
}
|
||||
|
||||
public @Nullable Throwable getException() {
|
||||
return exception;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(value, exception);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return this == obj || obj instanceof Result<?> other
|
||||
&& Objects.equals(value, other.value)
|
||||
&& Objects.equals(exception, other.exception);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user