In some contexts it's necessary to detect - in a ListChangeListener, without control about the list itself - a "all data swapped out", f.i. when we need to clear some state like selection - on completely new data the old state is meaningless.
Completely new data can be reached by
- list.setAll(...)
- list.set(otherObservableList) if list is a ListProperty
Thinking about which type of changes could be fired on setAll (c is the change, items is the observed list, "subChangeCount" pseudo-code for counting the subchanges):
// initially empty
assertEquals(0, items.size());
items.setAll(1, 2, 4);
assertEquals(1, c.subChangeCount());
assertTrue(c.wasAdded() && !c.wasReplaced());
assertEquals(0, c.getFrom());
assertEquals(c.getList().size(), c.getAddedSize());
// initially not empty
assertTrue(items.size() > 0);
items.setAll(1, 2, 4);
assertEquals(1, c.subChangeCount());
assertTrue(c.wasReplaced());
assertEquals(0, c.getFrom());
assertEquals(c.getList().size(), c.getAddedSize());
This seems to allow a utility check like:
boolean wasSetOrClearedAll(Change c) {
if (c.getList().isEmpty()) return true;
c.next();
if (c.getAddedSize() == c.getList().size()) return true;
return false;
}
In contrast, internal fx code, f.i. in listening to ComboBox' items:
while (c.next()) {
comboBox.wasSetAllCalled = comboBox.previousItemCount == c.getRemovedSize();
...
}
comboBox.previousItemCount = getItemCount();
stores the old itemCount and compare that against the current removedSize (which I'm uncomfortable with, old state gets stale far too often for my taste), nevertheless there's a good probability that I'm missing something with my approach.
Question is:
in which context would my utility method fail (and core approach would detect the setAll correctly)?