RFR: 8301604: Replace Collections.unmodifiableList with List.of

John Hendrikx jhendrikx at openjdk.org
Wed Feb 1 16:57:05 UTC 2023


On Wed, 1 Feb 2023 14:36:51 GMT, Nir Lisker <nlisker at openjdk.org> wrote:

>> `List.of` is cleaner, and can slightly reduce the memory footprint for lists of one or two elements.
>> 
>> Because `List.of` can only store non-null elements, I have only replaced a few usage.
>> 
>> Can someone open an Issue on JBS for me?
>
> modules/javafx.media/src/main/java/com/sun/media/jfxmedia/Media.java line 103:
> 
>> 101:             }
>> 102:         }
>> 103:         return returnValue;
> 
> This method can be reduced to
> 
>     public List<Track> getTracks() {
>         synchronized (tracks) {
>             return tracks.isEmpty() ? null : List.copyOf(tracks);
>         }
>     }
> 
> though I find it highly questionable that it returns `null` for an empty list instead of just an empty list. There are 2 use cases of this method and both would do better with just an empty list.

Yeah, I noticed this as well right away, it is documented to do this though.  The documentation however does seem to suggest it might be possible that there are three results:

1. Tracks haven't been scanned yet -> `null`
2. Tracks have been scanned, but none where found -> empty list
3. Tracks have been scanned and some were found -> list

Whether case 2 can ever happen is unclear, but distinguishing it from the case where nothing has been scanned yet with `null` does not seem unreasonable.

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

PR: https://git.openjdk.org/jfx/pull/1012


More information about the openjfx-dev mailing list