Example usage for com.google.common.base Preconditions checkElementIndex

List of usage examples for com.google.common.base Preconditions checkElementIndex

Introduction

In this page you can find the example usage for com.google.common.base Preconditions checkElementIndex.

Prototype

public static int checkElementIndex(int index, int size) 

Source Link

Document

Ensures that index specifies a valid element in an array, list or string of size size .

Usage

From source file:com.bazaarvoice.jless.ast.node.InternalNode.java

@Override
public Node removeChild(int index) {
    // remove and detach child
    Preconditions.checkElementIndex(index, _children.size());
    Node removed = _children.remove(index);
    removed.setParent(null);//from w  w  w .  ja v  a2s  . c  o m

    // notify iterators
    for (MutableChildIterator it : _childIteratorStack) {
        it.removeEvent(index);
    }

    return removed;
}

From source file:com.google.devtools.build.lib.util.LongArrayList.java

/**
 * Add certain elements from the given array at a certain position in this list.
 * @param array the array from which to take the elements
 * @param fromIndex the position of the first element to add
 * @param length how many elements to add
 * @param position at which position to add these elements, adds at the end if equal to the size
 * @return whether this list has changed
 * @throws IndexOutOfBoundsException if fromIndex and length violate the boundaries of the given
 *    array or atIndex is not a valid index in this array or equal to the size
 *///from  w  w w.  j av a 2s.co  m
public boolean addAll(long[] array, int fromIndex, int length, int position) {
    Preconditions.checkNotNull(array);
    Preconditions.checkPositionIndex(fromIndex + length, array.length);
    if (length == 0) {
        return false;
    }
    // check other positions later to allow "adding" empty arrays anywhere within this array
    Preconditions.checkElementIndex(fromIndex, array.length);
    Preconditions.checkPositionIndex(position, size);
    copyBackAndGrow(position, length);
    System.arraycopy(array, fromIndex, this.array, position, length);
    return true;
}

From source file:org.eclipse.viatra.query.runtime.localsearch.MatchingFrame.java

public boolean testAndSetValue(Integer position, Object value) {
    Preconditions.checkElementIndex(position, frame.length);
    if (frame[position] == null) {
        frame[position] = value;/*from   w  ww  . ja  v  a2 s . c  o m*/
        return true;
    } else {
        return frame[position].equals(value);
    }
}

From source file:com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Generic.GenericCommentsTableModel.java

@Override
public FormattedCharacterBuffer getLineFormatted(int rowIndex, int columnIndex, int lineIndex) {
    // Display a row that invites the user to enter a comment below the existing comments.
    Preconditions.checkElementIndex(columnIndex, columns.length);
    JCodeDisplayColumnDescription column = columns[columnIndex];
    if (rowIndex >= cachedComments.size()) {
        switch (columnIndex) {
        case USER_INDEX:
            String username = CodeDisplay.padRight("Your username ...", column.getWidth());
            return new FormattedCharacterBuffer(username, STANDARD_FONT, column.getDefaultFontColor(),
                    column.getDefaultBackgroundColor());
        case COMMENT_INDEX:
            String comment = CodeDisplay.padRight("Enter new comment ...", column.getWidth());
            return new FormattedCharacterBuffer(comment, STANDARD_FONT, column.getDefaultFontColor(),
                    column.getDefaultBackgroundColor());
        default://ww  w . j av  a2s. co m
            // Not useful, but required for style guide conformance.
            break;
        }
    }

    switch (columnIndex) {
    case USER_INDEX:
        String username = "";
        if (lineIndex == 0) {
            username = cachedComments.get(rowIndex).getUser().getUserName();
        }
        username = CodeDisplay.padRight(username, column.getWidth());
        return new FormattedCharacterBuffer(username, STANDARD_FONT, column.getDefaultFontColor(),
                column.getDefaultBackgroundColor());
    case COMMENT_INDEX:
        // Get the comment for the row in question.
        String commentLine = cachedComments.get(rowIndex).getCommentLine(lineIndex);
        commentLine = CodeDisplay.padRight(commentLine, columns[columnIndex].getWidth());
        return new FormattedCharacterBuffer(commentLine, STANDARD_FONT, column.getDefaultFontColor(),
                column.getDefaultBackgroundColor());
    }
    return null;
}

From source file:org.lenskit.data.ratings.PackedRatingDataBuilder.java

/**
 * Release the specified index. The index can then be re-used by a later call
 * to {@link #add(Preference)}./*w  ww  .ja v a  2s .com*/
 *
 * @param idx The index to remove.
 */
public void release(int idx) {
    Preconditions.checkElementIndex(idx, nprefs);
    freeList.enqueue(idx);
}

From source file:edu.byu.nlp.util.DoubleArrays.java

/**
 * Subtracts arr2 from arr1 and stores the result directly in arr1 starting at the specified positions in the
 * arrays. The subtraction is computed element-by-element, left-to-right, so care should be taken when arr1 == arr2.
 * Copying stops before the specified length would extend past the end of either array.
 * /*from  w ww  .j av a2  s.  c  o m*/
 * @throws IndexOutOfBoundsException if startIndex1 >= arr1.length or startIndex2 >= arr2.length
 * @return the actual number of elements copied
 */
public static int subtractToSelf(final double[] arr1, int startIndex1, final double[] arr2, int startIndex2,
        int length) {
    Preconditions.checkNotNull(arr1);
    Preconditions.checkNotNull(arr2);
    Preconditions.checkElementIndex(startIndex1, arr1.length);
    Preconditions.checkElementIndex(startIndex2, arr2.length);

    for (int index1 = startIndex1, index2 = startIndex2; index1 < arr1.length
            && index2 < arr2.length; index1++, index2++) {
        arr1[index1] -= arr2[index2];
    }
    return Math.min(Math.min(arr1.length - startIndex1, arr2.length - startIndex2), length);
}

From source file:ch.ge.ve.protopoc.service.algorithm.VoteCastingAuthorityAlgorithms.java

/**
 * Algorithm 7.22: CheckBallot/*from ww  w .j a v a2  s .  co  m*/
 *
 * @param i          the voter index
 * @param alpha      the submitted ballot, including the oblivious transfer query
 * @param pk         the encryption public key
 * @param bold_x_hat the vector of public voter credentials
 * @param upper_b    the current ballot list
 * @return true if the ballot was valid
 */
public boolean checkBallot(Integer i, BallotAndQuery alpha, EncryptionPublicKey pk, List<BigInteger> bold_x_hat,
        Collection<BallotEntry> upper_b) {
    Preconditions.checkNotNull(i);
    Preconditions.checkNotNull(alpha);
    List<BigInteger> bold_a = alpha.getBold_a();
    Preconditions.checkNotNull(bold_a);
    Preconditions.checkArgument(bold_a.stream().allMatch(generalAlgorithms::isMember),
            "All of the a_j's must be members of G_q");
    Preconditions.checkArgument(generalAlgorithms.isMember(alpha.getB()), "b must be a member of G_q");

    int numberOfSelections = bold_a.size();
    Preconditions.checkArgument(numberOfSelections > 0);
    Voter voter = electionSet.getVoters().get(i);
    int k_i = electionSet.getElections().stream().filter(e -> electionSet.isEligible(voter, e))
            .mapToInt(Election::getNumberOfSelections).sum();
    Preconditions.checkArgument(numberOfSelections == k_i,
            "A voter may not submit more than his allowed number of selections");
    Preconditions.checkNotNull(pk);
    Preconditions.checkNotNull(bold_x_hat);
    Preconditions.checkElementIndex(i, bold_x_hat.size());
    Preconditions.checkNotNull(upper_b);

    BigInteger p = publicParameters.getEncryptionGroup().getP();
    BigInteger x_hat_i = bold_x_hat.get(i);
    if (!hasBallot(i, upper_b) && alpha.getX_hat().compareTo(x_hat_i) == 0) {
        BigInteger a = bold_a.stream().reduce(BigInteger::multiply).orElse(ONE).mod(p);
        return checkBallotProof(alpha.getPi(), alpha.getX_hat(), a, alpha.getB(), pk);
    }
    return false;
}

From source file:com.facebook.hiveio.schema.HiveTableSchemaImpl.java

@Override
public HiveType columnType(int columnIndex) {
    Preconditions.checkElementIndex(columnIndex, hiveTypes.length);
    return hiveTypes[columnIndex];
}

From source file:com.google.devtools.build.lib.util.LongArrayList.java

/**
 * @return the element at the specified index
 * @throws IndexOutOfBoundsException if the index is outside the interval [0, {@link #size()})
 *///from w w w .ja v  a2 s.com
public long get(int index) {
    Preconditions.checkElementIndex(index, size);
    return array[index];
}

From source file:com.google.devtools.build.lib.util.LongArrayList.java

/**
 * Remove the element at the specified index and shift all elements at higher indices down by
 * one.//from w  w w .  ja va 2 s.c o  m
 * @return the removed element
 * @throws IndexOutOfBoundsException if the index is outside the interval [0, {@link #size()})
 */
public long remove(int index) {
    Preconditions.checkElementIndex(index, size);
    long previous = array[index];
    System.arraycopy(array, index + 1, array, index, size - index - 1);
    size--;
    return previous;
}