Example usage for com.google.common.collect Iterables get

List of usage examples for com.google.common.collect Iterables get

Introduction

In this page you can find the example usage for com.google.common.collect Iterables get.

Prototype

public static <T> T get(Iterable<T> iterable, int position) 

Source Link

Document

Returns the element at the specified position in an iterable.

Usage

From source file:org.spongepowered.common.mixin.core.scoreboard.MixinScoreboard.java

@Inject(method = "setObjectiveInDisplaySlot", at = @At("HEAD"), cancellable = true)
public void setObjectiveInDisplaySlot(int slot, ScoreObjective objective, CallbackInfo ci) {
    if (shouldEcho()) {
        this.scoreboard.allowRecursion = false;
        // The objective is nullable, so no need to check the result of getSpongeObjective
        //System.out.format("Scoreboard: {} Objecite: {} DisplaySlot: ", new Object[] {this.scoreboard, objective,Iterables.get((
        //        (SpongeGameRegistry) Sponge.getGame().getRegistry()).displaySlotMappings.values(), slot) });
        this.scoreboard.addObjective(((IMixinScoreObjective) objective).getSpongeObjective(), Iterables
                .get(((SpongeGameRegistry) Sponge.getGame().getRegistry()).displaySlotMappings.values(), slot));
        this.scoreboard.allowRecursion = true;
        ci.cancel();//  w w  w  . jav  a2  s.  co  m
    }
}

From source file:com.googlecode.blaisemath.graph.mod.generators.DegreeDistributionGenerator.java

/**
 * @return random value in given set/*from   ww  w  .  j  a v  a2  s.  c  o m*/
 */
private static <V> V random(Set<V> set) {
    return Iterables.get(set, new Random().nextInt(set.size()));
}

From source file:org.jamocha.dn.compiler.ecblocks.Randomizer.java

static <T> T getRandomElement(final Set<T> elements, final Random rand) {
    return Iterables.get(elements, rand.nextInt(elements.size()));
}

From source file:org.jclouds.virtualbox.functions.MastersLoadingCache.java

@Inject
public MastersLoadingCache(@BuildVersion String version,
        @Named(VIRTUALBOX_PRECONFIGURATION_URL) String preconfigurationUrl,
        @Named(VIRTUALBOX_WORKINGDIR) String workingDir, Function<MasterSpec, IMachine> masterLoader,
        Supplier<Map<Image, YamlImage>> yamlMapper, Supplier<VirtualBoxManager> manager,
        Factory runScriptOnNodeFactory, RetryIfSocketNotYetOpen socketTester, Supplier<NodeMetadata> host,
        @Provider Supplier<URI> providerSupplier,
        HardcodedHostToHostNodeMetadata hardcodedHostToHostNodeMetadata) {
    this.manager = checkNotNull(manager, "vboxmanager can't be null");
    this.masterCreatorAndInstaller = masterLoader;
    this.workingDir = workingDir == null ? VIRTUALBOX_DEFAULT_DIR : workingDir;
    this.isosDir = workingDir + File.separator + "isos";
    this.imageMapping = Maps.newLinkedHashMap();
    for (Entry<Image, YamlImage> entry : yamlMapper.get().entrySet()) {
        this.imageMapping.put(entry.getKey().getId(), entry.getValue());
    }//from  www . j a v a  2 s  .c om
    this.version = Iterables.get(Splitter.on('r').split(checkNotNull(version, "version")), 0);
    this.preconfigurationUrl = preconfigurationUrl;

    this.runScriptOnNodeFactory = checkNotNull(runScriptOnNodeFactory, "runScriptOnNodeFactory");
    this.socketTester = checkNotNull(socketTester, "socketTester");
    this.socketTester.seconds(3L);
    this.host = checkNotNull(host, "host");
    this.providerSupplier = checkNotNull(providerSupplier, "endpoint to virtualbox websrvd is needed");
    this.hardcodedHostToHostNodeMetadata = hardcodedHostToHostNodeMetadata;
}

From source file:com.github.naios.wide.framework.internal.storage.server.builder.SQLScope.java

