public class ReloadingController extends Object implements EventSource
A class for adding support for reload operations in a generic way.
A ReloadingController
monitors a specific source and triggers reloading events if necessary. So it does not
perform reloading itself, but only sends out notifications when it thinks that this should be done. This allows for a
very generic setup in which different components involved in reloading are loosely coupled via events.
A typical usage scenario is as follows:
ReloadingController
instance is created and initialized with a ReloadingDetector
object.EventListener
objects for reloading events can be registered at the controller.checkForReloading()
method is called whenever a check is to be performed. This could
be done for instance by a timer in regular intervals or by any other means appropriate for a specific
application.resetReloadingState()
method
must be called. It tells the controller that the last notification has been processed and that new checks are
possible again. It is important that this method is called. Otherwise, checkForReloading()
will not do any
new checks or send out event notifications any more.
This class can be accessed from multiple threads concurrently. It shields the associated ReloadingDetector
object for concurrent access, so that a concrete detector implementation does not have to be thread-safe.
Constructor and Description |
---|
ReloadingController(ReloadingDetector detect)
Creates a new instance of
ReloadingController and associates it with the given ReloadingDetector
object. |
Modifier and Type | Method and Description |
---|---|
<T extends Event> |
addEventListener(EventType<T> eventType,
EventListener<? super T> listener)
Adds an event listener for the specified event type.
|
boolean |
checkForReloading(Object data)
Performs a check whether a reload operation is necessary.
|
ReloadingDetector |
getDetector()
Returns the
ReloadingDetector used by this controller. |
boolean |
isInReloadingState()
Tests whether this controller is in reloading state.
|
<T extends Event> |
removeEventListener(EventType<T> eventType,
EventListener<? super T> listener)
Removes the event listener registration for the given event type and listener.
|
void |
resetReloadingState()
Resets the reloading state.
|
public ReloadingController(ReloadingDetector detect)
ReloadingController
and associates it with the given ReloadingDetector
object.detect
- the ReloadingDetector
(must not be null)IllegalArgumentException
- if the detector is undefinedpublic ReloadingDetector getDetector()
ReloadingDetector
used by this controller.ReloadingDetector
public <T extends Event> void addEventListener(EventType<T> eventType, EventListener<? super T> listener)
ReloadingEvent
.addEventListener
in interface EventSource
T
- the type of events processed by this listenereventType
- the event type (must not be null)listener
- the listener to be registered (must not be null)public <T extends Event> boolean removeEventListener(EventType<T> eventType, EventListener<? super T> listener)
EventSource
removeEventListener
in interface EventSource
T
- the type of events processed by this listenereventType
- the event typelistener
- the event listener to be removedpublic boolean isInReloadingState()
checkForReloading()
has detected the necessity for a reload operation, but
resetReloadingState()
has not been called yet. In this state no further reloading checks are possible.public boolean checkForReloading(Object data)
ReloadingDetector
and sends out notifications if necessary.
The argument can be an arbitrary data object; it will be part of the event notification sent out when a reload
operation should be performed. The return value indicates whether a change was detected and an event was sent. Once a
need for a reload is detected, this controller is in reloading state. Until this state is reset (by calling
resetReloadingState()
), no further reloading checks are performed by this method, and no events are fired;
it then returns always true.data
- additional data for an event notificationpublic void resetReloadingState()
Copyright © 2001–2022 The Apache Software Foundation. All rights reserved.