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

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

Introduction

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

Prototype

public static <T> T find(Iterable<T> iterable, Predicate<? super T> predicate) 

Source Link

Document

Returns the first element in iterable that satisfies the given predicate; use this method only when such an element is known to exist.

Usage

From source file:org.jclouds.rackspace.cloudservers.compute.functions.ServerToNodeMetadata.java

protected Hardware parseHardware(Server from) {
    try {// w  ww.  ja v a  2 s  . c om
        return Iterables.find(hardwares.get(), new FindHardwareForServer(from));
    } catch (NoSuchElementException e) {
        logger.warn("could not find a matching hardware for server %s", from);
    }
    return null;
}

From source file:org.jclouds.slicehost.compute.functions.SliceToNodeMetadata.java

protected Hardware parseHardware(Slice from) {
    try {//from  w  w  w  .  j a  v a 2 s. c  o m
        return Iterables.find(hardwares.get(), new FindHardwareForSlice(from));
    } catch (NoSuchElementException e) {
        logger.warn("could not find a matching hardware for slice %s", from);
    }
    return null;
}

From source file:com.groupon.jenkins.dynamic.build.plugins.downstream.DownstreamJobPlugin.java

private DynamicProject findJob(String jobName) {
    final String orgName = jobName.split("/")[0];
    final String repoName = jobName.split("/")[1];
    return Iterables.find(Jenkins.getInstance().getAllItems(DynamicProject.class),
            new Predicate<DynamicProject>() {
                @Override/*from w  ww  .j  a  va 2 s  .  co  m*/
                public boolean apply(DynamicProject project) {
                    return project.getParent().getName().equals(orgName) && project.getName().equals(repoName);
                }
            });
}

From source file:com.codemacro.jcm.model.Cluster.java

private Node getNode(final String spec) {
    try {// w ww .  java 2 s . c o m
        return Iterables.find(nodes, new Predicate<Node>() {
            public boolean apply(Node node) {
                return node.isMatch(spec);
            }
        });
    } catch (NoSuchElementException e) {
        return null;
    }
}

From source file:com.github.richardwilly98.esdms.DocumentImpl.java

@Override
@JsonIgnore/*from   ww  w  .j a  v  a2s. c o  m*/
public Version getVersion(final int versionId) {
    checkArgument(versionId > 0);
    try {
        return Iterables.find(versions, new Predicate<Version>() {
            @Override
            public boolean apply(Version version) {
                return (version.getVersionId() == versionId);
            }
        });
    } catch (NoSuchElementException ex) {
        return null;
    }
}

From source file:org.jclouds.ovh.OVHComputeClient.java

public OfferStruct getHardware(final String name) throws OvhWsException {
    return Iterables.find(listHardware(), new Predicate<OfferStruct>() {
        @Override/*  w  w w.j av a 2s . c  om*/
        public boolean apply(OfferStruct offer) {
            return offer.getName().equalsIgnoreCase(name);
        }
    });
}

From source file:org.dasein.cloud.jclouds.vcloud.director.network.VCloudDirectorNetworkSupport.java

