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.iyonger.apm.web.util.Preconditions.java

/**
 * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size {@code size}. An
 * element index may range from zero, inclusive, to {@code size}, exclusive.
 *
 * @param index a user-supplied index identifying an element of 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}/*from  ww  w .j  a  v  a2  s .c  om*/
 */
public static int checkElementIndex(int index, int size, @Nullable String desc) {
    // Carefully optimized for execution by hotspot (explanatory comment
    // above)
    if (index < 0 || index >= size) {
        throw new IndexOutOfBoundsException(buildBadElementIndexMessage(index, size, desc));
    }
    return index;
}

From source file:io.horizondb.io.buffers.CompositeBuffer.java

/**
 * Checks that the specified amount of bytes can be read.
 * //from www. j  ava2 s .  c om
 * @param numberOfBytes the number of bytes to read.
 * @throws IndexOutOfBoundsException if the specified amount of bytes cannot be read.
 */
private void checkReadable(int numberOfBytes) {

    if (readableBytes() < numberOfBytes) {

        @SuppressWarnings("boxing")
        String msg = format("bytes to read: %d readable bytes: %d", numberOfBytes, readableBytes());

        throw new IndexOutOfBoundsException(msg);
    }
}

From source file:org.opencron.common.utils.StringUtils.java

public static String toUpperCase(String str, int index, int len) {
    if (CommonUtils.isEmpty(str))
        throw new NullPointerException("str can not be empty??");
    if (index <= 0 || (index + len - 1) > str.length()) {
        throw new IndexOutOfBoundsException(
                "Position must be greater than 0 and not less than the length of the string to be processed");
    }/*from  w  ww . j  a  v  a 2  s.  c o  m*/

    if (index == 1) {//??
        return str.substring(0, len).toUpperCase() + str.substring(len);
    }
    return str.substring(0, index - 1) + str.substring(index - 1, len + 1).toUpperCase()
            + str.substring(index + len - 1);
}

From source file:com.cerema.cloud2.ui.activity.Uploader.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // click on folder in the list
    Log_OC.d(TAG, "on item click");
    // TODO Enable when "On Device" is recovered ?
    Vector<OCFile> tmpfiles = getStorageManager().getFolderContent(mFile /*, false*/);
    if (tmpfiles.size() <= 0)
        return;// w w w .  j a va 2s.  co  m
    // filter on dirtype
    Vector<OCFile> files = new Vector<OCFile>();
    for (OCFile f : tmpfiles)
        files.add(f);
    if (files.size() < position) {
        throw new IndexOutOfBoundsException("Incorrect item selected");
    }
    if (files.get(position).isFolder()) {
        OCFile folderToEnter = files.get(position);
        startSyncFolderOperation(folderToEnter);
        mParents.push(folderToEnter.getFileName());
        populateDirectoryList();
    }
}

From source file:CopyOnWriteArrayList.java

/**
 * Inserts the specified element at the specified position in this
 * list. Shifts the element currently at that position (if any) and
 * any subsequent elements to the right (adds one to their indices).
 *
 * @throws IndexOutOfBoundsException {@inheritDoc}
 *///from w  w w . j  a va2 s  .c o m
public synchronized void add(int index, Object element) {
    Object[] elements = getArray();
    int len = elements.length;
    if (index > len || index < 0)
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + len);
    Object[] newElements;
    int numMoved = len - index;
    if (numMoved == 0)
        newElements = copyOf(elements, len + 1);
    else {
        newElements = new Object[len + 1];
        System.arraycopy(elements, 0, newElements, 0, index);
        System.arraycopy(elements, index, newElements, index + 1, numMoved);
    }
    newElements[index] = element;
    setArray(newElements);
}

From source file:net.grinder.util.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.
 * // w  w w.j  a v a2  s  . co m
 * @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}
 */
public static int checkPositionIndex(int index, int size, @Nullable String desc) {
    // Carefully optimized for execution by hotspot (explanatory comment
    // above)
    if (index < 0 || index > size) {
        throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc));
    }
    return index;
}

From source file:bottombar.BottomBar.java

/**
 * Select a tab at the specified position.
 *
 * @param position the position to select.
 * @param animate should the tab change be animated or not.
 *//*  ww w  .  j a  va  2 s  .  c om*/
