Skip to content

Instantly share code, notes, and snippets.

@fge
Last active March 1, 2016 11:44
Show Gist options
  • Save fge/39b5ca079e2d3c6edb00 to your computer and use it in GitHub Desktop.
Save fge/39b5ca079e2d3c6edb00 to your computer and use it in GitHub Desktop.
A generic JavaFX "one shot" event implementation
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