Minor refactoring.

Replaced use of reflection with use of functional factory interface (API change).
This commit is contained in:
Sebastian Hartte
2020-06-30 18:57:26 +02:00
parent d17f8cec92
commit 8d8d3dd2d7
6 changed files with 89 additions and 44 deletions
+8 -8
View File
@@ -130,14 +130,14 @@ final class Registration {
final IRegistryContainer registries = api.registries();
final IGridCacheRegistry gcr = registries.gridCache();
gcr.registerGridCache(ITickManager.class, TickManagerCache.class);
gcr.registerGridCache(IEnergyGrid.class, EnergyGridCache.class);
gcr.registerGridCache(IPathingGrid.class, PathGridCache.class);
gcr.registerGridCache(IStorageGrid.class, GridStorageCache.class);
gcr.registerGridCache(P2PCache.class, P2PCache.class);
gcr.registerGridCache(ISpatialCache.class, SpatialPylonCache.class);
gcr.registerGridCache(ISecurityGrid.class, SecurityCache.class);
gcr.registerGridCache(ICraftingGrid.class, CraftingGridCache.class);
gcr.registerGridCache(ITickManager.class, TickManagerCache::new);
gcr.registerGridCache(IEnergyGrid.class, EnergyGridCache::new);
gcr.registerGridCache(IPathingGrid.class, PathGridCache::new);
gcr.registerGridCache(IStorageGrid.class, GridStorageCache::new);
gcr.registerGridCache(P2PCache.class, P2PCache::new);
gcr.registerGridCache(ISpatialCache.class, SpatialPylonCache::new);
gcr.registerGridCache(ISecurityGrid.class, SecurityCache::new);
gcr.registerGridCache(ICraftingGrid.class, CraftingGridCache::new);
registries.cell().addCellHandler(new BasicCellHandler());
registries.cell().addCellHandler(new CreativeCellHandler());
@@ -18,54 +18,57 @@
package appeng.core.features.registries;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridCache;
import appeng.api.networking.IGridCacheFactory;
import appeng.api.networking.IGridCacheRegistry;
import appeng.core.AELog;
public final class GridCacheRegistry implements IGridCacheRegistry {
private final Map<Class<? extends IGridCache>, Class<? extends IGridCache>> caches = new HashMap<>();
private final List<GridCacheRegistration<?>> registry = new ArrayList<>();
@Override
public void registerGridCache(final Class<? extends IGridCache> iface,
final Class<? extends IGridCache> implementation) {
if (iface.isAssignableFrom(implementation)) {
this.caches.put(iface, implementation);
} else {
throw new IllegalArgumentException(
"Invalid setup, grid cache must either be the same class, or an interface that the implementation implements. Gotten: "
+ iface + " and " + implementation);
public synchronized <T extends IGridCache> void registerGridCache(@Nonnull Class<T> iface,
@Nonnull IGridCacheFactory<T> factory) {
if (registry.stream().anyMatch(r -> r.cacheClass.equals(iface))) {
AELog.debug("Overriding grid cache factory for cache type %s", iface);
}
registry.add(new GridCacheRegistration<>(iface, factory));
}
@Override
public HashMap<Class<? extends IGridCache>, IGridCache> createCacheInstance(final IGrid g) {
final HashMap<Class<? extends IGridCache>, IGridCache> map = new HashMap<>();
public Map<Class<? extends IGridCache>, IGridCache> createCacheInstance(final IGrid g) {
final Map<Class<? extends IGridCache>, IGridCache> map = new HashMap<>(registry.size());
for (final Class<? extends IGridCache> iface : this.caches.keySet()) {
try {
final Constructor<? extends IGridCache> c = this.caches.get(iface).getConstructor(IGrid.class);
map.put(iface, c.newInstance(g));
} catch (final NoSuchMethodException e) {
AELog.error("Grid Caches must have a constructor with IGrid as the single param.");
throw new IllegalArgumentException(e);
} catch (final InvocationTargetException e) {
AELog.error("Grid Caches must have a constructor with IGrid as the single param.");
throw new IllegalStateException(e);
} catch (final InstantiationException e) {
AELog.error("Grid Caches must have a constructor with IGrid as the single param.");
throw new IllegalStateException(e);
} catch (final IllegalAccessException e) {
AELog.error("Grid Caches must have a constructor with IGrid as the single param.");
throw new IllegalStateException(e);
}
for (GridCacheRegistration<?> registration : registry) {
map.put(registration.cacheClass, registration.factory.createCache(g));
}
return map;
}
private static class GridCacheRegistration<T extends IGridCache> {
private final Class<T> cacheClass;
private final IGridCacheFactory<T> factory;
public GridCacheRegistration(Class<T> cacheClass, IGridCacheFactory<T> factory) {
this.cacheClass = cacheClass;
this.factory = factory;
}
}
}
+2 -1
View File
@@ -42,7 +42,7 @@ import appeng.util.ReadOnlyCollection;
public class Grid implements IGrid {
private final NetworkEventBus eventBus = new NetworkEventBus();
private final Map<Class<? extends IGridHost>, MachineSet> machines = new HashMap<>();
private final Map<Class<? extends IGridCache>, GridCacheWrapper> caches = new HashMap<>();
private final Map<Class<? extends IGridCache>, GridCacheWrapper> caches;
private GridNode pivot;
private int priority; // how import is this network?
private GridStorage myStorage;
@@ -52,6 +52,7 @@ public class Grid implements IGrid {
final Map<Class<? extends IGridCache>, IGridCache> myCaches = AEApi.instance().registries().gridCache()
.createCacheInstance(this);
this.caches = new HashMap<>(myCaches.size());
for (final Entry<Class<? extends IGridCache>, IGridCache> c : myCaches.entrySet()) {
final Class<? extends IGridCache> key = c.getKey();
final IGridCache value = c.getValue();
-2
View File
@@ -21,7 +21,6 @@ package appeng.me.cache;
import java.util.HashMap;
import java.util.PriorityQueue;
import appeng.helpers.Reflected;
import com.google.common.base.Preconditions;
import net.minecraft.crash.CrashReport;
@@ -47,7 +46,6 @@ public class TickManagerCache implements ITickManager {
private long currentTick = 0;
@Reflected
public TickManagerCache(@SuppressWarnings("unused") final IGrid g) {
}