Class Event<T>
- Type Parameters:
T
- the result type of the event. UseVoid
if handling the event does not produce a result
- All Implemented Interfaces:
Future<T>
,Associator
,Eligible
- Direct Known Subclasses:
ActionEvent
,Attached
,Close
,Closed
,CompletionEvent
,ConfigurationUpdate
,DataInput
,Detached
,DiscardSession
,Error
,FileChanged
,HalfClosed
,Handler.NoEvent
,IOEvent
,KeyValueStoreQuery
,KeyValueStoreUpdate
,MailFoldersUpdated
,MessageReceived
,NamedEvent
,NioRegistration
,Opened
,OpenFile
,Opening
,OpenMailConnection
,OpenSocketConnection
,Purge
,SendMailMessage
,Start
,StartProcess
,Stop
,UpdateMailFolders
,Upgraded
,WatchFile
By default (i.e. as implemented by this class), the event’s kind is
represented by its Java class and the eligibility is based on
the “is a” relationship between classes. An event is eligible if its class
is equal to or a super class of the class used as criterion.
This default behavior can be changed by overriding the
methods from Eligible
. See NamedEvent
as an example.
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.concurrent.Future
Future.State
-
Field Summary
Fields inherited from class org.jgrapes.core.internal.EventBase
completed, completionEvents, invokedFor
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddCompletionEvent
(Event<?> completionEvent) Adds the given event to the events to be thrown when this event has completed (seeFuture.isDone()
).<V> Optional
<V> associated
(Object by, Class<V> type) Retrieves the associated object following the association with the given “name”.boolean
cancel
(boolean mayInterruptIfRunning) Prevents the invocation of further handlers (likestop()
and (in addition) the invocation of any added completed events.Channel[]
channels()
Returns the channels associated with the event.<C> C[]
Returns the subset of channels that are assignable to the given type.Returns the events to be thrown when this event has completed (seeisDone()
).Allows access to the intermediate result before the completion of the event.Returns the class of this event as representation of its kind.forChannels
(Class<C> type, BiConsumer<E, C> handler) Execute the given handler for all channels of the given type.get()
Waits for the event to be completed (seeisDone()
) and returns the first (or only) result.Causes the invoking thread to wait until the processing of the event has been completed (seeisDone()
) or the given timeout has expired and returns the first (or only) result.protected void
handled()
Invoked after all handlers for the event have been executed.protected void
handlingError
(EventPipeline eventProcessor, Throwable throwable) Implements the default behavior for handling events thrown by a handler.boolean
boolean
isDone()
Check if this event has completed.boolean
isEligibleFor
(Object criterion) Returnstrue
if thecriterion
is of the same class or a base class of this event’s class.boolean
Returnstrue
ifstop()
has been called.Return the event pipeline that currently processes the event (if any).results()
void
Resume the invocation of handlers for this event.<A extends Associator>
AsetAssociated
(Object by, Object with) Establishes a “named” association to an associated object.setChannels
(Channel... channels) Sets the channels that the event is fired on if no channels are specified explicitly when firing the event (seeManager.fire(Event, Channel...)
).setRequiresResult
(boolean value) Sets the result of handling this event.stop()
Can be called during the execution of an event handler to indicate that the event should not be processed further.void
Suspend the invocation of the remaining handlers for this event.void
suspendHandling
(Runnable whenResumed) Suspend the invocation of the remaining handlers for this event.Tie the result of this event to the result of the other event.toString()
Methods inherited from class org.jgrapes.core.internal.EventBase
disableTracking, enqueued, firstResultAssigned, isTracked, onCompletion
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.jgrapes.core.Associator
associated, associated, associated, associatedGet
Methods inherited from interface java.util.concurrent.Future
exceptionNow, resultNow, state
-
Constructor Details
-
Event
Creates a new event.Passing channels is equivalent to first creating the event and then calling
setChannels(Channel...)
with the given channels.- Parameters:
channels
- the channels to set
-
-
Method Details
-
defaultCriterion
Returns the class of this event as representation of its kind.- Returns:
- the class of this event
- See Also:
-
isEligibleFor
Returnstrue
if thecriterion
is of the same class or a base class of this event’s class.- Parameters:
criterion
- the criterion- Returns:
- true if this meets the criterion
- See Also:
-
processedBy
Return the event pipeline that currently processes the event (if any).- Overrides:
processedBy
in classEventBase<T>
- Returns:
- the event pipeline if the event is being processed
-
handlingError
Implements the default behavior for handling events thrown by a handler.Fires a
handling error
event for this event and the given throwable.- Specified by:
handlingError
in classEventBase<T>
- Parameters:
eventProcessor
- the manager that has invoked the handlerthrowable
- the exception that has been thrown by the handler- See Also:
-
setChannels
Sets the channels that the event is fired on if no channels are specified explicitly when firing the event (seeManager.fire(Event, Channel...)
).- Parameters:
channels
- the channels to set- Returns:
- the object for easy chaining
- Throws:
IllegalStateException
- if the method is called after this event has been fired
-
channels
Returns the channels associated with the event.Before an event has been fired, this returns the channels set with
setChannels(Channel[])
. After an event has been fired, this returns the channels that the event has effectively been fired on (seeManager.fire(Event, Channel...)
). -
channels
Returns the subset of channels that are assignable to the given type.- Type Parameters:
C
- the given type’s class- Parameters:
type
- the class to look for- Returns:
- the filtered channels
- See Also:
-
forChannels
public <E extends EventBase<?>,C extends Channel> void forChannels(Class<C> type, BiConsumer<E, C> handler) Execute the given handler for all channels of the given type.- Type Parameters:
E
- the type of the eventC
- the type of the channel- Parameters:
type
- the channel typehandler
- the handler
-
completionEvents
Returns the events to be thrown when this event has completed (seeisDone()
).- Returns:
- the completed events
-
addCompletionEvent
Adds the given event to the events to be thrown when this event has completed (seeFuture.isDone()
).Such an event is called a “completion event”.
Completion events are considered to be caused by the event that caused the completed event. If an event e1 caused an event e2 which has a completion event e2c, e1 is only put in state completed when e2c has been handled.
Completion events are handled by the same
EventProcessor
as the event that has been completed.- Specified by:
addCompletionEvent
in classEventBase<T>
- Parameters:
completionEvent
- the completion event to add- Returns:
- the object for easy chaining
- See Also:
-
setRequiresResult
Description copied from class:EventBase
- Overrides:
setRequiresResult
in classEventBase<T>
-
isDone
Check if this event has completed.An event is completed if
- all its handlers have been invoked (or the event has been stopped or cancelled),
- all events caused by it have completed,
- no
CompletionLock
s remain, and - a result has been set (only required if
setRequiresResult(boolean)
has been called withtrue
).
- Returns:
- the completed state
-
handled
Invoked after all handlers for the event have been executed.May be overridden by derived classes to cause some immediate effect (instead of e.g. waiting for the completion event). The default implementation does nothing. This method is invoked by the event handler thread and must not block.
-
suspendHandling
Suspend the invocation of the remaining handlers for this event.May only be called in a handler for the event. Must be balanced by an invocation of
EventBase.resumeHandling()
.- Overrides:
suspendHandling
in classEventBase<T>
-
suspendHandling
Suspend the invocation of the remaining handlers for this event.May only be called in a handler for the event. Must be balanced by an invocation of
EventBase.resumeHandling()
.- Overrides:
suspendHandling
in classEventBase<T>
- Parameters:
whenResumed
- some function to be executed when handling is resumed
-
resumeHandling
Resume the invocation of handlers for this event.- Overrides:
resumeHandling
in classEventBase<T>
- See Also:
-
stop
Can be called during the execution of an event handler to indicate that the event should not be processed further.All remaining handlers for this event will be skipped.
- Returns:
- the object for easy chaining
-
isStopped
Returnstrue
ifstop()
has been called. -
cancel
Prevents the invocation of further handlers (likestop()
and (in addition) the invocation of any added completed events.- Parameters:
mayInterruptIfRunning
- ignored- Returns:
false
if the event has already been completed- See Also:
-
isCancelled
-
setResult
Sets the result of handling this event.If this method is invoked more then once, the various results are collected in a list. This can happen if the event is handled by several components.
- Parameters:
result
- the result to set- Returns:
- the object for easy chaining
-
currentResults
Allows access to the intermediate result before the completion of the event.- Specified by:
currentResults
in classEventBase<T>
- Returns:
- the intermediate results (which may be an empty list)
-
tieTo
Tie the result of this event to the result of the other event.Changes of either event’s results will subsequently be applied to both events.
This is useful when an event is replaced by another event during handling like: fire((new Event()).tieTo(oldEvent.stop()))
- Parameters:
other
- the event to tie to- Returns:
- the object for easy chaining
-
get
Waits for the event to be completed (seeisDone()
) and returns the first (or only) result.- Throws:
InterruptedException
- See Also:
-
get
Causes the invoking thread to wait until the processing of the event has been completed (seeisDone()
) or the given timeout has expired and returns the first (or only) result.- Returns:
- the result
- Throws:
InterruptedException
TimeoutException
- See Also:
-
results
Waits for the event to be completed (seeisDone()
) and returns the list of results (which may be empty if the event’s result type isVoid
).- Returns:
- the results
- Throws:
InterruptedException
- See Also:
-
results
Causes the invoking thread to wait until the processing of the event has been completed (seeisDone()
) or given timeout has expired and returns the list of results (which may be empty if the event’s result type isVoid
).- Returns:
- the results
- Throws:
InterruptedException
TimeoutException
- See Also:
-
setAssociated
Description copied from interface:Associator
Establishes a “named” association to an associated object.Note that anything that represents an id can be used as value for parameter
name
, it does not necessarily have to be a string.Passing
null
as parameterwith
clears the association.- Type Parameters:
A
- the associator’s type- Parameters:
by
- the “name”with
- the object to be associated- Returns:
- the sub channel for easy chaining
-
associated
Description copied from interface:Associator
Retrieves the associated object following the association with the given “name”.This general version of the method supports the retrieval of values of arbitrary types associated by any “name” types.
- Type Parameters:
V
- the type of the value to be retrieved- Parameters:
by
- the “name”type
- the type of the value to be retrieved- Returns:
- the associate with the given type, if any
-
toString
-