Created
February 16, 2011 11:05
-
-
Save alextkachman/829211 to your computer and use it in GitHub Desktop.
Groovy++ pattern for listeners
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
abstract static class Listener<T> { | |
abstract void call(T oldValue, T newValue) | |
} | |
private FList<Listener<T>> listeners = FList.emptyList | |
final Listener<T> addListener(Listener<T> listener, Executor executor = null) { | |
if(executor) { | |
def myListener = listener | |
listener = { oldValue, newValue -> | |
executor.execute { | |
myListener(oldValue, newValue) | |
} | |
} | |
} | |
for(;;) { | |
def old = listeners | |
if(listeners.compareAndSet(old, old + listener)) | |
break | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment