Request for Enhancement: Objects.checkPositionIndex(int, int)
Chen Liang
chen.l.liang at oracle.com
Tue Jan 14 00:02:47 UTC 2025
I tend to name such an item a "cursor position" as termed in ListIterator; I used the same term for Label in the ClassFile API.
I indeed think this API would be useful: List.add, List.listIterator, TypeDescriptor.OfMethod.insertParameterTypes, getting labels in a code array for class files...
________________________________
From: core-libs-dev <core-libs-dev-retn at openjdk.org> on behalf of Kevin Bourrillion <kevin.bourrillion at oracle.com>
Sent: Monday, January 13, 2025 2:19 PM
To: Glavo <zjx001202 at gmail.com>
Cc: core-libs-dev <core-libs-dev at openjdk.org>
Subject: Re: Request for Enhancement: Objects.checkPositionIndex(int, int)
Oh hey, I named those Guava methods. :-) This was my attempt to promote the terms “position indexes vs. element indexes” and hope that the idea caught on... which I’d say it still never has. I’m surprised you even noticed the methods.
The notion is that “element indexes” are the buckets and “position indexes” are the possible insertion points between buckets. I personally find it simpler to think of a method like `subList` as simply accepting two position indexes, rather than thinking about half-open ranges.
To me this was clarifying, but for people who never have this explained to them it’s probably not that clear.
And basically, the adoption counts for those methods always remained very low. And I remember seeing a number of misuses, too.
Not sure what to make of all this; just adding some context.
On Jan 13, 2025, at 11:36 AM, Glavo <zjx001202 at gmail.com> wrote:
Currently the java.util.Objects provide the method `checkIndex(int, int)` for checking indexes.
It is convenient, but cannot be used to implement methods such as List::add(int, E) because these methods accept an index pointing to the end of the collection.
So is it possible to add a new method to Objects to check this kind of index?
This is useful for implementing collections.
(The checkPositionIndex in the title is its name in guava. I don't like the name, but I don't have a better idea.)
Glavo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250114/ac868cff/attachment.htm>
More information about the core-libs-dev
mailing list