Created
February 6, 2014 00:18
-
-
Save azenla/8836100 to your computer and use it in GitHub Desktop.
State Container
This file contains hidden or 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 org.nanobot | |
| /** | |
| * Holds basic on/off statuses for different parts of a bot | |
| */ | |
| class StateContainer { | |
| private final Map<String, Boolean> states = [:] | |
| void on(String name) { | |
| states[name] = true | |
| } | |
| void off(String name) { | |
| states[name] = false | |
| } | |
| boolean has(String name) { | |
| return states[name] ?: false | |
| } | |
| void toggle(String name) { | |
| states[name] = !has(name) | |
| } | |
| void reset() { | |
| states.clear() | |
| } | |
| Set<String> current() { | |
| return states.findAll { | |
| it.value | |
| }.keySet().asImmutable() // Immutable to prevent people from modifying states in bulk | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment