I have multiple classes that implement method create.
public TeamScorePk create(TeamScore entity) {
TeamScorePk pk = entity.getPk();
if (!pk.validate()) {
throw new IllegalArgumentException("Part of primary key is null:" + entity);
}
if (INSTANCES.containsKey(pk)) {
throw new IllegalArgumentException(entity + " already exists.");
}
INSTANCES.put(pk, entity);
return pk;
}
Map INSTANCES serves like DB table.
The only difference between the create method in different classes are the types it uses. Thus I'd like to put the method into an abstract super class. Problem is with the map INSTANCES, because I have to get it from the extending classes. I could declare abstract method getInstances, but I have problem with the type of the Map it should return. If I use something like Map<? extends Pk, ? extends Entity>, I naturally get and error when putting types Pk and Entity into it.
Could someone give me an example or some advice, how the abstract class should look like?
Map<Pk,Entity>. "ChildOfPk" still IsA "Pk"