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:de.koethnig.sudoku.State.java

/**
 * Creates a new state and initializes the restrictions of the cell using
 * the given Base64 encoded string.<br/><br/>
 *
 * @param size//from w  ww.j  a  v a  2  s . c  o  m
 *         The size of the sudoku.
 * @param base64
 *         The Base64 encoded state.
 *
 * @return a state with the given Base64 encoded restrictions.
 *
 * @throws ExcludeException
 *         if the restrictions described by the base64-string contains
 *         cells without candidates left.
 *
 * @throws IndexOutOfBoundsException
 *         if the Base64 string encodes a wrong byte size.
 */
public static State createStateFromBase64(final SudokuSize size, final String base64) throws ExcludeException {
    final int ssize = size.size;
    final int qsize = ssize * ssize;
    final int bits = qsize * qsize * qsize;
    final int bytes = (bits + NUM_BITS_PER_BYTE - 1) / NUM_BITS_PER_BYTE;
    final byte[] byteArray = Base64.decodeBase64(base64);
    if (byteArray.length != bytes)
        throw new IndexOutOfBoundsException("Invalid size of Base64 string.");
    final State result = new State(size);
    int byteIndex = 0;
    int bitIndex = NUM_BITS_PER_BYTE - 1;
    for (final Cell cell : result.getCells()) {
        for (int val = 0; val < qsize; val++) {
            if ((byteArray[byteIndex] & 1 << bitIndex) == 0)
                cell.exclude(val);
            bitIndex--;
            if (bitIndex == -1) {
                bitIndex = NUM_BITS_PER_BYTE - 1;
                byteIndex++;
            }
        }
    }
    return result;
}

From source file:eu.stratosphere.types.StringValue.java

/**
 * Sets the value of the StringValue to a substring of the given string.
 * // ww w  . jav  a  2 s .co  m
 * @param value The new string value.
 * @param offset The position to start the substring.
 * @param len The length of the substring.
 */
public void setValue(CharSequence value, int offset, int len) {
    Validate.notNull(value);
    if (offset < 0 || len < 0 || offset > value.length() - len) {
        throw new IndexOutOfBoundsException("offset: " + offset + " len: " + len + " value.len: " + len);
    }

    ensureSize(len);
    this.len = len;
    for (int i = 0; i < len; i++) {
        this.value[i] = value.charAt(offset + i);
    }
    this.len = len;
    this.hashCode = 0;
}

From source file:br.msf.commons.util.CollectionUtils.java

/**
 * Returns the <code>index</code>-th value in {@link Iterator}, throwing
 * <code>IndexOutOfBoundsException</code> if there is no such element.
 * <p>/*from  w  ww. j a v a  2  s  .  co  m*/
 * The Iterator is advanced to <code>index</code> (or to the end, if
 * <code>index</code> exceeds the number of entries) as a side effect of this method.
 *
 * @param iterator  the iterator to get a value from
 * @param index  the index to get
 * @param <T> the type of object in the {@link Iterator}
 * @return the object at the specified index
 * @throws IndexOutOfBoundsException if the index is invalid
 * @throws IllegalArgumentException if the object type is invalid
 */
public static <T> T get(final Iterator<T> iterator, final int index) {
    int i = index;
    ArgumentUtils.rejectIfLessThan(index, 0);
    while (iterator.hasNext()) {
        i--;
        if (i == -1) {
            return iterator.next();
        }
        iterator.next();
    }
    throw new IndexOutOfBoundsException("Entry does not exist: " + i);
}

From source file:de.huberlin.cuneiform.dag.Invocation.java

public Resolveable getResolveable(int outputChannel, int idx) {

    if (idx < 0)
        throw new IndexOutOfBoundsException("Index must not be smaller than 0.");

    return new ResolvedInvocationReference(this, outputChannel, idx);
}

From source file:Main.java

