I have
public class Entity
On which works class:
public class Table<Target extends Entity>
{
public boolean save (Target target)
public Target load (int id)
}
Till i put this objects of Table class in Database class Map everything is ok but:
public class Database
{
public String name;
public Map<String, Table<? extends Entity>> tables = new HashMap <String, Table<? extends Entity>> ();
public Context context;
public int version;
public Database (Context context, int version)
{
this.context = context;
this.version = version;
}
public void add (Table<? extends Entity> table)
{
tables.put(table.name, table);
}
public Table <? extends Entity> table (String name)
{
return (Table<? extends Entity>) tables.get(name);
}
}
Assume we have:
public class Apple extends Entity
I want this code to work:
//init
database = new Database(getBaseContext(), 4);
new Table<Apple> (database, Apple.class);
//this is where solution need occurs !
database.table("apples").save (new Apple("green apple"));
The method save(capture#1-of ? extends Entity) in the type
Table(capture#1-of ? extends Entity)
is not applicable for the arguments (Note)
How to make the map.get method work ?
One thing you can do to make this more convenient is map the tables by the target class:
As the annotation implies, this isn’t at all typesafe – you’ll need to make sure you never mismatch the keys and values in
tables.