samedi 10 avril 2021

Java: Abstract generic data context class

I would like to store all application data in a data context class.

Having application data classes A and B implementing IApplicationData, the non-abstract, non-generic version could look like this:

NonGenericDataContext

private List<A> aList;
private List<B> bList;

public List<A>getAs() {
    return aList;
}

public List<B>getBs() {
    return bList;
}

Now I would like to abstract from the concrete application classes and use a generic data context to use common functionality in multiple applications. The data context class could then look like this:

AbstractGenericDataContext

protected Map<Class<? extends IApplicationData>, List<? extends IApplicationData>> listsByType;

public List<? extends IApplicationData> getListByType(Class<? extends IApplicationData> clazz) {
    return objectListsByType.get(clazz);
}

I would love to use proper generics, but Java erases the type during compilation, hence the construct with Class. Unfortunately, the construct above makes the code cumbersome and return values of getListByType() need to be casted.

Question How could the code be improved to achieve an abstract, generic (not necessarily in a JLS sense, but with similar usability) data context class?

I would like stick with a pure Java solution (so no code generators if possible). Reflection would be ok if it does not slow down data access in a critical way.





Aucun commentaire:

Enregistrer un commentaire