package org.warp.jcwdb; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectMap; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; public class TypesManager { private final Int2ObjectMap> types; private final Object2ObjectMap, DBTypeParser> typesByClass; private DBTypedObjectParser fallbackParser; public TypesManager(JCWDatabase db) { types = new Int2ObjectOpenHashMap<>(); typesByClass = new Object2ObjectOpenHashMap<>(); DBStandardTypes.registerStandardTypes(db, this); } public void registerType(Class clazz, int type, DBTypeParser parser) { this.types.put(type, parser); this.typesByClass.put(clazz, parser); } /** * Use this method with the most used classes to save disk space. * @param clazz * @param id * @param */ public void registerGenericClass(Class clazz, int id) { this.fallbackParser.registerClass(clazz, id); } public void registerTypeFallback(DBTypedObjectParser parser) { this.fallbackParser = parser; } public DBTypeParser get(int type) { if (types.containsKey(type) == false) { if (fallbackParser == null) { throw new NoParserFoundException("The type " + type + " can't be parsed."); } else { return fallbackParser.cast(); } } return types.get(type).cast(); } public DBTypeParser get(Class type) { if (typesByClass.containsKey(type) == false) { if (fallbackParser == null) { throw new NoParserFoundException("The class " + type.getSimpleName() + " can't be parsed."); } else { return fallbackParser.cast(); } } return typesByClass.get(type).cast(); } }