Skip to content

Instantly share code, notes, and snippets.

@alextkachman
Created February 16, 2011 11:05
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 alextkachman/829211 to your computer and use it in GitHub Desktop.
Save alextkachman/829211 to your computer and use it in GitHub Desktop.
Groovy++ pattern for listeners
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