• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using generics to return sets of different classes  RSS feed

 
Boris Golman
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I'm trying to grasp a better context of using generics in Java. I've got two methods, which, eventually, hit the database (serviceCall.findAll method) and retrieve a set of either DBClass1 (from one database table) or DBClass2 (from another table).
As you can see, both methods are, pretty much exactly the same, except for the return type - a set of different classes, which represent the database record (the classes have the same properties, they just, as I've already mentioned, reprensent results
from different tables). Is there a way, perhaps using generics, to use one method instead, where I can pass some kind of a flag that will tell it which set to return? Thanks in advance.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do DBClass1 and DBClass2 extend a common supertype?

What does serverCall.findAll(ids) return?
 
Boris Golman
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Do DBClass1 and DBClass2 extend a common supertype?

What does serverCall.findAll(ids) return?


To answer your first question - no. It would be simpler if they would, of course.

Now, your second question: findAll methods return the list of DBClass1 and the list of DBClass2, respectively. I should've been clearer, I do apologize. The instances "serviceCall" are different service calls and should be named differently. So, bottom line: one returns the list of DBClass1 objects, another one - list of DBClass2 objects.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that scenario, I can't see a way to create a single typesafe method. In the absence of a common superclass (other than Object) Generics becomes irrelevant.
 
Boris Golman
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Given that scenario, I can't see a way to create a single typesafe method. In the absence of a common superclass (other than Object) Generics becomes irrelevant.


Well, this seemed to work. Seems rather dirty (like explicit casting to T), but that's all I could think of. If anyone knows a better way, it'll be great. ;)

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not using generics to write generic code that's just merging two methods into one and making the code harder to read (and you have introduced a bug in the process if serviceCall is a different call for each case as stated in your earlier post).
BTW There is no advantage to using generics in this case, you may as well just hard code the cast to the type you require or better still (scrub that bit I hadn't read the code properly) just stick to what you had originally (possibly with some clearer naming).
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I concur with Tony, this is not how generics should be used. Sorry I can't give an alternative though, I'm feeling a little under the weather and I'm not really up for digging into a solution right now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!