Example usage for org.apache.commons.lang3.tuple MutablePair setLeft

List of usage examples for org.apache.commons.lang3.tuple MutablePair setLeft

Introduction

In this page you can find the example usage for org.apache.commons.lang3.tuple MutablePair setLeft.

Prototype

public void setLeft(final L left) 

Source Link

Document

Sets the left element of the pair.

Usage

From source file:bwem.map.MapImpl.java

public MutablePair<AreaId, AreaId> findNeighboringAreas(final WalkPosition p) {
    final MutablePair<AreaId, AreaId> result = new MutablePair<>(null, null);

    final WalkPosition[] deltas = { new WalkPosition(0, -1), new WalkPosition(-1, 0), new WalkPosition(+1, 0),
            new WalkPosition(0, +1) };
    for (final WalkPosition delta : deltas) {
        if (getData().getMapData().isValid(p.add(delta))) {
            final AreaId areaId = getData().getMiniTile(p.add(delta), CheckMode.NO_CHECK).getAreaId();
            if (areaId.intValue() > 0) {
                if (result.getLeft() == null) {
                    result.setLeft(areaId);
                } else if (!result.getLeft().equals(areaId)) {
                    if (result.getRight() == null || ((areaId.intValue() < result.getRight().intValue()))) {
                        result.setRight(areaId);
                    }//from  ww w.j a  va  2 s.  c  o m
                }
            }
        }
    }

    return result;
}

From source file:net.dv8tion.jda.ConnectionManager.java