/**
 * Returns the <code>index</code>-th value in <code>object</code>, throwing
 * <code>IndexOutOfBoundsException</code> if there is no such element or
 * <code>IllegalArgumentException</code> if <code>object</code> is not an
 * instance of one of the supported types.
 * <p>/*from   w ww  . j  a va  2s .  c  o m*/
 * The supported types, and associated semantics are:
 * <ul>
 * <li>Map -- the value returned is the <code>Map.Entry</code> in position
 * <code>index</code> in the map's <code>entrySet</code> iterator, if there
 * is such an entry.</li>
 * <li>List -- this method is equivalent to the list's get method.</li>
 * <li>Array -- the <code>index</code>-th array entry is returned, if there
 * is such an entry; otherwise an <code>IndexOutOfBoundsException</code> is
 * thrown.</li>
 * <li>Collection -- the value returned is the <code>index</code>-th object
 * returned by the collection's default iterator, if there is such an
 * element.</li>
 * <li>Iterator or Enumeration -- the value returned is the
 * <code>index</code>-th object in the Iterator/Enumeration, if there is
 * such an element. The Iterator/Enumeration is advanced to
 * <code>index</code> (or to the end, if <code>index</code> exceeds the
 * number of entries) as a side effect of this method.</li>
 * </ul>
 * 
 * @param object
 *            the object to get a value from
 * @param index
 *            the index to get
 * @return the object at the specified index
 * @throws IndexOutOfBoundsException
 *             if the index is invalid
 * @throws IllegalArgumentException
 *             if the object type is invalid
 */
public static Object get(Object object, int index) {
    if (index < 0) {
        throw new IndexOutOfBoundsException("Index cannot be negative: " + index);
    }
    if (object instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) object;
        Iterator<?> iterator = map.entrySet().iterator();
        return get(iterator, index);
    } else if (object instanceof List) {
        return ((List<?>) object).get(index);
    } else if (object instanceof Object[]) {
        return ((Object[]) object)[index];
    } else if (object instanceof Iterator) {
        Iterator<?> it = (Iterator<?>) object;
        while (it.hasNext()) {
            index--;
            if (index == -1) {
                return it.next();
            } else {
                it.next();
            }
        }
        throw new IndexOutOfBoundsException("Entry does not exist: " + index);
    } else if (object instanceof Collection) {
        Iterator<?> iterator = ((Collection<?>) object).iterator();
        return get(iterator, index);
    } else if (object instanceof Enumeration) {
        Enumeration<?> it = (Enumeration<?>) object;
        while (it.hasMoreElements()) {
            index--;
            if (index == -1) {
                return it.nextElement();
            } else {
                it.nextElement();
            }
        }
        throw new IndexOutOfBoundsException("Entry does not exist: " + index);
    } else if (object == null) {
        throw new IllegalArgumentException("Unsupported object type: null");
    } else {
        try {
            return Array.get(object, index);
        } catch (IllegalArgumentException ex) {
            throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
        }
    }
}

From source file:aldenjava.opticalmapping.data.data.DataNode.java

/**
 * Returns the segment length excluding the length of signals
 * /*from   w ww.  ja  v  a  2s  .co m*/
 * @param index
 * @return The segment length
 */
public long getRefl(int index) {
    if (index > refp.length || index < 0)
        throw new IndexOutOfBoundsException("Segment index is out of bound");
    if (index == 0 && refp.length == 0)
        return size;
    if (index == refp.length)
        return size - refp[refp.length - 1];
    if (index == 0)
        return refp[index] - 1;
    return refp[index] - refp[index - 1] - 1;

}

From source file:com.netxforge.oss2.xml.event.Events.java

/**
 * //from   w ww. j av  a 2 s .co  m
 * 
 * @param index
 * @param vEvent
 * @throws java.lang.IndexOutOfBoundsException if the index
 * given is outside the bounds of the collection
 */
public void setEvent(final int index, final com.netxforge.oss2.xml.event.Event vEvent)
        throws java.lang.IndexOutOfBoundsException {
    // check bounds for index
    if (index < 0 || index >= this._eventList.size()) {
        throw new IndexOutOfBoundsException(
                "setEvent: Index value '" + index + "' not in range [0.." + (this._eventList.size() - 1) + "]");
    }

    this._eventList.set(index, vEvent);
}

From source file:ArrayUtils.java

/**
 * <p>Inserts the specified element at the specified position in the array. 
 * Shifts the element currently at that position (if any) and any subsequent
 * elements to the right (adds one to their indices).</p>
 *
 * <p>This method returns a new array with the same elements of the input
 * array plus the given element on the specified position. The component 
 * type of the returned array is always the same as that of the input 
 * array.</p>//w w w.  j  a v a 2  s.  c o m
 *
 * <p>If the input array is <code>null</code>, a new one element array is returned
 *  whose component type is the same as the element.</p>
 * 
 * <pre>
 * ArrayUtils.insert(null, 0, null)      = [null]
 * ArrayUtils.insert(null, 0, "a")       = ["a"]
 * ArrayUtils.insert(["a"], 1, null)     = ["a", null]
 * ArrayUtils.insert(["a"], 1, "b")      = ["a", "b"]
 * ArrayUtils.insert(["a", "b"], 3, "c") = ["a", "b", "c"]
 * </pre>
 * 
 * @param array  the array to add the element to, may be <code>null</code>
 * @param index  the position of the new object
 * @param element  the object to add
 * @return A new array containing the existing elements and the new element
 * @throws IndexOutOfBoundsException if the index is out of range 
 * (index < 0 || index > array.length).
 */
@SuppressWarnings("unchecked")
public static <T> T[] insert(final Object array, final int index, final Object element) {
    if (array == null) {
        if (index != 0) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0");
        }
        Object joinedArray = Array.newInstance(element != null ? element.getClass() : Object.class, 1);
        Array.set(joinedArray, 0, element);
        return (T[]) joinedArray;
    }
    int length = getLength(array);
    if (index > length || index < 0) {
        throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
    }
    Object result = Array.newInstance(array.getClass().getComponentType(), length + 1);
    System.arraycopy(array, 0, result, 0, index);
    Array.set(result, index, element);
    if (index < length) {
        System.arraycopy(array, index, result, index + 1, length - index);
    }
    return (T[]) result;
}

From source file:com.netxforge.oss2.xml.event.Parms.java

/**
 * /*ww  w.  j  av a  2 s  .c o m*/
 * 
 * @param index
 * @param vParm
 * @throws java.lang.IndexOutOfBoundsException if the index
 * given is outside the bounds of the collection
 */
public void setParm(final int index, final com.netxforge.oss2.xml.event.Parm vParm)
        throws java.lang.IndexOutOfBoundsException {
    // check bounds for index
    if (index < 0 || index >= this._parmList.size()) {
        throw new IndexOutOfBoundsException(
                "setParm: Index value '" + index + "' not in range [0.." + (this._parmList.size() - 1) + "]");
    }

    this._parmList.set(index, vParm);
}

From source file:io.horizondb.io.files.DirectFileDataInput.java

/**
 * Reload data in the buffer if it does not contains the specified amount of bytes to read.
 * /*from  w ww  .  j a  va2  s  .c  om*/
 * @param numberOfBytes the number of bytes.
 * @throws IOException if a problem occurs while reading data.
 */
private void readDataIfNeeded(long numberOfBytes) throws IOException {

    int remaining = this.buffer.remaining();

    if (remaining < numberOfBytes) {

        int numberOfBytesRead = readData();

        if ((remaining + numberOfBytesRead) < numberOfBytes) {

            throw new IndexOutOfBoundsException(
                    "Index: " + (getPosition() + numberOfBytes) + ", Size: " + size());
        }
    }
}