Last active
March 1, 2016 11:44
-
-
Save fge/39b5ca079e2d3c6edb00 to your computer and use it in GitHub Desktop.
A generic JavaFX "one shot" event implementation
This file contains 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
public final class OneShotEvent<T extends Event> | |
implements EventHandler<T> | |
{ | |
private final AtomicBoolean triggered = new AtomicBoolean(false); | |
private final EventHandler<T> delegate; | |
public static <E extends Event> EventHandler<E> of(final EventHandler<E> delegate) | |
{ | |
return new OneShotEvent<>(delegate); | |
} | |
private OneShotEvent(final EventHandler<T> delegate) | |
{ | |
this.delegate = Objects.requireNonNull(delegate); | |
} | |
@Override | |
public void handle(final T event) | |
{ | |
if (!triggered.getAndSet(true)) | |
delegate.handle(event); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment