mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE] Универсальный адаптер и ProviderMap
This commit is contained in:
parent
b55e6f1f40
commit
2ef2d95fb9
2 changed files with 75 additions and 0 deletions
34
libLauncher/src/main/java/ru/gravit/utils/ProviderMap.java
Normal file
34
libLauncher/src/main/java/ru/gravit/utils/ProviderMap.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package ru.gravit.utils;
|
||||
|
||||
import ru.gravit.utils.helper.VerifyHelper;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class ProviderMap<R> {
|
||||
protected final Map<String, Class<? extends R>> PROVIDERS = new ConcurrentHashMap<>(4);
|
||||
protected boolean registredProviders = false;
|
||||
|
||||
|
||||
public void registerProvider(String name, Class<? extends R> adapter) {
|
||||
VerifyHelper.verifyIDName(name);
|
||||
VerifyHelper.putIfAbsent(PROVIDERS, name, Objects.requireNonNull(adapter, "adapter"),
|
||||
String.format("Protect handler has been already registered: '%s'", name));
|
||||
}
|
||||
|
||||
public Class<? extends R> getProviderClass(String name) {
|
||||
return PROVIDERS.get(name);
|
||||
}
|
||||
|
||||
public String getProviderName(Class<? extends R> clazz) {
|
||||
for (Map.Entry<String, Class<? extends R>> e : PROVIDERS.entrySet()) {
|
||||
if (e.getValue().equals(clazz)) return e.getKey();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Class<? extends R> unregisterProvider(String name)
|
||||
{
|
||||
return PROVIDERS.remove(name);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package ru.gravit.utils;
|
||||
|
||||
import com.google.gson.*;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class UniversalJsonAdapter<R> implements JsonSerializer<R>, JsonDeserializer<R> {
|
||||
public ProviderMap<R> providerMap;
|
||||
public String PROP_NAME = "type";
|
||||
|
||||
public UniversalJsonAdapter(ProviderMap<R> providerMap) {
|
||||
this.providerMap = providerMap;
|
||||
}
|
||||
|
||||
public UniversalJsonAdapter(ProviderMap<R> providerMap, String PROP_NAME) {
|
||||
this.providerMap = providerMap;
|
||||
this.PROP_NAME = PROP_NAME;
|
||||
}
|
||||
|
||||
public R deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
|
||||
Class<? extends R> cls = providerMap.getProviderClass(typename);
|
||||
if (cls == null) {
|
||||
LogHelper.error("AuthHandler %s not found", typename);
|
||||
return null;
|
||||
}
|
||||
return context.deserialize(json, cls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(R src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject jo = context.serialize(src).getAsJsonObject();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
String classPath = providerMap.getProviderName((Class<? extends R>) src.getClass());
|
||||
jo.add(PROP_NAME, new JsonPrimitive(classPath));
|
||||
|
||||
return jo;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue