io.github.dbstarll.utils.lang.launcher.AbstractLauncher Maven / Gradle / Ivy
The newest version!
package io.github.dbstarll.utils.lang.launcher;
import org.apache.commons.lang3.ArrayUtils;
import java.util.HashMap;
import java.util.Map;
public abstract class AbstractLauncher implements Task {
private final Map> tasks = new HashMap>();
protected final void addTask(String token, Class extends E> taskClass) {
tasks.put(token, taskClass);
}
@Override
public final int run(String... args) throws Throwable {
if (ArrayUtils.isEmpty(args)) {
throw new IllegalArgumentException("Need task token.");
}
String token = args[0];
Class extends E> taskClass = tasks.get(token);
if (taskClass == null) {
throw new IllegalArgumentException("Unknown task token: " + token);
}
try {
return run(taskClass, ArrayUtils.subarray(args, 1, args.length));
} catch (LaunchException ex) {
throw new IllegalArgumentException(ex);
}
}
protected abstract int run(Class extends E> taskClass, String... args) throws LaunchException, Throwable;
}