I have a method like this:
public void runMethod()
{
method1();
method2();
method3();
}
I want to call this runMethod multiple times based on an id. However, if say method2() fails for some reason, then when I call the runMethod, it should execute method3() and not try to execute method1() again (which has already run successfully for this id).
What would be the best way to achieve this?
Thanks a lot for the help
You could record in a map whether a method has executed successfully or not.
private Map<String, Boolean> executes = new HashMap<String, Boolean>();
public void method1() {
Boolean hasExecuted = executes.get("method1");
if (hasExecuted != null && hasExecuted) {
return;
}
executes.put("method1", true);
...
}
// and so forth, with method2, method3, etc
You are looking for some kind of state machine. Keep state of the method execution in a data structure(E.g map).
At the beginning of method, you need to check whether execution of method1 for given id is executed successfully or not.
public void runMethod()
{
method1();
method2()
method3();
}
private Set<Integer> method1Executed = new HashSet<Integer>();
private Set<Integer> method2Executed = new HashSet<Integer>();
private void method1(Integer id)
{
if (method1Executed.contains(id)) {
return;
}
// Processing.
method1Executed.add(id)
}
// Similar code for method2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With