private void buildDeletes(final StringBuilder builder,
        final Entry<ServerStorage<?>, Collection<ServerStorageStructure>> structures) {
    final SQLMaker sqlMaker = new SQLMaker(sqlBuilder, sqlBuilder.getDeleteConfig());

    final String tableName = Iterables.get(structures.getValue(), 0).getOwner().getTableName();

    final String keyPart = sqlMaker.createKeyPart(structures.getValue());
    builder.append(SQLMaker.createDeleteQuery(tableName, keyPart)).append(SQLMaker.NEWLINE);
}

From source file:uk.ac.stfc.isis.ibex.configserver.editing.EditableGroup.java

private List<EditableBlock> lookupBlocksByName(Collection<EditableBlock> blocks,
        final Collection<String> names) {
    List<EditableBlock> selectedBlocks = new ArrayList<EditableBlock>();
    List<String> allBlockNames = blockNames(blocks);
    for (String name : names) {
        int blockIndex = allBlockNames.indexOf(name);
        if (blockIndex >= 0) {
            EditableBlock block = Iterables.get(blocks, blockIndex);
            selectedBlocks.add(block);/*from   ww w.j  a  va2s.co m*/
        }
    }
    return selectedBlocks;
}

From source file:org.eclipse.papyrus.infra.table.controlmode.helpers.TableMoveHelper.java

/**
 * Creates an iterable containing all the Papyrus Tables that are descending from the context.
 * /*from  ww w  . j  av a2s .c o  m*/
 * @author olivier melois (Atos)
 */
public static Iterable<EObject> createDescendantTablesIterable(EObject context) {

    Set<EObject> result = Sets.newHashSet();

    TreeIterator<EObject> eAllContents = EcoreUtil.getAllProperContents(context, true); // was context.eAllContents().
    Iterator<EObject> contextAndDescendants = Iterators.concat(eAllContents,
            Iterators.singletonIterator(context));

    final Predicate<Setting> keepPapyrusTableInstances = new Predicate<Setting>() {

        public boolean apply(Setting setting) {
            boolean result = true;
            if (setting != null) {
                EObject settingEObject = setting.getEObject();
                result &= settingEObject instanceof PapyrusTableInstance;
                result &= PapyrustableinstancePackage.Literals.PAPYRUS_TABLE_INSTANCE__TABLE == setting
                        .getEStructuralFeature();
            } else {
                result = false;
            }
            return result;
        }
    };

    /*
     * Predicate used to keep the usages which are PapyrusTableInstances
     */
    Predicate<Setting> keepTableInstances = new Predicate<Setting>() {

        public boolean apply(Setting setting) {
            boolean result = true;
            if (setting != null) {
                EObject settingEObject = setting.getEObject();
                result &= settingEObject instanceof TableInstance;
                result &= setting
                        .getEStructuralFeature() == TableinstancePackage.Literals.TABLE_INSTANCE__CONTEXT;

                Collection<Setting> references = PapyrusEcoreUtils.getUsages(settingEObject);
                Iterable<Setting> papyrusTableInstances = Iterables.filter(references,
                        keepPapyrusTableInstances);
                //Veryfing that there is at least one papyrusTableInstance
                result = result && !Iterables.isEmpty(papyrusTableInstances);

            } else {
                result = false;
            }
            return result;
        }
    };

    /*
     * Function to get the eObject from a setting
     */
    Function<Setting, EObject> getEObject = new Function<Setting, EObject>() {

        public EObject apply(Setting input) {
            EObject settingEObject = input.getEObject();
            Collection<Setting> references = PapyrusEcoreUtils.getUsages(settingEObject);
            Iterable<Setting> papyrusTableInstances = Iterables.filter(references, keepPapyrusTableInstances);
            //Getting the eobject of thie first element of this iterable.
            return Iterables.get(papyrusTableInstances, 0).getEObject();
        }

    };

    /*
     * For the context and his descendants :
     */
    while (contextAndDescendants.hasNext()) {
        EObject current = contextAndDescendants.next();
        //Usages
        Iterable<Setting> usages = PapyrusEcoreUtils.getUsages(current);
        //Filtering to keep only papyrus table instances.
        Iterable<Setting> tableInstanceSettings = Iterables.filter(usages, keepTableInstances);
        //Getting the eObjects 
        Iterable<EObject> papyrusTableInstances = Iterables.transform(tableInstanceSettings, getEObject);
        //Adding all the kept usages.
        Iterables.addAll(result, papyrusTableInstances);
    }

    return result;
}

