I have objects that pre-generate some configuration once so they can process computation later on faster (possibly several times). I'm trying to genericize it to avoid passing the configuration as an Object and casting it each time.
interface IComputable<T> {
T configure(); // Generate configuration object
int compute(T conf); // Run the computation based on the pre-generated configuration
float precision(T conf); // Make the compute() computation finer-grained
...
}
class ComputableInfo {
IComputable<?> computable;
Object config; // Real type is <?>
int result;
ComputableInfo(String id) {
computable = ComputableFactory.createFrom(id);
config = computable.configure();
result = computable.compute(config); // <<<--- The method compute(capture#3-of ?) in the type TestInterface.IComputable<capture#3-of ?> is not applicable for the arguments (Object)
}
}
I'm getting a compilation error:
The method compute(capture#3-of ?) in the type TestInterface.IComputable<capture#3-of ?> is not applicable for the arguments (Object)
Of course, I can replace the int compute(T conf) by int compute(Object conf) but I would have to explicitly cast it to the appropriate T. It's not that big a problem, but it makes the code less obvious.
I could also make the ComputableInfo generic with
interface ComputableInfo<T> {
IComputable<T> computable;
T config;
...
but that would create compilation problems in some other places (mostly "raw types" warning) that I'd like to avoid more than the previous workaround (using Object instead of T).
Is there a way to achieve that? I'm even open to turning such problem from error to warning in the compiler settings, or maybe have an extra private method that would return both config and result in a single object?
EDIT: to add up on the "further compilation problems" if I make ComputableInfo generic: I have another method in the interface (see edited) that is called through the ComputableInfo:
ComputableInfo<?> info = getInfo(id);
info.computable.precision(info.config); // <<<--- (same kind of error)
The problem is that the ComputableInfo has no way to know the T type of Computable<T> (or no way I know), as it comes from a Factory that builds it from a configuration file.