Java: filtering for recurring events

I was wondering if the following is for ordinary Java users when receiving callbacks / events. These callbacks can be triggered by user input, but also in other ways, so it is not only associated with user interface events:

public void handleEvent( @NotNull final SomeEvent e ) { final boolean process; synchronized ( this ) { process = !e.equals(filter); filter = e; } if ( process ) { ... } } 

Basically, according to some complex scheme (a very complex interface with several representations of the same model and where the user can modify the model from different screens [for example, in complex 3D programs]). I have many events, and I noticed that I can filter out repeated events using the above snippet. If the event was processed, and the next event should coincide with the last processed event (stored in the filter link), then the event / callback is simply ignored.

It works great. I was wondering if filtering for recurring events is a common technique?

+4
source share
3 answers

Not always, but usually it can be a sign that some elements of the cascading chain of the event do not correctly detect that they do not need to send the event. The classic illustration is the bean installer that generates a PropertyChangeEvent, even if the value has not changed.

While what you have done is filtering out these events, it does not affect what might be the main underlying problem.

The problem is that these โ€œerrorsโ€ can combine to form infinite loops. Extending the above bean example, let's say you have a user interface that resets its editable value based on this bean field ... and resetting the user interface also causes the bean installer because proper authentication was not performed there or. The first time the value is edited, and an infinite loop will be executed.

These examples are obvious when they happen, but as the notification hierarchy becomes more complex, it becomes more difficult to keep track of these things, and they potentially occur in more intermittent times.

A good rule of thumb is to make each event generation component as conservative as possible. In the case of (heh), you receive notifications from components that you cannot control, and will forward your own events, then a filter like the one you installed may be the only way to prevent the spread of a potentially bigger problem than just performance.

+2
source

The only thing I can think of is to act on the ListSelectionEvent based on whether the selection changes (that is, the user still clicks and drags the mouse) or whether the event is a โ€œfinalโ€ selection event; eg.

 public class MyListSelectionListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent evt) { // Finished selecting if (!evt.getValueIsAdjusting()) { JOptionPane.showMessageDialog("Selection Complete!"); } } } 
+1
source

It looks like you might have a listener registered twice in the same source. It can cause it. Or, you may have registered one listener in multiple copies. If you see strangeness, look for endless cycles of events, which, unfortunately, because Swing organizes itself, can happen too easily. You will want to smash these listeners.

I have never done this and never met. As some people point out, some controls will trigger events when setting options, but there are ways to filter these things out.

The synchronized (this) block is suspicious, since you will always return to the Swing thread. If this is incorrect in your program, you are violating the Swing streaming rule, and this may explain the problem. On the bottom line, you do not need a synchronized block because Swing is single.

(I assume this is Swing, like some other posters, but from your code alone it is ambiguous).

+1
source

Source: https://habr.com/ru/post/1302538/


All Articles