Renamed NamespacedRegistry and added NamespacedFactoryRegistry
- NamespacedRegistry moved to NamespacedInstanceRegistry - Added NamespacedFactoryRegistry
This commit is contained in:
parent
5d7cfdb3bc
commit
56eaec522f
@ -1,8 +1,8 @@
|
||||
package ru.windcorp.progressia.client.comms.controls;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class ControlTriggerRegistry extends NamespacedRegistry<ControlTrigger> {
|
||||
public class ControlTriggerRegistry extends NamespacedInstanceRegistry<ControlTrigger> {
|
||||
|
||||
private static final ControlTriggerRegistry INSTANCE =
|
||||
new ControlTriggerRegistry();
|
||||
|
@ -22,9 +22,9 @@ import ru.windcorp.progressia.client.graphics.texture.Atlases.AtlasGroup;
|
||||
import ru.windcorp.progressia.client.graphics.texture.SimpleTexture;
|
||||
import ru.windcorp.progressia.client.graphics.texture.Texture;
|
||||
import ru.windcorp.progressia.common.resource.ResourceManager;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class BlockRenderRegistry extends NamespacedRegistry<BlockRender> {
|
||||
public class BlockRenderRegistry extends NamespacedInstanceRegistry<BlockRender> {
|
||||
|
||||
private static final BlockRenderRegistry INSTANCE =
|
||||
new BlockRenderRegistry();
|
||||
|
@ -6,9 +6,9 @@ import ru.windcorp.progressia.client.graphics.texture.TextureLoader;
|
||||
import ru.windcorp.progressia.client.graphics.texture.TexturePrimitive;
|
||||
import ru.windcorp.progressia.client.graphics.texture.TextureSettings;
|
||||
import ru.windcorp.progressia.common.resource.ResourceManager;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class EntityRenderRegistry extends NamespacedRegistry<EntityRender> {
|
||||
public class EntityRenderRegistry extends NamespacedInstanceRegistry<EntityRender> {
|
||||
|
||||
private static final EntityRenderRegistry INSTANCE =
|
||||
new EntityRenderRegistry();
|
||||
|
@ -22,9 +22,9 @@ import ru.windcorp.progressia.client.graphics.texture.Atlases.AtlasGroup;
|
||||
import ru.windcorp.progressia.client.graphics.texture.SimpleTexture;
|
||||
import ru.windcorp.progressia.client.graphics.texture.Texture;
|
||||
import ru.windcorp.progressia.common.resource.ResourceManager;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class TileRenderRegistry extends NamespacedRegistry<TileRender> {
|
||||
public class TileRenderRegistry extends NamespacedInstanceRegistry<TileRender> {
|
||||
|
||||
private static final TileRenderRegistry INSTANCE = new TileRenderRegistry();
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package ru.windcorp.progressia.common.comms.controls;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class ControlDataRegistry extends NamespacedRegistry<ControlData> {
|
||||
public class ControlDataRegistry extends NamespacedInstanceRegistry<ControlData> {
|
||||
|
||||
private static final ControlDataRegistry INSTANCE = new ControlDataRegistry();
|
||||
|
||||
|
@ -6,7 +6,7 @@ import java.util.WeakHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
/**
|
||||
* Registry-like object for identification of various {@link StatefulObject}
|
||||
@ -45,8 +45,8 @@ public class StatefulObjectRegistry<T extends StatefulObject> {
|
||||
|
||||
}
|
||||
|
||||
private final NamespacedRegistry<Type<T>> registry =
|
||||
new NamespacedRegistry<Type<T>>() {
|
||||
private final NamespacedInstanceRegistry<Type<T>> registry =
|
||||
new NamespacedInstanceRegistry<Type<T>>() {
|
||||
@Override
|
||||
public void register(Type<T> element) {
|
||||
super.register(element);
|
||||
|
@ -0,0 +1,114 @@
|
||||
package ru.windcorp.progressia.common.util.namespaces;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class NamespacedFactoryRegistry<E extends Namespaced>
|
||||
implements Map<String, NamespacedFactoryRegistry.Factory<E>> {
|
||||
|
||||
@FunctionalInterface
|
||||
public static interface Factory<E> {
|
||||
E build(String id);
|
||||
}
|
||||
|
||||
private final Map<String, Factory<E>> backingMap =
|
||||
Collections.synchronizedMap(new HashMap<>());
|
||||
|
||||
private final Logger logger = LogManager.getLogger(getClass());
|
||||
|
||||
public void register(String id, Factory<E> element) {
|
||||
if (get(id) != null) {
|
||||
throw new IllegalArgumentException("ID " + id + " is already registered in " + getClass().getSimpleName());
|
||||
}
|
||||
|
||||
logger.debug("Registering {} in {}", id, getClass().getSimpleName());
|
||||
backingMap.put(id, element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return backingMap.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return backingMap.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return backingMap.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean has(String id) {
|
||||
return backingMap.containsKey(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return backingMap.containsValue(value);
|
||||
}
|
||||
|
||||
public boolean isRegistered(E element) {
|
||||
return has(element.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Factory<E> get(Object key) {
|
||||
return backingMap.get(key);
|
||||
}
|
||||
|
||||
public E create(String id) {
|
||||
Factory<E> factory = get(id);
|
||||
E result = factory.build(id);
|
||||
if (!result.getId().equals(id)) {
|
||||
throw new IllegalStateException("Requested ID " + id + " but factory " + factory + " returned an object with ID " + result.getId());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Factory<E> put(String id, Factory<E> factory) {
|
||||
register(id, factory);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends String, ? extends Factory<E>> m) {
|
||||
synchronized (backingMap) {
|
||||
m.entrySet().forEach(e -> register(e.getKey(), e.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Factory<E> remove(Object key) {
|
||||
return backingMap.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
backingMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> keySet() {
|
||||
return backingMap.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Factory<E>> values() {
|
||||
return backingMap.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<String, Factory<E>>> entrySet() {
|
||||
return backingMap.entrySet();
|
||||
}
|
||||
|
||||
}
|
@ -7,17 +7,20 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.google.errorprone.annotations.DoNotCall;
|
||||
|
||||
public class NamespacedRegistry<E extends Namespaced>
|
||||
public class NamespacedInstanceRegistry<E extends Namespaced>
|
||||
implements Map<String, E> {
|
||||
|
||||
private final Map<String, E> backingMap =
|
||||
Collections.synchronizedMap(new HashMap<>());
|
||||
|
||||
private final Logger logger = LogManager.getLogger(getClass());
|
||||
|
||||
public void register(E element) {
|
||||
LogManager.getLogger(getClass()).debug("Registering " + element.getId());
|
||||
logger.debug("Registering {} in {}", element.getId(), getClass().getSimpleName());
|
||||
backingMap.put(element.getId(), element);
|
||||
}
|
||||
|
||||
@ -67,7 +70,7 @@ implements Map<String, E> {
|
||||
@DoNotCall @Deprecated
|
||||
public E put(String key, E value) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Use NamespacedRegistry.register(E)"
|
||||
"Use NamespacedInstanceRegistry.register(E)"
|
||||
);
|
||||
}
|
||||
|
||||
@ -83,7 +86,7 @@ implements Map<String, E> {
|
||||
@DoNotCall @Deprecated
|
||||
public void putAll(Map<? extends String, ? extends E> m) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Use NamespacedRegistry.registerAll(Collection<? extends E>)"
|
||||
"Use NamespacedInstanceRegistry.registerAll(Collection<? extends E>)"
|
||||
);
|
||||
}
|
||||
|
@ -17,9 +17,9 @@
|
||||
*******************************************************************************/
|
||||
package ru.windcorp.progressia.common.world.block;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class BlockDataRegistry extends NamespacedRegistry<BlockData> {
|
||||
public class BlockDataRegistry extends NamespacedInstanceRegistry<BlockData> {
|
||||
|
||||
private static final BlockDataRegistry INSTANCE = new BlockDataRegistry();
|
||||
|
||||
|
@ -17,9 +17,9 @@
|
||||
*******************************************************************************/
|
||||
package ru.windcorp.progressia.common.world.tile;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class TileDataRegistry extends NamespacedRegistry<TileData> {
|
||||
public class TileDataRegistry extends NamespacedInstanceRegistry<TileData> {
|
||||
|
||||
private static final TileDataRegistry INSTANCE = new TileDataRegistry();
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package ru.windcorp.progressia.server.comms.controls;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class ControlLogicRegistry extends NamespacedRegistry<ControlLogic> {
|
||||
public class ControlLogicRegistry extends NamespacedInstanceRegistry<ControlLogic> {
|
||||
|
||||
private static final ControlLogicRegistry INSTANCE =
|
||||
new ControlLogicRegistry();
|
||||
|
@ -1,8 +1,8 @@
|
||||
package ru.windcorp.progressia.server.world.block;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class BlockLogicRegistry extends NamespacedRegistry<BlockLogic> {
|
||||
public class BlockLogicRegistry extends NamespacedInstanceRegistry<BlockLogic> {
|
||||
|
||||
private static final BlockLogicRegistry INSTANCE = new BlockLogicRegistry();
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package ru.windcorp.progressia.server.world.entity;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class EntityLogicRegistry extends NamespacedRegistry<EntityLogic> {
|
||||
public class EntityLogicRegistry extends NamespacedInstanceRegistry<EntityLogic> {
|
||||
|
||||
private static final EntityLogicRegistry INSTANCE =
|
||||
new EntityLogicRegistry();
|
||||
|
@ -1,8 +1,8 @@
|
||||
package ru.windcorp.progressia.server.world.tile;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedRegistry;
|
||||
import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry;
|
||||
|
||||
public class TileLogicRegistry extends NamespacedRegistry<TileLogic> {
|
||||
public class TileLogicRegistry extends NamespacedInstanceRegistry<TileLogic> {
|
||||
|
||||
private static final TileLogicRegistry INSTANCE = new TileLogicRegistry();
|
||||
|
||||
|
Reference in New Issue
Block a user