Skip to content

Instantly share code, notes, and snippets.

@markus-mnm
Created June 9, 2017 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markus-mnm/3e79101b6754df1cb530ffec38dbdb65 to your computer and use it in GitHub Desktop.
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
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