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:Main.java

/**
 * Removes multiple array elements specified by index.
 * @param array source// w  w  w . j  av  a 2  s  .  c  om
 * @param indices to remove, WILL BE SORTED--so only clones of user-owned arrays!
 * @return new array of same type minus elements specified by unique values of {@code indices}
 * @since 3.0.1
 */
private static Object removeAll(Object array, int... indices) {
    int length = getLength(array);
    int diff = 0;

    if (isNotEmpty(indices)) {
        Arrays.sort(indices);

        int i = indices.length;
        int prevIndex = length;
        while (--i >= 0) {
            int index = indices[i];
            if (index < 0 || index >= length) {
                throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
            }
            if (index >= prevIndex) {
                continue;
            }
            diff++;
            prevIndex = index;
        }
    }
    Object result = Array.newInstance(array.getClass().getComponentType(), length - diff);
    if (diff < length) {
        int end = length;
        int dest = length - diff;
        for (int i = indices.length - 1; i >= 0; i--) {
            int index = indices[i];
            if (end - index > 1) {
                int cp = end - index - 1;
                dest -= cp;
                System.arraycopy(array, index + 1, result, dest, cp);
            }
            end = index;
        }
        if (end > 0) {
            System.arraycopy(array, 0, result, 0, end);
        }
    }
    return result;
}

From source file:com.netflix.config.ConcurrentCompositeConfiguration.java

/**
 * Change the position of the <em>container configuration</em> to a new index.
 * //from  w w w . j a  va  2 s .  co m
 * @throws IndexOutOfBoundsException
 */
public void setContainerConfigurationIndex(int newIndex) throws IndexOutOfBoundsException {
    if (newIndex < 0 || newIndex >= configList.size()) {
        throw new IndexOutOfBoundsException(
                "Cannot change to the new index " + newIndex + " in the list of size " + configList.size());
    } else if (newIndex == configList.indexOf(containerConfiguration)) {
        // nothing to do
        return;
    }

    containerConfigurationChanged = true;
    configList.remove(containerConfiguration);
    configList.add(newIndex, containerConfiguration);
}

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

/**
 * Checks that the specified amount of bytes can be read.
 * //from ww  w . j  a  v  a2  s.  c om
 * @param numberOfBytes the number of bytes to read.
 * @throws IllegalArgumentException if the specified number of bytes cannot be read.
 */
private void checkReadable(int numberOfBytes) {

    int readableBytes = readableBytes();

    if (numberOfBytes > readableBytes) {

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

        throw new IndexOutOfBoundsException(msg);
    }
}

From source file:com.community.yuequ.bottombar.BottomBar.java

/**
 * Select a tab at the specified position.
 *
 * @param position the position to select.
 *///  www.j a  v  a  2  s .c om
public void selectTabAtPosition(int position) {
    if (position > getTabCount() - 1 || position < 0) {
        throw new IndexOutOfBoundsException(
                "Can't select tab at position " + position + ". This BottomBar has no items at that position.");
    }

    selectTabAtPosition(position, false);
}

From source file:edu.chalmers.dat255.audiobookplayer.model.Book.java

/**
 * Throws an IndexOutOfBoundsException if the given index is not legal.
 * //from w w  w  .j  ava2 s  .  c o m
 * @param trackIndex
 *            The given index.
 */
private void checkTrackIndexLegal(int trackIndex) {
    if (!isLegalTrackIndex(trackIndex)) {
        throw new IndexOutOfBoundsException("Track index is illegal: " + trackIndex);
    }
}

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

public static String toUpperCase(String str, int position) {
    if (CommonUtils.isEmpty(str))
        throw new NullPointerException("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");
    }//  ww w  .j  a v a2 s  .  co  m

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

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

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

/**
 * Checks that the specified amount of bytes can be written.
 * //w  ww.  ja v a 2 s.  co m
 * @param numberOfBytes the number of bytes to write.
 * @throws IllegalArgumentException if the specified number of bytes cannot be written.
 */
private void checkWriteable(int numberOfBytes) {

    int writeableBytes = writeableBytes();

    if (numberOfBytes > writeableBytes) {

        @SuppressWarnings("boxing")
        String msg = format("the number of bytes to write (%d) exceed the number of writeable bytes (%d)",
                numberOfBytes, writeableBytes);

        throw new IndexOutOfBoundsException(msg);
    }
}

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

/**
 * Method getAutoaction.//from www .  ja v  a 2s.c om
 * 
 * @param index
 * @throws IndexOutOfBoundsException
 *             if the index given is outside the bounds of the collection
 * @return the value of the Autoaction at the
 *         given index
 */
public Autoaction getAutoaction(final int index) throws IndexOutOfBoundsException {
    // check bounds for index
    if (index < 0 || index >= _autoactionList.size()) {
        throw new IndexOutOfBoundsException("getAutoaction: Index value '" + index + "' not in range [0.."
                + (_autoactionList.size() - 1) + "]");
    }

    return _autoactionList.get(index);
}

From source file:com.google.uzaygezen.core.LongBitVector.java

@Override
public void copySectionFrom(int offset, BitVector src) {
    int srcSize = src.size();
    int toIndex = offset + srcSize;
    if (offset < 0 | toIndex > size) {
        throw new IndexOutOfBoundsException("invalid range: offset=" + offset + " src.size()=" + src.size());
    }/* w ww  .  j a  va  2  s.  c om*/
    if (offset != toIndex) {
        unsafeClearNonEmptySection(offset, toIndex);
        long srcData = src.toExactLong();
        data |= srcData << offset;
    }
}

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

/**
 * Select a tab at the specified position.
 *
 * @param position the position to select.
 */// ww  w .j  a v a  2 s . c o  m
public void selectTabAtPosition(int position, boolean animate) {
    if (mItems == null || mItems.length == 0) {
        throw new UnsupportedOperationException(
                "Can't select tab at " + "position " + position + ". This BottomBar has no items set yet.");
    } else if (position > mItems.length - 1 || position < 0) {
        throw new IndexOutOfBoundsException(
                "Can't select tab at position " + position + ". This BottomBar has no items at that position.");
    }

    unselectTab(mItemContainer.findViewWithTag(TAG_BOTTOM_BAR_VIEW_ACTIVE), animate);
    selectTab(mItemContainer.getChildAt(position), animate);

    updateSelectedTab(position);
}