From source file:org.richfaces.request.MultipartRequestParser.java

private String getFirstParameterValue(Multimap<String, String> multimap, String key) {
    Collection<String> values = multimap.get(key);

    if (values.isEmpty()) {
        return null;
    }//from ww  w.  j  a  v  a  2 s  . com

    return Iterables.get(values, 0);
}

From source file:org.eclipse.reqcycle.traceability.table.view.TraceabilityTableView.java

private void createModel() {
    createTableViewerColumn("Link type", 50, 0).setLabelProvider(new LinkLabelProvider(styleProvider) {

        @Override/*from w w  w.  java  2 s .c  om*/
        public String getText(Object element) {
            if (element instanceof Link) {
                TType kind = ((Link) element).getKind();
                StringBuilder builder = new StringBuilder(kind.getLabel());
                TType superKind = kind.getSuperType();
                if (superKind != null) {
                    builder.append(String.format(" [Transverse : %s]", superKind.getLabel()));
                }
                return builder.toString();
            }
            return super.getText(element);
        }
    });

    createTableViewerColumn("Upstream", 200, 1).setLabelProvider(new LinkLabelProvider(styleProvider) {

        @Override
        public String getText(Object element) {
            if (element instanceof Link) {
                Set<Reachable> set = ((Link) element).getSources();
                if (set != null && set.size() == 1) {
                    Reachable reachable = Iterables.get(set, 0);
                    return TraceabilityUtils.getText(reachable);
                }
            }
            return super.getText(element);
        }
    });

    createTableViewerColumn("Downstream", 200, 2).setLabelProvider(new LinkLabelProvider(styleProvider) {

        @Override
        public String getText(Object element) {
            if (element instanceof Link) {
                Set<Reachable> set = ((Link) element).getTargets();
                if (set != null && set.size() == 1) {
                    Reachable reachable = Iterables.get(set, 0);
                    return TraceabilityUtils.getText(reachable);
                }
            }
            return super.getText(element);
        }
    });
}

From source file:org.jclouds.virtualbox.functions.IMachineToNodeMetadata.java

private NodeMetadataBuilder getIpAddresses(IMachine vm, NodeMetadataBuilder nodeMetadataBuilder) {
    List<String> publicIpAddresses = Lists.newArrayList();
    List<String> privateIpAddresses = Lists.newArrayList();
    for (long slot = 0; slot < 4; slot++) {
        INetworkAdapter adapter = vm.getNetworkAdapter(slot);
        if (adapter != null) {
            if (adapter.getAttachmentType() == NetworkAttachmentType.NAT) {
                String hostIP = adapter.getNATEngine().getHostIP();
                if (!hostIP.isEmpty())
                    publicIpAddresses.add(hostIP);
                for (String nameProtocolnumberAddressInboudportGuestTargetport : adapter.getNATEngine()
                        .getRedirects()) {
                    Iterable<String> stuff = Splitter.on(',')
                            .split(nameProtocolnumberAddressInboudportGuestTargetport);
                    String protocolNumber = Iterables.get(stuff, 1);
                    String hostAddress = Iterables.get(stuff, 2);
                    String inboundPort = Iterables.get(stuff, 3);
                    String targetPort = Iterables.get(stuff, 5);
                    if ("1".equals(protocolNumber) && "22".equals(targetPort)) {
                        int inPort = Integer.parseInt(inboundPort);
                        publicIpAddresses.add(hostAddress);
                        nodeMetadataBuilder.loginPort(inPort);
                    }/*  w w  w. jav  a2 s  .  c  o m*/
                }
            } else if (adapter.getAttachmentType() == NetworkAttachmentType.Bridged) {
                String clientIpAddress = networkUtils.getIpAddressFromNicSlot(vm.getName(), adapter.getSlot());
                privateIpAddresses.add(clientIpAddress);

            } else if (adapter.getAttachmentType() == NetworkAttachmentType.HostOnly) {
                String clientIpAddress = networkUtils.getValidHostOnlyIpFromVm(vm.getName());
                publicIpAddresses.add(clientIpAddress);
            }
        }
    }
    nodeMetadataBuilder.publicAddresses(publicIpAddresses);
    nodeMetadataBuilder.privateAddresses(publicIpAddresses);
    return nodeMetadataBuilder;
}