Skip to content

Instantly share code, notes, and snippets.

@huxi
Created February 19, 2013 09:52
Show Gist options
  • Save huxi/4984506 to your computer and use it in GitHub Desktop.
Save huxi/4984506 to your computer and use it in GitHub Desktop.
import java.util.*;
public class MagicalLand {
public static void main(String[] args) {
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
}
}
for (int i = 0; i < (Math.random() * 500) + 2; i++) {
if (Unicorn.pat()) {
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
}
}
System.out.println("END OF PROGRAM");
}
private static class Unicorn {
private static Set<List<StackTraceElement>> pats = new HashSet<List<StackTraceElement>>();
public static boolean pat() {
Throwable t = new Throwable();
StackTraceElement[] stackTrace = t.getStackTrace();
List<StackTraceElement> trace = Arrays.asList(stackTrace);
if(pats.contains(trace)) {
return false;
}
pats.add(trace);
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment