How to restart a method from the last point of failure

I have a way like this:

    public void runMethod()
    {
    method1();
    method2();
    method3();
    }

I want to call this runMethod several times based on identifier. However, if, for some reason, method2 () fails, then when I call runMethod, it should execute method3 () and not try again to execute method1 () (which already successfully completed this identifier).

What would be the best way to achieve this?

Thank you very much for your help

+5
source share
3 answers

You can write to the card whether the method completed 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
+3
source

You are looking for some kind of state machine. Save the execution state of a method in a data structure (for example, a map).

, 1 .

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.
+1

int - , , . :

public int runMethod(int flag) {
    if (flag < 1) {
        method1();
        if (method1failed) {
            return 1;
        }
    }
    if (flag < 2) {
        method2();
        if (method2failed) {
            return 2;
        }
    }
    if (flag < 3) {
        method3();
        if (method3failed) {
            return 3;
        }
    }
    return 4;
}
+1
source

All Articles