Created
August 7, 2012 13:26
-
-
Save Vazkii/3285337 to your computer and use it in GitHub Desktop.
String Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package UnlimitedBits.TDW.core; | |
import java.util.HashMap; | |
public class StringConversionHelper { | |
public static HashMap<Class, StringConverter> converters = new HashMap(); | |
public static void registerConverter(Class clazz, StringConverter converter){ | |
converters.put(clazz, converter); | |
} | |
public static String get(Object o){ | |
Class clazz = o.getClass(); | |
while(clazz != Object.class){ | |
if(converters.containsKey(clazz)) | |
return converters.get(clazz).convert(o); | |
clazz = o.getClass().getSuperclass(); | |
} | |
return o.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package UnlimitedBits.TDW.core; | |
public interface StringConverter<T> { | |
public String convert(T object); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package UnlimitedBits.TDW.core; | |
import net.minecraft.src.ItemStack; | |
public class StringConverter_ItemStack implements StringConverter<ItemStack> { | |
@Override | |
public String convert(ItemStack object){ | |
return object.itemID + ":" + object.getItemDamage(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package UnlimitedBits.TDW.core; | |
import java.util.HashMap; | |
/** | |
* A HashMap that has {@link StringConversionHelper}.get(T) keys rather than K keys. | |
*/ | |
public class StringMap<K, V> extends HashMap<K, V> { | |
@Override | |
public V get(Object key) { | |
return super.get(StringConversionHelper.get(key)); | |
} | |
@Override | |
public V put(K key, V value) { | |
return super.put((K)StringConversionHelper.get(key), value); | |
} | |
@Override | |
public boolean containsKey(Object key) { | |
return super.containsKey(StringConversionHelper.get(key)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment