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, @Nullable String desc) 

Source Link

Document

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

Usage

From source file:ch.ge.ve.protopoc.service.protocol.DefaultBulletinBoard.java

@Override
public List<Encryption> getPreviousShuffle(int j) {
    Preconditions.checkElementIndex(j, publicParameters.getS(), "j needs to be within bounds");
    Preconditions.checkArgument(shuffles.containsKey(j), "Can't retrieve a shuffle that hasn't been inserted");
    return shuffles.get(j);
}

From source file:ch.ge.ve.protopoc.service.protocol.DefaultBulletinBoard.java

@Override
public void publishPartialDecryptionAndProof(int j, List<BigInteger> partialDecryption, DecryptionProof proof) {
    Preconditions.checkElementIndex(j, publicParameters.getS(), "j needs to be within bounds");
    Preconditions.checkState(shuffles.size() == publicParameters.getS(),
            "The decryptions may only start when all the shuffles have been published");
    Preconditions.checkState(shuffleProofs.size() == publicParameters.getS(),
            "The decryptions may only start when all the shuffles proofs have been published");
    Preconditions.checkArgument(!partialDecryptions.containsKey(j), "Partial decryptions may not be updated");
    Preconditions.checkArgument(!decryptionProofs.containsKey(j),
            "Partial decryptions proofs may not be updated");
    partialDecryptions.put(j, partialDecryption);
    decryptionProofs.put(j, proof);/*www . ja va  2  s .  c o m*/
}

From source file:org.joda.collect.grid.DenseGrid.java

@Override
public List<V> row(int row) {
    Preconditions.checkElementIndex(row, rowCount(), "Row index");
    int base = row * rowCount;
    return new Inner<V>(this, base, columnCount, 1);
}

From source file:org.joda.collect.grid.DenseGrid.java

@Override
public List<V> column(int column) {
    Preconditions.checkElementIndex(column, columnCount(), "Column index");
    return new Inner<V>(this, column, rowCount, columnCount);
}

From source file:gobblin.source.extractor.extract.kafka.KafkaExtractor.java

protected KafkaPartition getCurrentPartition() {
    Preconditions.checkElementIndex(this.currentPartitionIdx, this.partitions.size(),
            "KafkaExtractor has finished extracting all partitions. There's no current partition.");
    return this.partitions.get(this.currentPartitionIdx);
}

From source file:org.codice.ddf.libs.klv.data.Klv.java

/**
 * Sets the key according to the key found in the byte array
 * and of the given length. If <tt>keyLength</tt> is different
 * than what was previously set for this KLV, then this KLV's
 * key length parameter will be updated.
 *
 * @param inTheseBytes The byte array containing the key (and other stuff)
 * @param offset       The offset where to look for the key
 * @param keyLength    The length of the key
 * @return <tt>this</tt> to aid in stringing together commands
 * @throws IndexOutOfBoundsException If offset is invalid
 */// w ww  .ja v  a2 s.  c  om
private Klv setKey(final byte[] inTheseBytes, final int offset, final KeyLength keyLength) {
    Preconditions.checkElementIndex(offset, inTheseBytes.length,
            String.format("Offset %d is out of range (byte array length: %d).", offset, inTheseBytes.length));

    final int remaining = inTheseBytes.length - offset;
    checkEnoughBytesRemaining(remaining, keyLength.value(),
            String.format("Not enough bytes for %d-byte key.", keyLength.value()));

    // Set key according to length of key
    this.keyLength = keyLength;
    switch (keyLength) {
    case OneByte:
        this.keyIfShort = inTheseBytes[offset] & 0xFF;
        this.keyIfLong = null;
        break;

    case TwoBytes:
        this.keyIfShort = (inTheseBytes[offset] & 0xFF) << 8;
        this.keyIfShort |= inTheseBytes[offset + 1] & 0xFF;
        this.keyIfLong = null;
        break;

    case FourBytes:
        this.keyIfShort = (inTheseBytes[offset] & 0xFF) << 24;
        this.keyIfShort |= (inTheseBytes[offset + 1] & 0xFF) << 16;
        this.keyIfShort |= (inTheseBytes[offset + 2] & 0xFF) << 8;
        this.keyIfShort |= inTheseBytes[offset + 3] & 0xFF;
        this.keyIfLong = null;
        break;

    case SixteenBytes:
        this.keyIfLong = new byte[16];
        System.arraycopy(inTheseBytes, offset, this.keyIfLong, 0, 16);
        this.keyIfShort = 0;
        break;
    }
    return this;
}

