clean up mapping classes
This commit is contained in:
parent
7c495f3d1c
commit
0ca276b561
7 changed files with 58 additions and 103 deletions
|
@ -132,13 +132,13 @@ import common.item.CheatTab;
|
|||
import common.log.Log;
|
||||
import common.model.TextureAnimation;
|
||||
import common.util.ObjectIntIdentityMap;
|
||||
import common.util.DefaultedMapping;
|
||||
import common.util.Mapping;
|
||||
import common.util.Util;
|
||||
import common.world.State;
|
||||
|
||||
public abstract class BlockRegistry {
|
||||
private static final String AIR_ID = "air";
|
||||
public static final DefaultedMapping<String, Block> REGISTRY = new DefaultedMapping(AIR_ID);
|
||||
public static final Mapping<String, Block> REGISTRY = new Mapping(AIR_ID);
|
||||
public static final ObjectIntIdentityMap<State> STATEMAP = new ObjectIntIdentityMap();
|
||||
|
||||
private static int nextBlockId = 1;
|
||||
|
@ -204,7 +204,7 @@ public abstract class BlockRegistry {
|
|||
static void register() {
|
||||
REGISTRY.register(0, BlockRegistry.AIR_ID, (new BlockAir()).setDisplay("Luft"));
|
||||
registerBlocks();
|
||||
REGISTRY.validateKey();
|
||||
REGISTRY.finish();
|
||||
|
||||
for(Block block : REGISTRY) {
|
||||
if(block != Blocks.air
|
||||
|
|
|
@ -551,6 +551,8 @@ public abstract class ItemRegistry {
|
|||
// Log.info("Block " + BlockRegistry.getNameFromBlock(block) + " hat kein Item");
|
||||
}
|
||||
|
||||
REGISTRY.finish();
|
||||
|
||||
Log.SYSTEM.debug("%d Gegenstände registriert", nextItemId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
package common.util;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import common.collect.Maps;
|
||||
|
||||
public class BaseMapping<K, V> implements Iterable<V> {
|
||||
protected final Map<K, V> mapping = this.createUnderlyingMap();
|
||||
|
||||
protected Map<K, V> createUnderlyingMap() {
|
||||
return Maps.<K, V>newHashMap();
|
||||
}
|
||||
|
||||
public V getObject(K name) {
|
||||
return this.mapping.get(name);
|
||||
}
|
||||
|
||||
public void putObject(K key, V value) {
|
||||
if(key == null)
|
||||
throw new NullPointerException("Schlüssel ist null");
|
||||
if(value == null)
|
||||
throw new NullPointerException("Wert ist null");
|
||||
this.mapping.put(key, value);
|
||||
}
|
||||
|
||||
public Set<K> getKeys() {
|
||||
return Collections.<K>unmodifiableSet(this.mapping.keySet());
|
||||
}
|
||||
|
||||
public boolean containsKey(K key) {
|
||||
return this.mapping.containsKey(key);
|
||||
}
|
||||
|
||||
public Iterator<V> iterator() {
|
||||
return this.mapping.values().iterator();
|
||||
}
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package common.util;
|
||||
|
||||
public class DefaultedMapping<K, V> extends Mapping<K, V> {
|
||||
private final K defaultKey;
|
||||
|
||||
private V defaultValue;
|
||||
|
||||
public DefaultedMapping(K def) {
|
||||
this.defaultKey = def;
|
||||
}
|
||||
|
||||
public void register(int id, K key, V value) {
|
||||
if(this.defaultKey.equals(key))
|
||||
this.defaultValue = value;
|
||||
super.register(id, key, value);
|
||||
}
|
||||
|
||||
public void validateKey() {
|
||||
if(this.defaultValue == null)
|
||||
throw new NullPointerException("Standard-Wert ist null");
|
||||
}
|
||||
|
||||
public V getObject(K name) {
|
||||
V v = super.getObject(name);
|
||||
return v == null ? this.defaultValue : v;
|
||||
}
|
||||
|
||||
public V getObjectExact(K name) {
|
||||
return super.getObject(name);
|
||||
}
|
||||
|
||||
public V getObjectById(int id) {
|
||||
V v = super.getObjectById(id);
|
||||
return v == null ? this.defaultValue : v;
|
||||
}
|
||||
|
||||
public V getObjectExact(int id) {
|
||||
return super.getObjectById(id);
|
||||
}
|
||||
}
|
|
@ -1,36 +1,66 @@
|
|||
package common.util;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import common.collect.BiMap;
|
||||
import common.collect.HashBiMap;
|
||||
|
||||
public class Mapping<K, V> extends BaseMapping<K, V> implements IObjectIntIterable<V> {
|
||||
protected final ObjectIntIdentityMap<V> idmap = new ObjectIntIdentityMap();
|
||||
protected final Map<V, K> namemap;
|
||||
public class Mapping<K, V> implements Iterable<V>, IObjectIntIterable<V> {
|
||||
private final Map<K, V> mapping = HashBiMap.<K, V>create();
|
||||
private final ObjectIntIdentityMap<V> idmap = new ObjectIntIdentityMap();
|
||||
private final Map<V, K> namemap;
|
||||
private final K defaultKey;
|
||||
|
||||
private V defaultValue;
|
||||
private boolean registered;
|
||||
|
||||
public Mapping(K def) {
|
||||
this.namemap = ((BiMap)this.mapping).inverse();
|
||||
this.defaultKey = def;
|
||||
}
|
||||
|
||||
public Mapping() {
|
||||
this.namemap = ((BiMap)this.mapping).inverse();
|
||||
this(null);
|
||||
}
|
||||
|
||||
public void register(int id, K key, V value) {
|
||||
if(this.registered)
|
||||
throw new IllegalStateException("Es können keine neuen Werte registriert werden");
|
||||
if(key == null)
|
||||
throw new NullPointerException("Schlüssel ist null");
|
||||
if(value == null)
|
||||
throw new NullPointerException("Wert ist null");
|
||||
if(this.mapping.containsKey(key))
|
||||
throw new IllegalArgumentException(
|
||||
"Schlüssel " + String.valueOf(key) + " ist bereits mit ID " + this.idmap.get(this.mapping.get(key)) + " registriert");
|
||||
if(this.idmap.getByValue(id) != null)
|
||||
throw new IllegalArgumentException("ID " + id + " ist bereits mit Name " + this.namemap.get(this.idmap.getByValue(id)) + " registriert");
|
||||
|
||||
this.idmap.put(value, id);
|
||||
this.putObject(key, value);
|
||||
this.mapping.put(key, value);
|
||||
if(key.equals(this.defaultKey))
|
||||
this.defaultValue = value;
|
||||
}
|
||||
|
||||
protected Map<K, V> createUnderlyingMap() {
|
||||
return HashBiMap.<K, V>create();
|
||||
public void finish() {
|
||||
if(this.defaultKey != null && this.defaultValue == null)
|
||||
throw new NullPointerException("Standard-Wert zu Standard-Schlüssel ist null");
|
||||
this.registered = true;
|
||||
}
|
||||
|
||||
public Set<K> getKeys() {
|
||||
return Collections.<K>unmodifiableSet(this.mapping.keySet());
|
||||
}
|
||||
|
||||
public V getObject(K name) {
|
||||
return super.getObject(name);
|
||||
V v = this.mapping.get(name);
|
||||
return v == null ? this.defaultValue : v;
|
||||
}
|
||||
|
||||
public V getObjectExact(K name) {
|
||||
return this.mapping.get(name);
|
||||
}
|
||||
|
||||
public K getNameForObject(V value) {
|
||||
|
@ -38,7 +68,7 @@ public class Mapping<K, V> extends BaseMapping<K, V> implements IObjectIntIterab
|
|||
}
|
||||
|
||||
public boolean containsKey(K key) {
|
||||
return super.containsKey(key);
|
||||
return this.mapping.containsKey(key);
|
||||
}
|
||||
|
||||
public int getIDForObject(V value) {
|
||||
|
@ -46,6 +76,11 @@ public class Mapping<K, V> extends BaseMapping<K, V> implements IObjectIntIterab
|
|||
}
|
||||
|
||||
public V getObjectById(int id) {
|
||||
V v = this.idmap.getByValue(id);
|
||||
return v == null ? this.defaultValue : v;
|
||||
}
|
||||
|
||||
public V getObjectExact(int id) {
|
||||
return this.idmap.getByValue(id);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue