Package org.omegazero.common.eventbus
Class Event
java.lang.Object
org.omegazero.common.eventbus.Event
Primarily used for calling event bus subscribers using an
EventBus
. An object of this class contains the method signature of the target method to call when
dispatching this Event
and several additional parameters for event execution.- Since:
- 2.1
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
cancel()
Cancels this event.static String
createEventSignature
(String methodName, Class<?>[] params, Class<?> returnType) Creates a string containing unambiguous information about the givenparams
,returnType
and name of a method.Returns a string containing the method name and a string representation of method parameters and the return type of the target event method.Returns the method name of this event, which is the name of the method that is called when this event occurs.Class<?>[]
Returns the method parameters of the target method of this event.Class<?>
Returns the return type of the target method of this event.boolean
Whether this event was configured to allow cancellation usingcancel()
.boolean
Returnstrue
if this event was canceled.boolean
Whether all event bus subscriber handlers should be called to collect a list of return values when dispatching this event.boolean
Returns true if this event has a variable state.void
reset()
Resets this event instance to initial values for reuse.void
setCanceled
(boolean canceled) Sets whether this event is canceled.
-
Constructor Details
-
Event
SeeEvent(String, boolean, Class[], Class, boolean)
The event defaults to being non-cancelable and configured to not collect all return values from subscribers (includeAllReturns is
false
). The default return type isvoid
.- Parameters:
methodName
- The name of the method that should be called when this event occursparams
- The parameter types of the called method for this event. May be of length 0
-
Event
SeeEvent(String, boolean, Class[], Class, boolean)
The event defaults to being non-cancelable and configured to not collect all return values from subscribers (includeAllReturns is
false
).- Parameters:
methodName
- The name of the method that should be called when this event occursparams
- The parameter types of the called method for this event. May be of length 0returnType
- The return type of event listener methods
-
Event
public Event(String methodName, boolean cancelable, Class<?>[] params, Class<?> returnType, boolean includeAllReturns) Creates a general-purposeEvent
object.- Parameters:
methodName
- The name of the method that should be called when this event occurscancelable
- Whether this event may be canceled usingcancel()
params
- The parameter types of the called method for this event. May be of length 0returnType
- The return type of event listener methodsincludeAllReturns
- Include return values from all listener methods instead of just the first
-
-
Method Details
-
getMethodName
Returns the method name of this event, which is the name of the method that is called when this event occurs.Further details are usage-defined.
- Returns:
- The method name of this event
-
isCancelable
public boolean isCancelable()Whether this event was configured to allow cancellation usingcancel()
.- Returns:
- true if this event may be canceled
-
getParams
Returns the method parameters of the target method of this event.- Returns:
- The method parameters of this event
-
getReturnType
Returns the return type of the target method of this event.- Returns:
- The return type of this event
-
isIncludeAllReturns
public boolean isIncludeAllReturns()Whether all event bus subscriber handlers should be called to collect a list of return values when dispatching this event. If this isfalse
, event execution will be stopped when the first handler returns a non-null
value.- Returns:
true
if all return values should be collected
-
getEventSignature
Returns a string containing the method name and a string representation of method parameters and the return type of the target event method.- Returns:
- The method signature
-
isCanceled
public boolean isCanceled()Returnstrue
if this event was canceled.- Returns:
true
if this event was canceled
-
setCanceled
public void setCanceled(boolean canceled) Sets whether this event is canceled. When an event handler calls this method with a value oftrue
, subsequent event handlers will not be called.- Parameters:
canceled
- Whether this event is canceled- Throws:
UnsupportedOperationException
- If this method is called with a value oftrue
but this event is not cancelable
-
cancel
public void cancel()Cancels this event. An event may only be canceled if it is explicitly cancelable.A call to this method is equivalent to a call to
setCanceled
(true)- Throws:
UnsupportedOperationException
- If this event is not cancelable
-
reset
public void reset()Resets this event instance to initial values for reuse. -
isStateful
public boolean isStateful()Returns true if this event has a variable state. If it does not, this event object may be safely used multiple times and/or concurrently. Otherwise, it should not be used concurrently andreset()
must be called before reuse.- Returns:
- true if this event is stateful
-
createEventSignature
public static String createEventSignature(String methodName, Class<?>[] params, Class<?> returnType) Creates a string containing unambiguous information about the givenparams
,returnType
and name of a method.- Parameters:
methodName
- A method nameparams
- The parameters of the methodreturnType
- The return type- Returns:
- The string
- See Also:
-