Remove redundant codes
This commit is contained in:
@@ -17,8 +17,6 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.download;
|
||||
|
||||
import org.jackhuang.hmcl.util.CacheRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
package org.jackhuang.hmcl.download;
|
||||
|
||||
import org.jackhuang.hmcl.download.forge.ForgeBMCLVersionList;
|
||||
import org.jackhuang.hmcl.download.forge.ForgeVersionList;
|
||||
import org.jackhuang.hmcl.download.game.GameVersionList;
|
||||
import org.jackhuang.hmcl.download.liteloader.LiteLoaderVersionList;
|
||||
import org.jackhuang.hmcl.download.optifine.OptiFineBMCLVersionList;
|
||||
|
||||
@@ -36,7 +36,6 @@ import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher
|
||||
* Copyright (C) 2019 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.task;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalRunnable;
|
||||
|
||||
/**
|
||||
* Determines how a task is executed.
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public abstract class Scheduler {
|
||||
|
||||
/**
|
||||
* Schedules the given task.
|
||||
*
|
||||
* @return the future
|
||||
*/
|
||||
public abstract Future<?> schedule(ExceptionalRunnable<?> block);
|
||||
|
||||
}
|
||||
@@ -17,10 +17,14 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.task;
|
||||
|
||||
import org.jackhuang.hmcl.util.*;
|
||||
import org.jackhuang.hmcl.util.Lang;
|
||||
import org.jackhuang.hmcl.util.Logging;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalRunnable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Level;
|
||||
@@ -108,15 +112,12 @@ public final class TaskExecutor {
|
||||
})
|
||||
.thenApplyAsync(unused -> (Exception) null)
|
||||
.exceptionally(throwable -> {
|
||||
if (!(throwable instanceof CompletionException))
|
||||
throw new AssertionError();
|
||||
|
||||
Throwable resolved = resolveException(throwable);
|
||||
if (resolved instanceof Exception) {
|
||||
return (Exception) resolved;
|
||||
} else {
|
||||
// If an error occurred, we just rethrow it.
|
||||
throw (CompletionException) throwable;
|
||||
throw new CompletionException(throwable);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -137,9 +138,7 @@ public final class TaskExecutor {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
})
|
||||
.thenComposeAsync(unused -> {
|
||||
return executeTasks(task.getDependents());
|
||||
})
|
||||
.thenComposeAsync(unused -> executeTasks(task.getDependents()))
|
||||
.thenComposeAsync(dependentsException -> {
|
||||
boolean isDependentsSucceeded = dependentsException == null;
|
||||
|
||||
@@ -160,9 +159,7 @@ public final class TaskExecutor {
|
||||
rethrow(throwable);
|
||||
});
|
||||
})
|
||||
.thenComposeAsync(unused -> {
|
||||
return executeTasks(task.getDependencies());
|
||||
})
|
||||
.thenComposeAsync(unused -> executeTasks(task.getDependencies()))
|
||||
.thenComposeAsync(dependenciesException -> {
|
||||
boolean isDependenciesSucceeded = dependenciesException == null;
|
||||
|
||||
@@ -176,7 +173,7 @@ public final class TaskExecutor {
|
||||
return CompletableFuture.completedFuture(dependenciesException);
|
||||
}
|
||||
})
|
||||
.thenApplyAsync(dependenciesException -> {
|
||||
.thenAcceptAsync(dependenciesException -> {
|
||||
boolean isDependenciesSucceeded = dependenciesException == null;
|
||||
|
||||
if (!isDependenciesSucceeded && task.isRelyingOnDependencies()) {
|
||||
@@ -193,14 +190,8 @@ public final class TaskExecutor {
|
||||
taskListeners.forEach(it -> it.onFinished(task));
|
||||
|
||||
task.setState(Task.TaskState.SUCCEEDED);
|
||||
|
||||
return null;
|
||||
})
|
||||
.thenApplyAsync(unused -> null)
|
||||
.exceptionally(throwable -> {
|
||||
if (!(throwable instanceof CompletionException))
|
||||
throw new AssertionError();
|
||||
|
||||
Throwable resolved = resolveException(throwable);
|
||||
if (resolved instanceof Exception) {
|
||||
Exception e = (Exception) resolved;
|
||||
@@ -226,7 +217,7 @@ public final class TaskExecutor {
|
||||
task.setState(Task.TaskState.FAILED);
|
||||
}
|
||||
|
||||
throw (CompletionException) throwable; // rethrow error
|
||||
throw new CompletionException(resolved); // rethrow error
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -25,29 +25,57 @@ import java.util.EventListener;
|
||||
*/
|
||||
public abstract class TaskListener implements EventListener {
|
||||
|
||||
/**
|
||||
* Executed when a Task execution chain starts.
|
||||
*/
|
||||
public void onStart() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed before the task's pre-execution and dependents execution.
|
||||
*
|
||||
* TaskState of this task is READY.
|
||||
*
|
||||
* @param task the task that gets ready.
|
||||
*/
|
||||
public void onReady(Task<?> task) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed when the task's execution starts.
|
||||
*
|
||||
* TaskState of this task is RUNNING.
|
||||
*
|
||||
* @param task the task which is being run.
|
||||
*/
|
||||
public void onRunning(Task<?> task) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed after the task's dependencies and post-execution finished.
|
||||
*
|
||||
* TaskState of the task is EXECUTED.
|
||||
*
|
||||
* @param task the task which finishes its work.
|
||||
*/
|
||||
public void onFinished(Task<?> task) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed when an exception occurred during the task's execution.
|
||||
*
|
||||
* @param task the task which finishes its work.
|
||||
*/
|
||||
public void onFailed(Task<?> task, Throwable throwable) {
|
||||
onFinished(task);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed when the task execution chain stopped.
|
||||
*
|
||||
* @param success true if no error occurred during task execution.
|
||||
* @param executor the task executor with responsibility to the task execution.
|
||||
*/
|
||||
public void onStop(boolean success, TaskExecutor executor) {
|
||||
}
|
||||
|
||||
public static class DefaultTaskListener extends TaskListener {
|
||||
private DefaultTaskListener() {
|
||||
}
|
||||
|
||||
public static final DefaultTaskListener INSTANCE = new DefaultTaskListener();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher
|
||||
* Copyright (C) 2019 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 java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A map that support auto casting.
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public final class AutoTypingMap<K> {
|
||||
|
||||
private final Map<K, Object> impl;
|
||||
|
||||
public AutoTypingMap(Map<K, Object> impl) {
|
||||
this.impl = impl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value associated with given {@code key} in the mapping.
|
||||
*
|
||||
* Be careful of the return type {@code <V>}, as you must ensure that {@code <V>} is correct
|
||||
*
|
||||
* @param key the key that the value associated with
|
||||
* @param <V> the type of value which you must ensure type correction
|
||||
* @return the value associated with given {@code key}
|
||||
* @throws ClassCastException if the return type {@code <V>} is incorrect.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized <V> V get(K key) throws ClassCastException {
|
||||
return (V) impl.get(key);
|
||||
}
|
||||
|
||||
public synchronized <V> Optional<V> getOptional(K key) {
|
||||
return Optional.ofNullable(get(key));
|
||||
}
|
||||
|
||||
public synchronized void set(K key, Object value) {
|
||||
if (value != null)
|
||||
impl.put(key, value);
|
||||
}
|
||||
|
||||
public Collection<Object> values() {
|
||||
return impl.values();
|
||||
}
|
||||
|
||||
public Set<K> keys() {
|
||||
return impl.keySet();
|
||||
}
|
||||
|
||||
public boolean containsKey(K key) {
|
||||
return impl.containsKey(key);
|
||||
}
|
||||
|
||||
public Object remove(K key) {
|
||||
return impl.remove(key);
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ package org.jackhuang.hmcl.util.io;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
Reference in New Issue
Block a user