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:net.automatalib.commons.util.array.RichArray.java

public void swap(int i, int j) {
    Preconditions.checkElementIndex(i, length);
    Preconditions.checkElementIndex(j, length);
    doSwap(contents, start + i, start + j);
}

From source file:org.grouplens.lenskit.collections.LongKeyDomain.java

/**
 * Set the active flag for a single key.
 * @param idx The key's index./*from   w w w .j  ava  2  s.  c  o  m*/
 * @param active Whether the key is active.
 * @return The key set (for chaining).
 */
public LongKeyDomain setActive(int idx, boolean active) {
    Preconditions.checkElementIndex(idx, domainSize);
    mask.set(idx, active);
    return this;
}

From source file:net.automatalib.commons.util.array.RichArray.java

public void transformSingle(int index, Function<? super T, ? extends T> transformer) {
    Preconditions.checkElementIndex(index, length);
    Objects.requireNonNull(transformer);
    int effIdx = start + index;
    T elem = contents[effIdx];//from   w  w  w .  j a v  a  2s  .c o  m
    contents[effIdx] = transformer.apply(elem);
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.inference.BlockStateService.java

/**
 * Computes distance along a blockConfigEntry for a given starting stop-time
 * index.<br>/*from w  ww . j a  v  a2s.co m*/
 * Here's an example of how to get the index:<br>
 * 
 * <pre>
 * {@code
 * List<BlockStopTimeEntry> stopTimes = blockConfig.getStopTimes();
 * int n = stopTimes.size();
 * int index = GenericBinarySearch.search(blockConfig, n, scheduleTime,
 *     IndexAdapters.BLOCK_STOP_TIME_DEPARTURE_INSTANCE);
 * }
 * </pre>
 * 
 * <br>
 * Code taken from ScheduledBlockLocationServiceImpl.
 * 
 * @param blockEntry
 * @param fromStopTimeIndex
 * @param scheduleTime
 * @return
 */
public double getDistanceAlongBlock(BlockConfigurationEntry blockEntry, int fromStopTimeIndex,
        int scheduleTime) {

    final List<BlockStopTimeEntry> stopTimes = blockEntry.getStopTimes();

    Preconditions.checkElementIndex(fromStopTimeIndex, stopTimes.size() + 1);

    double distanceAlongBlock = Double.NaN;
    if (fromStopTimeIndex == 0) {
        final BlockStopTimeEntry blockStopTime = stopTimes.get(0);
        final StopTimeEntry stopTime = blockStopTime.getStopTime();

        /**
         * If we have more than one stop time in the block (we'd hope!), then we
         * attempt to interpolate the distance along the block
         */
        if (stopTimes.size() > 1) {

            final BlockStopTimeEntry secondBlockStopTime = stopTimes.get(1);
            final StopTimeEntry secondStopTime = secondBlockStopTime.getStopTime();

            distanceAlongBlock = InterpolationLibrary.interpolatePair(stopTime.getDepartureTime(),
                    blockStopTime.getDistanceAlongBlock(), secondStopTime.getArrivalTime(),
                    secondBlockStopTime.getDistanceAlongBlock(), scheduleTime);

            if (distanceAlongBlock < 0)
                distanceAlongBlock = 0.0;
        }
    } else {
        while (fromStopTimeIndex < stopTimes.size()) {
            final int t = blockEntry.getDepartureTimeForIndex(fromStopTimeIndex);
            if (scheduleTime <= t)
                break;
            fromStopTimeIndex++;
        }

        if (fromStopTimeIndex < stopTimes.size()) {
            final BlockStopTimeEntry blockBefore = stopTimes.get(fromStopTimeIndex - 1);
            final BlockStopTimeEntry blockAfter = stopTimes.get(fromStopTimeIndex);

            final StopTimeEntry before = blockBefore.getStopTime();
            final StopTimeEntry after = blockAfter.getStopTime();

            final int fromTime = before.getDepartureTime();
            final int toTime = after.getArrivalTime();

            final double ratio = (scheduleTime - fromTime) / ((double) (toTime - fromTime));

            final double fromDistance = blockBefore.getDistanceAlongBlock();
            final double toDistance = blockAfter.getDistanceAlongBlock();

            distanceAlongBlock = ratio * (toDistance - fromDistance) + fromDistance;
        } else {
            distanceAlongBlock = blockEntry.getTotalBlockDistance();
        }
    }

    return distanceAlongBlock;
}

From source file:codecrafter47.bungeetablistplus.tablisthandler.logic.AbstractTabListLogic.java

@Override
public void setSlot(int index, Icon skin, String text, int ping) {
    Preconditions.checkElementIndex(index, size);

    if (!clientSkin[index].equals(skin)) {
        if (clientSkin[index].getPlayer() != null) {
            skinUuidToSlotMap.remove(clientSkin[index].getPlayer(), index);
        }/* w  ww . ja  va  2s .co m*/
        if (skin.getPlayer() != null) {
            skinUuidToSlotMap.put(skin.getPlayer(), index);
        }
    }

    if (!passtrough) {
        if (clientSkin[index].equals(skin)) {
            updatePingInternal(index, ping);
        } else {
            boolean updated = false;
            if (size < 80) {
                int slot;
                if (!clientUuid[index].equals(skin.getPlayer())) {
                    boolean moveOld = false;
                    boolean moveNew = false;
                    if ((!clientUuid[index].equals(getUniqueId())
                            || serverTabList.get(getUniqueId()).getGamemode() != 3)) {
                        moveOld = clientUuid[index] != fakePlayerUUIDs[index];
                        moveNew = skin.getPlayer() != null && serverTabList.containsKey(skin.getPlayer())
                                && (!skin.getPlayer().equals(getUniqueId())
                                        || serverTabList.get(getUniqueId()).getGamemode() != 3)
                                && (clientSkin[(slot = uuidToSlotMap.getInt(skin.getPlayer()))]
                                        .getPlayer() == null
                                        || !clientSkin[slot].getPlayer().equals(skin.getPlayer()));
                    }

                    UUID oldUuid = clientUuid[index];

                    if (moveOld && !moveNew && serverTabList.size() < size) {
                        clientSkin[index] = skin;
                        clientPing[index] = ping;
                        useFakePlayerForSlot(index);
                        if (skinUuidToSlotMap.containsKey(oldUuid)) {
                            for (IntIterator iterator = skinUuidToSlotMap.get(oldUuid).iterator(); iterator
                                    .hasNext();) {
                                int i = iterator.nextInt();
                                if (clientUuid[i] != fakePlayerUUIDs[i] && (!getUniqueId().equals(clientUuid[i])
                                        || serverTabList.get(getUniqueId()).getGamemode() != 3)) {
                                    int target = findSlotForPlayer(clientUuid[i]);
                                    useRealPlayerForSlot(target, clientUuid[i]);
                                    useRealPlayerForSlot(i, oldUuid);
                                    updated = true;
                                    break;
                                }
                            }
                        }
                        if (!updated) {
                            int target = findSlotForPlayer(oldUuid);
                            useRealPlayerForSlot(target, oldUuid);
                        }
                        updated = true;
                    } else if (moveNew && !moveOld && serverTabList.size() < size) {
                        clientSkin[index] = skin;
                        clientPing[index] = ping;
                        slot = uuidToSlotMap.getInt(skin.getPlayer());
                        useFakePlayerForSlot(slot);
                        useRealPlayerForSlot(index, skin.getPlayer());
                        updated = true;
                    } else if (moveNew && moveOld) {
                        clientSkin[index] = skin;
                        clientPing[index] = ping;
                        slot = uuidToSlotMap.getInt(skin.getPlayer());
                        useFakePlayerForSlot(slot);
                        if (skinUuidToSlotMap.containsKey(oldUuid)) {
                            for (IntIterator iterator = skinUuidToSlotMap.get(oldUuid).iterator(); iterator
                                    .hasNext();) {
                                int i = iterator.nextInt();
                                if (clientUuid[i] != fakePlayerUUIDs[i] && (!getUniqueId().equals(clientUuid[i])
                                        || serverTabList.get(getUniqueId()).getGamemode() != 3)) {
                                    int target = findSlotForPlayer(clientUuid[i]);
                                    useRealPlayerForSlot(target, clientUuid[i]);
                                    useRealPlayerForSlot(i, oldUuid);
                                    updated = true;
                                    break;
                                }
                            }
                        }
                        if (!updated) {
                            int target = findSlotForPlayer(oldUuid);
                            useRealPlayerForSlot(target, oldUuid);
                        }
                        useRealPlayerForSlot(index, skin.getPlayer());
                        updated = true;
                    }
                }
            }
            if (!updated) {
                if (clientUuid[index] == fakePlayerUUIDs[index]) {
                    PlayerListItem packet = new PlayerListItem();
                    PlayerListItem.Item item = new PlayerListItem.Item();
                    item.setUuid(clientUuid[index]);
                    item.setUsername(clientUsername[index]);
                    item.setPing(ping);
                    item.setDisplayName(text);
                    item.setProperties(skin.getProperties());
                    packet.setAction(ADD_PLAYER);
                    packet.setItems(new PlayerListItem.Item[] { item });
                    sendPacket(packet);
                    clientText[index] = "";
                } else {
                    updatePingInternal(index, ping);
                }
            }
        }
        updateTextInternal(index, text);
    }
    clientSkin[index] = skin;
    clientText[index] = text;
    clientPing[index] = ping;
}

From source file:codecrafter47.bungeetablistplus.tablisthandler.logic.AbstractTabListLogic.java

private void updateTextInternal(int index, String text) {
    Preconditions.checkElementIndex(index, size);

    if (!passtrough && !clientText[index].equals(text)) {
        PlayerListItem packet = new PlayerListItem();
        PlayerListItem.Item item = new PlayerListItem.Item();
        item.setUuid(clientUuid[index]);
        item.setDisplayName(text);//from w  w w.  j  a va  2s.  co  m
        packet.setAction(UPDATE_DISPLAY_NAME);
        packet.setItems(new PlayerListItem.Item[] { item });
        sendPacket(packet);
    }
    clientText[index] = text;
}

From source file:codecrafter47.bungeetablistplus.tablisthandler.logic.AbstractTabListLogic.java

private void updatePingInternal(int index, int ping) {
    Preconditions.checkElementIndex(index, size);

    if (!passtrough && clientPing[index] != ping) {
        PlayerListItem packet = new PlayerListItem();
        PlayerListItem.Item item = new PlayerListItem.Item();
        item.setUuid(clientUuid[index]);
        item.setPing(ping);/*from  ww  w.  ja v  a  2  s  .c om*/
        packet.setAction(UPDATE_LATENCY);
        packet.setItems(new PlayerListItem.Item[] { item });
        sendPacket(packet);
    }
    clientPing[index] = ping;
}