Handling unwanted event
<p>It's a very specific situation. There is a state object (the model), and a handler class which listens for a change (a controller... well, sort of) and then does some action. The problem? That action action may cause state change, thus it may cause the action to be executed recursively. Which is of course unwanted. Results are unpredictable at best, and may lead to stack overflow at worst (oh, the irony).</p>

<p>To clarify, a piece of code:</p>

<pre>
public class Handler() {
   private final StateListener stateListener=new StateListener() {
      public void onClearValue(Key key) {
         action();   
      }
   }

   private State state;
   private List keys; //Initialized elsewhere

   public void init(State state) {
      this.state=state;
      state.addListener(stateListener); //In real code, stateListener is removed from previously set state object.
   }

   public void action() {
      for(final Key key : keys) {
         if(someCondition) {
            state.clearValue(key); //Fires onClearValue, which it's not wanted
         }
      }
   }
}
</pre>

<p>It's my code and all these classes (State, StateListener, Handler) are my own, so I can change them anyway I want. I thought about adding to state a method like <code>clearValue(Key key, boolean suppressEvent)</code> but in such case other listeners would not be notified about change, so it's not a good solution. I considered also adding a "caller" object to the distributed event... so if handler sees, that even is caused but it's action, it could ignore it. But it seems to be somewhat crude.</p>

<p>How did I end like this, anyway? Until now, state was not na observable. <code>action()</code> method has been called manually after state change and of course this metod never called itself. But as state can be used (and modified...) outside Handler class, this had to be changed.</p>

<p>Thanks for your advice!</p>
