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.google.uzaygezen.core.BitSetBackedBitVector.java

private void checkIndex(int bitIndex) {
    if (bitIndex < 0 | bitIndex >= size) {
        throw new IndexOutOfBoundsException("BitIndex should be smaller than size : " + bitIndex);
    }/* w ww . j  a  v  a2 s  . c o  m*/
}

From source file:Main.java

/**
 * Parse a date from ISO-8601 formatted string. It expects a format
 * yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
 *
 * @param date ISO string to parse in the appropriate format.
 * @return the parsed date//from   www.  j av a 2  s .  c o  m
 * @throws IllegalArgumentException if the date is not in the appropriate format
 */
public static Date parse(String date) {
    try {
        int offset = 0;

        // extract year
        int year = parseInt(date, offset, offset += 4);
        checkOffset(date, offset, '-');

        // extract month
        int month = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, '-');

        // extract day
        int day = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, 'T');

        // extract hours, minutes, seconds and milliseconds
        int hour = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int minutes = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int seconds = parseInt(date, offset += 1, offset += 2);
        // milliseconds can be optional in the format
        // always use 0 otherwise returned date will include millis of current time
        int milliseconds = 0;

        if (date.charAt(offset) == '.') {
            checkOffset(date, offset, '.');
            int digitCount = 1;
            while (offset + digitCount < date.length() && digitCount < 3
                    && date.charAt(offset + 1 + digitCount) != 'Z'
                    && date.charAt(offset + 1 + digitCount) != '+'
                    && date.charAt(offset + 1 + digitCount) != '-') {
                digitCount++;
            }
            String msString = date.substring(offset += 1, offset += digitCount);
            while (msString.length() < 3) {
                msString += '0';
            }
            milliseconds = parseInt(msString, 0, 3);
        }

        // extract timezone
        String timezoneId = null;
        while (offset < date.length()) {
            char timezoneIndicator = date.charAt(offset);
            if (timezoneIndicator == '+' || timezoneIndicator == '-') {
                timezoneId = GMT_ID + date.substring(offset);
                break;
            } else if (timezoneIndicator == 'Z') {
                timezoneId = GMT_ID;
                break;
            }
            offset++;
        }
        if (timezoneId == null) {
            throw new IndexOutOfBoundsException("Invalid time zone indicator ");
        }
        TimeZone timezone = TimeZone.getTimeZone(timezoneId);
        if (!timezone.getID().equals(timezoneId)) {
            throw new IndexOutOfBoundsException();
        }

        Calendar calendar = new GregorianCalendar(timezone);
        calendar.setLenient(false);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, seconds);
        calendar.set(Calendar.MILLISECOND, milliseconds);

        return calendar.getTime();
    } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    }
}

From source file:libepg.epg.util.datetime.DateTimeFieldConverter.java

/**
 * ?yyyymmdd????? 1900 3 1 ?2100 2 28 ??????
 *
 * @param mjd mjd??(16)//  www . j a  v  a  2 s. c  o  m
 * @return yyyymmdd??
 */
public static synchronized String mjdToString(byte[] mjd) {
    if (mjd.length != 2) {
        throw new IndexOutOfBoundsException(
                "?????2????????");
    }

    if (Arrays.equals(mjd, UNDEFINED_MJD_DATE_BLOCK.getData())) {
        throw new IllegalArgumentException("???????????");
    }
    int tnum = ByteConverter.bytesToInt(mjd);
    int yy = (int) ((tnum - 15078.2) / 365.25);
    int mm = (int) (((tnum - 14956.1) - (int) (yy * 365.25)) / 30.6001);
    int dd = (tnum - 14956) - (int) (yy * 365.25) - (int) (mm * 30.6001);

    if (mm == 14 || mm == 15) {
        yy += 1;
        mm = mm - 1 - (1 * 12);
    } else {
        mm = mm - 1;
    }
    yy += 1900;
    if (LOG.isTraceEnabled()) {
        LOG.trace("yy=" + yy + " mm=" + mm + " dd=" + dd);
    }

    StringBuilder sb = new StringBuilder();
    sb.append(yy);
    sb.append(ZERO_FILL_FORMAT.format(mm));
    sb.append(ZERO_FILL_FORMAT.format(dd));
    return sb.toString();
}

From source file:net.signalr.client.json.jackson.JacksonElement.java

@Override
public JsonElement get(final int index) {
    if (!_node.isArray()) {
        throw new IndexOutOfBoundsException(String.valueOf(index));
    }/*from   www  . j a va  2 s .  c o  m*/

    // Returns: Node that represent value of the specified element,
    // if this node is an array and has specified element.
    // Null otherwise.
    final JsonNode node = _node.get(index);

    if (node == null) {
        throw new IndexOutOfBoundsException(String.valueOf(index));
    }

    return new JacksonElement(_mapper, node);
}

From source file:ru.jts_dev.common.packets.IncomingMessageWrapper.java

public final int readShort() {
    if (payload.readableBytes() < Short.BYTES)
        throw new IndexOutOfBoundsException("At least 2 bytes must be readable in payload");

    return payload.readShort();
}

From source file:com.jabyftw.lobstercraft.world.ChunkLocation.java

/**
 * We are getting an array of chunks centered on the given chunk location:<br>
 * A C C<br>//w ww.j a  v  a 2 s. co m
 * C X C<br>
 * C C B<br>
 * This would be an example with range sized at 1:<br>
 * A (X.x - range, X.z - range)<br>
 * B (X.x + range, X.z + range)
 *
 * @param range the range of chunks, must be greater than 0
 * @return list of chunks, including the centered one
 */
public Set<ChunkLocation> getNearChunks(int range) {
    // Check if the range is valid
    if (range < 1)
        throw new IndexOutOfBoundsException("Range must be greater than zero!");
    HashSet<ChunkLocation> chunkLocations = new HashSet<>(Util.getNumberOfChunksAround(range));

    // As we can see in the documentation, we will get a chunk from A to B
    for (int dx = -range; dx <= range; dx++) {
        for (int dz = -range; dz <= range; dz++) {
            chunkLocations.add(new ChunkLocation(worldId, chunkX + dx, chunkZ + dz));
        }
    }

    return chunkLocations;
}

From source file:net.pkhsolutions.pecsapp.model.PageModel.java

@NotNull
public PictureModel getPictureModel(int col, int row) {
    if (col < 0 || col > layout.getValue().getColumns()) {
        throw new IndexOutOfBoundsException("Invalid column index");
    }//from   w w w.  j ava2 s . co  m
    if (row < 0 || row > layout.getValue().getRows()) {
        throw new IndexOutOfBoundsException("Invalid row index");
    }

    Pair<Integer, Integer> coordinates = Pair.with(col, row);
    PictureModel model = pictureModelMap.get(coordinates);
    if (model == null) {
        model = new PictureModel(this, pictureService);
        pictureModelMap.put(coordinates, model);
    }
    return model;
}

From source file:ListOfLists.java

/**
 * Replaces the element at the specified position in underlying list with the
 * specified element./*  w w w  . j  a va2s.c o m*/
 *
 * @param index index of element to replace.
 * @param element element to be stored at the specified position.
 * @return the element previously at the specified position.
 */
public Object set(int index, Object element) {
    int size = size();
    if (index < 0)
        throw new IndexOutOfBoundsException("index: " + index + "; size: " + size);

    Iterator it = lists.iterator();
    while (it.hasNext()) {
        List list = (List) it.next();
        if (index < list.size()) {
            return list.set(index, element);
        } else
            index -= list.size();
    }

    // if value has not been returned yet - IndexOutOfBoundsException is thrown
    throw new IndexOutOfBoundsException("index: " + index + "; size: " + size);
}

From source file:ru.jts_dev.common.packets.IncomingMessageWrapper.java

public final int readInt() {
    if (payload.readableBytes() < Integer.BYTES)
        throw new IndexOutOfBoundsException("At least 4 bytes must be readable in payload");

    return payload.readInt();
}

From source file:de.cosmocode.json.JsonArrayList.java

@Override
public Object set(int index, Object element) {
    if (index < 0 || index >= size())
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
    if (element == null)
        throw new NullPointerException("Element must not be null");
    try {//w  w w .  j  a  v  a  2  s .  co  m
        final Object oldValue = get(index);
        array.put(index, element);
        return oldValue;
    } catch (JSONException e) {
        throw new IllegalArgumentException(e);
    }
}