|
|
@@ -1,109 +0,0 @@
|
|
|
-package net.ranides.assira.events;
|
|
|
-
|
|
|
-
|
|
|
-import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
-
|
|
|
-/**
|
|
|
- * It is quite specific base class for events, which can be observed for completion.
|
|
|
- *
|
|
|
- * When you create ActiveEvent, you can pass it into any dispatcher and wait using #await method.
|
|
|
- * This method will block as long as event will be processed by some ActiveEventListener.
|
|
|
- *
|
|
|
- * You can cancel event at any time, and then ActiveEventListener will ignore it.
|
|
|
- *
|
|
|
- * Please note, that this event must be sent to dispatcher implementing ActiveEventListener
|
|
|
- * interface. If not, then the whole functionality won't work.
|
|
|
- */
|
|
|
-public class ActiveEvent implements Event {
|
|
|
-
|
|
|
- private final Object lock = new Object();
|
|
|
-
|
|
|
- private int counter = 0;
|
|
|
-
|
|
|
- private final AtomicBoolean canceled = new AtomicBoolean(false);
|
|
|
-
|
|
|
- /**
|
|
|
- * Creates new event
|
|
|
- */
|
|
|
- protected ActiveEvent() {
|
|
|
- // do nothing
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Cancel this event. ActiveEventListeners ignore canceled events.
|
|
|
- *
|
|
|
- * @return true
|
|
|
- */
|
|
|
- public boolean cancel() {
|
|
|
- return canceled.compareAndSet(false, true);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void dispatch(EventListener<?> listener) {
|
|
|
- // please note that we keep track of ActiveEventListeners only
|
|
|
- // we do not care about other listeners
|
|
|
- if(!(listener instanceof ActiveEventListener)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- synchronized (lock) {
|
|
|
- counter++;
|
|
|
- lock.notify();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void process(EventListener<?> listener) {
|
|
|
- // please note that we keep track of ActiveEventListeners only
|
|
|
- // we do not care about other listeners
|
|
|
- if(!(listener instanceof ActiveEventListener)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- synchronized (lock) {
|
|
|
- counter--;
|
|
|
- lock.notify();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Returns true if this event is canceled
|
|
|
- *
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- public boolean canceled() {
|
|
|
- return canceled.get();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Returns true if this event was processed by some ActiveEventListener
|
|
|
- *
|
|
|
- * Please note that this method returns "true" if processing did not started yet.
|
|
|
- *
|
|
|
- * It can be used only to detect, that processing is in progress.
|
|
|
- *
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- public boolean processed() {
|
|
|
- synchronized (lock) {
|
|
|
- return counter == 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Waits until event is processed by some ActiveEventListener
|
|
|
- *
|
|
|
- * Please note that this method returns immediately if processing did not started yet.
|
|
|
- *
|
|
|
- * It can be used only to wait, if processing is already in progress.
|
|
|
- *
|
|
|
- * @return true if not canceled
|
|
|
- * @throws InterruptedException if terminated
|
|
|
- */
|
|
|
- public boolean await() throws InterruptedException {
|
|
|
- synchronized (lock) {
|
|
|
- while (counter > 0) {
|
|
|
- lock.wait();
|
|
|
- }
|
|
|
- }
|
|
|
- return !canceled.get();
|
|
|
- }
|
|
|
-}
|