@Override
public Iterable<NetworkInterface> listNetworkInterfaces(String forVmId)
        throws CloudException, InternalException {
    RestContext<VCloudDirectorAdminClient, VCloudDirectorAdminAsyncClient> ctx = provider.getCloudClient();

    try {/*  w w  w  .  j  a v  a2  s.c  o  m*/
        try {
            Set<Reference> refs = provider.getOrg().getNetworks();
            List<NetworkInterface> list = Lists.newArrayList();
            List<Network> networks = Lists.newArrayList();
            Vm vm = ctx.getApi().getVmClient().getVm(provider.toHref(ctx, forVmId));
            NetworkConnection def = null;

            if (refs != null) {
                for (Reference t : refs) {
                    if (t.getType().equals(VCloudDirectorMediaType.NETWORK)) {
                        Network network = ctx.getApi().getNetworkClient().getNetwork(t.getHref());

                        if (network != null) {
                            networks.add(network);
                        }
                    }
                }
            }
            NetworkConnectionSection section = (NetworkConnectionSection) Iterables.find(vm.getSections(),
                    Predicates.instanceOf(NetworkConnectionSection.class));
            for (NetworkConnection c : section.getNetworkConnections()) {
                NetworkInterface nic = new NetworkInterface();

                nic.setProviderNetworkInterfaceId(c.getMACAddress());
                nic.setIpAddress(c.getIpAddress());
                nic.setProviderVirtualMachineId(forVmId);
                for (Network network : networks) {
                    if (network.getName().equals(c.getNetwork())) {
                        IpScope scope = network.getConfiguration().getIpScope();

                        if (def == null || def.getNetworkConnectionIndex() > c.getNetworkConnectionIndex()) {
                            def = c;
                        }
                        nic.setGatewayAddress(scope.getGateway());
                        nic.setNetmask(scope.getNetmask());
                        nic.setProviderVlanId(provider.toId(ctx, network.getHref()));
                    }
                }
            }
            if (def != null) {
                for (NetworkInterface nic : list) {
                    if (def.getMACAddress().equals(nic.getProviderNetworkInterfaceId())) {
                        nic.setDefaultRoute(true);
                    }
                }
            }
            return list;
        } catch (RuntimeException e) {
            logger.error("Error listing network interfaces for " + forVmId + ": " + e.getMessage());
            if (logger.isDebugEnabled()) {
                e.printStackTrace();
            }
            throw new CloudException(e);
        }
    } finally {
        ctx.close();
    }
}

From source file:com.eucalyptus.compute.common.internal.vm.VmVolumeState.java

private VmVolumeAttachment resolveVolumeId(final String volumeId) throws NoSuchElementException {
    final VmVolumeAttachment v = Iterables.find(this.attachments, VmVolumeAttachment.volumeIdFilter(volumeId));
    if (v == null) {
        throw new NoSuchElementException("Failed to find volume attachment for instance "
                + this.getVmInstance().getInstanceId() + " and volume " + volumeId);
    } else {//from w ww.  j  a  v a 2  s. c om
        return v;
    }
}

From source file:com.efficios.jabberwocky.lttng.kernel.views.timegraph.threads.ThreadsModelArrowProviderCpus.java

private static TimeGraphTreeElement getTreeElementFromThread(TimeGraphTreeRender treeRender, int tid,
        @Nullable Integer cpu) {/* w  w w  .j  a  v a 2s . c  o  m*/
    if (tid != 0) {
        // FIXME Could be improved via indexing, to avoid iterating the
        // whole array for every single tid.
        return Iterables.find(treeRender.getAllTreeElements(), treeElem -> {
            if (!(treeElem instanceof ThreadsTreeElement)) {
                return false;
            }
            ThreadsTreeElement cfvTreeElem = (ThreadsTreeElement) treeElem;
            return (cfvTreeElem.getTid() == tid);
        });
    }
    if (cpu == null) {
        throw new IllegalStateException();
    }
    String prefix = "0/" + cpu.toString(); //$NON-NLS-1$
    return Iterables.find(treeRender.getAllTreeElements(), treeElem -> {
        if (!(treeElem instanceof ThreadsTreeElement)) {
            return false;
        }
        ThreadsTreeElement cfvTreeElem = (ThreadsTreeElement) treeElem;
        return cfvTreeElem.getName().startsWith(prefix);
    });
}

From source file:org.jclouds.byon.functions.NodeToNodeMetadata.java

private Location findLocationWithId(final String locationId) {
    if (locationId == null)
        return location.get();
    try {//from w ww .j av a 2 s  .  co m
        Location location = Iterables.find(locations.get(), new Predicate<Location>() {

            @Override
            public boolean apply(Location input) {
                return input.getId().equals(locationId);
            }

        });
        return location;

    } catch (NoSuchElementException e) {
        logger.debug("couldn't match instance location %s in: %s", locationId, locations.get());
        return location.get();
    }
}