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.rimuhosting.miro.compute.suppliers.RimuHostingHardwareSupplier.java

@Override
public Set<? extends Hardware> get() {
    final Set<Hardware> sizes = Sets.newHashSet();
    logger.debug(">> providing sizes");
    for (final PricingPlan from : sync.getPricingPlanList()) {
        try {//from   w ww  .  j  ava  2 s . c  o  m

            final Location location = Iterables.find(locations.get(), new Predicate<Location>() {

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

            });
            sizes.add(new HardwareBuilder().ids(from.getId()).location(location)
                    .processors(ImmutableList.of(new Processor(1, 1.0))).ram(from.getRam())
                    .volumes(ImmutableList.<Volume>of(new VolumeImpl((float) from.getDiskSize(), true, true)))
                    .build());
        } catch (NullPointerException e) {
            logger.warn("datacenter not present in " + from.getId());
        }
    }
    logger.debug("<< sizes(%d)", sizes.size());
    return sizes;
}

From source file:com.atlassian.jira.rest.client.api.domain.EntityHelper.java

public static <T extends Attachment> T findAttachmentByFileName(Iterable<T> attachments,
        final String fileName) {
    return Iterables.find(attachments, HasFileNamePredicate.forFileName(fileName));
}

From source file:org.jclouds.abiquo.binders.cloud.BindNetworkConfigurationRefToPayload.java

@Override
public <R extends HttpRequest> R bindToRequest(final R request, final Object input) {
    checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest,
            "this binder is only valid for GeneratedHttpRequests");
    checkArgument(checkNotNull(input, "input") instanceof VLANNetworkDto,
            "this binder is only valid for VLANNetworkDto");
    GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request;
    checkState(gRequest.getInvocation().getArgs() != null, "args should be initialized at this point");

    VLANNetworkDto network = (VLANNetworkDto) input;
    VirtualMachineDto vm = (VirtualMachineDto) Iterables.find(gRequest.getInvocation().getArgs(),
            Predicates.instanceOf(VirtualMachineDto.class));

    RESTLink configLink = checkNotNull(vm.searchLink("configurations"), "missing required link");

    LinksDto dto = new LinksDto();
    dto.addLink(new RESTLink("network_configuration", configLink.getHref() + "/" + network.getId()));

    return super.bindToRequest(request, dto);
}

From source file:org.sonar.core.issue.DefaultIssuable.java

private Issue findIssue(final Issue issue) {
    return Iterables.find(issues, new Predicate<Issue>() {
        public boolean apply(Issue currentIssue) {
            return currentIssue.key().equals(issue.key());
        }/*from  w w  w  .j ava  2s . co  m*/
    });
}

From source file:org.testfx.service.finder.impl.WindowFinderImpl.java

@Override
public Window window(Predicate<Window> predicate) {
    List<Window> windows = fetchWindowsByProximityTo(lastTargetWindow);
    return Iterables.find(windows, predicate);
}

From source file:com.bennavetta.vetinari.build.internal.phase.TemplatePhase.java

private TemplateEngine getTemplateEngine(Page page, Site site) {
    TemplateEngine templateEngine = null;
    // Use the second file extension, if any. The first is for the renderer.
    final String templateExtension = getFileExtension(getNameWithoutExtension(page.getPath().toString()));
    Optional<TemplateEngine> templateEngineFromExtension = Iterables.tryFind(templateEngines,
            t -> Iterables.contains(t.getFileExtensions(), templateExtension));
    if (templateEngineFromExtension.isPresent()) {
        templateEngine = templateEngineFromExtension.get();
    } else if (page.getMetadata().hasPath("templateEngine")) {
        final String templateEngineName = page.getMetadata().getString("templateEngine");
        templateEngine = Iterables.find(templateEngines, t -> templateEngineName.equals(t.getName()));
    } else {//w w w.  j  a va  2 s .  c o m
        templateEngine = Iterables.find(templateEngines,
                t -> site.getDefaultTemplateEngine().equals(t.getName()));
    }
    return templateEngine;
}

From source file:org.jclouds.openstack.keystone.v2_0.suppliers.LocationIdToURIFromAccessForTypeAndVersionSupplier.java

@Override
public Map<String, Supplier<URI>> get() {
    Access accessResponse = access.get();
    Service service = null;/* ww w . ja va2s.  c o m*/
    try {
        service = Iterables.find(accessResponse.getServiceCatalog(), new Predicate<Service>() {

            @Override
            public boolean apply(Service input) {
                return input.getType().equals(apiType);
            }

        });
    } catch (NoSuchElementException e) {
        throw new NoSuchElementException(String.format("apiType %s not found in catalog %s", apiType,
                accessResponse.getServiceCatalog()));
    }
    Map<String, Endpoint> locationIdToEndpoint = Maps
            .uniqueIndex(Iterables.filter(service.getEndpoints(), new Predicate<Endpoint>() {

                @Override
                public boolean apply(Endpoint input) {
                    if (input.getVersionId() == null) {
                        return true;
                    }
                    return input.getVersionId().equals(apiVersion);
                }

            }), endpointToLocationId);
    return Maps.transformValues(locationIdToEndpoint, endpointToSupplierURI);
}

From source file:org.apache.brooklyn.entity.software.base.test.mysql.DynamicToyMySqlEntityBuilder.java

public static final String installDir(Entity e, boolean isLocalhost) {
    String url = downloadUrl(e, isLocalhost);
    String archive = Iterables.find(Splitter.on('/').omitEmptyStrings().split(url),
            Predicates.containsPattern(".tar.gz"));
    return archive.replace(".tar.gz", "");
}

From source file:de.cosmocode.commons.validation.AbstractRule.java

@Override
public T find(Iterable<? extends T> iterable) {
    Preconditions.checkNotNull(iterable, "Iterable");
    return Iterables.find(iterable, this);
}

From source file:org.testatoo.core.ListSelection.java

public T find(Predicate<? super T> predicate) throws NoSuchElementException {
    return Iterables.find(this, predicate);
}