[FEATURE] Именнованный провайдеры и HookSet

This commit is contained in:
Gravit 2019-04-24 13:08:22 +07:00
parent dbdf3f3c89
commit f32844aa15
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
9 changed files with 52 additions and 8 deletions

View file

@ -8,7 +8,7 @@
import java.util.UUID; import java.util.UUID;
public abstract class AuthHandler implements AutoCloseable { public abstract class AuthHandler implements AutoCloseable {
public static ProviderMap<AuthHandler> providers = new ProviderMap<>(); public static ProviderMap<AuthHandler> providers = new ProviderMap<>("AuthHandler");
private static boolean registredHandl = false; private static boolean registredHandl = false;

View file

@ -6,7 +6,7 @@
import java.util.List; import java.util.List;
public abstract class HWIDHandler implements AutoCloseable { public abstract class HWIDHandler implements AutoCloseable {
public static ProviderMap<HWIDHandler> providers = new ProviderMap<>(); public static ProviderMap<HWIDHandler> providers = new ProviderMap<>("HWIDHandler");
private static boolean registredHandl = false; private static boolean registredHandl = false;

View file

@ -4,7 +4,7 @@
import ru.gravit.utils.ProviderMap; import ru.gravit.utils.ProviderMap;
public abstract class PermissionsHandler implements AutoCloseable { public abstract class PermissionsHandler implements AutoCloseable {
public static ProviderMap<PermissionsHandler> providers = new ProviderMap<>(); public static ProviderMap<PermissionsHandler> providers = new ProviderMap<>("PermissionsHandler");
private static boolean registredHandl = false; private static boolean registredHandl = false;
public static void registerHandlers() { public static void registerHandlers() {

View file

@ -4,7 +4,7 @@
import ru.gravit.utils.ProviderMap; import ru.gravit.utils.ProviderMap;
public abstract class ProtectHandler { public abstract class ProtectHandler {
public static ProviderMap<ProtectHandler> providers = new ProviderMap<>(); public static ProviderMap<ProtectHandler> providers = new ProviderMap<>("ProtectHandler");
private static boolean registredHandl = false; private static boolean registredHandl = false;

View file

@ -6,7 +6,7 @@
import java.io.IOException; import java.io.IOException;
public abstract class AuthProvider implements AutoCloseable { public abstract class AuthProvider implements AutoCloseable {
public static ProviderMap<AuthProvider> providers = new ProviderMap<>(); public static ProviderMap<AuthProvider> providers = new ProviderMap<>("AuthProvider");
private static boolean registredProv = false; private static boolean registredProv = false;
public static AuthProviderResult authError(String message) throws AuthException { public static AuthProviderResult authError(String message) throws AuthException {

View file

@ -7,7 +7,7 @@
import java.util.UUID; import java.util.UUID;
public abstract class TextureProvider implements AutoCloseable { public abstract class TextureProvider implements AutoCloseable {
public static ProviderMap<TextureProvider> providers = new ProviderMap<>(); public static ProviderMap<TextureProvider> providers = new ProviderMap<>("TextureProvider");
private static boolean registredProv = false; private static boolean registredProv = false;
public static void registerProviders() { public static void registerProviders() {

View file

@ -0,0 +1,29 @@
package ru.gravit.utils;
import java.util.HashSet;
import java.util.Set;
public class HookSet<R> {
public Set<Hook<R>> list = new HashSet<>();
@FunctionalInterface
public interface Hook<R>
{
boolean hook(R context);
}
public void registerHook(Hook<R> hook)
{
list.add(hook);
}
public boolean unregisterHook(Hook<R> hook)
{
return list.remove(hook);
}
public boolean hook(R context)
{
for(Hook<R> hook : list)
{
if(hook.hook(context)) return true;
}
return false;
}
}

View file

@ -8,13 +8,25 @@
public class ProviderMap<R> { public class ProviderMap<R> {
protected final Map<String, Class<? extends R>> PROVIDERS = new ConcurrentHashMap<>(4); protected final Map<String, Class<? extends R>> PROVIDERS = new ConcurrentHashMap<>(4);
protected final String name;
protected boolean registredProviders = false; protected boolean registredProviders = false;
public ProviderMap(String name) {
this.name = name;
}
public ProviderMap() {
this.name = "Unnamed";
}
public String getName()
{
return name;
}
public void registerProvider(String name, Class<? extends R> adapter) { public void registerProvider(String name, Class<? extends R> adapter) {
VerifyHelper.verifyIDName(name); VerifyHelper.verifyIDName(name);
VerifyHelper.putIfAbsent(PROVIDERS, name, Objects.requireNonNull(adapter, "adapter"), VerifyHelper.putIfAbsent(PROVIDERS, name, Objects.requireNonNull(adapter, "adapter"),
String.format("Protect handler has been already registered: '%s'", name)); String.format("%s has been already registered: '%s'", this.name, name));
} }
public Class<? extends R> getProviderClass(String name) { public Class<? extends R> getProviderClass(String name) {

View file

@ -7,15 +7,18 @@
public class UniversalJsonAdapter<R> implements JsonSerializer<R>, JsonDeserializer<R> { public class UniversalJsonAdapter<R> implements JsonSerializer<R>, JsonDeserializer<R> {
public final ProviderMap<R> providerMap; public final ProviderMap<R> providerMap;
public final String name;
public final String PROP_NAME; public final String PROP_NAME;
public UniversalJsonAdapter(ProviderMap<R> providerMap) { public UniversalJsonAdapter(ProviderMap<R> providerMap) {
this.providerMap = providerMap; this.providerMap = providerMap;
this.name = providerMap.getName();
this.PROP_NAME = "type"; this.PROP_NAME = "type";
} }
public UniversalJsonAdapter(ProviderMap<R> providerMap, String PROP_NAME) { public UniversalJsonAdapter(ProviderMap<R> providerMap, String PROP_NAME) {
this.providerMap = providerMap; this.providerMap = providerMap;
this.name = providerMap.getName();
this.PROP_NAME = PROP_NAME; this.PROP_NAME = PROP_NAME;
} }
@ -23,7 +26,7 @@ public R deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<? extends R> cls = providerMap.getProviderClass(typename); Class<? extends R> cls = providerMap.getProviderClass(typename);
if (cls == null) { if (cls == null) {
LogHelper.error("Provider %s not found", typename); LogHelper.error("%s %s not found", name, typename);
return null; return null;
} }
return context.deserialize(json, cls); return context.deserialize(json, cls);