Update Readme.md for HMCLCore introduction
This commit is contained in:
@@ -183,9 +183,9 @@ public abstract class Task {
|
||||
return new TaskExecutor(this);
|
||||
}
|
||||
|
||||
public final TaskExecutor executor(TaskListener taskListener) {
|
||||
public final TaskExecutor executor(Function<TaskExecutor, TaskListener> taskListener) {
|
||||
TaskExecutor executor = new TaskExecutor(this);
|
||||
executor.setTaskListener(taskListener);
|
||||
executor.setTaskListener(taskListener.apply(executor));
|
||||
return executor;
|
||||
}
|
||||
|
||||
@@ -204,13 +204,21 @@ public abstract class Task {
|
||||
}
|
||||
|
||||
public final TaskExecutor subscribe(Scheduler scheduler, ExceptionalConsumer<AutoTypingMap<String>, ?> closure) {
|
||||
return subscribe(of(closure, scheduler));
|
||||
return subscribe(of(scheduler, closure));
|
||||
}
|
||||
|
||||
public final TaskExecutor subscribe(Scheduler scheduler, ExceptionalRunnable<?> closure) {
|
||||
return subscribe(of(scheduler, i -> closure.run()));
|
||||
}
|
||||
|
||||
public final TaskExecutor subscribe(ExceptionalConsumer<AutoTypingMap<String>, ?> closure) {
|
||||
return subscribe(of(closure));
|
||||
}
|
||||
|
||||
public final TaskExecutor subscribe(ExceptionalRunnable<?> closure) {
|
||||
return subscribe(of(closure));
|
||||
}
|
||||
|
||||
public final Task then(Task b) {
|
||||
return then(s -> b);
|
||||
}
|
||||
@@ -237,13 +245,17 @@ public abstract class Task {
|
||||
}
|
||||
|
||||
public static Task of(ExceptionalConsumer<AutoTypingMap<String>, ?> closure) {
|
||||
return of(closure, Schedulers.defaultScheduler());
|
||||
return of(Schedulers.defaultScheduler(), closure);
|
||||
}
|
||||
|
||||
public static Task of(ExceptionalConsumer<AutoTypingMap<String>, ?> closure, Scheduler scheduler) {
|
||||
public static Task of(Scheduler scheduler, ExceptionalConsumer<AutoTypingMap<String>, ?> closure) {
|
||||
return new SimpleTask(closure, scheduler);
|
||||
}
|
||||
|
||||
public static Task of(Scheduler scheduler, ExceptionalRunnable<?> closure) {
|
||||
return new SimpleTask(i -> closure.run(), scheduler);
|
||||
}
|
||||
|
||||
public static <V> TaskResult<V> ofResult(String id, Callable<V> callable) {
|
||||
return new TaskCallable<>(id, callable);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -38,6 +39,10 @@ public final class AutoTypingMap<K> {
|
||||
return (V) impl.get(key);
|
||||
}
|
||||
|
||||
public <V> Optional<V> getOptional(K key) {
|
||||
return Optional.ofNullable(get(key));
|
||||
}
|
||||
|
||||
public void set(K key, Object value) {
|
||||
if (value != null)
|
||||
impl.put(key, value);
|
||||
|
||||
@@ -71,12 +71,12 @@ public final class Lang {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T, R, E extends Exception> Function<T, R> hideException(ExceptionalFunction<T, R, E> function) {
|
||||
public static <T, R, E extends Exception> Function<T, R> hideFunction(ExceptionalFunction<T, R, E> function) {
|
||||
return r -> invoke(function, r);
|
||||
}
|
||||
|
||||
public static <T, R, E extends Exception> Function<T, R> liftException(ExceptionalFunction<T, R, E> function) throws E {
|
||||
return hideException(function);
|
||||
public static <T, R, E extends Exception> Function<T, R> liftFunction(ExceptionalFunction<T, R, E> function) throws E {
|
||||
return hideFunction(function);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,6 +107,33 @@ public final class Lang {
|
||||
return hideException(supplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will call a method without checked exceptions
|
||||
* by treating the compiler.
|
||||
*
|
||||
* If this method throws a checked exception,
|
||||
* it will still abort the application because of the exception.
|
||||
*
|
||||
* @param <T> type of result.
|
||||
* @param consumer your method.
|
||||
* @return the result of the method to invoke.
|
||||
*/
|
||||
public static <T, E extends Exception> void invokeConsumer(ExceptionalConsumer<T, E> consumer, T t) {
|
||||
try {
|
||||
consumer.accept(t);
|
||||
} catch (Exception e) {
|
||||
throwable(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T, E extends Exception> Consumer<T> hideConsumer(ExceptionalConsumer<T, E> consumer) {
|
||||
return it -> invokeConsumer(consumer, it);
|
||||
}
|
||||
|
||||
public static <T, E extends Exception> Consumer<T> liftConsumer(ExceptionalConsumer<T, E> consumer) throws E {
|
||||
return hideConsumer(consumer);
|
||||
}
|
||||
|
||||
public static <E extends Exception> boolean test(ExceptionalSupplier<Boolean, E> r) {
|
||||
try {
|
||||
return r.get();
|
||||
@@ -248,17 +275,25 @@ public final class Lang {
|
||||
return () -> asIterator(enumeration);
|
||||
}
|
||||
|
||||
public static int parseInt(String string, int defaultValue) {
|
||||
public static int parseInt(Object string, int defaultValue) {
|
||||
try {
|
||||
return Integer.parseInt(string);
|
||||
return Integer.parseInt(string.toString());
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer toIntOrNull(String string) {
|
||||
public static Integer toIntOrNull(Object string) {
|
||||
try {
|
||||
return Integer.parseInt(string);
|
||||
return Integer.parseInt(string.toString());
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Double toDoubleOrNull(Object string) {
|
||||
try {
|
||||
return Double.parseDouble(string.toString());
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
@@ -268,4 +303,9 @@ public final class Lang {
|
||||
for (T a : t) if (a != null) return a;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> T apply(T t, Consumer<T> consumer) {
|
||||
consumer.accept(t);
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user