Base class for Java Observable which needs to be aware of whether it's "busy" and expose this information to its clients
public abstract class BaseBusyObservable<LISTENER_CLASS> extends BaseObservable<LISTENER_CLASS> { | |
private final AtomicBoolean mIsBusy = new AtomicBoolean(false); | |
public final boolean isBusy() { | |
return mIsBusy.get(); | |
} | |
/** | |
* Atomically assert not busy and become busy | |
* @throws IllegalStateException if wasn't busy when this method was called | |
*/ | |
protected final void assertNotBusyAndBecomeBusy() { | |
if (!mIsBusy.compareAndSet(false, true)) { | |
throw new IllegalStateException("assertion violation: mustn't be busy"); | |
} | |
} | |
/** | |
* Atomically check whether not busy and become busy | |
* @return true if was "free" when this method was called; false if was busy | |
*/ | |
protected final boolean isFreeAndBecomeBusy() { | |
return mIsBusy.compareAndSet(false, true); | |
} | |
/** | |
* Unconditionally become not busy | |
*/ | |
protected final void becomeNotBusy() { | |
mIsBusy.set(false); | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Agree! |
This comment has been minimized.
This comment has been minimized.
Hey folks, thanks for your input. |
This comment has been minimized.
This comment has been minimized.
Well, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
isFreeAndBecomeBusy
name is misleading. What abouttryBecomeBusy
ofbecomeBusyIfFree
?assertNotBusyAndBecomeBusy
is very long. What aboutforceMakeBusy
?