Last active
December 3, 2017 16:04
-
-
Save fullkomnun/96d014d2a1127866d989c9845c26154e to your computer and use it in GitHub Desktop.
switch case fallthrough-like recursive flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun verify(driver: TestDriver) { | |
// a switch case fallthrough-like recursive flow | |
tailrec fun verify(verification: Verification) { | |
when (verification) { | |
NO_VERIFICATION -> return // THE END | |
VERIFY_PHASE_1 -> verifyPhase1(driver) | |
VERIFY_PHASE_2 -> verifyPhase2(driver) | |
VERIFY_PHASE_3 -> verifyPhase3(driver) | |
VERIFY_PHASE_4 -> verifyPhase4(driver) | |
} | |
verify(verification.prev()) // fall-through! | |
} | |
verify(verification) | |
} | |
enum class Verification { NO_VERIFICATION, VERIFY_PHASE_1, VERIFY_PHASE_2, VERIFY_PHASE_3, VERIFY_PHASE_4 } | |
inline fun <reified T : Enum<T>> T.prev(): T { | |
val values = enumValues<T>() | |
val prevOrdinal = (ordinal - 1) % values.size | |
return values[prevOrdinal] | |
} | |
inline fun <reified T : Enum<T>> T.next(): T { | |
val values = enumValues<T>() | |
val nextOrdinal = (ordinal + 1) % values.size | |
return values[nextOrdinal] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("unused") class Verifications { | |
private final Verification verification = Verification.NO_VERIFICATION; | |
void verify(TestDriver driver) { | |
switch (verification) { | |
case VERIFY_PHASE_4: | |
verifyPhase4(driver); | |
case VERIFY_PHASE_3: | |
verifyPhase3(driver); | |
case VERIFY_PHASE_2: | |
verifyPhase2(driver); | |
case VERIFY_PHASE_1: | |
verifyPhase1(driver); | |
case NO_VERIFICATION: | |
} | |
} | |
private void verifyPhase1(TestDriver driver) {} | |
private void verifyPhase2(TestDriver driver) {} | |
private void verifyPhase3(TestDriver driver) {} | |
private void verifyPhase4(TestDriver driver) {} | |
static class TestDriver { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun verify(driver: TestDriver) = | |
when (verification) { | |
NO_VERIFICATION -> Unit | |
VERIFY_PHASE_1 -> verifyPhase1(driver) | |
VERIFY_PHASE_2 -> { | |
verifyPhase1(driver) | |
verifyPhase2(driver) | |
} | |
VERIFY_PHASE_3 -> { | |
verifyPhase1(driver) | |
verifyPhase2(driver) | |
verifyPhase3(driver) | |
} | |
VERIFY_PHASE_4 -> { | |
verifyPhase1(driver) | |
verifyPhase2(driver) | |
verifyPhase3(driver) | |
verifyPhase4(driver) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment