Example usage for java.lang IndexOutOfBoundsException IndexOutOfBoundsException

List of usage examples for java.lang IndexOutOfBoundsException IndexOutOfBoundsException

Introduction

In this page you can find the example usage for java.lang IndexOutOfBoundsException IndexOutOfBoundsException.

Prototype

public IndexOutOfBoundsException(int index) 

Source Link

Document

Constructs a new IndexOutOfBoundsException class with an argument indicating the illegal index.

Usage

From source file:com.clark.func.Functions.java

/**
 * <p>//from w w  w  . j  a v  a2s. c o m
 * Validates that the index is within the bounds of the argument character
 * sequence; otherwise throwing an exception with the specified message.
 * </p>
 * 
 * <pre>
 * Validate.validIndex(myStr, 2, &quot;The string index is invalid: &quot;);
 * </pre>
 * 
 * <p>
 * If the character sequence is <code>null</code>, then the message of the
 * exception is &quot;The validated object is null&quot;.
 * </p>
 * 
 * @param <T>
 *            the character sequence type
 * @param chars
 *            the character sequence to check
 * @param index
 *            the index
 * @param message
 *            the exception message if invalid
 * @return the validated character sequence (never <code>null</code> for
 *         method chaining)
 * @throws NullPointerException
 *             if the character sequence is <code>null</code>
 * @throws IndexOutOfBoundsException
 *             if the index is invalid
 * @see #validIndex(CharSequence, int)
 */
public static <T extends CharSequence> T validIndex(T chars, int index, String message, Object... values) {
    notNull(chars);
    if (index < 0 || index >= chars.length()) {
        throw new IndexOutOfBoundsException(String.format(message, values));
    }
    return chars;
}