RFR: 8091429: ObservableList<E>#setAll(int from, int to, Collection<? extends E> col)
John Hendrikx
jhendrikx at openjdk.org
Wed Oct 15 14:41:18 UTC 2025
On Wed, 15 Oct 2025 13:00:14 GMT, Michael Strauß <mstrauss at openjdk.org> wrote:
>> Initially, I only wanted to support a "pure" `set` function (to avoid a `set(int)` loop + notifications), where you can have a range of elements replaced by another range of the exact same size (so an `int` and `Collection` is enough to define this). However, there was an open ticket to have a variant `(from, to, Collection)` which is more powerful, but is not truly a "set" function anymore IMHO.
>>
>> I figured I could implement both, but I think having only `set(int, Collection)` would be sufficient for most use cases.
>>
>> I suppose it is possible to relax this, but it would have a bit of weird usage I think:
>>
>> List.of(A, B, C, D).setAll(1, List.of(E, F, G)) -> A, E, F, G
>> List.of(A, B, C, D).setAll(2, List.of(E, F, G)) -> A, B, E, F, G
>> List.of(A, B, C, D).setAll(3, List.of(E, F, G)) -> A, B, C, E, F, G
>>
>> Now it can also extend the collection, which looks weird. The other `setAll(from, to, col)` does indeed allow this, but perhaps it shouldn't be called `setAll` as it is not strictly doing what a loop of `set` calls normally could be doing...
>>
>> Perhaps I shouldn't have attempted to reuse an old ticket, and just keep it simple with only a `setAll(int, Collection)`...
>
> You're restricting functionality because you perceive the usage to be weird. No other bulk operation does this without technical necessity. Now, to me it doesn't look weird, but that's besides the point. I don't think a general-purpose API should police subjective weirdness.
>
> As far as naming is concerned, the `setAll(int, int, Collection)` overload might also be called `replace` or `replaceRange`.
I was restricting it because "set" does not allow you to add an element either when giving `size()` as the index. I only belatedly realized that `setAll(int, int, Collection)` is violating this, and allows to add elements and so changes the list size.
Naming is really important, so calling the range method `setAll` then is I think a poor choice. So the possible methods we could introduce are:
- `setAll(int, Collection)` and disallow it to change size; this method is a combination of `setAll(Collection)` and `set(int, T)` similar to how `addAll(int, Collection)` is a combination of `add(int, T)` and `addAll(Collection)`.
- `replaceAll(int, Collection)` similar to the above, but allows adding of elements if it runs out of elements to update
- `replaceAll(int, int, Collection)` a proper name for `setAll(int, int, Collection)` (`splice` was also suggested in the ticket).
For me I'd either go with:
- Have `setAll(int, Collection)`
- Have both `setAll(int, Collection)` and `replaceAll(int, int, Collection)`
The `replaceAll(int, Collection)` I think does not carry its weight, and would just be confusing given that there would be a much more clear variant `replaceAll(int, int, Collection)`.
-------------
PR Review Comment: https://git.openjdk.org/jfx/pull/1937#discussion_r2432834214
More information about the openjfx-dev
mailing list