Created
June 9, 2017 02:16
-
-
Save markus-mnm/3e79101b6754df1cb530ffec38dbdb65 to your computer and use it in GitHub Desktop.
Example of two different implementations to check some condition before running some code
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
package com.toursgds.transport.protocol; | |
import org.apache.commons.lang3.time.StopWatch; | |
public class TestException { | |
private static class ObfuscationNotSetUpException extends RuntimeException {} | |
boolean configured = false; | |
private InternalClass internalThing = new InternalClass(); | |
public void setupObfuscation() { | |
if (!configured) { | |
throw new ObfuscationNotSetUpException(); | |
} | |
} | |
private class InternalClass { | |
public void doSomethingWithOuterClass() { | |
try { | |
setupObfuscation(); | |
} catch (ObfuscationNotSetUpException e) { | |
if (configured) { | |
throw new IllegalStateException(); | |
} | |
} | |
} | |
public void doSomethingConditionallyWithOuterClass() { | |
if (configured) { | |
try { | |
setupObfuscation(); | |
} catch (ObfuscationNotSetUpException e) { | |
throw new IllegalStateException(); | |
} | |
} | |
} | |
} | |
public static void main(String[] args) { | |
TestException testException = new TestException(); | |
testException.internalThing.doSomethingWithOuterClass(); | |
StopWatch sw = new StopWatch(); | |
sw.start(); | |
for (int i = 0; i < 10_000_000; i++) { | |
testException.internalThing.doSomethingWithOuterClass(); | |
} | |
sw.stop(); | |
System.out.println(sw.getTime()); | |
sw.reset(); | |
sw.start(); | |
for (int i = 0; i < 10_000_000; i++) { | |
testException.internalThing.doSomethingConditionallyWithOuterClass(); | |
} | |
sw.stop(); | |
System.out.println(sw.getTime()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment