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:gobblin.source.extractor.extract.kafka.MultiLongWatermark.java

/**
 * Increment the idx'th value. idx must be between 0 and size()-1.
 *//*from w  w  w.ja  va  2s  . c o  m*/
public void increment(int idx) {
    Preconditions.checkElementIndex(idx, this.values.size());
    Preconditions.checkArgument(this.values.get(idx) < Long.MAX_VALUE);
    this.values.set(idx, this.values.get(idx) + 1);
}

From source file:com.google.appengine.tools.mapreduce.MockClock.java

/**
 * Returns the next time in the sequence.
 *///from ww  w  . ja v  a2 s  .c  om
@Override
public long currentTimeMillis() {
    Preconditions.checkNotNull(times);
    Preconditions.checkState(times.size() > 0);
    Preconditions.checkElementIndex(currentOffset, times.size());

    long timeToReturn = times.get(currentOffset).longValue();

    if (!repeatLastTime || currentOffset < times.size() - 1) {
        currentOffset++;
    }

    return timeToReturn;
}

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

/**
 * Only if list://from   w ww  .j  a v a2s.  com
 * The semantics of the collection returned by this method become undefined if the backing collection (i.e., this
 * collection) is structurally modified in any way other than via the returned collection. (Structural modifications
 * are those that change the size of this collection, or otherwise perturb it in such a fashion that iterations in
 * progress may yield incorrect results.)
 */
public static <E> Collection<E> skip(final Collection<E> coll, final int numberToSkip) {
    if (coll instanceof List) {
        return ((List<E>) coll).subList(numberToSkip, coll.size());
    }

    Preconditions.checkElementIndex(numberToSkip, coll.size());

    return new AbstractCollection<E>() {

        @Override
        public Iterator<E> iterator() {
            Iterator<E> it = coll.iterator();
            Iterators.advance(it, numberToSkip);
            return it;
        }

        @Override
        public int size() {
            return Math.max(0, coll.size() - numberToSkip);
        }

    };
}

From source file:org.eclipse.incquery.runtime.localsearch.MatchingFrame.java

/**
 * Returns the value stored inside the matching frame.
 * /*from  www. ja v a2 s.  c om*/
 * @param position
 * @return the element stored in the selected position in the frame, or null if it is not yet set
 * @throws IndexOutOfBoundsException
 *             if position is negative
 * @throws IllegalArgumentException
 *             if the position is larger then the length of the frame
 */
public Object getValue(Integer position) {
    Preconditions.checkElementIndex(position, frame.length);
    return frame[position];
}

From source file:org.grouplens.lenskit.vectors.Vec.java

/**
 * Get the value from the vector at the specified position.
 * @param i The index into the vector./*  w  w w  . jav a  2s  .c o  m*/
 * @return The value at index {@code i}.
 * @throws IllegalArgumentException if {@code i} is not in the range [0,{@link #size()}).
 */
public final double get(int i) {
    Preconditions.checkElementIndex(i, data.length);
    return data[i];
}

From source file:com.facebook.presto.util.MaterializedRow.java

public Object getField(int field) {
    Preconditions.checkElementIndex(field, values.size());
    Object o = values.get(field);
    return (o instanceof ApproximateDouble) ? ((ApproximateDouble) o).getValue() : o;
}

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

@SuppressWarnings("unchecked")
@Override
public E get(int i) {
    Preconditions.checkElementIndex(i, size);

    return (E) values[actualIndex(i)];
}

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

/**
 * Finds the index of the maximum element in the subarray with range [startIndex, endIndex).
 * If endIndex is greater than arr.length, the subarray stops at arr.length. The index that is
 * returned is relative to index; the absolute index can be obtained as follows.
 * {@code/*w  w w  .  j  av a  2 s. c  o m*/
 * int absolute = argMax(arr, startIndex, endIndex) + startIndex;
 * }
 * 
 * @throws IllegalArgumentException if len < 0
 * @throws IndexOutOfBoundsException if index < 0, index >= arr.length, or endIndex < 0
 * 
 * @return -1 if the subarray is empty; otherwise, the index of the max element
 */
public static int argMax(final double[] arr, final int startIndex, final int endIndex) {
    Preconditions.checkNotNull(arr);
    Preconditions.checkElementIndex(startIndex, arr.length);
    Preconditions.checkArgument(startIndex <= endIndex, "End index (%d) is less than start index (%d)",
            endIndex, startIndex);

    int stop = Math.min(endIndex, arr.length);
    if (stop == startIndex) {
        return -1;
    }

    int argMax = startIndex;
    double max = arr[startIndex];
    for (int i = startIndex + 1; i < stop; i++) {
        if (arr[i] > max) {
            max = arr[i];
            argMax = i;
        }
    }
    return argMax - startIndex;
}

From source file:org.grouplens.lenskit.vectors.MutableVec.java

/**
 * Set a value in this vector./*  w  ww  .j a v  a 2 s . co m*/
 * @param i The index.
 * @param v The value to set.
 * @return The old value at {@code i}.
 * @throws IllegalArgumentException if {@code i} is not a valid index in this vector.
 */
public double set(int i, double v) {
    Preconditions.checkElementIndex(i, size());
    final double old = data[i];
    data[i] = v;
    return old;
}

From source file:org.grouplens.lenskit.data.dao.packed.BufferBackedIntList.java

@Override
public int getInt(int i) {
    Preconditions.checkElementIndex(i, size());
    return buffer.get(offset + i);
}