Skip to content

Instantly share code, notes, and snippets.

@werand
Created February 27, 2013 21:14
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 werand/5051788 to your computer and use it in GitHub Desktop.
Save werand/5051788 to your computer and use it in GitHub Desktop.
/**
* Solution for the java puzzle from http://zeroturnaround.com/fun/magical-java-puzzle-pat-the-unicorns/
*/
public class Unicorn {
private static int currentLine = 0;
public static boolean pat() {
int line = getExecutionLineFromMagicalLand();
boolean isUniquePat = line != currentLine;
currentLine = line;
return isUniquePat;
}
private static int getExecutionLineFromMagicalLand() {
Exception e = new Exception();
e.fillInStackTrace();
StackTraceElement[] stackTrace = e.getStackTrace();
return stackTrace[2].getLineNumber();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment