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

FXCollections.unmodifiableObservableSet does not implement Set::contains

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8u65
    • Fix Version/s: 9
    • Component/s: javafx
    • Labels:
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux_ubuntu

      Description

      A DESCRIPTION OF THE REQUEST :
      The UnmodifiableObservableSet class does not delegate its contains method to the backing set. It defaults to the implementation found in AbstractCollection.

      JUSTIFICATION :
      The default implementation in AbstractCollection is O(n). Most sets have better time complexity than that. HashSets have O(1), TreeSets have O(log(n)).


      ---------- BEGIN SOURCE ----------
          private static class UnmodifiableObservableSet<E> extends AbstractSet<E> implements ObservableSet<E> {

              private final ObservableSet<E> backingSet;
              private SetListenerHelper<E> listenerHelper;
              private SetChangeListener<E> listener;

              public UnmodifiableObservableSet(ObservableSet<E> backingSet) {
                  this.backingSet = backingSet;
                  this.listener = null;
              }

              private void initListener() {
                  if (listener == null) {
                      listener = c -> {
                          callObservers(new SetAdapterChange<E>(UnmodifiableObservableSet.this, c));
                      };
                      this.backingSet.addListener(new WeakSetChangeListener<E>(listener));
                  }
              }

              private void callObservers(SetChangeListener.Change<? extends E> change) {
                  SetListenerHelper.fireValueChangedEvent(listenerHelper, change);
              }

              @Override
              public Iterator<E> iterator() {
                  return new Iterator<E>() {
                      private final Iterator<? extends E> i = backingSet.iterator();

                      @Override
                      public boolean hasNext() {
                          return i.hasNext();
                      }

                      @Override
                      public E next() {
                          return i.next();
                      }

                      @Override
                      public void remove() {
                          throw new UnsupportedOperationException();
                      }
                  };
              }

              @Override
              public int size() {
                  return backingSet.size();
              }

              @Override
              public void addListener(InvalidationListener listener) {
                  initListener();
                  listenerHelper = SetListenerHelper.addListener(listenerHelper, listener);
              }

              @Override
              public void removeListener(InvalidationListener listener) {
                  listenerHelper = SetListenerHelper.removeListener(listenerHelper, listener);
              }

              @Override
              public void addListener(SetChangeListener<? super E> listener) {
                  initListener();
                  listenerHelper = SetListenerHelper.addListener(listenerHelper, listener);
              }

              @Override
              public void removeListener(SetChangeListener<? super E> listener) {
                  listenerHelper = SetListenerHelper.removeListener(listenerHelper, listener);
              }

              @Override
              public boolean add(E e) {
                  throw new UnsupportedOperationException();
              }

              @Override
              public boolean remove(Object o) {
                  throw new UnsupportedOperationException();
              }

              @Override
              public boolean addAll(Collection<? extends E> c) {
                  throw new UnsupportedOperationException();
              }

              @Override
              public boolean retainAll(Collection<?> c) {
                  throw new UnsupportedOperationException();
              }

              @Override
              public boolean removeAll(Collection<?> c) {
                  throw new UnsupportedOperationException();
              }

              @Override
              public void clear() {
                  throw new UnsupportedOperationException();
              }
          }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            • Assignee:
              vadim Vadim Pakhnushev
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: