I want to make a general function which will fill a HashMap<String,TYPE extends GameObject>
based on contend of a text file. It should work for any TYPE
which implements methods getName()
and fromString()
. Currently, I found obstacle, that I cannot call constructor of TYPE
.
The code:
public static <TYPE extends GameObject> void loadHashMap( Map<String,TYPE> mp, String filename ){
BufferedReader reader = FileSystem.getReader( filename );
String line;
try{
while( null != ( line = reader.readLine() ) ){
TYPE item = new TYPE(); item.fromString( line ); // alternative 1
// TYPE item = new TYPE( line ); // alternative 2
mp.put( item.getName(), item );
};
} catch (Exception e) { e.printStackTrace(); };
}
where GameObject
is defined as:
public class GameObject {
String name;
public String getName(){ return name; };
public void fromString( String s ){ name = s; }
public GameObject ( ){ }
public GameObject ( String s ){ name=name; }
}