public void selectTabAtPosition(int position, boolean animate) {
    if (position > getTabCount() - 1 || position < 0) {
        throw new IndexOutOfBoundsException(
                "Can't select tab at position " + position + ". This BottomBar has no items at that position.");
    }

    BottomBarTab oldTab = getCurrentTab();
    BottomBarTab newTab = getTabAtPosition(position);

    oldTab.deselect(animate);
    newTab.select(animate);

    updateSelectedTab(position);
    shiftingMagic(oldTab, newTab, animate);
    handleBackgroundColorChange(newTab, animate);
}

From source file:com.example.ray.firstapp.bottombar.BottomBar.java

/**
 * Sets the default tab for this BottomBar that is shown until the user changes
 * the selection./* w w w  .ja v a2 s.  c  o m*/
 *
 * @param defaultTabPosition the default tab position.
 */
public void setDefaultTabPosition(int defaultTabPosition) {
    if (mItems == null || mItems.length == 0) {
        throw new UnsupportedOperationException("Can't set default tab at " + "position " + defaultTabPosition
                + ". This BottomBar has no items set yet.");
    } else if (defaultTabPosition > mItems.length - 1 || defaultTabPosition < 0) {
        throw new IndexOutOfBoundsException("Can't set default tab at position " + defaultTabPosition
                + ". This BottomBar has no items at that position.");
    }

    if (!mIsComingFromRestoredState) {
        selectTabAtPosition(defaultTabPosition, false);
    }
}

From source file:com.amazon.sqs.javamessaging.message.SQSBytesMessage.java

/**
 * Reads a portion of the bytes message stream.
 * <P>/*w w  w  . jav  a  2  s  . c om*/
 * If the length of array value is less than the number of bytes remaining
 * to be read from the stream, the array should be filled. A subsequent call
 * reads the next increment, and so on.
 * <P>
 * If the number of bytes remaining in the stream is less than the length of
 * array value, the bytes should be read into the array. The return value of
 * the total number of bytes read will be less than the length of the array,
 * indicating that there are no more bytes left to be read from the stream.
 * The next read of the stream returns -1.
 * <P>
 * If length is negative, then an <code>IndexOutOfBoundsException</code> is
 * thrown. No bytes will be read from the stream for this exception case.
 * 
 * @param value
 *            The buffer into which the data is read
 * @param length
 *            The number of bytes to read; must be less than or equal to
 *            value.length
 * @return the total number of bytes read into the buffer, or -1 if there is
 *         no more data because the end of the stream has been reached
 * @throws JMSException
 *             If the JMS provider fails to read the message due to some
 *             internal error.
 * @throws MessageNotReadableException
 *             If the message is in write-only mode.
 */
@Override
public int readBytes(byte[] value, int length) throws JMSException {
    if (length < 0) {
        throw new IndexOutOfBoundsException("Length bytes to read can't be smaller than 0 but was " + length);
    }
    checkCanRead();
    try {
        /**
         * Almost copy of readFully implementation except that EOFException
         * is not thrown if the stream is at the end of file and no byte is
         * available
         */
        int n = 0;
        while (n < length) {
            int count = dataIn.read(value, n, length - n);
            if (count < 0) {
                break;
            }
            n += count;
        }
        /**
         * JMS specification mentions that the next read of the stream
         * returns -1 if the previous read consumed the byte stream and
         * there are no more bytes left to be read from the stream
         */
        if (n == 0 && length > 0) {
            n = -1;
        }
        return n;
    } catch (IOException e) {
        throw convertExceptionToJMSException(e);
    }
}

From source file:org.opencron.common.utils.StringUtils.java

public static String toLowerCase(String str, int position) {
    AssertUtils.notNull(str, "str can not be empty??");
    if (position <= 0 || position > str.length()) {
        throw new IndexOutOfBoundsException(
                "Position must be greater than 0 and not less than the length of the string to be processed");
    }//from   ww  w. j a va 2  s  .c  o m

    if (position == 1) {//??
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    return str.substring(0, position - 1) + str.substring(position - 1, position).toLowerCase()
            + str.substring(position);
}