org.opendoors.util
Class PostponeObservable

java.lang.Object
  extended byjava.util.Observable
      extended byorg.opendoors.util.PostponeObservable
All Implemented Interfaces:
java.util.EventListener, java.beans.PropertyChangeListener

public class PostponeObservable
extends java.util.Observable
implements java.beans.PropertyChangeListener

PostponeObservable is the delegated instance for an observable with the following characteristic: notification of the change arrives after the observable has not changed for a period of n milliseconds.

This pattern solves a fundamental problem, namely thrashing. Thrashing occurs when the same action occurs repeatedly over short periods of time because the same event is fired, usually hundreds or thousand of times per second.

So, for example, if a cache changes thousands of times per second during a load or a transition to a new working set, how many times do you want to refresh it? Once or a thousand times? An instance of PostponeObservable allows your cache (or any changing object) to notify that it has changed just once.

Thus, changes are deferred so that the observers of the changes are not inundated with notifications.

Since this class implements the PropertyChangeListener interface, the class PropertyChangeSupport may also be employed as well to notify observers of changes.

This class is extends java.util.Observable. Observers must implement the interface java.util.Observer.

Note: Shared instances of a PostponeObservable must implement their own synchronizing strategy.

Postponeability may be enabled/disabled at runtime.

See Also:
Observable, Observer

Field Summary
protected  int postponeInterval
          On property change, postpone event notification this number of millis.
protected  boolean resetClockOnUpdate
          While waiting to fire a notification, reset the interval if another change comes in.
 
Constructor Summary
PostponeObservable()
          Constructs a default observable.
PostponeObservable(int postponeInterval, boolean resetClockOnUpdate)
          Constructs a default observable with the following settings.
 
Method Summary
 void destroy()
          Destoys this instance and the associated timer.
 void enablePostponeability(boolean enable)
          Enables postponeability if true.
protected  void init()
          Initializes the instance.
 void propertyChange(java.beans.PropertyChangeEvent evt)
          Using the property event model propagate a change event to the observable.
 void setChanged()
          Signals that the observable has changed.
 void setPostponePeriod(int postponeInterval)
          Sets the observable period.
 void timerAction()
          Call back from the timer when the observation period has expired.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

postponeInterval

protected int postponeInterval
On property change, postpone event notification this number of millis.


resetClockOnUpdate

protected boolean resetClockOnUpdate
While waiting to fire a notification, reset the interval if another change comes in.

Constructor Detail

PostponeObservable

public PostponeObservable()
Constructs a default observable.


PostponeObservable

public PostponeObservable(int postponeInterval,
                          boolean resetClockOnUpdate)
Constructs a default observable with the following settings.

Method Detail

init

protected void init()
Initializes the instance.


setPostponePeriod

public void setPostponePeriod(int postponeInterval)
Sets the observable period.


enablePostponeability

public void enablePostponeability(boolean enable)
Enables postponeability if true.


propertyChange

public void propertyChange(java.beans.PropertyChangeEvent evt)
Using the property event model propagate a change event to the observable.

Specified by:
propertyChange in interface java.beans.PropertyChangeListener
Parameters:
evt - The property change event which can be null.

timerAction

public void timerAction()
Call back from the timer when the observation period has expired.


setChanged

public void setChanged()
Signals that the observable has changed.

Observers will be notified when

 current time > (time of last change + postponeInterval)
 
provided postponeability is enabled.


destroy

public void destroy()
Destoys this instance and the associated timer.