Issue
I have a class. Its public interface (excluding the constructor) is not generic, but its internal logic is. A simplified example of this is:
class Summarizer<T> {
private final ComplicatedProducer<T> producer;
private final Processor<T> processor;
StreamSummarizer(ComplicatedProducer<T> producer, Processor<T> processor) {
...
}
public int summarizeAll() {
int sum = 0
// some business logic goes here
for (...) {
sum += processor.process(producer.next());
}
return sum;
}
public int summarizeSomePortion(Parameters p) {
// some other business logic goes here
...
}
}
Note that (after construction), the user of Summarizer
has no interest in T
's type. In my case, the type isn't known at compile time (we use reflection to construct ComplicatedProducer
and Processor
), and I'd rather not have to have a factory function that returns Summarizer<?>
. Is there any clean way to make Summarizer
non-generic without resorting to the use of raw types?
Solution
You can always have a single-level wrapper:
class SummarizerImpl<T> {
private final ComplicatedProducer<T> producer;
private final Processor<T> processor;
SummarizerImpl(ComplicatedProducer<T> producer, Processor<T> processor) {
...
}
}
class Summarizer {
private final SummarizerImpl<?> impl;
private Summarizer(SummarizerImpl<?> impl) { ... }
public static <T> Summarizer create(
ComplicatedProducer<T> producer, Processor<T> processor) {
return new Summarizer(new SummarizerImpl<T>(producer, processor));
}
}
Answered By - Louis Wasserman