Example usage for java.lang StringIndexOutOfBoundsException StringIndexOutOfBoundsException

List of usage examples for java.lang StringIndexOutOfBoundsException StringIndexOutOfBoundsException

Introduction

In this page you can find the example usage for java.lang StringIndexOutOfBoundsException StringIndexOutOfBoundsException.

Prototype

public StringIndexOutOfBoundsException(int index) 

Source Link

Document

Constructs a new StringIndexOutOfBoundsException class with an argument indicating the illegal index.

Usage

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Deletes the character at the specified index.
 * //w  ww .  j a va  2  s . c  om
 * @see #charAt(int)
 * @see #setCharAt(int, char)
 * @param index
 *            the index to delete
 * @return this, to enable chaining
 * @throws IndexOutOfBoundsException
 *             if the index is invalid
 */
public StringCompiler deleteCharAt(int index) {
    if (index < 0 || index >= size) {
        throw new StringIndexOutOfBoundsException(index);
    }
    deleteImpl(index, index + 1, 1);
    return this;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Copies the character array into the specified array.
 * //from w  ww  . j a  v  a  2 s . c  om
 * @param startIndex
 *            first index to copy, inclusive, must be valid
 * @param endIndex
 *            last index, exclusive, must be valid
 * @param destination
 *            the destination array, must not be null or too small
 * @param destinationIndex
 *            the index to start copying in destination
 * @throws NullPointerException
 *             if the array is null
 * @throws IndexOutOfBoundsException
 *             if any index is invalid
 */
public void getChars(int startIndex, int endIndex, char destination[], int destinationIndex) {
    if (startIndex < 0) {
        throw new StringIndexOutOfBoundsException(startIndex);
    }
    if (endIndex < 0 || endIndex > length()) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (startIndex > endIndex) {
        throw new StringIndexOutOfBoundsException("end < start");
    }
    System.arraycopy(buffer, startIndex, destination, destinationIndex, endIndex - startIndex);
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Inserts part of the character array into this builder.
 * Inserting null will use the stored null text value.
 * //from  ww w . j  a va  2  s .c o  m
 * @param index
 *            the index to add at, must be valid
 * @param chars
 *            the char array to insert
 * @param offset
 *            the offset into the character array to start at, must be valid
 * @param length
 *            the length of the character array part to copy, must be
 *            positive
 * @return this, to enable chaining
 * @throws IndexOutOfBoundsException
 *             if any index is invalid
 */
public StringCompiler insert(int index, char chars[], int offset, int length) {
    validateIndex(index);
    if (chars == null) {
        return insert(index, nullText);
    }
    if (offset < 0 || offset > chars.length) {
        throw new StringIndexOutOfBoundsException("Invalid offset: " + offset);
    }
    if (length < 0 || offset + length > chars.length) {
        throw new StringIndexOutOfBoundsException("Invalid length: " + length);
    }
    if (length > 0) {
        ensureCapacity(size + length);
        System.arraycopy(buffer, index, buffer, index + length, size - index);
        System.arraycopy(chars, offset, buffer, index, length);
        size += length;
    }
    return this;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Sets the character at the specified index.
 * //w w w .  ja v a2s. co m
 * @see #charAt(int)
 * @see #deleteCharAt(int)
 * @param index
 *            the index to set
 * @param ch
 *            the new character
 * @return this, to enable chaining
 * @throws IndexOutOfBoundsException
 *             if the index is invalid
 */
public StringCompiler setCharAt(int index, char ch) {
    if (index < 0 || index >= length()) {
        throw new StringIndexOutOfBoundsException(index);
    }
    buffer[index] = ch;
    return this;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Updates the length of the builder by either dropping the last characters
 * or adding filler of Unicode zero./*from  w ww .  j av a 2  s .com*/
 * 
 * @param length
 *            the length to set to, must be zero or positive
 * @return this, to enable chaining
 * @throws IndexOutOfBoundsException
 *             if the length is negative
 */
public StringCompiler setLength(int length) {
    if (length < 0) {
        throw new StringIndexOutOfBoundsException(length);
    }
    if (length < size) {
        size = length;
    } else if (length > size) {
        ensureCapacity(length);
        int oldEnd = size;
        int newEnd = length;
        size = length;
        for (int i = oldEnd; i < newEnd; i++) {
            buffer[i] = '\0';
        }
    }
    return this;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * {@inheritDoc}//  w ww  .j ava  2  s. co  m
 * 
 * @since 3.0
 */
public CharSequence subSequence(int startIndex, int endIndex) {
    if (startIndex < 0) {
        throw new StringIndexOutOfBoundsException(startIndex);
    }
    if (endIndex > size) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (startIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - startIndex);
    }
    return substring(startIndex, endIndex);
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Validates parameters defining a single index in the builder.
 * /*from   w w  w  . j  ava 2s . co  m*/
 * @param index
 *            the index, must be valid
 * @throws IndexOutOfBoundsException
 *             if the index is invalid
 */
protected void validateIndex(int index) {
    if (index < 0 || index > size) {
        throw new StringIndexOutOfBoundsException(index);
    }
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Validates parameters defining a range of the builder.
 * /*from  w  ww  .j a  v  a  2 s  . c o m*/
 * @param startIndex
 *            the start index, inclusive, must be valid
 * @param endIndex
 *            the end index, exclusive, must be valid except
 *            that if too large it is treated as end of string
 * @return the new string
 * @throws IndexOutOfBoundsException
 *             if the index is invalid
 */
protected int validateRange(int startIndex, int endIndex) {
    if (startIndex < 0) {
        throw new StringIndexOutOfBoundsException(startIndex);
    }
    if (endIndex > size) {
        endIndex = size;
    }
    if (startIndex > endIndex) {
        throw new StringIndexOutOfBoundsException("end < start");
    }
    return endIndex;
}