mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-11 12:56:53 -04:00
Add comments
This commit is contained in:
parent
57a7eaccf5
commit
9ae952bb78
@ -64,7 +64,7 @@ public final class Accounts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean hasCurrentCharacter(Account account) {
|
public static boolean hasCurrentCharacter(Account account) {
|
||||||
return Lang.get(account.getProperties(), "character", String.class, null) != null;
|
return Lang.get(account.getProperties(), "character", String.class).isPresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getCurrentCharacter(Account account) {
|
public static String getCurrentCharacter(Account account) {
|
||||||
|
@ -69,7 +69,7 @@ public class Settings {
|
|||||||
{
|
{
|
||||||
for (Map<Object, Object> settings : SETTINGS.getAccounts()) {
|
for (Map<Object, Object> settings : SETTINGS.getAccounts()) {
|
||||||
Optional<String> characterName = Accounts.getCurrentCharacter(settings);
|
Optional<String> characterName = Accounts.getCurrentCharacter(settings);
|
||||||
AccountFactory<?> factory = Accounts.ACCOUNT_FACTORY.get(Lang.get(settings, "type", String.class, ""));
|
AccountFactory<?> factory = Accounts.ACCOUNT_FACTORY.get(Lang.get(settings, "type", String.class).orElse(""));
|
||||||
if (factory == null || !characterName.isPresent()) {
|
if (factory == null || !characterName.isPresent()) {
|
||||||
// unrecognized account type, so remove it.
|
// unrecognized account type, so remove it.
|
||||||
SETTINGS.getAccounts().remove(settings);
|
SETTINGS.getAccounts().remove(settings);
|
||||||
|
@ -114,7 +114,7 @@ public final class FXUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void removeListener(Node node, String key) {
|
public static void removeListener(Node node, String key) {
|
||||||
Lang.convert(node.getProperties().get(key), ListenerPair.class)
|
Lang.cast(node.getProperties().get(key), ListenerPair.class)
|
||||||
.ifPresent(info -> {
|
.ifPresent(info -> {
|
||||||
info.unbind();
|
info.unbind();
|
||||||
node.getProperties().remove(key);
|
node.getProperties().remove(key);
|
||||||
@ -329,7 +329,7 @@ public final class FXUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void unbindEnum(JFXComboBox<?> comboBox) {
|
public static void unbindEnum(JFXComboBox<?> comboBox) {
|
||||||
ChangeListener listener = Lang.get(comboBox.getProperties(), "listener", ChangeListener.class, null);
|
ChangeListener listener = Lang.get(comboBox.getProperties(), "listener", ChangeListener.class).orElse(null);
|
||||||
if (listener == null) return;
|
if (listener == null) return;
|
||||||
comboBox.getSelectionModel().selectedIndexProperty().removeListener(listener);
|
comboBox.getSelectionModel().selectedIndexProperty().removeListener(listener);
|
||||||
}
|
}
|
||||||
|
@ -66,9 +66,9 @@ public final class DownloadWizardProvider implements WizardProvider {
|
|||||||
if (!settings.containsKey(ModpackPage.MODPACK_FILE))
|
if (!settings.containsKey(ModpackPage.MODPACK_FILE))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
File selected = Lang.get(settings, ModpackPage.MODPACK_FILE, File.class, null);
|
File selected = Lang.get(settings, ModpackPage.MODPACK_FILE, File.class).orElse(null);
|
||||||
Modpack modpack = Lang.get(settings, ModpackPage.MODPACK_CURSEFORGE_MANIFEST, Modpack.class, null);
|
Modpack modpack = Lang.get(settings, ModpackPage.MODPACK_CURSEFORGE_MANIFEST, Modpack.class).orElse(null);
|
||||||
String name = Lang.get(settings, ModpackPage.MODPACK_NAME, String.class, null);
|
String name = Lang.get(settings, ModpackPage.MODPACK_NAME, String.class).orElse(null);
|
||||||
if (selected == null || modpack == null || name == null) return null;
|
if (selected == null || modpack == null || name == null) return null;
|
||||||
|
|
||||||
return ModpackHelper.getInstallTask(profile, selected, name, modpack);
|
return ModpackHelper.getInstallTask(profile, selected, name, modpack);
|
||||||
|
@ -41,7 +41,7 @@ public abstract class AccountFactory<T extends Account> {
|
|||||||
|
|
||||||
public final T fromStorage(Map<Object, Object> storage) {
|
public final T fromStorage(Map<Object, Object> storage) {
|
||||||
T account = fromStorageImpl(storage);
|
T account = fromStorageImpl(storage);
|
||||||
Map<?, ?> properties = Lang.get(storage, "properties", Map.class, null);
|
Map<?, ?> properties = Lang.get(storage, "properties", Map.class).orElse(null);
|
||||||
if (properties == null) return account;
|
if (properties == null) return account;
|
||||||
account.getProperties().putAll(properties);
|
account.getProperties().putAll(properties);
|
||||||
return account;
|
return account;
|
||||||
|
@ -39,8 +39,8 @@ public class AuthlibInjectorAccountFactory extends AccountFactory<YggdrasilAccou
|
|||||||
.orElseThrow(() -> new IllegalArgumentException("storage does not have key " + STORAGE_KEY_SERVER_BASE_URL));
|
.orElseThrow(() -> new IllegalArgumentException("storage does not have key " + STORAGE_KEY_SERVER_BASE_URL));
|
||||||
|
|
||||||
AuthlibInjectorAccount account = new AuthlibInjectorAccount(injectorJarPathSupplier, serverBaseURL, username);
|
AuthlibInjectorAccount account = new AuthlibInjectorAccount(injectorJarPathSupplier, serverBaseURL, username);
|
||||||
account.setUserId(Lang.get(storage, STORAGE_KEY_USER_ID, String.class, username));
|
account.setUserId(Lang.get(storage, STORAGE_KEY_USER_ID, String.class).orElse(username));
|
||||||
account.setAccessToken(Lang.get(storage, STORAGE_KEY_ACCESS_TOKEN, String.class, null));
|
account.setAccessToken(Lang.get(storage, STORAGE_KEY_ACCESS_TOKEN, String.class).orElse(null));
|
||||||
account.setClientToken(Lang.get(storage, STORAGE_KEY_CLIENT_TOKEN, String.class)
|
account.setClientToken(Lang.get(storage, STORAGE_KEY_CLIENT_TOKEN, String.class)
|
||||||
.orElseThrow(() -> new IllegalArgumentException("storage does not have key " + STORAGE_KEY_CLIENT_TOKEN)));
|
.orElseThrow(() -> new IllegalArgumentException("storage does not have key " + STORAGE_KEY_CLIENT_TOKEN)));
|
||||||
|
|
||||||
|
@ -58,8 +58,8 @@ public class YggdrasilAccountFactory extends AccountFactory<YggdrasilAccount> {
|
|||||||
.orElseThrow(() -> new IllegalArgumentException("storage does not have key " + STORAGE_KEY_USER_NAME));
|
.orElseThrow(() -> new IllegalArgumentException("storage does not have key " + STORAGE_KEY_USER_NAME));
|
||||||
|
|
||||||
YggdrasilAccount account = new YggdrasilAccount(baseAuthServer, baseSessionServer, username);
|
YggdrasilAccount account = new YggdrasilAccount(baseAuthServer, baseSessionServer, username);
|
||||||
account.setUserId(Lang.get(storage, STORAGE_KEY_USER_ID, String.class, username));
|
account.setUserId(Lang.get(storage, STORAGE_KEY_USER_ID, String.class).orElse(username));
|
||||||
account.setAccessToken(Lang.get(storage, STORAGE_KEY_ACCESS_TOKEN, String.class, null));
|
account.setAccessToken(Lang.get(storage, STORAGE_KEY_ACCESS_TOKEN, String.class).orElse(null));
|
||||||
account.setClientToken(Lang.get(storage, STORAGE_KEY_CLIENT_TOKEN, String.class)
|
account.setClientToken(Lang.get(storage, STORAGE_KEY_CLIENT_TOKEN, String.class)
|
||||||
.orElseThrow(() -> new IllegalArgumentException("storage does not have key " + STORAGE_KEY_CLIENT_TOKEN)));
|
.orElseThrow(() -> new IllegalArgumentException("storage does not have key " + STORAGE_KEY_CLIENT_TOKEN)));
|
||||||
|
|
||||||
|
@ -20,6 +20,13 @@ public final class Lang {
|
|||||||
private Lang() {
|
private Lang() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a mutable map by given key-value pairs.
|
||||||
|
* @param pairs entries in the new map
|
||||||
|
* @param <K> the type of keys
|
||||||
|
* @param <V> the type of values
|
||||||
|
* @return the map which contains data in {@code pairs}.
|
||||||
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <K, V> Map<K, V> mapOf(Pair<K, V>... pairs) {
|
public static <K, V> Map<K, V> mapOf(Pair<K, V>... pairs) {
|
||||||
HashMap<K, V> map = new HashMap<>();
|
HashMap<K, V> map = new HashMap<>();
|
||||||
@ -38,7 +45,8 @@ public final class Lang {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <E extends Throwable> void throwable(Throwable exception) throws E {
|
@SuppressWarnings("unchecked")
|
||||||
|
private static <E extends Throwable> void throwable(Throwable exception) throws E {
|
||||||
throw (E) exception;
|
throw (E) exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +116,6 @@ public final class Lang {
|
|||||||
*
|
*
|
||||||
* @param <T> type of result.
|
* @param <T> type of result.
|
||||||
* @param consumer your method.
|
* @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) {
|
public static <T, E extends Exception> void invokeConsumer(ExceptionalConsumer<T, E> consumer, T t) {
|
||||||
try {
|
try {
|
||||||
@ -175,7 +182,7 @@ public final class Lang {
|
|||||||
public static <T> T ignoringException(ExceptionalSupplier<T, ?> supplier, T defaultValue) {
|
public static <T> T ignoringException(ExceptionalSupplier<T, ?> supplier, T defaultValue) {
|
||||||
try {
|
try {
|
||||||
return supplier.get();
|
return supplier.get();
|
||||||
} catch (Exception e) {
|
} catch (Exception ignore) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -183,24 +190,32 @@ public final class Lang {
|
|||||||
public static void ignoringException(ExceptionalRunnable<?> runnable) {
|
public static void ignoringException(ExceptionalRunnable<?> runnable) {
|
||||||
try {
|
try {
|
||||||
runnable.run();
|
runnable.run();
|
||||||
} catch (Exception e) {
|
} catch (Exception ignore) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <V> Optional<V> convert(Object o, Class<V> clazz) {
|
/**
|
||||||
if (o == null || !ReflectionHelper.isInstance(clazz, o))
|
* Cast {@code obj} to V dynamically.
|
||||||
|
* @param obj the object reference to be cast.
|
||||||
|
* @param clazz the class reference of {@code V}.
|
||||||
|
* @param <V> the type that {@code obj} is being cast to.
|
||||||
|
* @return {@code obj} in the type of {@code V}.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <V> Optional<V> cast(Object obj, Class<V> clazz) {
|
||||||
|
if (obj == null || !ReflectionHelper.isInstance(clazz, obj))
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
else
|
else
|
||||||
return Optional.of((V) o);
|
return Optional.of((V) obj);
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> V convert(Object o, Class<V> clazz, V defaultValue) {
|
|
||||||
if (o == null || !ReflectionHelper.isInstance(clazz, o))
|
|
||||||
return defaultValue;
|
|
||||||
else
|
|
||||||
return (V) o;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the element at the specific position {@code index} in {@code list}.
|
||||||
|
*
|
||||||
|
* @param index the index of element to be return
|
||||||
|
* @param <V> the type of elements in {@code list}
|
||||||
|
* @return the element at the specific position, null if index is out of bound.
|
||||||
|
*/
|
||||||
public static <V> Optional<V> get(List<V> list, int index) {
|
public static <V> Optional<V> get(List<V> list, int index) {
|
||||||
if (index < 0 || index >= list.size())
|
if (index < 0 || index >= list.size())
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
@ -208,14 +223,28 @@ public final class Lang {
|
|||||||
return Optional.ofNullable(list.get(index));
|
return Optional.ofNullable(list.get(index));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the value to which the specific key is mapped,
|
||||||
|
* or {@code null} if {@code map} has no mapping for the key
|
||||||
|
* or the value is not in the type of {@code clazz}.
|
||||||
|
*
|
||||||
|
* @param map the map
|
||||||
|
* @param key the key for finding the associate value.
|
||||||
|
* @param <V> the type of values in {@code map}
|
||||||
|
* @return the value to which the specific key is mapped, or {@code null} if {@code map} has no mapping for the key or the type is not correct.
|
||||||
|
*/
|
||||||
public static <V> Optional<V> get(Map<?, ?> map, Object key, Class<V> clazz) {
|
public static <V> Optional<V> get(Map<?, ?> map, Object key, Class<V> clazz) {
|
||||||
return convert(map.get(key), clazz);
|
return cast(map.get(key), clazz);
|
||||||
}
|
|
||||||
|
|
||||||
public static <V> V get(Map<?, ?> map, Object key, Class<V> clazz, V defaultValue) {
|
|
||||||
return convert(map.get(key), clazz, defaultValue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Join two collections into one list.
|
||||||
|
*
|
||||||
|
* @param a one collection, to be joined.
|
||||||
|
* @param b another collection to be joined.
|
||||||
|
* @param <T> the super type of elements in {@code a} and {@code b}
|
||||||
|
* @return the joint collection
|
||||||
|
*/
|
||||||
public static <T> List<T> merge(Collection<? extends T> a, Collection<? extends T> b) {
|
public static <T> List<T> merge(Collection<? extends T> a, Collection<? extends T> b) {
|
||||||
List<T> result = new ArrayList<>();
|
List<T> result = new ArrayList<>();
|
||||||
if (a != null)
|
if (a != null)
|
||||||
@ -236,14 +265,32 @@ public final class Lang {
|
|||||||
}, null, isDaemon);
|
}, null, isDaemon);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start a thread invoking {@code runnable} immediately.
|
||||||
|
* @param runnable code to run.
|
||||||
|
* @return the reference of the started thread
|
||||||
|
*/
|
||||||
public static Thread thread(Runnable runnable) {
|
public static Thread thread(Runnable runnable) {
|
||||||
return thread(runnable, null);
|
return thread(runnable, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start a thread invoking {@code runnable} immediately.
|
||||||
|
* @param runnable code to run
|
||||||
|
* @param name the name of thread
|
||||||
|
* @return the reference of the started thread
|
||||||
|
*/
|
||||||
public static Thread thread(Runnable runnable, String name) {
|
public static Thread thread(Runnable runnable, String name) {
|
||||||
return thread(runnable, name, false);
|
return thread(runnable, name, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start a thread invoking {@code runnable} immediately.
|
||||||
|
* @param runnable code to run
|
||||||
|
* @param name the name of thread
|
||||||
|
* @param isDaemon true if thread will be terminated when only daemon threads are running.
|
||||||
|
* @return the reference of the started thread
|
||||||
|
*/
|
||||||
public static Thread thread(Runnable runnable, String name, boolean isDaemon) {
|
public static Thread thread(Runnable runnable, String name, boolean isDaemon) {
|
||||||
Thread thread = new Thread(runnable);
|
Thread thread = new Thread(runnable);
|
||||||
if (isDaemon)
|
if (isDaemon)
|
||||||
@ -297,6 +344,12 @@ public final class Lang {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the first non-null reference in given list.
|
||||||
|
* @param t nullable references list.
|
||||||
|
* @param <T> the type of nullable references
|
||||||
|
* @return the first non-null reference.
|
||||||
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <T> T nonNull(T... t) {
|
public static <T> T nonNull(T... t) {
|
||||||
for (T a : t) if (a != null) return a;
|
for (T a : t) if (a != null) return a;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user