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.framework.utils.error.PreConditions.java

/**
 * Ensures that {@code index} specifies a valid <i>position</i> in an array, list or string of
 * size {@code size}. A position index may range from zero to {@code size}, inclusive.
 *
 * @param index a user-supplied index identifying a position in an array, list or string
 * @param size  the size of that array, list or string
 * @param desc  the text to use to describe this index in an error message
 *
 * @return the value of {@code index}/* w w  w  .j a v a2  s . co m*/
 *
 * @throws IndexOutOfBoundsException if {@code index} is negative or is greater than {@code size}
 * @throws IllegalArgumentException  if {@code size} is negative
 */
public static int checkPositionIndex(int index, int size, String desc) {
    // Carefully optimized for execution by hotspot (explanatory comment above)
    if (index < 0 || index > size) {
        throw new PreConditionException(new IndexOutOfBoundsException(badPositionIndex(index, size, desc)));
    }
    return index;
}

From source file:de.openknowledge.jaxrs.versioning.conversion.CompatibilityMapper.java

private VersionPropertyValue getPropertyValue(VersionType<?> versionType, String[] pathElements, int index,
        DefaultVersionContext context) {
    String propertyName = pathElements[index];
    if (propertyName.equals("..")) {
        context = context.getParentContext();
        return getPropertyValue(versionTypeFactory.get(context.getParent().getClass()), pathElements, index + 1,
                context);//from www . jav  a 2s. co  m
    }
    int collectionIndex = -1;
    if (isCollectionProperty(propertyName)) {
        int bracketIndex = propertyName.lastIndexOf('[');
        propertyName = propertyName.substring(0, bracketIndex);
        collectionIndex = Integer
                .parseInt(pathElements[index].substring(bracketIndex + 1, pathElements[index].length() - 1));
        if (collectionIndex < 0) {
            throw new IndexOutOfBoundsException("index in " + pathElements[index] + " may not be negative");
        }
    }
    VersionProperty property = versionType.getProperty(propertyName);
    if (property == null) {
        throw new IllegalArgumentException("@MoveFrom contains unknown property " + propertyName);
    }
    if (pathElements.length == index + 1) {
        return createPropertyValue(property, collectionIndex, context);
    }
    Object value = null;
    if (isCollection(collectionIndex)) {
        CollectionElementValue collectionElementValue = new CollectionElementValue(property, collectionIndex,
                context);
        value = collectionElementValue.get();
    } else if (!property.isDefault(context.getParent())) {
        value = property.get(context.getParent());
    }
    if (value == null) {
        value = versionTypeFactory.get(property.getType()).newInstance();
    }
    return getPropertyValue(versionTypeFactory.get(property.getType()), pathElements, index + 1,
            context.getChildContext(value));
}

From source file:com.github.antoniodisanto92.swipeselector.SwipeAdapter.java

protected void selectItemAt(int position, boolean animate) {
    if (position < 0 || position >= mItems.size()) {
        throw new IndexOutOfBoundsException(
                "This SwipeSelector does " + "not have an item at position " + position + ".");
    }/*from   ww w  .  j  a  v  a2  s.  c o m*/

    mViewPager.setCurrentItem(position, animate);
}

From source file:com.kamuda.common.exception.NestableDelegate.java

/**
 * Returns the index, numbered from 0, of the first <code>Throwable</code>
 * that matches the specified type in the chain of <code>Throwable</code>s
 * held in this delegate's <code>Nestable</code> with an index greater than
 * or equal to the specified index, or -1 if the type is not found.
 *
 * @param type <code>Class</code> to be found
 * @param fromIndex the index, numbered from 0, of the starting position in
 * the chain to be searched/*www.j  ava2  s  .  c  o  m*/
 * @return index of the first occurrence of the type in the chain, or -1 if
 * the type is not found
 * @throws IndexOutOfBoundsException if the <code>fromIndex</code> argument
 * is negative or not less than the count of <code>Throwable</code>s in the
 * chain
 * @since 2.0
 */
public int indexOfThrowable(Class type, int fromIndex) {
    if (fromIndex < 0) {
        throw new IndexOutOfBoundsException("The start index was out of bounds: " + fromIndex);
    }
    Throwable[] throwables = ExceptionUtils.getThrowables(this.nestable);
    if (fromIndex >= throwables.length) {
        throw new IndexOutOfBoundsException(
                "The start index was out of bounds: " + fromIndex + " >= " + throwables.length);
    }
    for (int i = fromIndex; i < throwables.length; i++) {
        if (throwables[i].getClass().equals(type)) {
            return i;
        }
    }
    return -1;
}

From source file:IntArrayList.java

/**
 * Inserts all of the elements in the specified array into this list,
 * starting at the specified position./*w  w w . j  av a  2 s  . c  om*/
 * Shifts the element currently at that position (if any) and any subsequent
 * elements to the right (increases their indices).
 * The new elements will appear in the list in the order that they exist in
 * the array argument.
 *
 * @param index
 *      Index at which to insert first element from the specified array.
 * @param ai
 *      Elements to be inserted into this list.
 *
 * @exception IndexOutOfBoundsException
 *      If index is out of range <tt>(index &lt; 0 || index &gt; size())</tt>.
 */

public void addAll(int index, int[] ai) {
    if (index < 0 || index > size)
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);

    int numNew = ai.length;
    ensureCapacity(size + numNew);

    int numMoved = size - index;
    if (numMoved > 0)
        System.arraycopy(elements, index, elements, index + numNew, numMoved);

    System.arraycopy(ai, 0, elements, index, numNew);

    size += numNew;
}

