
com.freya02.botcommands.api.waiter.EventWaiterBuilder Maven / Gradle / Ivy
package com.freya02.botcommands.api.waiter;
import com.freya02.botcommands.internal.waiter.WaitingEvent;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.internal.utils.Checks;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
* Builder for {@link EventWaiter}
*
* @param Type of the JDA event to wait after
*/
public class EventWaiterBuilder {
private final Class eventType;
private final List> preconditions = new ArrayList<>();
private Consumer onSuccess;
private Runnable onTimeout, onCancelled;
private CompletedFutureEvent onComplete;
private long timeout = -1;
private TimeUnit timeoutUnit;
EventWaiterBuilder(Class eventType) {
this.eventType = eventType;
}
/**
* Sets the timeout for this event waiter, this means the action will no longer be usable after the time has elapsed
*
* @param timeout Amount of time before the timeout occurs
* @param timeoutUnit Unit of time for the timeout (minutes / seconds / millis...)
* @return This builder for chaining convenience
*/
public EventWaiterBuilder setTimeout(long timeout, TimeUnit timeoutUnit) {
Checks.positive(timeout, "timeout");
this.timeout = timeout;
this.timeoutUnit = timeoutUnit;
return this;
}
/**
* Adds a precondition to this event waiter, this means your actions won't be executed unless all your preconditions are met
* You can have multiple preconditions
*
* @param precondition The precondition to check on each event
* @return This builder for chaining convenience
*/
public EventWaiterBuilder addPrecondition(Predicate precondition) {
Checks.notNull(precondition, "Event waiter precondition");
this.preconditions.add(precondition);
return this;
}
/**
* Sets the consumer called after the event waiter has all its preconditions met and the task has not timeout nor been cancelled
*
* @param onSuccess The success consumer to call
* @return This builder for chaining convenience
*/
public EventWaiterBuilder setOnSuccess(Consumer onSuccess) {
this.onSuccess = onSuccess;
return this;
}
/**
* Sets the consumer called when the event waiter has expired due to a timeout
*
* @param onTimeout The timeout consumer to call
* @return This builder for chaining convenience
*/
public EventWaiterBuilder setOnTimeout(Runnable onTimeout) {
this.onTimeout = onTimeout;
return this;
}
/**
* Sets the consumer called after the event waiter has been cancelled
*
* @param onCancelled The cancellation consumer to call
* @return This builder for chaining convenience
*/
public EventWaiterBuilder setOnCancelled(Runnable onCancelled) {
this.onCancelled = onCancelled;
return this;
}
/**
* Sets the consumer called after the event waiter has "completed", i.e. it has either been successfully ran, or been cancelled, or has been timeout
*
* @param onComplete The consumer to call when the waiter is completed
* @return This builder for chaining convenience
*/
public EventWaiterBuilder setOnComplete(CompletedFutureEvent onComplete) {
this.onComplete = onComplete;
return this;
}
/**
* Submits the event waiter to the event waiting queue and returns the corresponding future, This operation is not blocking
*
* @return The {@link Future} of this event waiter, can be cancelled
*/
public CompletableFuture submit() {
return EventWaiter.submit(new WaitingEvent<>(eventType, preconditions, onComplete, onSuccess, onTimeout, onCancelled, timeout, timeoutUnit));
}
/**
* Submits the event waiter to the event waiting queue, waits for the event to arrive and returns the event, This operation is blocking
*
* @return The event specified in {@link EventWaiter#of(Class)}
* @throws CancellationException If the event waiter has been cancelled by your code
* @throws ExecutionException If an exception occured in the event waiter or in a callback
* @throws InterruptedException If this thread gets interrupted while waiting for the event
*/
public T complete() throws CancellationException, ExecutionException, InterruptedException {
return submit().get();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy