Basically what I want, istwo public methods with slightly different return values to call the same method to do whatever work is needed. They both return the return value of the private method, however the private method will know how to return the correct value depending on the public method that called it.
Example methods:
public Map<Type1, Type3> doSomething1();
public Map<Type2, Type3> doSomething2();
private Map<Type1/Type2, Type3> doSomething();
So, in the above example, doSomething() returns either Type1 or Type2 as the Map’s key-type, depending on which public method called it. It would be able to perform a simple check, and populate the map with objects of the correct type.
Maybe this can be done through some clever Java reflection? I’m not sure. It all seems pretty dodgy, so if there’s a much better way to go about this, I’m all ears.
I strongly suggest that reflection-mojo should be avoided here. A function must do one thing propertly, and must not depend on who call it.
A better way to do the same would be to refactor doSomething() into smaller functions, create two new functions called doSomethingFor1() and doSomethingFor2(). Both these functions can reuse the refactored parts of the old doSomething().
Now have doSomething1() call and use doSomethingFor1().
Likewise, doSomething2() should use doSomethingFor2().
cheers,
jrh.