RFR 8135248: Add utility methods to check indexes and ranges
Paul Sandoz
paul.sandoz at oracle.com
Tue Sep 22 08:09:56 UTC 2015
Hi Remi,
On 22 Sep 2015, at 08:08, Remi Forax <forax at univ-mlv.fr> wrote:
> Hi Paul,
> to summarize, there are a lot of codes that do bound checking in the JDK that report different kind of exceptions.
>
Yes, and I suspect it is a similar state of affairs for code outside of the JDK too.
> A way to retrofit most of then is to use a lambda to let the user code to choose its convention.
> But because lambda creations are backed by invokedynamic, you can not retrofit
> code that is used before the method handle mechanism is set up during the bootstraping of the JDK.
>
We can retrofit, but we will not use lambda expressions or method refs in those cases.
> There are several kinds of bound checking, and i see no gain to force them to use the same function type.
> Also, I have use a wildcard instead of a type variable given that it's an unchecked exception for the compiler.
>
> For the simplest form of bound checking, 0 <= index < length, aka checkIndex, because there is only one index to report, i propose:
> public static void checkIndex(int index, int length, IntFunction<? extends RuntimeException> exceptionCreator)
>
I disagree, we need BiFunction. I have eyeballed a sufficient number of use-cases that report index and length in the exception message.
> For the form of that uses a size, 0 <= index < size <= length, aka checkFromIndexSize, as you said, let's use a BiFunction<Integer, Integer, ...>,
> public static void checkRange(int index, int size, int length, BiFunction<Integer, Integer, ? extends RuntimeException> exceptionCreator)
>
> For the form that uses an offset, 0 <= index <= index + offset < length,
> public static void checkRangeWithOffset(int index, int offset, int length, BiFunction<Integer, Integer, ? extends RuntimeException> exceptionCreator)
>
> Also, the documentation should be clear that if a lambda captures values from the context, the performance will be worst than inlining the check by hand.
>
Yes, i can write some api note. FWIW for the sub-range checks it probably matters less as those are often followed by some bulk operation.
Paul.
More information about the core-libs-dev
mailing list