移除Lang.get(Map,Object,Class)

This commit is contained in:
yushijinhun
2018-05-27 12:28:23 +08:00
parent 23037da406
commit 2e46d76165
9 changed files with 41 additions and 68 deletions

View File

@@ -6,7 +6,6 @@ import org.jackhuang.hmcl.auth.CharacterSelector;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilService;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilSession;
import org.jackhuang.hmcl.util.ExceptionalSupplier;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.NetworkUtils;
import org.jackhuang.hmcl.util.UUIDTypeAdapter;
@@ -15,6 +14,8 @@ import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import static org.jackhuang.hmcl.util.Lang.tryCast;
public class AuthlibInjectorAccountFactory extends AccountFactory<AuthlibInjectorAccount> {
private final ExceptionalSupplier<String, ?> injectorJarPathSupplier;
@@ -43,13 +44,13 @@ public class AuthlibInjectorAccountFactory extends AccountFactory<AuthlibInjecto
Objects.requireNonNull(storage);
Objects.requireNonNull(proxy);
String username = Lang.get(storage, "username", String.class)
String username = tryCast(storage.get("username"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have username"));
String clientToken = Lang.get(storage, "clientToken", String.class)
String clientToken = tryCast(storage.get("clientToken"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have client token."));
String character = Lang.get(storage, "clientToken", String.class)
String character = tryCast(storage.get("clientToken"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have selected character name."));
String apiRoot = Lang.get(storage, "serverBaseURL", String.class)
String apiRoot = tryCast(storage.get("serverBaseURL"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have API root."));
return new AuthlibInjectorAccount(new YggdrasilService(new AuthlibInjectorProvider(apiRoot), proxy),

View File

@@ -20,14 +20,12 @@ package org.jackhuang.hmcl.auth.offline;
import org.jackhuang.hmcl.auth.AccountFactory;
import org.jackhuang.hmcl.auth.CharacterSelector;
import org.jackhuang.hmcl.util.DigestUtils;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.UUIDTypeAdapter;
import java.net.Proxy;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import static org.jackhuang.hmcl.util.Lang.tryCast;
/**
*
@@ -46,9 +44,9 @@ public class OfflineAccountFactory extends AccountFactory<OfflineAccount> {
@Override
public OfflineAccount fromStorage(Map<Object, Object> storage, Proxy proxy) {
String username = Lang.get(storage, "username", String.class)
String username = tryCast(storage.get("username"), String.class)
.orElseThrow(() -> new IllegalStateException("Offline account configuration malformed."));
String uuid = Lang.get(storage, "uuid", String.class)
String uuid = tryCast(storage.get("uuid"), String.class)
.orElse(getUUIDFromUserName(username));
// Check if the uuid is vaild

View File

@@ -20,7 +20,6 @@ package org.jackhuang.hmcl.auth.yggdrasil;
import org.jackhuang.hmcl.auth.AccountFactory;
import org.jackhuang.hmcl.auth.AuthenticationException;
import org.jackhuang.hmcl.auth.CharacterSelector;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.UUIDTypeAdapter;
import java.net.Proxy;
@@ -28,6 +27,8 @@ import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import static org.jackhuang.hmcl.util.Lang.tryCast;
/**
*
* @author huangyuhui
@@ -57,11 +58,11 @@ public class YggdrasilAccountFactory extends AccountFactory<YggdrasilAccount> {
Objects.requireNonNull(storage);
Objects.requireNonNull(proxy);
String username = Lang.get(storage, "username", String.class)
String username = tryCast(storage.get("username"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have username"));
String clientToken = Lang.get(storage, "clientToken", String.class)
String clientToken = tryCast(storage.get("clientToken"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have client token."));
String character = Lang.get(storage, "clientToken", String.class)
String character = tryCast(storage.get("clientToken"), String.class)
.orElseThrow(() -> new IllegalArgumentException("storage does not have selected character name."));
return new YggdrasilAccount(new YggdrasilService(provider, proxy), username, clientToken, character, YggdrasilSession.fromStorage(storage));

View File

@@ -1,12 +1,13 @@
package org.jackhuang.hmcl.auth.yggdrasil;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.UUIDTypeAdapter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static org.jackhuang.hmcl.util.Lang.tryCast;
public class YggdrasilSession {
private final String accessToken;
@@ -54,20 +55,20 @@ public class YggdrasilSession {
}
public static YggdrasilSession fromStorage(Map<?, ?> storage) {
Optional<String> profileId = Lang.get(storage, "uuid", String.class);
Optional<String> profileName = Lang.get(storage, "displayName", String.class);
Optional<String> profileId = tryCast(storage.get("uuid"), String.class);
Optional<String> profileName = tryCast(storage.get("displayName"), String.class);
GameProfile profile = null;
if (profileId.isPresent() && profileName.isPresent()) {
profile = new GameProfile(UUIDTypeAdapter.fromString(profileId.get()), profileName.get(),
Lang.get(storage, "profileProperties", Map.class).map(PropertyMap::fromMap).orElseGet(PropertyMap::new));
tryCast(storage.get("profileProperties"), Map.class).map(PropertyMap::fromMap).orElseGet(PropertyMap::new));
}
return new YggdrasilSession(
Lang.get(storage, "accessToken", String.class).orElse(null),
tryCast(storage.get("accessToken"), String.class).orElse(null),
profile,
null,
Lang.get(storage, "userid", String.class)
.map(userId -> new User(userId, Lang.get(storage, "userProperties", Map.class).map(PropertyMap::fromMap).orElse(null)))
tryCast(storage.get("userid"), String.class)
.map(userId -> new User(userId, tryCast(storage.get("userProperties"), Map.class).map(PropertyMap::fromMap).orElse(null)))
.orElse(null)
);
}

View File

@@ -218,12 +218,12 @@ public final class Lang {
* @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))
public static <V> Optional<V> tryCast(Object obj, Class<V> clazz) {
if (clazz.isInstance(obj)) {
return Optional.of(clazz.cast(obj));
} else {
return Optional.empty();
else
return Optional.of((V) obj);
}
}
/**
@@ -240,20 +240,6 @@ public final class Lang {
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) {
return cast(map.get(key), clazz);
}
/**
* Join two collections into one list.
*

View File

@@ -17,33 +17,12 @@
*/
package org.jackhuang.hmcl.util;
import java.util.Map;
/**
*
* @author huangyuhui
*/
public final class ReflectionHelper {
private static final Map<String, Class<?>> PRIMITIVES;
static {
PRIMITIVES = Lang.mapOf(
new Pair<>("byte", Byte.class),
new Pair<>("short", Short.class),
new Pair<>("int", Integer.class),
new Pair<>("long", Long.class),
new Pair<>("char", Character.class),
new Pair<>("float", Float.class),
new Pair<>("double", Double.class),
new Pair<>("boolean", Boolean.class)
);
}
public static boolean isInstance(Class<?> superClass, Object obj) {
return superClass.isInstance(obj) || PRIMITIVES.get(superClass.getName()) == obj.getClass();
}
public static StackTraceElement getCaller() {
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
StackTraceElement caller = elements[2];