net_io.msg.MsgClass Maven / Gradle / Ivy
The newest version!
package net_io.msg;
import java.util.HashMap;
import java.util.Map;
public class MsgClass {
private Map> msgClassMap = new HashMap>();
@SuppressWarnings({ "rawtypes", "unchecked" })
public void register(int msgID, Class msgClass) {
if(BaseMsg.class.isAssignableFrom(msgClass) == false) {
throw new RuntimeException("The msg class("+msgClass.getName()+") is not extends of BaseMsg.");
}
msgClassMap.put(msgID, msgClass);
}
public BaseMsg createMsg(int msgID) throws InstantiationException,IllegalAccessException {
Class cls = msgClassMap.get(msgID);
if(cls == null) {
return null;
}
BaseMsg msg = cls.newInstance();
msg.resetMsgID(msgID);
return msg;
}
}