clean up mapping classes
This commit is contained in:
parent
7ec9c174a1
commit
de8d2477a3
4 changed files with 38 additions and 46 deletions
|
@ -131,7 +131,7 @@ import common.color.DyeColor;
|
|||
import common.item.CheatTab;
|
||||
import common.log.Log;
|
||||
import common.model.TextureAnimation;
|
||||
import common.util.ObjectIntIdentityMap;
|
||||
import common.util.IdMap;
|
||||
import common.util.Mapping;
|
||||
import common.util.Util;
|
||||
import common.world.State;
|
||||
|
@ -139,7 +139,7 @@ import common.world.State;
|
|||
public abstract class BlockRegistry {
|
||||
private static final String AIR_ID = "air";
|
||||
public static final Mapping<Block> REGISTRY = new Mapping(AIR_ID);
|
||||
public static final ObjectIntIdentityMap<State> STATEMAP = new ObjectIntIdentityMap();
|
||||
public static final IdMap<State> STATEMAP = new IdMap();
|
||||
|
||||
private static int nextBlockId = 1;
|
||||
|
||||
|
|
34
common/src/main/java/common/util/IdMap.java
Executable file
34
common/src/main/java/common/util/IdMap.java
Executable file
|
@ -0,0 +1,34 @@
|
|||
package common.util;
|
||||
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import common.collect.Iterators;
|
||||
import common.collect.Lists;
|
||||
|
||||
public class IdMap<T> implements Iterable<T> {
|
||||
private final IdentityHashMap<T, Integer> mapping = new IdentityHashMap(512);
|
||||
private final List<T> list = Lists.<T>newArrayList();
|
||||
|
||||
public void put(T key, int value) {
|
||||
this.mapping.put(key, value);
|
||||
while(this.list.size() <= value) {
|
||||
this.list.add(null);
|
||||
}
|
||||
this.list.set(value, key);
|
||||
}
|
||||
|
||||
public int get(T key) {
|
||||
Integer i = this.mapping.get(key);
|
||||
return i == null ? -1 : i.intValue();
|
||||
}
|
||||
|
||||
public final T getByValue(int value) {
|
||||
return value >= 0 && value < this.list.size() ? this.list.get(value) : null;
|
||||
}
|
||||
|
||||
public Iterator<T> iterator() {
|
||||
return Iterators.filter(this.list.iterator(), Predicates.notNull());
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ import common.collect.HashBiMap;
|
|||
|
||||
public class Mapping<V> implements Iterable<V> {
|
||||
private final Map<String, V> mapping = HashBiMap.<String, V>create();
|
||||
private final ObjectIntIdentityMap<V> idMap = new ObjectIntIdentityMap();
|
||||
private final IdMap<V> idMap = new IdMap<V>();
|
||||
private final Map<V, String> nameMap;
|
||||
private final String defaultKey;
|
||||
|
||||
|
@ -46,7 +46,7 @@ public class Mapping<V> implements Iterable<V> {
|
|||
|
||||
public void finish() {
|
||||
if(this.defaultKey != null && this.defaultValue == null)
|
||||
throw new NullPointerException("Standard-Wert zu Standard-Schlüssel ist null");
|
||||
throw new NullPointerException("Wert zu Standard-Schlüssel " + this.defaultKey + " ist nicht vorhanden");
|
||||
this.registered = true;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
package common.util;
|
||||
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import common.collect.Iterators;
|
||||
import common.collect.Lists;
|
||||
|
||||
public class ObjectIntIdentityMap<T> implements Iterable<T>
|
||||
{
|
||||
private final IdentityHashMap<T, Integer> identityMap = new IdentityHashMap(512);
|
||||
private final List<T> objectList = Lists.<T>newArrayList();
|
||||
|
||||
public void put(T key, int value)
|
||||
{
|
||||
this.identityMap.put(key, value);
|
||||
|
||||
while (this.objectList.size() <= value)
|
||||
{
|
||||
this.objectList.add(null);
|
||||
}
|
||||
|
||||
this.objectList.set(value, key);
|
||||
}
|
||||
|
||||
public int get(T key)
|
||||
{
|
||||
Integer integer = (Integer)this.identityMap.get(key);
|
||||
return integer == null ? -1 : integer.intValue();
|
||||
}
|
||||
|
||||
public final T getByValue(int value)
|
||||
{
|
||||
return (T)(value >= 0 && value < this.objectList.size() ? this.objectList.get(value) : null);
|
||||
}
|
||||
|
||||
public Iterator<T> iterator()
|
||||
{
|
||||
return Iterators.filter(this.objectList.iterator(), Predicates.notNull());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue