Class EventWaiter
- All Implemented Interfaces:
net.dv8tion.jda.api.hooks.EventListener
The EventWaiter is capable of handling specialized forms of
Event
that must meet criteria not normally specifiable
without implementation of an EventListener
.
Creating an EventWaiter requires provision and/or creation of a
Executor
, and thus a proper
shutdown of said executor. The default constructor for an EventWaiter sets up a
working, "live", EventWaiter whose shutdown is triggered via JDA firing a
ShutdownEvent
.
A more "shutdown adaptable" constructor allows the provision of a
ScheduledExecutorService
and a choice of how exactly shutdown will be handled
(see EventWaiter(ScheduledExecutorService, boolean)
for more details).
As a final note, if you intend to use the EventWaiter, it is highly recommended you DO NOT create multiple EventWaiters! Doing this will cause unnecessary increases in memory usage.
- Author:
- John Grosh (jagrosh)
-
Constructor Summary
ConstructorDescriptionConstructs an empty EventWaiter.EventWaiter
(ScheduledExecutorService threadpool, boolean shutdownAutomatically) Constructs an EventWaiter using the providedExecutor
as it's threadpool. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Gets whether the EventWaiter's internal ScheduledExecutorServiceis shutdown
.final void
onEvent
(net.dv8tion.jda.api.events.GenericEvent event) void
shutdown()
Closes this EventWaiter if it doesn't normally shutdown automatically.<T extends net.dv8tion.jda.api.events.Event>
voidwaitForEvent
(Class<T> classType, Predicate<T> condition, Consumer<T> action) Waits an indefinite amount of time for anEvent
that returnstrue
when tested with the providedPredicate
.<T extends net.dv8tion.jda.api.events.Event>
voidwaitForEvent
(Class<T> classType, Predicate<T> condition, Consumer<T> action, long timeout, TimeUnit unit, Runnable timeoutAction) Waits a predetermined amount of time for anEvent
that returnstrue
when tested with the providedPredicate
.
-
Constructor Details
-
EventWaiter
public EventWaiter()Constructs an empty EventWaiter. -
EventWaiter
Constructs an EventWaiter using the providedExecutor
as it's threadpool.A developer might choose to use this constructor over the
default
, for using a alternate form of threadpool, as opposed to asingle thread executor
.
A developer might also favor this over the default as they use the same waiter for multiple shards, and thus shutdown must be handled externally if a special shutdown sequence is being used.shutdownAutomatically
is required to be manually specified by developers as a way of verifying a contract that the developer will conform to the behavior of the newly generated EventWaiter:- If
true
, shutdown is handled when aShutdownEvent
is fired. This means that any external functions of the provided Executor is now impossible and any externally queued tasks are lost if they have yet to be run. - If
false
, shutdown is now placed as a responsibility of the developer, and no attempt will be made to shutdown the provided Executor.
EventWaiter#shutdown()
. However, this operation is only supported for EventWaiters that are not supposed to shutdown automatically, otherwise invocation ofEventWaiter#shutdown()
will result in anUnsupportedOperationException
.- Parameters:
threadpool
- The ScheduledExecutorService to use for this EventWaiter's threadpool.shutdownAutomatically
- Whether or not thethreadpool
will shutdown automatically when aShutdownEvent
is fired.- Throws:
IllegalArgumentException
- If the threadpool provided isnull
oris shutdown
- See Also:
- If
-
-
Method Details
-
isShutdown
public boolean isShutdown()Gets whether the EventWaiter's internal ScheduledExecutorServiceis shutdown
.- Returns:
true
if the ScheduledExecutorService is shutdown,false
otherwise.
-
waitForEvent
public <T extends net.dv8tion.jda.api.events.Event> void waitForEvent(Class<T> classType, Predicate<T> condition, Consumer<T> action) Waits an indefinite amount of time for anEvent
that returnstrue
when tested with the providedPredicate
.When this occurs, the provided
Consumer
will accept and execute using the same Event.- Type Parameters:
T
- The type of Event to wait for.- Parameters:
classType
- TheClass
of the Event to wait for. Never null.condition
- The Predicate to test when Events of the provided type are thrown. Never null.action
- The Consumer to perform an action when the condition Predicate returnstrue
. Never null.- Throws:
IllegalArgumentException
- One of two reasons:- 1) Either the
classType
,condition
, oraction
wasnull
. - 2) The internal threadpool is shut down, meaning that no more tasks can be submitted.
- 1) Either the
-
waitForEvent
public <T extends net.dv8tion.jda.api.events.Event> void waitForEvent(Class<T> classType, Predicate<T> condition, Consumer<T> action, long timeout, TimeUnit unit, Runnable timeoutAction) Waits a predetermined amount of time for anEvent
that returnstrue
when tested with the providedPredicate
.Once started, there are two possible outcomes:
- Type Parameters:
T
- The type of Event to wait for.- Parameters:
classType
- TheClass
of the Event to wait for. Never null.condition
- The Predicate to test when Events of the provided type are thrown. Never null.action
- The Consumer to perform an action when the condition Predicate returnstrue
. Never null.timeout
- The maximum amount of time to wait for, or-1
if there is no timeout.unit
- TheTimeUnit
measurement of the timeout, ornull
if there is no timeout.timeoutAction
- The Runnable to run if the time runs out before a correct Event is thrown, ornull
if there is no action on timeout.- Throws:
IllegalArgumentException
- One of two reasons:- 1) Either the
classType
,condition
, oraction
wasnull
. - 2) The internal threadpool is shut down, meaning that no more tasks can be submitted.
- 1) Either the
-
onEvent
public final void onEvent(net.dv8tion.jda.api.events.GenericEvent event) - Specified by:
onEvent
in interfacenet.dv8tion.jda.api.hooks.EventListener
-
shutdown
public void shutdown()Closes this EventWaiter if it doesn't normally shutdown automatically.IF YOU USED THE DEFAULT CONSTRUCTOR WITH NO ARGUMENTS DO NOT CALL THIS!
Calling this method on an EventWaiter that does shutdown automatically will result in anUnsupportedOperationException
being thrown.- Throws:
UnsupportedOperationException
- The EventWaiter is supposed to close automatically.
-