Created
July 5, 2019 13:48
-
-
Save ardovic/185bd21bf3175e9098992237094734b8 to your computer and use it in GitHub Desktop.
PendingUpdater construct, great for 2-key locks
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
| public class PendingUpdater<T> { | |
| private final Action<T> updateAction; | |
| private boolean pendingUpdate; | |
| private T value; | |
| public PendingUpdater(Action<T> updateAction) { | |
| this.updateAction = updateAction; | |
| } | |
| public void requestUpdate() { | |
| if (value != null) { | |
| updateAction.call(value); | |
| } else { | |
| pendingUpdate = true; | |
| } | |
| } | |
| public void setValue(T value) { | |
| this.value = value; | |
| if (pendingUpdate) { | |
| updateAction.call(value); | |
| pendingUpdate = false; | |
| } | |
| } | |
| public interface Action<T> { | |
| void call(T t); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment