Reputation: 613
I have a following problem:
I had a few methods that are basically used to get something from Salesforce.
Here is an example:
public Map<String, Customer> findSomethingByIds(String[] somethingIds) throws... {
return binding.findSomethingByIds(somethingIds);
}
For a number of reasons I needed to retry the execution of this method in a very rare cases (f.e session expires etc.), so I used this.
So now I have something like this:
public Map<String, Something> findSomethingByIds(final String[] somethingIds) throws ... {
Map<String, Something> myList = null;
Callable<Map<String, Something>> task = new Callable<Map<String, Something>>() {
@Override
public Map<String, Something> call() throws Exception {
return binding.findSomethingByIds(somethingIds);
}
};
RetriableTask<Map<String, Something>> r = new RetriableTask<>(2, 1000, task);
try {
myList = r.call();
} catch (Exception e) {
// Ex. handling
}
return myList;
}
Now, there are a lot of such methods in my code, so if I want to use the RetriableTask interface I have to add a lot of code to those methods, similar to the one above, which I want to avoid at all costs. All those methods pretty much return something different, so I can't use a Factory here (or I don't know how). Does anyone know any solution for this? Any help would be appreciated.
Upvotes: 1
Views: 655
Reputation: 11592
If you have a method doing something similar and the only difference is the return type, try using generics:
public Map<String, T> findSomethingByIds(final String[] somethingIds) throws ... {
}
This will allow you to perform equivalent processing on different object types without copying and pasting code everywhere.
Responding to the comments, if they take different parameter types, you can still use generics in the parameters. If you mean they have a different number of parameters (i.e., have a completely different signature), then you can create wrapper methods which perform the processing which is unique to that object type, and after that you can pass control to the generic method for the processing which is common to all object types.
Upvotes: 5