Hey, do you guys have any idea on how could I make an event wait for a task to complete?
I mean, you have event A, which has a method "awaitForCompletion", that method passes a suplier as argument, which provides with "EventTaskResult".
How could I make wait the event for the task to complete?
I currently have this
public class StaffSwitchStatusEvent implements StaffEvent {
private final StaffMember member;
private boolean cancelled = false;
private final Lock lock = new ReentrantLock();
private final Condition eventCompleted = lock.newCondition();
public StaffSwitchStatusEvent(final StaffMember member) {
this.member = member;
}
/**
* Get the staff member
*
* @return the staff member
*/
public StaffMember getMember() {
return member;
}
/**
* Await for an action to complete
*
* @param wait the wait action
* @throws InterruptedException if the asynchronous waiter fails
* to wait
*/
public void awaitFor(AwaitEventResult wait) throws InterruptedException {
lock.lock();
try {
// If the event has already completed, return immediately
if (cancelled) {
return;
}
while (!wait.isComplete() && !cancelled) {
eventCompleted.await();
}
if (!cancelled) eventCompleted.signalAll();
} finally {
lock.unlock();
}
}
/**
* Get if the event is cancelled
*
* @return the event status
*/
@Override
public boolean isCancelled() {
return cancelled;
}
/**
* Set if the event is cancelled
*
* @param cancel the cancel status
*/
@Override
public void setCancelled(final boolean cancel) {
cancelled = cancel;
}
}