Event Filtering

Adapters can be used for filtering out events that don’t meet certain criteria. The adapter would analyze the information contained in the event and decide whether to forward it to the target object. This could be useful when the event source generates many events and only a small number of them are interesting to the listener.

For example, let’s say that we were going to monitor the temperature in our simulator with a special listener that is interested only in temperature changes above or below certain values. We could create an adapter that filters out all temperature change events where the new temperature is between a low and a high threshold value. The GenericTemperatureAdapter can be easily modified to perform this task. The new adapter might be called a GenericTemperatureThresholdAdapter. We add data members to store the threshold temperatures and assign them default values as follows:

protected double lowThreshold = 0.0;
protected double highThreshold = 100.0;

Now we need a way to assign the threshold values. For our example, we create methods for getting and setting these values. You’ll see in Chapter 4, that these values represent properties of the adapter. The new methods of the GenericTemperatureThresholdAdapter look like this:

public void setLowThreshold (double low)
{
   lowThreshold = low;
}

public double getLowThreshold()
{
   return lowThreshold;
}

public void setHighThreshold(double high)
{
   highThreshold = high;
}
public double getHighThreshold() { return ...

Get Developing Java Beans now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.