Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Map of Class object keys and type parameterized with that class

Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic