From 2c7ca6d3542980ba4f7fb01df93b797303e0ee9b Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Tue, 2 Oct 2018 22:01:42 +0800 Subject: [PATCH] Remove some methods in Lang --- .../java/org/jackhuang/hmcl/ui/FXUtils.java | 8 +- .../authlibinjector/AuthlibInjectorDnD.java | 16 +- .../hmcl/auth/yggdrasil/YggdrasilService.java | 15 +- .../download/game/LibraryDownloadTask.java | 2 +- .../org/jackhuang/hmcl/task/TaskExecutor.java | 8 +- .../java/org/jackhuang/hmcl/util/Lang.java | 155 ------------------ .../jackhuang/hmcl/util/SimpleMultimap.java | 2 +- .../org/jackhuang/hmcl/util/io/FileUtils.java | 7 +- .../jackhuang/hmcl/util/io/NetworkUtils.java | 36 +++- 9 files changed, 63 insertions(+), 186 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java index a60bd048d..483107ec5 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java @@ -44,7 +44,6 @@ import javafx.util.Callback; import javafx.util.Duration; import javafx.util.StringConverter; import org.jackhuang.hmcl.util.*; -import org.jackhuang.hmcl.util.function.ExceptionalSupplier; import org.jackhuang.hmcl.util.i18n.I18n; import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.javafx.SelectedItemProperties; @@ -52,6 +51,7 @@ import org.jackhuang.hmcl.util.platform.OperatingSystem; import java.io.File; import java.io.IOException; +import java.io.UncheckedIOException; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.net.URI; @@ -223,7 +223,11 @@ public final class FXUtils { FXMLLoader loader = new FXMLLoader(node.getClass().getResource(absolutePath), I18n.getResourceBundle()); loader.setRoot(node); loader.setController(node); - Lang.invoke((ExceptionalSupplier) loader::load); + try { + loader.load(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } } public static void installTooltip(Node node, String tooltip) { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDnD.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDnD.java index ec6de3446..24f0aa491 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDnD.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDnD.java @@ -17,16 +17,16 @@ */ package org.jackhuang.hmcl.auth.authlibinjector; +import static org.jackhuang.hmcl.util.io.NetworkUtils.decodeURL; + +import java.util.Optional; +import java.util.function.Consumer; + import javafx.event.EventHandler; import javafx.scene.input.DragEvent; import javafx.scene.input.Dragboard; import javafx.scene.input.TransferMode; -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.util.Optional; -import java.util.function.Consumer; - /** * @author yushijinhun * @see https://github.com/yushijinhun/authlib-injector/wiki/%E5%90%AF%E5%8A%A8%E5%99%A8%E6%8A%80%E6%9C%AF%E8%A7%84%E8%8C%83#dnd-%E6%96%B9%E5%BC%8F%E6%B7%BB%E5%8A%A0-yggdrasil-%E6%9C%8D%E5%8A%A1%E7%AB%AF @@ -44,11 +44,7 @@ public final class AuthlibInjectorDnD { String[] uriElements = uri.split(":"); if (uriElements.length == 3 && SCHEME.equals(uriElements[0]) && PATH_YGGDRASIL_SERVER.equals(uriElements[1])) { - try { - return Optional.of(URLDecoder.decode(uriElements[2], "UTF-8")); - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException(e); - } + return Optional.of(decodeURL(uriElements[2])); } return Optional.empty(); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilService.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilService.java index cdd1484f4..50c5a004c 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilService.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilService.java @@ -16,7 +16,6 @@ import java.net.URL; import java.util.*; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.jackhuang.hmcl.util.Lang.liftFunction; import static org.jackhuang.hmcl.util.Lang.mapOf; import static org.jackhuang.hmcl.util.Pair.pair; @@ -114,11 +113,15 @@ public class YggdrasilService { public Optional> getTextures(GameProfile profile) throws AuthenticationException { Objects.requireNonNull(profile); - return Optional.ofNullable(profile.getProperties()) - .flatMap(properties -> Optional.ofNullable(properties.get("textures"))) - .map(encodedTextures -> new String(Base64.getDecoder().decode(encodedTextures), UTF_8)) - .map(liftFunction(textures -> fromJson(textures, TextureResponse.class))) - .flatMap(response -> Optional.ofNullable(response.textures)); + Optional encodedTextures = Optional.ofNullable(profile.getProperties()) + .flatMap(properties -> Optional.ofNullable(properties.get("textures"))); + + if (encodedTextures.isPresent()) { + TextureResponse texturePayload = fromJson(new String(Base64.getDecoder().decode(encodedTextures.get()), UTF_8), TextureResponse.class); + return Optional.ofNullable(texturePayload.textures); + } else { + return Optional.empty(); + } } private static YggdrasilSession handleAuthenticationResponse(String responseText, String clientToken) throws AuthenticationException { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/LibraryDownloadTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/LibraryDownloadTask.java index 1f9faa43e..4724a7b63 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/LibraryDownloadTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/LibraryDownloadTask.java @@ -101,7 +101,7 @@ public class LibraryDownloadTask extends Task { try { URL packXz = NetworkUtils.toURL(url + ".pack.xz"); - if (NetworkUtils.URLExists(packXz)) { + if (NetworkUtils.urlExists(packXz)) { task = new FileDownloadTask(packXz, xzFile, null); xz = true; } else { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/TaskExecutor.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/TaskExecutor.java index 4bbb986bc..8c41dd7a2 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/TaskExecutor.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/TaskExecutor.java @@ -19,7 +19,6 @@ package org.jackhuang.hmcl.task; import org.jackhuang.hmcl.util.*; import org.jackhuang.hmcl.util.function.ExceptionalRunnable; -import org.jackhuang.hmcl.util.function.ExceptionalSupplier; import java.util.*; import java.util.concurrent.*; @@ -79,7 +78,12 @@ public final class TaskExecutor { taskListeners.forEach(it -> it.onStop(flag.get(), this)); }); workerQueue.add(future); - Lang.invoke((ExceptionalSupplier) future::get); + try { + future.get(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (ExecutionException | CancellationException e) { + } return flag.get(); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java index aab860d70..f9c437e28 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java @@ -20,11 +20,6 @@ package org.jackhuang.hmcl.util; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Supplier; - -import org.jackhuang.hmcl.util.function.ExceptionalConsumer; -import org.jackhuang.hmcl.util.function.ExceptionalFunction; import org.jackhuang.hmcl.util.function.ExceptionalRunnable; import org.jackhuang.hmcl.util.function.ExceptionalSupplier; @@ -57,104 +52,6 @@ public final class Lang { return Collections.unmodifiableList(Arrays.asList(elements)); } - public static V computeIfAbsent(Map map, K key, Supplier computingFunction) { - V value = map.get(key); - if (value == null) { - V newValue = computingFunction.get(); - map.put(key, newValue); - return newValue; - } - return value; - } - - @SuppressWarnings("unchecked") - private static void throwable(Throwable exception) throws E { - throw (E) exception; - } - - /** - * 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 type of argument. - * @param type of result. - * @param function your method. - * @return the result of the method to invoke. - */ - public static R invoke(ExceptionalFunction function, T t) { - try { - return function.apply(t); - } catch (Exception e) { - throwable(e); - throw new Error(); // won't get to here. - } - } - - public static Function hideFunction(ExceptionalFunction function) { - return r -> invoke(function, r); - } - - public static Function liftFunction(ExceptionalFunction function) throws E { - return hideFunction(function); - } - - /** - * 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 type of result. - * @param supplier your method. - * @return the result of the method to invoke. - */ - public static T invoke(ExceptionalSupplier supplier) { - try { - return supplier.get(); - } catch (Exception e) { - throwable(e); - throw new Error(); // won't get to here. - } - } - - public static Supplier hideException(ExceptionalSupplier supplier) { - return () -> invoke(supplier); - } - - public static Supplier liftException(ExceptionalSupplier supplier) throws E { - 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 type of result. - * @param consumer your method. - */ - public static void invokeConsumer(ExceptionalConsumer consumer, T t) { - try { - consumer.accept(t); - } catch (Exception e) { - throwable(e); - } - } - - public static Consumer hideConsumer(ExceptionalConsumer consumer) { - return it -> invokeConsumer(consumer, it); - } - - public static Consumer liftConsumer(ExceptionalConsumer consumer) throws E { - return hideConsumer(consumer); - } - public static boolean test(ExceptionalSupplier r) { try { return r.get(); @@ -163,40 +60,6 @@ public final class Lang { } } - /** - * 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 r your method. - */ - public static void invoke(ExceptionalRunnable r) { - try { - r.run(); - } catch (Exception e) { - throwable(e); - } - } - - public static Runnable hideException(ExceptionalRunnable r) { - return () -> invoke(r); - } - - public static Runnable liftException(ExceptionalRunnable r) { - return hideException(r); - } - - public static boolean test(ExceptionalRunnable r) { - try { - r.run(); - return true; - } catch (Exception e) { - return false; - } - } - public static T ignoringException(ExceptionalSupplier supplier) { return ignoringException(supplier, null); } @@ -309,24 +172,6 @@ public final class Lang { return thread; } - public static Iterator asIterator(Enumeration enumeration) { - return new Iterator() { - @Override - public boolean hasNext() { - return enumeration.hasMoreElements(); - } - - @Override - public T next() { - return enumeration.nextElement(); - } - }; - } - - public static Iterable asIterable(Enumeration enumeration) { - return () -> asIterator(enumeration); - } - public static int parseInt(Object string, int defaultValue) { try { return Integer.parseInt(string.toString()); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/SimpleMultimap.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/SimpleMultimap.java index 111f6c59e..08476d9f3 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/SimpleMultimap.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/SimpleMultimap.java @@ -62,7 +62,7 @@ public final class SimpleMultimap { } public Collection get(K key) { - return Lang.computeIfAbsent(map, key, valuer); + return map.computeIfAbsent(key, any -> valuer.get()); } public void put(K key, V value) { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java index 0936fa6d4..32b4283d4 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/FileUtils.java @@ -113,7 +113,12 @@ public final class FileUtils { } public static boolean deleteDirectoryQuietly(File directory) { - return Lang.test(() -> deleteDirectory(directory)); + try { + deleteDirectory(directory); + return true; + } catch (IOException e) { + return false; + } } public static void copyDirectory(Path src, Path dest) throws IOException { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java index 0f250a8dc..1140bb2d1 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java @@ -19,12 +19,9 @@ package org.jackhuang.hmcl.util.io; import java.io.*; import java.net.*; -import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Map.Entry; -import org.jackhuang.hmcl.util.Lang; - import static java.nio.charset.StandardCharsets.UTF_8; import static org.jackhuang.hmcl.util.StringUtils.*; @@ -125,13 +122,18 @@ public final class NetworkUtils { String disposition = conn.getHeaderField("Content-Disposition"); if (disposition == null || !disposition.contains("filename=")) { String u = conn.getURL().toString(); - return Lang.invoke(() -> URLDecoder.decode(substringAfterLast(u, '/'), StandardCharsets.UTF_8.name())); - } else - return Lang.invoke(() -> URLDecoder.decode(removeSurrounding(substringAfter(disposition, "filename="), "\""), StandardCharsets.UTF_8.name())); + return decodeURL(substringAfterLast(u, '/')); + } else { + return decodeURL(removeSurrounding(substringAfter(disposition, "filename="), "\"")); + } } public static URL toURL(String str) { - return Lang.invoke(() -> new URL(str)); + try { + return new URL(str); + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e); + } } public static boolean isURL(String str) { @@ -143,11 +145,29 @@ public final class NetworkUtils { } } - public static boolean URLExists(URL url) throws IOException { + public static boolean urlExists(URL url) throws IOException { try (InputStream stream = url.openStream()) { return true; } catch (FileNotFoundException e) { return false; } } + + // ==== Shortcut methods for encoding/decoding URLs in UTF-8 ==== + public static String encodeURL(String toEncode) { + try { + return URLEncoder.encode(toEncode, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new Error(); + } + } + + public static String decodeURL(String toDecode) { + try { + return URLDecoder.decode(toDecode, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new Error(); + } + } + // ==== }