From source file:org.objectweb.proactive.extensions.p2p.structured.deployment.scheduler.SchedulerNodeProvider.java

/**
 * {@inheritDoc}/*  w ww .  j a v a 2 s  .c  o m*/
 */
@Override
public synchronized Node getANode() {
    Preconditions.checkState(this.isStarted(),
            "Cannot get a node because the jobs have not yet been submitted");

    try {
        if (this.nodes == null) {
            List<UniqueID> nodeRequestIds = new ArrayList<UniqueID>();

            for (GcmVirtualNodeEntry virtualNodeEntry : this.virtualNodeEntries) {
                nodeRequestIds.add(virtualNodeEntry.nodeRequestId);
            }
            this.nodes = this.schedulerNodeProvider
                    .getNodes(nodeRequestIds.toArray(new UniqueID[nodeRequestIds.size()]));
        }

        Preconditions.checkElementIndex(this.nodeIndex, this.nodes.size(), "No node available");

        Node node = this.nodes.get(this.nodeIndex);

        this.nodeIndex++;

        return node;
    } catch (NodeProviderException npe) {
        throw new IllegalStateException("Failed to get a node", npe);
    }
}

From source file:org.codice.ddf.libs.klv.data.Klv.java

/**
 * Sets the length according to the length found in the byte array
 * and of the given length encoding.//from   w w  w.j a  v a 2 s .  c o m
 * If <tt>lengthEncoding</tt> is different
 * than what was previously set for this KLV, then this KLV's
 * length encoding parameter will be updated.
 * An array of the appropriate length will be initialized.
 *
 * @param inTheseBytes   The byte array containing the key (and other stuff)
 * @param offset         The offset where to look for the key
 * @param lengthEncoding The length of the key
 * @return Offset where value field would begin after length
 * @throws IndexOutOfBoundsException If offset is invalid
 */
private int setLength(final byte[] inTheseBytes, final int offset, final LengthEncoding lengthEncoding) {
    Preconditions.checkElementIndex(offset, inTheseBytes.length,
            String.format("Offset %d is out of range (byte array length: %d).", offset, inTheseBytes.length));

    int length = 0;
    int valueOffset = 0;
    final int remaining = inTheseBytes.length - offset;
    final String lengthEncodingErrorMessage = String.format("Not enough bytes for %s length encoding.",
            lengthEncoding);

    switch (lengthEncoding) {
    case OneByte:
        checkEnoughBytesRemaining(remaining, 1, lengthEncodingErrorMessage);

        length = inTheseBytes[offset] & 0xFF;
        setLength(length, lengthEncoding);
        valueOffset = offset + 1;
        break;

    case TwoBytes:
        checkEnoughBytesRemaining(remaining, 2, lengthEncodingErrorMessage);

        length = (inTheseBytes[offset] & 0xFF) << 8;
        length |= inTheseBytes[offset + 1] & 0xFF;
        setLength(length, lengthEncoding);
        valueOffset = offset + 2;
        break;

    case FourBytes:
        checkEnoughBytesRemaining(remaining, 4, lengthEncodingErrorMessage);

        length = (inTheseBytes[offset] & 0xFF) << 24;
        length |= (inTheseBytes[offset + 1] & 0xFF) << 16;
        length |= (inTheseBytes[offset + 2] & 0xFF) << 8;
        length |= inTheseBytes[offset + 3] & 0xFF;
        setLength(length, lengthEncoding);
        valueOffset = offset + 4;
        break;

    case BER:
        // Short BER form: If high bit is not set, then
        // use the byte to determine length of payload.
        // Long BER form: If high bit is set (0x80),
        // then use low seven bits to determine how many
        // bytes that follow are themselves an unsigned
        // integer specifying the length of the payload.
        // Using more than four bytes to specify the length
        // is not supported in this code, though it's not
        // exactly illegal KLV notation either.
        checkEnoughBytesRemaining(remaining, 1, lengthEncodingErrorMessage);
        final int ber = inTheseBytes[offset] & 0xFF;

        // Easy case: low seven bits is length
        if ((ber & 0x80) == 0) {
            setLength(ber, lengthEncoding);
            valueOffset = offset + 1;
        } else {
            final int following = ber & 0x7F; // Low seven bits
            checkEnoughBytesRemaining(remaining, following + 1, lengthEncodingErrorMessage);

            for (int i = 0; i < following; i++) {
                length |= (inTheseBytes[offset + 1 + i] & 0xFF) << (following - 1 - i) * 8;
            }
            setLength(length, lengthEncoding);
            valueOffset = offset + 1 + following;
        }
        break;
    }

    return valueOffset;
}