From source file:org.apache.taverna.scufl2.api.common.Scufl2Tools.java

public ProcessorBinding processorBindingForProcessor(Processor processor, Profile profile) {
    List<ProcessorBinding> bindings = processorBindingsForProcessor(processor, profile);
    if (bindings.isEmpty())
        throw new IndexOutOfBoundsException("Could not find bindings for " + processor);
    if (bindings.size() > 1)
        throw new IllegalStateException("More than one proc binding for " + processor);
    return bindings.get(0);
}

From source file:ch.entwine.weblounge.common.impl.content.page.PageImpl.java

/**
 * {@inheritDoc}//from ww  w . j  a  v a 2 s  . c  o m
 * 
 * @see ch.entwine.weblounge.common.content.page.Page#removePagelet(java.lang.String,
 *      int)
 */
public Pagelet removePagelet(String composer, int position) {
    List<Pagelet> pagelets = composers.get(composer);

    // Test index
    if (pagelets == null || pagelets.size() < position)
        throw new IndexOutOfBoundsException("No pagelet found at position " + position);

    // Remove the pagelet and update uris of following pagelets
    Pagelet pagelet = pagelets.remove(position);
    for (int i = position; i < pagelets.size(); i++) {
        pagelets.get(i).getURI().setPosition(i);
    }
    return pagelet;
}

From source file:ArrayUtils.java

/**
 * <p>Removes the element at the specified position from the specified array.
 * All subsequent elements are shifted to the left (substracts one from
 * their indices).</p>// w w  w . jav  a 2  s .c o m
 *
 * <p>This method returns a new array with the same elements of the input
 * array except the element on the specified position. The component 
 * type of the returned array is always the same as that of the input 
 * array.</p>
 *
 * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
 * will be thrown, because in that case no valid index can be specified.</p>
 * 
 * @param array  the array to remove the element from, may not be <code>null</code>
 * @param index  the position of the element to be removed
 * @return A new array containing the existing elements except the element
 *         at the specified position.
 * @throws IndexOutOfBoundsException if the index is out of range 
 * (index < 0 || index >= array.length), or if the array is <code>null</code>.
 * @since 2.1
 */
@SuppressWarnings("unchecked")
public static <T> T[] remove(final T[] array, final int index) {
    int length = getLength(array);
    if (index < 0 || index >= length) {
        throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
    }
    Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
    System.arraycopy(array, 0, result, 0, index);
    if (index < length - 1) {
        System.arraycopy(array, index + 1, result, index, length - index - 1);
    }
    return (T[]) result;
}

From source file:com.free.exception.NestableDelegate.java

/**
 * Returns the index, numbered from 0, of the first <code>Throwable</code>
 * that matches the specified type in the chain of <code>Throwable</code>s
 * held in this delegate's <code>Nestable</code> with an index greater than or
 * equal to the specified index, or -1 if the type is not found.
 * /*  w w  w  . j a  v a  2s . co m*/
 * @param type
 *          <code>Class</code> to be found
 * @param fromIndex
 *          the index, numbered from 0, of the starting position in the chain
 *          to be searched
 * @return index of the first occurrence of the type in the chain, or -1 if
 *         the type is not found
 * @throws IndexOutOfBoundsException
 *           if the <code>fromIndex</code> argument is negative or not less
 *           than the count of <code>Throwable</code>s in the chain
 * @since 2.0
 */
public int indexOfThrowable(Class<?> type, int fromIndex) {
    if (fromIndex < 0) {
        throw new IndexOutOfBoundsException("The start index was out of bounds: " + fromIndex);
    }
    Throwable[] throwables = ExceptionHelper.getThrowables(this.nestable);
    if (fromIndex >= throwables.length) {
        throw new IndexOutOfBoundsException(
                "The start index was out of bounds: " + fromIndex + " >= " + throwables.length);
    }
    for (int i = fromIndex; i < throwables.length; i++) {
        if (throwables[i].getClass().equals(type)) {
            return i;
        }
    }
    return -1;
}

From source file:com.gc.iotools.stream.is.ChunkInputStream.java

/**
 * {@inheritDoc}./* ww w . j av a2s  . co  m*/
 */
@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
    if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
        throw new IndexOutOfBoundsException(
                "b.length[" + b.length + "] offset[" + off + "] length[" + len + "");
    } else if (len == 0) {
        return 0;
    }

    findStartMarker();
    int ret;
    if (this.copyToOuter) {
        if (this.stop.length > 0) {
            final int readSize = len - off + this.stop.length;
            final byte[] tmpBuffer = new byte[readSize];
            this.wrappedIs.mark(readSize);
            ret = StreamUtils.tryReadFully(this.wrappedIs, tmpBuffer, 0, readSize);
            this.wrappedIs.reset();
            if (ret != -1) {
                final int position = ArrayTools.indexOf(tmpBuffer, this.stop);
                if (position == -1) {
                    // stop marker not found
                    ret = Math.min(ret, len - off);
                    this.wrappedIs.skip(ret);
                    System.arraycopy(tmpBuffer, 0, b, off, ret);
                } else if (position == 0) {
                    this.wrappedIs.skip(this.stop.length);
                    this.copyToOuter = false;
                    ret = this.read(b, off, len);
                } else {
                    // position >0
                    ret = position;
                    final int bytesToSkip = position + this.stop.length;
                    this.wrappedIs.skip(bytesToSkip);
                    System.arraycopy(tmpBuffer, 0, b, off, position);
                    this.copyToOuter = false;
                }
            }
        } else {
            ret = this.wrappedIs.read(b, off, len);
        }
    } else {
        ret = -1;
    }
    return ret;
}