AdapterReferenceCollector { static <T, A extends BindingCollectionAdapter<T>> WeakReference<A> createRef(A adapter, ObservableList<T> items, ObservableList.OnListChangedCallback callback) { if (thread == null || !thread.isAlive()) { thread = new PollReferenceThread(); thread.start(); } return new AdapterRef<>(adapter, items, callback); }    }
@Test public void removesCallbackWhenAdapterIsCollected() throws Exception { BindingCollectionAdapter adapter = mock(BindingCollectionAdapter.class); ObservableList items = mock(ObservableList.class); ObservableList.OnListChangedCallback callback = mock(ObservableList.OnListChangedCallback.class); WeakReference ref = AdapterReferenceCollector.createRef(adapter, items, callback); adapter = null; System.gc(); do { Thread.sleep(50); } while (ref.get() != null); verify(items).removeOnListChangedCallback(callback); }