package com.xinda.protocol; import com.xinda.protocol.base.annotation.Protocol; import com.xinda.protocol.base.model.ActiveModel; import com.xinda.protocol.util.ArrayMap; import com.xinda.protocol.util.ClassUtils; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 消息架构管理类 * * @author bill */ public class WModelManager { private final Map> typeIdMapping; private final Map> typeClassMapping; public WModelManager() { this(128); } public WModelManager(int initialCapacity) { this.typeIdMapping = new HashMap<>(initialCapacity); this.typeClassMapping = new HashMap<>(initialCapacity); } public WModelManager(String... basePackages) { this(256, basePackages); } public WModelManager(int initialCapacity, String... basePackages) { this(initialCapacity); for (String basePackage : basePackages) { List types = ClassUtils.getClassList(basePackage); for (Class type : types) { Protocol protocol = type.getAnnotation(Protocol.class); if (protocol != null) { int[] values = protocol.value(); for (Integer typeId : values) { loadRuntimeSchema(typeId, type); } } } } } public void loadRuntimeSchema(Integer typeId, Class typeClass) { ArrayMap schemaMap = ProtocolLoadUtils.getActiveMap(typeClassMapping, typeClass); if (schemaMap != null) { typeIdMapping.put(typeId, schemaMap); } } public ActiveModel getActiveMap(Class typeClass, int version) { ArrayMap schemaMap = ProtocolLoadUtils.getActiveMap(typeClassMapping, typeClass); if (schemaMap == null) { return null; } return schemaMap.getOrDefault(version); } public ArrayMap getActiveMap(Class typeClass) { return ProtocolLoadUtils.getActiveMap(typeClassMapping, typeClass); } public ActiveModel getActiveMap(Integer typeId, int version) { ArrayMap schemaMap = typeIdMapping.get(typeId); if (schemaMap == null) { return null; } return schemaMap.getOrDefault(version); } public ArrayMap getActiveMap(Integer typeId) { return typeIdMapping.get(typeId); } }