Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8097929

[Base] ObservableValue.addListener allows duplicates

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P5 P5
    • 8u20
    • 8
    • javafx
    • None

      From the docs of ObservableValue.addListener:

      "If the same listener is added more than once, then it will be notified more than once."

      Ask yourself this: Would there ever, in any part of the universe be a reason to add the same listener twice to the same property?

      I suggest improving this by changing the addListener method to always ensure no duplicates are added.

      My current workaround is to invoke removeListener before invoking addListener, e.g.

      someProperty.removeListener(changeListener);
      someProperty.addListener(changeListener);

      I recommend changing JavaFX to no longer add duplicates, allowing me to simply write

      someProperty.addListener(changeListener);

      Alternatively, the API could be improved by adding an isListener method which would allow me to do the check for you, however, I believe this is the least preferable approach, since I would then have to write the following code over and over again:

      if(!someProperty.isListener(changeListener))
          someProperty.addListener(changeListener);

      Still, even this less favourable solution would be better than the remove-then-add solution we have today.

            msladecek Martin Sládeček
            risaksen Randahl Isaksen
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: