Example usage for java.lang ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException

List of usage examples for java.lang ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException

Introduction

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

Prototype

public ArrayIndexOutOfBoundsException(int index) 

Source Link

Document

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

Usage

From source file:com.clust4j.utils.VecUtils.java

public static double[] slice(final double[] a, final int startInc, final int endExc) {
    checkDims(a);//from w  ww  .  j a  va  2  s.co  m

    if (endExc > a.length)
        throw new ArrayIndexOutOfBoundsException(endExc);
    if (startInc < 0 || startInc > a.length)
        throw new ArrayIndexOutOfBoundsException(startInc);
    if (startInc > endExc)
        throw new IllegalArgumentException("start index cannot exceed end index");
    if (startInc == endExc)
        return new double[] {};

    final double[] out = new double[endExc - startInc];
    for (int i = startInc, j = 0; i < endExc; i++, j++)
        out[j] = a[i];

    return out;
}

From source file:com.clust4j.utils.VecUtils.java

public static int[] slice(final int[] a, final int startInc, final int endExc) {
    checkDims(a);/*  w  ww  .  j a  va2s  .  c om*/

    if (endExc > a.length)
        throw new ArrayIndexOutOfBoundsException(endExc);
    if (startInc < 0 || startInc > a.length)
        throw new ArrayIndexOutOfBoundsException(startInc);
    if (startInc > endExc)
        throw new IllegalArgumentException("start index cannot exceed end index");
    if (startInc == endExc)
        return new int[] {};

    final int[] out = new int[endExc - startInc];
    for (int i = startInc, j = 0; i < endExc; i++, j++)
        out[j] = a[i];

    return out;
}

From source file:xtremweb.common.XWPropertyDefs.java

/**
 * This retrieves a property from its name as found in config file This is
 * for backward compatibility only// w w  w. j  a  va2  s .  c o  m
 *
 * @param s
 *            is the property name
 * @return the property from its property name
 * @exception ArrayIndexOutOfBoundsException
 *                if no property can be found given the property name
 * @since 8.2.0
 */
public static XWPropertyDefs fromString(final String s) throws ArrayIndexOutOfBoundsException {

    for (final XWPropertyDefs p : XWPropertyDefs.values()) {
        if (s.compareToIgnoreCase(p.propertyName()) == 0) {
            return p;
        }
    }

    throw new ArrayIndexOutOfBoundsException("unknown property : " + s);
}

From source file:org.eclipse.dataset.AbstractDataset.java

private static void throwAIOOBException(int i, int s, int d) {
    throw new ArrayIndexOutOfBoundsException(
            "Index (" + i + ") out of range [-" + s + "," + s + "] in dimension " + d);
}

From source file:uk.ac.diamond.scisoft.analysis.dataset.AbstractDataset.java

/**
 * Check the position given against the shape to make sure it is valid and sanitise it
 * //  w w w. j  ava  2 s  . co m
 * @param pos
 * @return boolean
 */
protected boolean isPositionInShape(final int... pos) {
    int pmax = pos.length;

    // check the dimensionality of the request
    if (pmax > shape.length) {
        throw new IllegalArgumentException();
    }

    // if it's the right size or less, check to see if it's within bounds
    for (int i = 0; i < pmax; i++) {
        final int si = shape[i];
        if (pos[i] < 0) {
            pos[i] += si;
        }
        if (pos[i] < 0) {
            throw new ArrayIndexOutOfBoundsException(
                    "Index (" + pos[i] + ") out of range [-" + si + "," + si + ") in dimension " + i);
        }
        if (pos[i] >= si) {
            if (extendible) {
                return false;
            }
            throw new ArrayIndexOutOfBoundsException(
                    "Index (" + pos[i] + ") out of range [-" + si + "," + si + ") in dimension " + i);
        }
    }

    return true;
}

From source file:com.clust4j.utils.MatUtils.java

/**
 * Slice the matrix row-wise from a start index (inclusive) to an end index
 * (exclusive), and return a copy of the rows.
 * @param a//ww w.  j a  v a 2  s . c o  m
 * @param startInc
 * @param endExc
 * @throws ArrayIndexOutOfBoundsException if the end index is greater than the matrix length
 * or if the start index is less than 0
 * @throws IllegalArgumentException if the rows are empty or if the start index exceeds the end index
 * @return
 */
public static double[][] slice(final double[][] a, final int startInc, final int endExc) {
    checkDims(a);

    if (endExc > a.length)
        throw new ArrayIndexOutOfBoundsException(endExc);
    if (startInc < 0 || startInc > a.length)
        throw new ArrayIndexOutOfBoundsException(startInc);
    if (startInc > endExc)
        throw new IllegalArgumentException("start index cannot exceed end index");
    if (startInc == endExc)
        return new double[][] {};

    final double[][] out = new double[endExc - startInc][];
    for (int i = startInc, j = 0; i < endExc; i++, j++)
        out[j] = VecUtils.copy(a[i]);

    return out;
}

From source file:org.apache.hadoop.hbase.regionserver.wal.HLogSplitter.java

/**
 * This function is used to construct mutations from a WALEntry. It also reconstructs HLogKey &
 * WALEdit from the passed in WALEntry/*www  .j  av  a2 s. c  o  m*/
 * @param entry
 * @param cells
 * @param logEntry pair of HLogKey and WALEdit instance stores HLogKey and WALEdit instances
 *          extracted from the passed in WALEntry.
 * @param addLogReplayTag
 * @return list of Pair<MutationType, Mutation> to be replayed
 * @throws IOException
 */
public static List<MutationReplay> getMutationsFromWALEntry(WALEntry entry, CellScanner cells,
        Pair<HLogKey, WALEdit> logEntry, boolean addLogReplayTag) throws IOException {

    if (entry == null) {
        // return an empty array
        return new ArrayList<MutationReplay>();
    }

    int count = entry.getAssociatedCellCount();
    List<MutationReplay> mutations = new ArrayList<MutationReplay>();
    Cell previousCell = null;
    Mutation m = null;
    HLogKey key = null;
    WALEdit val = null;
    if (logEntry != null)
        val = new WALEdit();

    for (int i = 0; i < count; i++) {
        // Throw index out of bounds if our cell count is off
        if (!cells.advance()) {
            throw new ArrayIndexOutOfBoundsException("Expected=" + count + ", index=" + i);
        }
        Cell cell = cells.current();
        if (val != null)
            val.add(KeyValueUtil.ensureKeyValue(cell));

        boolean isNewRowOrType = previousCell == null || previousCell.getTypeByte() != cell.getTypeByte()
                || !CellUtil.matchingRow(previousCell, cell);
        if (isNewRowOrType) {
            // Create new mutation
            if (CellUtil.isDelete(cell)) {
                m = new Delete(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                // Deletes don't have nonces.
                mutations.add(
                        new MutationReplay(MutationType.DELETE, m, HConstants.NO_NONCE, HConstants.NO_NONCE));
            } else {
                m = new Put(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                // Puts might come from increment or append, thus we need nonces.
                long nonceGroup = entry.getKey().hasNonceGroup() ? entry.getKey().getNonceGroup()
                        : HConstants.NO_NONCE;
                long nonce = entry.getKey().hasNonce() ? entry.getKey().getNonce() : HConstants.NO_NONCE;
                mutations.add(new MutationReplay(MutationType.PUT, m, nonceGroup, nonce));
            }
        }
        if (CellUtil.isDelete(cell)) {
            ((Delete) m).addDeleteMarker(KeyValueUtil.ensureKeyValue(cell));
        } else {
            Cell tmpNewCell = cell;
            if (addLogReplayTag) {
                tmpNewCell = tagReplayLogSequenceNumber(entry, cell);
            }
            ((Put) m).add(KeyValueUtil.ensureKeyValue(tmpNewCell));
        }
        previousCell = cell;
    }

    // reconstruct HLogKey
    if (logEntry != null) {
        WALKey walKey = entry.getKey();
        List<UUID> clusterIds = new ArrayList<UUID>(walKey.getClusterIdsCount());
        for (HBaseProtos.UUID uuid : entry.getKey().getClusterIdsList()) {
            clusterIds.add(new UUID(uuid.getMostSigBits(), uuid.getLeastSigBits()));
        }
        key = new HLogKey(walKey.getEncodedRegionName().toByteArray(),
                TableName.valueOf(walKey.getTableName().toByteArray()), walKey.getLogSequenceNumber(),
                walKey.getWriteTime(), clusterIds, walKey.getNonceGroup(), walKey.getNonce());
        logEntry.setFirst(key);
        logEntry.setSecond(val);
    }

    return mutations;
}

From source file:org.eclipse.january.dataset.DatasetUtils.java

/**
 * Choose content from choices where condition is true, otherwise use default. All inputs are broadcasted to a maximum shape
 * @param index integer dataset (ideally, items should be in [0, n) range, if there are n choices)
 * @param choices array of datasets or objects
 * @param throwAIOOBE if true, throw array index out of bound exception
 * @param clip true to clip else wrap indices out of bounds; only used when throwAOOBE is false
 * @return dataset/*  w  w  w . j av  a  2  s  . c o  m*/
 */
public static Dataset choose(IntegerDataset index, Object[] choices, boolean throwAIOOBE, boolean clip) {
    final int n = choices.length;
    Object[] all = new Object[n + 1];
    System.arraycopy(choices, 0, all, 0, n);
    all[n] = index;
    Dataset[] dChoices = BroadcastUtils.convertAndBroadcast(all);
    int dt = -1;
    int ds = -1;
    int mr = -1;
    for (int i = 0; i < n; i++) {
        Dataset a = dChoices[i];
        int r = a.getRank();
        if (r > mr)
            mr = r;
        int t = a.getDType();
        if (t > dt)
            dt = t;
        int s = a.getElementsPerItem();
        if (s > ds)
            ds = s;
    }
    if (dt < 0 || ds < 1) {
        throw new IllegalArgumentException("Dataset types of choices are invalid");
    }
    index = (IntegerDataset) dChoices[n];
    dChoices[n] = null;

    @SuppressWarnings("deprecation")
    Dataset r = DatasetFactory.zeros(ds, index.getShape(), dt);
    IndexIterator iter = index.getIterator(true);
    final int[] pos = iter.getPos();
    int i = 0;
    while (iter.hasNext()) {
        int j = index.getAbs(iter.index);
        if (j < 0) {
            if (throwAIOOBE)
                throw new ArrayIndexOutOfBoundsException(j);
            if (clip) {
                j = 0;
            } else {
                j %= n;
                j += n; // as remainder still negative
            }
        }
        if (j >= n) {
            if (throwAIOOBE)
                throw new ArrayIndexOutOfBoundsException(j);
            if (clip) {
                j = n - 1;
            } else {
                j %= n;
            }
        }
        Dataset c = dChoices[j];
        r.setObjectAbs(i++, c.getObject(pos));
    }
    return r;
}

From source file:org.eclipse.january.dataset.DatasetUtils.java

/**
 * Calculate indexes in given shape from datasets of position
 * @param positions as a list of datasets where each holds the position in a dimension
 * @param shape//from   ww  w .ja v  a 2 s.  c om
 * @param mode either null, zero-length, unit length or length of rank of shape where
 *  0 = raise exception, 1 = wrap, 2 = clip
 * @return indexes as an integer dataset
 */
public static IntegerDataset calcIndexesFromPositions(List<? extends Dataset> positions, int[] shape,
        int... mode) {
    int rank = shape.length;
    if (positions.size() != rank) {
        throw new IllegalArgumentException("Number of position datasets must be equal to rank of shape");
    }

    if (mode == null || mode.length == 0) {
        mode = new int[rank];
    } else if (mode.length == 1) {
        int m = mode[0];
        mode = new int[rank];
        Arrays.fill(mode, m);
    } else if (mode.length != rank) {
        throw new IllegalArgumentException("Mode length greater than one must match rank of shape");
    }
    for (int i = 0; i < rank; i++) {
        int m = mode[i];
        if (m < 0 || m > 2) {
            throw new IllegalArgumentException("Unknown mode value - it must be 0, 1, or 2");
        }
    }

    Dataset p = positions.get(0);
    IntegerDataset indexes = new IntegerDataset(p.getShapeRef());
    IndexIterator it = p.getIterator(true);
    int[] iPos = it.getPos();
    int[] tPos = new int[rank];
    while (it.hasNext()) {
        for (int i = 0; i < rank; i++) {
            p = positions.get(i);
            int j = p.getInt(iPos);
            int d = shape[i];
            if (mode[i] == 0) {
                if (j < 0 || j >= d) {
                    throw new ArrayIndexOutOfBoundsException("Position value exceeds dimension in shape");
                }
            } else if (mode[i] == 1) {
                while (j < 0)
                    j += d;
                while (j >= d)
                    j -= d;
            } else {
                if (j < 0)
                    j = 0;
                if (j >= d)
                    j = d - 1;
            }
            tPos[i] = j;
        }
        indexes.set(ShapeUtils.getFlat1DIndex(shape, tPos), iPos);
    }

    return indexes;
}

From source file:org.apache.hadoop.hbase.wal.WALSplitter.java

/**
 * This function is used to construct mutations from a WALEntry. It also
 * reconstructs WALKey &amp; WALEdit from the passed in WALEntry
 * @param entry//from   w  w w  . j a  v  a  2 s.  co  m
 * @param cells
 * @param logEntry pair of WALKey and WALEdit instance stores WALKey and WALEdit instances
 *          extracted from the passed in WALEntry.
 * @return list of Pair&lt;MutationType, Mutation&gt; to be replayed
 * @throws IOException
 */
public static List<MutationReplay> getMutationsFromWALEntry(WALEntry entry, CellScanner cells,
        Pair<WALKey, WALEdit> logEntry, Durability durability) throws IOException {

    if (entry == null) {
        // return an empty array
        return new ArrayList<MutationReplay>();
    }

    long replaySeqId = (entry.getKey().hasOrigSequenceNumber()) ? entry.getKey().getOrigSequenceNumber()
            : entry.getKey().getLogSequenceNumber();
    int count = entry.getAssociatedCellCount();
    List<MutationReplay> mutations = new ArrayList<MutationReplay>();
    Cell previousCell = null;
    Mutation m = null;
    WALKey key = null;
    WALEdit val = null;
    if (logEntry != null)
        val = new WALEdit();

    for (int i = 0; i < count; i++) {
        // Throw index out of bounds if our cell count is off
        if (!cells.advance()) {
            throw new ArrayIndexOutOfBoundsException("Expected=" + count + ", index=" + i);
        }
        Cell cell = cells.current();
        if (val != null)
            val.add(cell);

        boolean isNewRowOrType = previousCell == null || previousCell.getTypeByte() != cell.getTypeByte()
                || !CellUtil.matchingRow(previousCell, cell);
        if (isNewRowOrType) {
            // Create new mutation
            if (CellUtil.isDelete(cell)) {
                m = new Delete(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                // Deletes don't have nonces.
                mutations.add(
                        new MutationReplay(MutationType.DELETE, m, HConstants.NO_NONCE, HConstants.NO_NONCE));
            } else {
                m = new Put(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                // Puts might come from increment or append, thus we need nonces.
                long nonceGroup = entry.getKey().hasNonceGroup() ? entry.getKey().getNonceGroup()
                        : HConstants.NO_NONCE;
                long nonce = entry.getKey().hasNonce() ? entry.getKey().getNonce() : HConstants.NO_NONCE;
                mutations.add(new MutationReplay(MutationType.PUT, m, nonceGroup, nonce));
            }
        }
        if (CellUtil.isDelete(cell)) {
            ((Delete) m).addDeleteMarker(cell);
        } else {
            ((Put) m).add(cell);
        }
        m.setDurability(durability);
        previousCell = cell;
    }

    // reconstruct WALKey
    if (logEntry != null) {
        org.apache.hadoop.hbase.protobuf.generated.WALProtos.WALKey walKeyProto = entry.getKey();
        List<UUID> clusterIds = new ArrayList<UUID>(walKeyProto.getClusterIdsCount());
        for (HBaseProtos.UUID uuid : entry.getKey().getClusterIdsList()) {
            clusterIds.add(new UUID(uuid.getMostSigBits(), uuid.getLeastSigBits()));
        }
        // we use HLogKey here instead of WALKey directly to support legacy coprocessors.
        key = new HLogKey(walKeyProto.getEncodedRegionName().toByteArray(),
                TableName.valueOf(walKeyProto.getTableName().toByteArray()), replaySeqId,
                walKeyProto.getWriteTime(), clusterIds, walKeyProto.getNonceGroup(), walKeyProto.getNonce());
        logEntry.setFirst(key);
        logEntry.setSecond(val);
    }

    return mutations;
}