Skip to content

Instantly share code, notes, and snippets.

@nickspacek
Created February 19, 2013 14:16
Show Gist options
  • Save nickspacek/4986289 to your computer and use it in GitHub Desktop.
Save nickspacek/4986289 to your computer and use it in GitHub Desktop.
Omnipresent unicorn game keeper...
import java.util.HashMap;
import java.util.Map;
public class Unicorn {
// The locations of unicorns in our magical, one-dimensional land
private static final int UNICORN_ONE_LOCATION = 4;
private static final int UNICORN_TWO_LOCATION = 10;
// Let's create a game keeper for the magical land to keep track of where all the Unicorns are!
private static final Map<Integer, Unicorn> UNICORN_GAME_KEEPER = new HashMap<Integer, Unicorn>();
static {
// We'll whisper in his ear and tell him where they are hiding
UNICORN_GAME_KEEPER.put(UNICORN_ONE_LOCATION, new Unicorn());
UNICORN_GAME_KEEPER.put(UNICORN_TWO_LOCATION, new Unicorn());
}
// Keep track of whether this unicorn has been patted
private boolean patted;
public static boolean pat() {
// Where is this patting action being performed?
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
int location = stackTrace[stackTrace.length - 1].getLineNumber();
// Let's ask the game keeper if the disembodied hand doing the patting touches a unicorn
Unicorn unicorn = UNICORN_GAME_KEEPER.get(location);
if (unicorn != null && !unicorn.patted) {
// Yup, it did!
unicorn.patted = true;
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment