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.mstiles92.plugins.stileslib.menu.menus.Menu.java

/**
 * Set a MenuItem to a specific position in the Menu. The position is zero-based, so the top-left slot is 0, the
 * top-right is 8, the far-left in the second row is 9, and so on.
 *
 * @param position the zero-based position in which to put the MenuItem
 * @param item the MenuItem to put in the Menu
 *//*from w w w  .j av  a2s  .co m*/
public void setItem(int position, MenuItem item) {
    Preconditions.checkElementIndex(position, contents.length);

    contents[position] = item;
}

From source file:org.apache.flink.streaming.api.datastream.StreamProjection.java

protected StreamProjection(DataStream<IN> dataStream, int[] fieldIndexes) {
    if (!dataStream.getType().isTupleType()) {
        throw new RuntimeException("Only Tuple DataStreams can be projected");
    }/* w  ww .j  a va2  s. c o  m*/
    if (fieldIndexes.length == 0) {
        throw new IllegalArgumentException("project() needs to select at least one (1) field.");
    } else if (fieldIndexes.length > Tuple.MAX_ARITY - 1) {
        throw new IllegalArgumentException(
                "project() may select only up to (" + (Tuple.MAX_ARITY - 1) + ") fields.");
    }

    int maxFieldIndex = (dataStream.getType()).getArity();
    for (int i = 0; i < fieldIndexes.length; i++) {
        Preconditions.checkElementIndex(fieldIndexes[i], maxFieldIndex);
    }

    this.dataStream = dataStream;
    this.fieldIndexes = fieldIndexes;
}

From source file:org.ygl.plexc.PlexcLibrary.java

/**
 * Predicate to determine if the hour of the day is between the two
 * specified values./*from  www. j ava  2  s  . c o  m*/
 *
 * @param start the start hour, 0-23
 * @param end the end hour, 0-23
 * @return
 */
public boolean hourBetween_2(Int start, Int end) {
    Preconditions.checkElementIndex(start.intValue(), 24);
    Preconditions.checkElementIndex(end.intValue(), 24);
    int time = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    return (time >= start.intValue() && time <= end.intValue());
}

From source file:com.cloudera.fts.pig.ProcessSession.java

@Override
public DataBag exec(Tuple inputTuple) throws IOException {

    if (inputTuple.size() != 1) {
        throw new ExecException("Expecting 1 input, found " + inputTuple.size(), PigException.INPUT);
    }// w ww .  j  a v a2  s.co m

    if (inputTuple.get(0) == null) {
        return null;
    }

    if (!(inputTuple.get(0) instanceof DataBag)) {
        throw new ExecException("Usage ProcessSession(DataBag of (timestamp, page) tuples)",
                PigException.INPUT);
    }

    try {
        DataBag outBag = mBagFactory.newDefaultBag();
        Map<String, Long> timesOnPage = Maps.newHashMap();

        long nextStamp = 0L;
        for (Tuple view : (DataBag) inputTuple.get(0)) {
            Preconditions.checkElementIndex(1, view.size());
            try {
                long timestamp = Long.parseLong(view.get(0).toString());
                String page = view.get(1).toString();
                if (nextStamp != 0L) {
                    timesOnPage.put(page,
                            timesOnPage.containsKey(page) ? timesOnPage.get(page) + nextStamp - timestamp
                                    : nextStamp - timestamp);
                }
                nextStamp = timestamp;
            } catch (NumberFormatException e) {
                throw new ExecException("The first element of the tuples should be timestamp, got a "
                        + view.get(0).getClass().getCanonicalName() + " with value " + view.get(0).toString(),
                        PigException.INPUT);
            }
        }
        Set<LongStringPair> sortedTimes = Sets.newTreeSet();
        for (Map.Entry<String, Long> entry : timesOnPage.entrySet()) {
            sortedTimes.add(new LongStringPair(entry.getValue(), entry.getKey()));
        }
        int rank = 0;
        for (LongStringPair pair : sortedTimes) {
            Tuple tup = mTupleFactory.newTuple(3);
            tup.set(0, new Integer(++rank));
            tup.set(1, new Long(pair.lvalue));
            tup.set(2, pair.svalue);
            outBag.add(tup);
        }
        return outBag;
    } catch (Exception e) {
        String msg = "Encountered error while processing visits in " + this.getClass().getCanonicalName();
        throw new ExecException(msg, PigException.BUG, e);
    }
}

From source file:com.opengamma.strata.market.surface.ConstantSurface.java

@Override
public double getParameter(int parameterIndex) {
    Preconditions.checkElementIndex(parameterIndex, 1);
    return zValue;
}

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

/**
 * Returns the value stored inside the matching frame.
 * //from ww  w.j a  v  a 2s  .c o  m
 * @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(int position) {
    Preconditions.checkElementIndex(position, frame.length);
    return frame[position];
}

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

@Override
public FormattedCharacterBuffer getHeader(int columnIndex) {
    Preconditions.checkElementIndex(columnIndex, columns.length);
    return columns[columnIndex].getHeader();
}

From source file:com.opengamma.strata.market.curve.ConstantCurve.java

@Override
public double getParameter(int parameterIndex) {
    Preconditions.checkElementIndex(parameterIndex, 1);
    return yValue;
}

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

/**
 * Set the preference data at a particular index.
 *
 * @param idx  The index.//from  ww w .j  a v a  2 s  . c om
 * @param pref The preference data.
 */
public void set(int idx, Preference pref) {
    Preconditions.checkElementIndex(idx, nprefs);
    final int ci = chunk(idx);
    final int ei = element(idx);
    set(ci, ei, pref);
}

From source file:org.sonar.gherkin.checks.DuplicatedStepsCheck.java

private void createIssue(List<StepTree> duplicatedSteps, @Nullable List<StepTree> duplicatedStepsInBackground) {
    Preconditions.checkElementIndex(0, duplicatedSteps.size());

    PreciseIssue issue = addPreciseIssue(duplicatedSteps.get(0), "Remove this duplicated step.");
    addSecondaryLocation(issue, duplicatedSteps.stream().skip(1).collect(Collectors.toList()));

    if (duplicatedStepsInBackground != null) {
        addSecondaryLocation(issue, duplicatedStepsInBackground);
    }/*from   ww  w . j  a  v a2s .co  m*/
}