Skip to content
Snippets Groups Projects
Commit c0efcb0a authored by Brutzman, Don's avatar Brutzman, Don
Browse files

accept Terry's changes

parent 32fa7d1c
No related branches found
No related tags found
No related merge requests found
package edu.nps.moves.dis7.entities;
import edu.nps.moves.dis7.pdus.EntityType;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class EntityTypeFactory
{
private static Map<Integer,String> uid2ClassNameMap = new HashMap<>();
static {
try {
InputStream ins = EntityTypeFactory.class.getResourceAsStream("uid2EntityClass.properties");
Properties prop = new Properties();
prop.load(ins);
prop.entrySet().stream().forEach(e->uid2ClassNameMap.put(Integer.parseInt(e.getKey().toString()), e.getValue().toString()));
}
catch( IOException ex) {
throw new RuntimeException (ex);
}
}
public static EntityType makeEntity(int uid)
{
String name = uid2ClassNameMap.get(uid);
if(name==null)
return null;
try {
Class cls = Class.forName(name);
Constructor<EntityType> constr = (Constructor<EntityType>) cls.getConstructors()[0];
return constr.newInstance();
}
catch(ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException ex) {
System.err.println(name+"(uid "+uid+") could not be instantiated. Check for proper entity jar(s) on classpath.");
return null;
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment