RFR: 8266571: Sequenced Collections

ExE Boss duke at openjdk.org
Mon Mar 20 23:45:05 UTC 2023


On Thu, 3 Nov 2022 01:56:05 GMT, Stuart Marks <smarks at openjdk.org> wrote:

>> src/java.base/share/classes/java/util/LinkedHashMap.java line 384:
>> 
>>> 382:             return this.put(k, v);
>>> 383:         } finally {
>>> 384:             putMode = PUT_NORM;
>> 
>> @stuart-marks Would it be an alternative to have an  `internalPut(mode, k, v)` so there is no need to have an internal variable which needs to be read/written multiple time per operation? 🤔
>
> Yeah, the coupling here is rather distasteful. (Otherwise known as a quick and dirty hack.) Unfortunately the coupling between HashMap and LinkedHashMap is pretty special-purposed for exactly the intended usage modes (insertion-order and access-order). It could be improved, but it would probably require some refactoring in HashMap, which I didn't want to do right now, in order to keep the sequenced stuff separate.

Another issue are custom `LinkedHashMap` subclasses which override `put(…)` to do input validation, which would get bypassed by `internalPut(…)`. A better solution to a private field would be to use [JEP 429: Scoped values].

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

That said, using `internalPut(…)` for the case where `getClass() == LinkedHashMap.class` would probably be fine.

[JEP 429: Scoped values]: https://openjdk.org/jeps/429

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

PR Review Comment: https://git.openjdk.org/jdk/pull/7387#discussion_r1012574601


More information about the core-libs-dev mailing list