我在OpenJdk中发现了另一个与该更改相关的票证。如前所述
显式编写边界检查并不难,但很容易犯一些微不足道的错误,例如引入溢出错误。从正确性和安全性/完整性的角度合并此类检查是有利的。此外,在某些情况下,这是一个机会,通过内在的,某些检查进行优化,并引导热点进行无符号比较。
对 Java 平台的增强将允许在大于 int 值的最小和最大范围的边界上优化循环,要求对长值进行边界检查。
在外部内存访问 API (JEP 393) 中,内存段的边界表示为长值。由于目前尚未优化涉及 long 的绑定检查,因此外部内存访问 API 的实现不得不采用几种技巧来衡量内存段是否可以被视为“小”(例如,其大小适合 int 值),然后相应地在小段上使用 int 操作。虽然在大多数情况下,这些解决方法隐藏在API实现中,但它们在复杂性和长期维护方面增加了显着的成本。
解决方案 重载在 java.util.对象中定义的现有 int 接受边界检查方法,这些对象具有长接受边界检查方法。
以下静态方法被添加到 java.util.Objects 中。该规范与具有相同方法名称的现有 int 接受边界检查方法的规范相同。
/**
* Checks if the {@code index} is within the bounds of the range from
* {@code 0} (inclusive) to {@code length} (exclusive).
*
* <p>The {@code index} is defined to be out of bounds if any of the
* following inequalities is true:
* <ul>
* <li>{@code index < 0}</li>
* <li>{@code index >= length}</li>
* <li>{@code length < 0}, which is implied from the former inequalities</li>
* </ul>
*
* @param index the index
* @param length the upper-bound (exclusive) of the range
* @return {@code index} if it is within bounds of the range
* @throws IndexOutOfBoundsException if the {@code index} is out of bounds
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* @since 16
*/
public static
long checkIndex(long index, long length)
/**
* Checks if the sub-range from {@code fromIndex} (inclusive) to
* {@code toIndex} (exclusive) is within the bounds of range from {@code 0}
* (inclusive) to {@code length} (exclusive).
*
* <p>The sub-range is defined to be out of bounds if any of the following
* inequalities is true:
* <ul>
* <li>{@code fromIndex < 0}</li>
* <li>{@code fromIndex > toIndex}</li>
* <li>{@code toIndex > length}</li>
* <li>{@code length < 0}, which is implied from the former inequalities</li>
* </ul>
*
* @param fromIndex the lower-bound (inclusive) of the sub-range
* @param toIndex the upper-bound (exclusive) of the sub-range
* @param length the upper-bound (exclusive) the range
* @return {@code fromIndex} if the sub-range within bounds of the range
* @throws IndexOutOfBoundsException if the sub-range is out of bounds
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* @since 16
*/
public static
long checkFromToIndex(long fromIndex, long toIndex, long length)
/**
* Checks if the sub-range from {@code fromIndex} (inclusive) to
* {@code fromIndex + size} (exclusive) is within the bounds of range from
* {@code 0} (inclusive) to {@code length} (exclusive).
*
* <p>The sub-range is defined to be out of bounds if any of the following
* inequalities is true:
* <ul>
* <li>{@code fromIndex < 0}</li>
* <li>{@code size < 0}</li>
* <li>{@code fromIndex + size > length}, taking into account integer overflow</li>
* <li>{@code length < 0}, which is implied from the former inequalities</li>
* </ul>
*
* @param fromIndex the lower-bound (inclusive) of the sub-interval
* @param size the size of the sub-range
* @param length the upper-bound (exclusive) of the range
* @return {@code fromIndex} if the sub-range within bounds of the range
* @throws IndexOutOfBoundsException if the sub-range is out of bounds
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* @since 16
*/
public static
long checkFromIndexSize(long fromIndex, long size, long length)
以下构造函数被添加到 java.lang.IndexOutOfBoundsException:
/**
* Constructs a new {@code IndexOutOfBoundsException} class with an
* argument indicating the illegal index.
*
* <p>The index is included in this exception's detail message. The
* exact presentation format of the detail message is unspecified.
*
* @param index the illegal index.
* @since 16
*/
public IndexOutOfBoundsException(long index)
Jira 问题:添加实用工具方法来检查长索引和范围