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.github.haixing_hu.csl.util.Argument.java

public static int requireIndexInOpenRange(final int index, final int left, final int right) {
    if ((index <= left) || (index >= right)) {
        throw new IndexOutOfBoundsException(
                "The index must in the open range (" + left + ", " + right + "), " + " but it is " + index);
    }//from w  ww.j a va  2 s .com
    return index;
}

From source file:com.github.haixing_hu.csl.util.Argument.java

public static int requireIndexInLeftOpenRange(final int index, final int left, final int right) {
    if ((index <= left) || (index > right)) {
        throw new IndexOutOfBoundsException("The index must in the left open range (" + left + ", " + right
                + "], " + " but it is " + index);
    }//from  w ww.  jav a 2s .co  m
    return index;
}

From source file:com.github.haixing_hu.csl.util.Argument.java

public static int requireIndexInRightOpenRange(final int index, final int left, final int right) {
    if ((index < left) || (index >= right)) {
        throw new IndexOutOfBoundsException("The index must in the right open range [" + left + ", " + right
                + "), " + " but it is " + index);
    }//from w  w  w .  j  a  va2  s .com
    return index;
}

From source file:com.clust4j.utils.MatUtils.java

/**
 * Set the column within a matrix in place.
 * @param a//from   w  w w .  j  a  v a 2s  .c  o m
 * @param idx
 * @param v
 * @throws IllegalArgumentException if there are no rows in the matrix
 * @throws NonUniformMatrixException if the matrix is not uniform
 * @throws IndexOutOfBoundsException if idx is less than 0 or greater than the col dims
 * @throws DimensionMismatchException if the dimensions of v do not match row dims of the matrix
 */
public static void setColumnInPlace(final double[][] a, final int idx, final double[] v) {
    checkDimsForUniformity(a);

    final int m = a.length, n = a[0].length;
    if (idx < 0 || idx >= n)
        throw new IndexOutOfBoundsException("illegal idx: " + idx);
    if (v.length != m)
        throw new DimensionMismatchException(m, v.length);

    for (int i = 0; i < m; i++)
        a[i][idx] = v[i];
}

From source file:com.clust4j.utils.MatUtils.java

/**
 * Set the row within a matrix in place/*from   ww  w .ja  v  a2s  .  c  o  m*/
 * @param a
 * @param idx
 * @param v
 * @throws IllegalArgumentException if there are no rows in the matrix
 * @throws IndexOutOfBoundsException if idx is less than 0 or greater than row dims
 * @throws DimensionMismatchException if the dims of v do not match col dims of the matrix
 */
public static void setRowInPlace(final double[][] a, final int idx, final double[] v) {
    checkDimsPermitEmpty(a);

    final int m = a.length;
    if (idx < 0 || idx >= m)
        throw new IndexOutOfBoundsException("illegal idx: " + idx);

    final int n = a[idx].length;
    if (v.length != n)
        throw new DimensionMismatchException(n, v.length);

    for (int i = 0; i < n; i++)
        a[idx][i] = v[i];
}

From source file:mondrian.olap.Util.java

/**
 * Returns the sole item in a list.//from  ww  w. jav a 2  s.c  o  m
 *
 * <p>If the list has 0 or more than one element, throws.</p>
 *
 * @param list List
 * @param <T> Element type
 * @return Sole item in the list
 * @throws IndexOutOfBoundsException if list is empty or has more than 1 elt
 */
public static <T> T only(List<T> list) {
    if (list.size() != 1) {
        throw new IndexOutOfBoundsException("list " + list + " has " + list.size() + " elements, expected 1");
    }
    return list.get(0);
}

From source file:com.xwtec.xwserver.util.json.JSONArray.java

public ListIterator listIterator(int index) {
    if (index < 0 || index > size())
        throw new IndexOutOfBoundsException("Index: " + index);

    return new JSONArrayListIterator(index);
}

From source file:hybridewah.HybridBitmap.java

/**
 * set the bit at position i to true, the bits must be set in increasing
 * order. For example, set(15) and then set(7) will fail. You must do set(7)
 * and then set(15)./*from w ww  . j av a  2s  .c om*/
 * 
 * @param i
 *          the index
 * @return true if the value was set (always true when i>= sizeInBits()).
 * @throws IndexOutOfBoundsException
 *           if i is negative or greater than Integer.MAX_VALUE - 64
 */
public boolean set(final int i) {
    if ((i > Integer.MAX_VALUE - wordinbits) || (i < 0))
        throw new IndexOutOfBoundsException(
                "Set values should be between 0 and " + (Integer.MAX_VALUE - wordinbits));
    if (i < this.sizeinbits)
        return false;
    // distance in words:
    final int dist = (i + wordinbits) / wordinbits - (this.sizeinbits + wordinbits - 1) / wordinbits;
    this.sizeinbits = i + 1;
    if (dist > 0) {// easy
        if (dist > 1)
            fastaddStreamOfEmptyWords(false, dist - 1);
        addLiteralWord(1l << (i % wordinbits));
        return true;
    }
    if (this.rlw.getNumberOfLiteralWords() == 0) {
        this.rlw.setRunningLength(this.rlw.getRunningLength() - 1);
        addLiteralWord(1l << (i % wordinbits));
        return true;
    }
    this.buffer[this.actualsizeinwords - 1] |= 1l << (i % wordinbits);
    if (this.buffer[this.actualsizeinwords - 1] == ~0l) {
        this.buffer[this.actualsizeinwords - 1] = 0;
        --this.actualsizeinwords;
        this.rlw.setNumberOfLiteralWords(this.rlw.getNumberOfLiteralWords() - 1);
        // next we add one clean word
        addEmptyWord(true);
    }
    return true;
}

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

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

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

/**
 * <p>//from w  ww.  jav a2 s  .c  o  m
 * Validates that the index is within the bounds of the argument collection;
 * otherwise throwing an exception with the specified message.
 * </p>
 * 
 * <pre>
 * Validate.validIndex(myCollection, 2, &quot;The collection index is invalid: &quot;);
 * </pre>
 * 
 * <p>
 * If the collection is <code>null</code>, then the message of the exception
 * is &quot;The validated object is null&quot;.
 * </p>
 * 
 * @param <T>
 *            the collection type
 * @param collection
 *            the collection to check
 * @param index
 *            the index
 * @param message
 *            the exception message if invalid
 * @return the validated collection (never <code>null</code> for chaining)
 * @throws NullPointerException
 *             if the collection is <code>null</code>
 * @throws IndexOutOfBoundsException
 *             if the index is invalid
 * @see #validIndex(Collection, int)
 */
public static <T extends Collection<?>> T validIndex(T collection, int index, String message,
        Object... values) {
    notNull(collection);
    if (index < 0 || index >= collection.size()) {
        throw new IndexOutOfBoundsException(String.format(message, values));
    }
    return collection;
}