Last active
December 28, 2021 13:38
-
-
Save techyourchance/6602815188294c1c58779d3e8d16f12b to your computer and use it in GitHub Desktop.
Revisions
-
techyourchance revised this gist
Aug 15, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ public void unregisterListener(LISTENER_CLASS listener) { protected Set<LISTENER_CLASS> getListeners() { synchronized (MONITOR) { return Collections.unmodifiableSet(new HashSet<>(mListeners)); } } -
techyourchance created this gist
Aug 15, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ public abstract class BaseObservable<LISTENER_CLASS> { private final Object MONITOR = new Object(); private final Set<LISTENER_CLASS> mListeners = new HashSet<>(); public void registerListener(LISTENER_CLASS listener) { synchronized (MONITOR) { boolean hadNoListeners = mListeners.size() == 0; mListeners.add(listener); if (hadNoListeners && mListeners.size() == 1) { onFirstListenerRegistered(); } } } public void unregisterListener(LISTENER_CLASS listener) { synchronized (MONITOR) { boolean hadOneListener = mListeners.size() == 1; mListeners.remove(listener); if (hadOneListener && mListeners.size() == 0) { onLastListenerUnregistered(); } } } protected Set<LISTENER_CLASS> getListeners() { synchronized (MONITOR) { return Collections.unmodifiableSet(mListeners); } } protected void onFirstListenerRegistered() { } protected void onLastListenerUnregistered() { } }