private void setupSendingThread() {
    ratelimitThread = new Thread("Core ConnectionManager Thread") {

        @Override//from   w w w .  ja  v  a  2 s.  c  o m
        public void run() {
            boolean needRatelimit;
            boolean attemptedToSend;
            while (!this.isInterrupted()) {
                try {
                    //Make sure that we don't send any packets before sending auth info.
                    //                        if (!sentAuthInfo)
                    //                        {
                    //                            Thread.sleep(500);
                    //                            continue;
                    //                        }
                    attemptedToSend = false;
                    needRatelimit = false;

                    Map.Entry<String, MutablePair<Long, String>> requestEntry = getNextAudioConnectRequest();

                    System.out.println(requestEntry);
                    if (requestEntry != null) {
                        String guildId = requestEntry.getKey();
                        MutablePair<Long, String> audioRequest = requestEntry.getValue();
                        String channelId = audioRequest.getRight();
                        AudioManager audioManager = core.getAudioManager(guildId);
                        JSONObject audioConnectPacket = new JSONObject().put("op", 4).put("d",
                                new JSONObject().put("guild_id", guildId).put("channel_id", channelId)
                                        .put("self_mute", audioManager.isSelfMuted())
                                        .put("self_deaf", audioManager.isSelfDeafened()));
                        needRatelimit = !send(audioConnectPacket.toString());
                        System.out.println(needRatelimit);
                        if (!needRatelimit) {
                            //If we didn't get RateLimited, Next allowed connect request will be 2 seconds from now
                            audioRequest.setLeft(System.currentTimeMillis() + 2000);

                            //If the connection is already established, then the packet just sent
                            // was a move channel packet, thus, it won't trigger the removal from
                            // queuedAudioConnections in VoiceServerUpdateHandler because we won't receive
                            // that event just for a move, so we remove it here after successfully sending.
                            if (audioManager.isConnected()) {
                                queuedAudioConnections.remove(guildId);
                            }
                        }
                        attemptedToSend = true;
                    }

                    if (needRatelimit || !attemptedToSend) {
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException ignored) {
                    LOG.debug("ConnectionManager thread interrupted. Most likely shutting down.");
                    break;
                }
            }
        }
    };
    ratelimitThread.start();
}

From source file:hu.mta.sztaki.lpds.cloud.simulator.DeferredEvent.java

/**
 * If the call for eventAction() is no longer necessary at the previously
 * specified time then the user can cancel this call to arrive with this
 * function./*  w  ww . ja  va 2 s  .c om*/
 * 
 * Calling this function will have no effect on events that are already past
 * due.
 */
public void cancel() {
    if (received)
        return;
    if (!cancelled) {
        cancelled = true;
        MutablePair<Integer, DeferredEvent[]> simultaneousReceiverPairs = toSweep.get(eventArrival);
        if (simultaneousReceiverPairs != null) {
            int len = simultaneousReceiverPairs.getLeft();
            DeferredEvent[] simultaneousReceivers = simultaneousReceiverPairs.getRight();
            // For performance reasons this removal operation does not keep
            // the order of the array entries
            for (int i = 0; i < len; i++) {
                if (simultaneousReceivers[i] == this) {
                    len--;
                    if (len > i) {
                        simultaneousReceivers[i] = simultaneousReceivers[len];
                    }
                    simultaneousReceivers[len] = null;
                    break;
                }
            }
            if (len == 0) {
                toSweep.remove(eventArrival);
                dispatcherSingleton.updateDispatcher();
            } else {
                simultaneousReceiverPairs.setLeft(len);
            }
        }
    }
}

From source file:com.mirth.connect.client.ui.codetemplate.CodeTemplatePanel.java

private void updateContextTable(CodeTemplateContextSet context) {
    DefaultMutableTreeTableNode root = (DefaultMutableTreeTableNode) templateContextTreeTable
            .getTreeTableModel().getRoot();

    for (Enumeration<? extends MutableTreeTableNode> groups = root.children(); groups.hasMoreElements();) {
        MutableTreeTableNode group = groups.nextElement();
        MutablePair<Integer, String> groupPair = (MutablePair<Integer, String>) group.getUserObject();
        boolean allChildren = true;
        boolean noChildren = true;

        for (Enumeration<? extends MutableTreeTableNode> children = group.children(); children
                .hasMoreElements();) {/*from w  w w .j  a v  a  2  s .co m*/
            MutableTreeTableNode child = children.nextElement();
            MutablePair<Integer, ContextType> childPair = (MutablePair<Integer, ContextType>) child
                    .getUserObject();

            if (context.contains(childPair.getRight())) {
                childPair.setLeft(MirthTriStateCheckBox.CHECKED);
                noChildren = false;
            } else {
                childPair.setLeft(MirthTriStateCheckBox.UNCHECKED);
                allChildren = false;
            }
        }

        if (allChildren) {
            groupPair.setLeft(MirthTriStateCheckBox.CHECKED);
        } else if (noChildren) {
            groupPair.setLeft(MirthTriStateCheckBox.UNCHECKED);
        } else {
            groupPair.setLeft(MirthTriStateCheckBox.PARTIAL);
        }
    }

    templateContextTreeTable.expandAll();
}

From source file:nl.basjes.parse.useragent.servlet.ParseService.java

private Pair<String, String> prefixSplitter(String input) {
    MutablePair<String, String> result = new MutablePair<>("", input);
    if (input.startsWith("Device")) {
        result.setLeft("Device");
        result.setRight(input.replaceFirst("Device", ""));
    } else if (input.startsWith("OperatingSystem")) {
        result.setLeft("Operating System");
        result.setRight(input.replaceFirst("OperatingSystem", ""));
    } else if (input.startsWith("LayoutEngine")) {
        result.setLeft("Layout Engine");
        result.setRight(input.replaceFirst("LayoutEngine", ""));
    } else if (input.startsWith("Agent")) {
        result.setLeft("Agent");
        result.setRight(input.replaceFirst("Agent", ""));
    }/*from  w  w w .  j  a va  2s  .  c o  m*/
    return result;
}

From source file:org.apache.apex.malhar.lib.window.accumulation.Average.java

@Override
public MutablePair<Double, Long> accumulate(MutablePair<Double, Long> accu, Double input) {
    accu.setLeft(accu.getLeft() * ((double) accu.getRight() / (accu.getRight() + 1))
            + input / (accu.getRight() + 1));
    accu.setRight(accu.getRight() + 1);//from   w  w  w. j  a  v a 2s.  co  m
    return accu;
}

From source file:org.apache.apex.malhar.lib.window.accumulation.Average.java

@Override
public MutablePair<Double, Long> merge(MutablePair<Double, Long> accu1, MutablePair<Double, Long> accu2) {
    accu1.setLeft(accu1.getLeft() * ((double) accu1.getRight() / accu1.getRight() + accu2.getRight())
            + accu2.getLeft() * ((double) accu2.getRight() / accu1.getRight() + accu2.getRight()));
    accu1.setRight(accu1.getRight() + accu2.getRight());
    return accu1;
}

From source file:org.apache.giraph.block_app.reducers.map.BasicMapReduce.java

/**
 * Registers one new reducer, that will reduce BasicMap,
 * by reducing individual elements corresponding to the same key
 * using {@code elementReduceOp}./*from   w  w w  .j a  va  2s.c  o  m*/
 *
 * This function will return ReducerMapHandle, by which
 * individual elements can be manipulated separately.
 *
 * @param keyTypeOps TypeOps of keys
 * @param typeOps TypeOps of individual elements
 * @param elementReduceOp ReduceOperation for individual elements
 * @param createFunction Function for creating a reducer
 * @return Created ReducerMapHandle
 */
public static <K extends WritableComparable, S, R extends Writable> ReducerMapHandle<K, S, R> createMapHandles(
        final PrimitiveIdTypeOps<K> keyTypeOps, final PrimitiveTypeOps<R> typeOps,
        ReduceOperation<S, R> elementReduceOp, CreateReducerFunctionApi createFunction) {
    final ReducerHandle<Pair<K, S>, Basic2ObjectMap<K, R>> reduceHandle = createFunction
            .createReducer(new BasicMapReduce<>(keyTypeOps, typeOps, elementReduceOp));
    final K curIndex = keyTypeOps.create();
    final R reusableValue = typeOps.create();
    final R initialValue = elementReduceOp.createInitialValue();
    final MutablePair<K, S> reusablePair = MutablePair.of(null, null);
    final ReducerHandle<S, R> elementReduceHandle = new ReducerHandle<S, R>() {
        @Override
        public R getReducedValue(MasterGlobalCommUsage master) {
            Basic2ObjectMap<K, R> result = reduceHandle.getReducedValue(master);
            R value = result.get(curIndex);
            if (value == null) {
                typeOps.set(reusableValue, initialValue);
            } else {
                typeOps.set(reusableValue, value);
            }
            return reusableValue;
        }

        @Override
        public void reduce(S valueToReduce) {
            reusablePair.setLeft(curIndex);
            reusablePair.setRight(valueToReduce);
            reduceHandle.reduce(reusablePair);
        }

        @Override
        public BroadcastHandle<R> broadcastValue(BlockMasterApi master) {
            throw new UnsupportedOperationException();
        }
    };

    return new ReducerMapHandle<K, S, R>() {
        @Override
        public ReducerHandle<S, R> get(K key) {
            keyTypeOps.set(curIndex, key);
            return elementReduceHandle;
        }

        @Override
        public int getReducedSize(BlockMasterApi master) {
            return reduceHandle.getReducedValue(master).size();
        }

        @Override
        public BroadcastMapHandle<K, R> broadcastValue(BlockMasterApi master) {
            final BroadcastHandle<Basic2ObjectMap<K, R>> broadcastHandle = reduceHandle.broadcastValue(master);
            final K curIndex = keyTypeOps.create();
            final R reusableValue = typeOps.create();
            final BroadcastHandle<R> elementBroadcastHandle = new BroadcastHandle<R>() {
                @Override
                public R getBroadcast(WorkerBroadcastUsage worker) {
                    Basic2ObjectMap<K, R> result = broadcastHandle.getBroadcast(worker);
                    R value = result.get(curIndex);
                    if (value == null) {
                        typeOps.set(reusableValue, initialValue);
                    } else {
                        typeOps.set(reusableValue, value);
                    }
                    return reusableValue;
                }
            };
            return new BroadcastMapHandle<K, R>() {
                @Override
                public BroadcastHandle<R> get(K key) {
                    keyTypeOps.set(curIndex, key);
                    return elementBroadcastHandle;
                }

                @Override
                public int getBroadcastedSize(WorkerBroadcastUsage worker) {
                    return broadcastHandle.getBroadcast(worker).size();
                }
            };
        }
    };
}

From source file:org.apache.giraph.ooc.data.DiskBackedDataStore.java

/**
 * Adds a data entry for a given partition to the current data store. If data
 * of a given partition in data store is already offloaded to disk, adds the
 * data entry to appropriate raw data buffer list.
 *
 * @param partitionId id of the partition to add the data entry to
 * @param entry data entry to add/*w  w  w  .  j  a v a2s .  c o  m*/
 */
protected void addEntry(int partitionId, T entry) {
    // Addition of data entries to a data store is much more common than
    // out-of-core operations. Besides, in-memory data store implementations
    // existing in the code base already account for parallel addition to data
    // stores. Therefore, using read lock would optimize for parallel addition
    // to data stores, specially for cases where the addition should happen for
    // partitions that are entirely in memory.
    ReadWriteLock rwLock = getPartitionLock(partitionId);
    rwLock.readLock().lock();
    if (hasPartitionDataOnDisk.contains(partitionId)) {
        List<T> entryList = new ArrayList<>();
        entryList.add(entry);
        int entrySize = entrySerializedSize(entry);
        MutablePair<Integer, List<T>> newPair = new MutablePair<>(entrySize, entryList);
        Pair<Integer, List<T>> oldPair = dataBuffers.putIfAbsent(partitionId, newPair);
        if (oldPair != null) {
            synchronized (oldPair) {
                newPair = (MutablePair<Integer, List<T>>) oldPair;
                newPair.setLeft(oldPair.getLeft() + entrySize);
                newPair.getRight().add(entry);
            }
        }
    } else {
        addEntryToInMemoryPartitionData(partitionId, entry);
    }
    rwLock.readLock().unlock();
}

From source file:org.apache.giraph.ooc.data.OutOfCoreDataManager.java

/**
 * Adds a data entry for a given partition to the current data store. If data
 * of a given partition in data store is already offloaded to disk, adds the
 * data entry to appropriate raw data buffer list.
 *
 * @param partitionId id of the partition to add the data entry to
 * @param entry data entry to add//from  w w w  .  ja  va 2  s . com
 */
protected void addEntry(int partitionId, T entry) {
    // Addition of data entries to a data store is much more common than
    // out-of-core operations. Besides, in-memory data store implementations
    // existing in the code base already account for parallel addition to data
    // stores. Therefore, using read lock would optimize for parallel addition
    // to data stores, specially for cases where the addition should happen for
    // partitions that are entirely in memory.
    ReadWriteLock rwLock = getPartitionLock(partitionId);
    rwLock.readLock().lock();
    if (hasPartitionDataOnDisk.contains(partitionId)) {
        List<T> entryList = new ArrayList<>();
        entryList.add(entry);
        int entrySize = entrySerializedSize(entry);
        MutablePair<Integer, List<T>> newPair = new MutablePair<>(entrySize, entryList);
        Pair<Integer, List<T>> oldPair = dataBuffers.putIfAbsent(partitionId, newPair);
        if (oldPair != null) {
            synchronized (oldPair) {
                newPair = (MutablePair<Integer, List<T>>) oldPair;
                newPair.setLeft(oldPair.getLeft() + entrySize);
                newPair.getRight().add(entry);
            }
        }
    } else {
        addEntryToImMemoryPartitionData(partitionId, entry);
    }
    rwLock.readLock().unlock();
}