2

Let's say I have:

public void one() {
  two();
  // continue here
}
public void two() {
  three();
}
public void three() {
  // exits two() and three() and continues back in one()
}

Are there any methods to doing this?

Alex
  • 163
  • 7
Hamlyn
  • 23
  • 1
  • 1
  • 3

3 Answers3

5

The only way to do this without change method two() is to throw an Exception.

If you can change the code you can return a boolean which tells the caller to return.

However the simplest solution is to inline the methods into one larger method. If this is too large you should retsructure it another way and not place complex controls between methods like this.


Say you have

public void one() {
    System.out.println("Start of one.");
    two();
// do something
    System.out.println("End of one.");
}

public void two() {
    System.out.println("Start of two.");
    three();
// do something
    System.out.println("End of two.");
}

public void three() {
    System.out.println("Start of three.");
// do something
    System.out.println("End of three.");
}

You can add an unchecked exception if you cannot change two();

public void one() {
    System.out.println("Start of one.");
    try {
        two();
    } catch (CancellationException expected) {
    }
// do something
    System.out.println("End of one.");
}

public void two() {
    System.out.println("Start of two.");
    three();
// do something
    System.out.println("End of two.");
}

public void three() {
    System.out.println("Start of three.");
// do something
    System.out.println("End of three.");
    throw new CancellationException(); // use your own exception if possible.
}

You can return a boolean to say return, if you can change two()

public void one() {
    System.out.println("Start of one.");
    two();
// do something
    System.out.println("End of one.");
}

public void two() {
    System.out.println("Start of two.");
    if (three()) return;
// do something
    System.out.println("End of two.");
}

public boolean three() {
    System.out.println("Start of three.");
// do something
    System.out.println("End of three.");
    return true;
}

Or you can inline the structures

public void one() {
    System.out.println("Start of one.");
    two();
// do something
    System.out.println("End of one.");
}

public void two() {
    System.out.println("Start of two.");
    System.out.println("Start of three.");
// do something for three
    System.out.println("End of three.");
    boolean condition = true;
    if (!condition) {
// do something for two
        System.out.println("End of two.");
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    thanks alot. i think i will go with the return method. code is too big and difficult to shrink it. – Hamlyn Apr 02 '11 at 08:35
  • Throwing exceptions was exactly what I needed but somehow I could not figure it out at first, thanks! – djule5 Oct 19 '11 at 04:08
1

Looking at your code, if you call one, it then calls two, which calls three.. If you leave it as it is, thats exactly what it WILL do. The line after two (in your one) function, will be only done once its come back from two, and it wont do that until two has finished with three..

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • 1
    +0: You have to assume the methods do more than suggested in the code, otherwise the simplest solution is to delete them as they don't do anything. ;) – Peter Lawrey Apr 02 '11 at 08:03
  • hahah. i had actually simplified the methods. of course the methods would be more complex and i want to be able to exit upon a condition – Hamlyn Apr 02 '11 at 08:26
1

Assuming you can change the two() method, perhaps you want something like this?

public void one() {
    two();
    // continue here from condition
}

public void two() {
    if (three()) {
        // go back due to condition 
        return;
    }

    // condition wasn't met
}

public boolean three() {
    // some condition is determined here

    if (condition) {
        // exits two() and three() and continues back in one()
        return true;
    }

    // condition wasn't met, keep processing here

    // finally return false so two() keeps going too
    return false;
}
WhiteFang34
  • 70,765
  • 18
  • 106
  • 111