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

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

Introduction

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

Prototype

public static int checkPositionIndex(int index, int size, @Nullable String desc) 

Source Link

Document

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

Usage

From source file:com.facebook.stats.cardinality.StaticModel.java

@Override
public SymbolInfo getSymbolInfo(int symbol) {
    Preconditions.checkPositionIndex(symbol, counts.length, "symbol");
    return new SymbolInfo(symbol, counts[symbol], counts[symbol + 1]);
}

From source file:org.jalphanode.util.ReflectionUtils.java

/**
 * Inspects the parameter for the specified annotation.
 *
 * @param   method           method/*from   ww  w.  j  a v  a 2s  .  c  om*/
 * @param   index            parameter index
 * @param   annotationClass  annotation to search for
 *
 * @return  the annotation instance, or null if the annotation cannot be found.
 */
public static <A extends Annotation> A getParameterAnnotation(final Method method, final int index,
        final Class<A> annotationClass) {
    Preconditions.checkNotNull(method, "method");
    Preconditions.checkNotNull(annotationClass, "annotationClass");

    Annotation[][] annotations = method.getParameterAnnotations();
    Preconditions.checkPositionIndex(index, annotations.length, "index");

    A annotation = null;

    for (Annotation a : method.getParameterAnnotations()[index]) {
        if (annotationClass.isInstance(a)) {
            annotation = annotationClass.cast(a);
            break;
        }
    }

    return annotation;
}

From source file:com.facebook.stats.cardinality.SortedStaticModel.java

@Override
public SymbolInfo getSymbolInfo(int symbol) {
    Preconditions.checkPositionIndex(symbol, symbolToIndex.length, "symbol");
    int symbolIndex = symbolToIndex[symbol];
    return new SymbolInfo(symbol, countsByIndex[symbolIndex], countsByIndex[symbolIndex + 1]);
}

From source file:com.hippo.leveldb.table.BlockIterator.java

/**
 * Seeks to and reads the entry at the specified restart position.
 * <p/>/*from   ww  w . j  a  va  2 s .  co  m*/
 * After this method, nextEntry will contain the next entry to return, and the previousEntry will be null.
 */
private void seekToRestartPosition(int restartPosition) {
    Preconditions.checkPositionIndex(restartPosition, restartCount, "restartPosition");

    // seek data readIndex to the beginning of the restart block
    int offset = restartPositions.getInt(restartPosition * SIZE_OF_INT);
    data.setPosition(offset);

    // clear the entries to assure key is not prefixed
    nextEntry = null;

    // read the entry
    nextEntry = readEntry(data, null);
}

From source file:io.pravega.test.integration.service.selftest.TruncateableArray.java

/**
 * Truncates a number of bytes from the beginning of the array.
 *
 * @param truncationLength The number of bytes to truncate.
 *//*from  w  w w .  j a va  2  s  .c  om*/
void truncate(int truncationLength) {
    Preconditions.checkPositionIndex(truncationLength, this.length,
            "trimLength must be non-negative and less than the length of the array.");
    this.length -= truncationLength;

    while (this.arrays.size() > 0 && truncationLength > 0) {
        byte[] first = this.arrays.getFirst();
        if (truncationLength >= first.length - this.firstArrayOffset) {
            // We need to truncate more than what is available in the first array; chop it all off.
            this.arrays.removeFirst();
            truncationLength -= first.length - this.firstArrayOffset;
            this.firstArrayOffset = 0;
        } else {
            // We need to truncate less than what is available in the first array; adjust offset.
            this.firstArrayOffset += truncationLength;
            truncationLength = 0;
        }
    }

    assert truncationLength == 0 : "not all requested bytes were truncated";
    if (this.arrays.size() == 0) {
        assert this.firstArrayOffset == 0 : "first entry offset not reset when no entries exist";
        assert this.length == 0 : "non-zero length when no entries exist";
    }
}

From source file:com.hippo.leveldb.impl.Version.java

public boolean overlapInLevel(int level, Slice smallestUserKey, Slice largestUserKey) {
    Preconditions.checkPositionIndex(level, levels.size(), "Invalid level");
    Preconditions.checkNotNull(smallestUserKey, "smallestUserKey is null");
    Preconditions.checkNotNull(largestUserKey, "largestUserKey is null");

    if (level == 0) {
        return level0.someFileOverlapsRange(smallestUserKey, largestUserKey);
    }//from   www  .j  a v a  2 s  .c  om
    return levels.get(level - 1).someFileOverlapsRange(smallestUserKey, largestUserKey);
}

From source file:org.apache.flume.channel.recoverable.memory.wal.WAL.java

/**
 * Reads in a WAL and writes out a new WAL. Used if for some reason a replay
 * cannot occur due to the size of the WAL or assumptions about the number of
 * sequenceids./*ww w  . ja  va 2  s .com*/
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws IOException, ClassNotFoundException {
    Preconditions.checkPositionIndex(0, args.length, "input directory is a required arg");
    Preconditions.checkPositionIndex(1, args.length, "output directory is a required arg");
    Preconditions.checkPositionIndex(2, args.length, "classname is a required arg");
    String input = args[0];
    String output = args[1];
    Class clazz = Class.forName(args[2].trim());
    WAL inputWAL = new WAL(new File(input), clazz);
    if (args.length == 4) {
        inputWAL.numReplaySequenceIDOverride = Integer.parseInt(args[3]);
        System.out.println("Overridng numReplaySequenceIDOverride: " + inputWAL.numReplaySequenceIDOverride);
    }
    WALReplayResult<?> result = inputWAL.replay();
    inputWAL.close();
    System.out.println("     SeqID: " + result.getSequenceID());
    System.out.println("NumEntries: " + result.getResults().size());
    WAL outputWAL = new WAL(new File(output), clazz);
    outputWAL.writeEntries(result.getResults());
    outputWAL.close();
}