On Wed, 15 Oct 2025 03:40:24 GMT, John Hendrikx <[email protected]> wrote:

> This PR implements two new default methods on `ObservableList` to be able to 
> replace elements at a given position or within a specified range.
> 
> Justification for this change is to allow an `ObservableList` to be bulk 
> modified resulting in a single `ListChangeListener` call back.  In this way 
> the callbacks don't observe the list changing its size from S to S-X back to 
> S again(*). Currently the only way to bulk replace a range of items is to 
> remove X items then add X items, resulting in two listener callbacks in 
> between which the size of the list can be observed to change.
> 
> The other alternative is to call `set` individually for each item, which 
> results in many change notifications.
> 
> With the addition of this PR, and the changes in 
> `ModifiableObservableListBase`, replacing a range of items becomes a single 
> change callback.
> 
> (*) The list may indeed change size still as plain `List` does not have 
> `setAll` operations; size listeners may observe this, but it will no longer 
> be observable from a `ListChangeListener` due to multiple separate callbacks.

> How about doing this through the SubList view? JavaFX's implementation 
> already has a `SubObservableList` inner class that propagates changes on it 
> to the list's listeners:
> 
> I think that this would be more flexible/composable and result in a smaller 
> API surface (current bulk methods would also be made redundant technically).

This is the first thing I tried, but we can't change the return type of 
`ObservableList#subList`.  So you'd be forced to do a cast to get access to the 
`setAll` method which is `ObservableList` specific.  Aside from that, I don't 
know what would happen if `SubObservableList` becomes a full `ObservableList` 
(with listeners, etc) as it currently definitely does not take such use into 
account (it basically is only using a standard `List` as its input).  For sure 
that would require many tests, and I'm not looking forward to ensure that 
listening on some random sublist will behave properly in cases where the main 
list was modified (but did not include the sublist range), or when it did 
include only **part** of the sublist range (would need to transform the 
`Change` provided I think), etc...

I thought about just not allowing listeners on these sublists, but the required 
cast still is super annoying.

So, since `ObservableList` has already decided to extend the `List` interface 
with additional methods (`setAll`, `remove(int, int)`) I think doing it in the 
proposed way is much simpler and in line with what is already there.

-------------

PR Comment: https://git.openjdk.org/jfx/pull/1937#issuecomment-3406160998

Reply via email to