code: public static void checkIndex(int index, int size) {
      if (size < 0) {
        throw new IllegalArgumentException("Negative size: " + size);
      }
      if (index < 0 || index >= size) {
        throw new IndexOutOfBoundsException("Index out of bounds: size=" + size + ", index=" + index);
      }
    }
docs: Validates an index in an array or other container. This method throws an {@link
  IllegalArgumentException} if the size is negative and throws an {@link
  IndexOutOfBoundsException} if the index is negative or greater than or equal to the size. This
  method is similar to {@code Preconditions.checkElementIndex(int, int)} from Guava.
  
  @param index the index to validate.
  @param size the size of the array or container.