Sorry for the delay on this, but I don't think I've checked this forum in a while. Anyway, assuming I'm not still asleep this morning, I can see three really simple solutions.
First off, you can simply use Object as the return type. Since Java uses a singly-rooted object hierarchy, this would cover all non-primitive cases (unlike in C++, where you often have objects with no common superclasses). However, this won't work with primitive types (though you can of course use the equivalent wrapper classes), you'll need to downcast the objects, and you may need to use instanceof()
to check the correct type first. In the function itself, you can use a HashMap
or some other form of lookup collection to select the correct table name.
Second, you can make this method and the class it is in abstract
, and create a subclass for each of the cases which handles the specific instances, calling the parent's method to handle the actual performance. I doubt that this would be much more acceptable than the current situation, however, as there would still be a lot of redundancy, but it would at least reduce
the amount of duplication.
Finally, if you are using the newest version of Java (1.5), you can use generics
, which work very similarly to C++ templates. There are some differences, however, and you'll want to check to make sure that what you have in mind would work.