I'm currently developing a lightweight, general-purpose simulation framework. The goal is to allow people to subclass the Simulation and Scenario objects for their domain-specific needs. Generics seemed like an appropriate way to achieve this, but I'm afraid I might be dipping into generics hell.
The Sim
object provides access to the simulation entities and controls the sim (start/pause/stop)
The Scenario
object allows you to populate the Sim with simulation entities.
Sim:
public class Sim
{
public <T extends Sim> void loadScenario(Scenario<T> scenario)
{
reset();
scenario.load(this);
}
}
Scenario:
public interface Scenario<T extends Sim>
{
public void load(T sim);
}
The goal is to allow users to create a MySim
that extends Sim
and a MyScenario
that implements Scenario<MySim>
for their domain.
e.g. MyScenario:
public class MyScenario<MySim>
{
public void load(MySim sim)
{
// make calls to sim.addMySimEntity(...)
}
}
Specifically, using the code above, the scenario.load(this)
call in Sim.loadScenario
gives me the error: The method load(T) in the type Scenario is not applicable for the arguments (Sim). I understand this is because I'm loading this
(which is of type Sim
) when what is required is T extends Sim
which means somehow I should be passing in an object that can be any subtype of Sim.
What is the way to rectify this issue to achieve what I want to accomplish? Or, is it even possible? Perhaps generics can't do this for me.