I'm trying to create a general-purpose report generator, where there is a specific report criteria class that ties to a specific report generator. I have a marker interface for the criteria. Individual criteria classes implement this interface:
I then have an interface for the report generators
I want to store the generator for each criteria type in a map, keyed by the class object for the criteria type
The actual generating is done by:
which will use criteria.getClass() to get the class object to pass to the map.get(). That will retrieve the generator for class T, which knows how to get the values from T to generate the report.
I couldn't find a way to write a HashMap declaration that would work without @SuppressWarnings annotations, the avoidance of which is one of my goals (at least in the code I expect my users to write - if I really need an annotation in my framework code, and know that I've prevented bad casts, that's OK). So I ended up with my own class that behaves like a Map:
I'm willling to live with the warnings suppression because I know that the put method enforced the class relationship when an item was added. Like:
where BillingDetailReportCriteria implements ReportCriteriaInterface, and BilllingDetailReportGenerator implements ReportGenerator<BillingEntryDetailReportCriteria>
So, all good so far. But, I can't find a way to actually use the map and generate my report without so kludging:
Issue #1 was that an error was flagged until I added the cast in the line with issue #2
Issue #2 is that adding the cast required that I suppress warning for unchecked.
I'd like to avoid the suppressing warnings, because this is code I expect the users of the class to write.
Note that I could have done the block as
if I added suppression of rawtypes warnings as well.
So, is there any way for me to write this in a type-safe manner, while avoiding the warnings suppression when using the generators?