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

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

Introduction

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

Prototype

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

Source Link

Document

Returns an Optional containing the first element in iterable that satisfies the given predicate, if such an element exists.

Usage

From source file:ec.nbdemetra.ui.DemetraUI.java

private static <X, Y> X find(Class<X> clazz, Function<? super X, Y> toName, Y... names) {
    Collection<? extends X> items = Lookup.getDefault().lookupAll(clazz);
    for (Y o : names) {
        Optional<? extends X> result = Iterables.tryFind(items,
                Predicates.compose(Predicates.equalTo(o), toName));
        if (result.isPresent()) {
            return result.get();
        }/*from  ww w.ja  v a  2  s.c o m*/
    }
    return null;
}

From source file:com.urswolfer.gerrit.client.rest.http.GerritRestClient.java

private Optional<Cookie> findCookie(final String cookieName) {
    List<Cookie> cookies = cookieStore.getCookies();
    return Iterables.tryFind(cookies, new Predicate<Cookie>() {
        @Override/*  w w  w .  j ava  2 s  .c om*/
        public boolean apply(Cookie cookie) {
            return cookie.getName().equals(cookieName);
        }
    });
}

From source file:com.eucalyptus.component.TopologyDnsResolver.java

@SuppressWarnings("ConstantConditions")
public static InetAddress maphost(final InetAddress listenerAddress, final InetAddress hostAddress) {
    final Map<Cidr, Cidr> mappings = hostMappings.apply(hostMapping);

    InetAddress result = hostAddress;
    for (final Map.Entry<Cidr, Cidr> mapping : mappings.entrySet()) {
        if (mapping.getKey().apply(listenerAddress)) {
            final Host host = Hosts.lookup(hostAddress);
            if (host != null) {
                result = Iterables.tryFind(host.getHostAddresses(), mapping.getValue()).or(result);
            }/*from w w w .j  ava2  s  .co m*/
        }
    }

    return result;
}

From source file:org.killbill.billing.payment.dao.MockPaymentDao.java

@Override
public PaymentAttemptModelDao getPaymentAttempt(final UUID attemptId, final InternalTenantContext context) {
    synchronized (this) {
        return Iterables.tryFind(attempts.values(), new Predicate<PaymentAttemptModelDao>() {
            @Override/* ww  w.  j av  a  2  s . com*/
            public boolean apply(final PaymentAttemptModelDao input) {
                return input.getId().equals(attemptId);
            }
        }).orNull();
    }
}

From source file:org.opencms.xml.containerpage.CmsFormatterConfiguration.java

/**
 * Returns the formatter from this configuration that is to be used for the preview in the ADE gallery GUI,
 * or <code>null</code> if there is no preview formatter configured.<p>
 *
 * @return the formatter from this configuration that is to be used for the preview in the ADE gallery GUI,
 * or <code>null</code> if there is no preview formatter configured
 */// ww  w. ja v a 2 s.  c  om
public I_CmsFormatterBean getPreviewFormatter() {

    Optional<I_CmsFormatterBean> result;
    result = Iterables.tryFind(m_allFormatters, new Predicate<I_CmsFormatterBean>() {

        public boolean apply(I_CmsFormatterBean formatter) {

            return formatter.isPreviewFormatter();
        }
    });
    if (!result.isPresent()) {
        result = Iterables.tryFind(m_allFormatters, new Predicate<I_CmsFormatterBean>() {

            public boolean apply(I_CmsFormatterBean formatter) {

                if (formatter.isTypeFormatter()) {
                    return formatter.getContainerTypes().contains(CmsFormatterBean.PREVIEW_TYPE);
                } else {
                    return (formatter.getMinWidth() <= CmsFormatterBean.PREVIEW_WIDTH)
                            && (CmsFormatterBean.PREVIEW_WIDTH <= formatter.getMaxWidth());
                }
            }
        });
    }
    if (!result.isPresent()) {
        result = Iterables.tryFind(m_allFormatters, new Predicate<I_CmsFormatterBean>() {

            public boolean apply(I_CmsFormatterBean formatter) {

                return !formatter.isTypeFormatter()
                        && (formatter.getMaxWidth() >= CmsFormatterBean.PREVIEW_WIDTH);

            }
        });
    }
    if (!result.isPresent() && !m_allFormatters.isEmpty()) {
        result = Optional.fromNullable(m_allFormatters.iterator().next());
    }
    return result.orNull();
}

From source file:com.eucalyptus.www.X509Download.java

@SuppressWarnings("ConstantConditions")
private static URI hostMap(final URI uri) {
    final Optional<Cidr> hostMatcher = InetAddresses.isInetAddress(uri.getHost())
            ? Cidr.parse().apply(AuthenticationProperties.CREDENTIAL_DOWNLOAD_HOST_MATCH)
            : Optional.<Cidr>absent();
    if (hostMatcher.isPresent()) {
        final Host host = Hosts.lookup(InetAddresses.forString(uri.getHost()));
        if (host != null) {
            final Optional<InetAddress> mappedHost = Iterables.tryFind(host.getHostAddresses(),
                    hostMatcher.get());// w w w. j ava 2 s. co m
            if (mappedHost.isPresent()) {
                return URI
                        .create(uri.toString().replaceFirst(uri.getHost(), mappedHost.get().getHostAddress()));
            }
        }
    }
    return uri;
}

From source file:io.redlink.sdk.impl.analysis.model.RDFStructureParser.java

private void setEntityAnnotationData(EntityAnnotation entityAnnotation, BindingSet result,
        RepositoryConnection conn, Multimap<Enhancement, String> relations)
        throws RepositoryException, EnhancementParserException {

    if (!relations.containsKey(entityAnnotation)) {
        setEnhancementData(entityAnnotation, result);
        if (result.hasBinding("entityLabel")) {
            Binding entityLabel = result.getBinding("entityLabel");
            entityAnnotation.setEntityLabel(entityLabel.getValue().stringValue());
            if (!result.hasBinding("language") && (entityLabel.getValue() instanceof Literal))
                entityAnnotation.setLanguage(((Literal) entityLabel.getValue()).getLanguage());

        }//from   w w  w  . jav a 2 s.com

        if (result.hasBinding("site")) {
            entityAnnotation.setDataset(result.getBinding("site").getValue().stringValue());
        }

        if (result.hasBinding("entityReference")) {
            entityAnnotation.setEntityReference(
                    parseEntity(conn, result.getBinding("entityReference").getValue().stringValue(),
                            entityAnnotation.getDataset()));
        }

        if (result.hasBinding("relation")) {
            String nextRelationUri = result.getBinding("relation").getValue().stringValue();
            relations.put(entityAnnotation, nextRelationUri);
        }

        Collection<String> types = new HashSet<String>();
        if (result.hasBinding("entityType")) {
            types.add(result.getBinding("entityType").getValue().stringValue());
        }
        entityAnnotation.setEntityTypes(types);
    } else {
        if (result.hasBinding("relation")) {
            final String nextRelationUri = result.getBinding("relation").getValue().stringValue();
            if (!relations.containsEntry(entityAnnotation, nextRelationUri))
                relations.put(entityAnnotation, nextRelationUri);
        }

        if (result.hasBinding("entityType")) {
            final String entityType = result.getBinding("entityType").getValue().stringValue();
            Collection<String> types = entityAnnotation.getEntityTypes();
            Optional<String> eType = Iterables.tryFind(types, new Predicate<String>() {

                @Override
                public boolean apply(String arg0) {
                    return arg0.equals(entityType);
                }

            });
            if (!eType.isPresent())
                types.add(entityType);
        }
    }

}

From source file:brooklyn.entity.pool.ServerPoolImpl.java

private Optional<Entity> getMemberWithStatus0(Collection<Entity> entities, final MachinePoolMemberStatus status,
        final boolean includeUnremovableMachines) {
    return Iterables.tryFind(entities, new Predicate<Entity>() {
        @Override//  w  w w  .j ava  2 s . com
        public boolean apply(Entity input) {
            return (includeUnremovableMachines || isRemovable(input))
                    && status.equals(input.getAttribute(SERVER_STATUS));
        }
    });
}

From source file:org.eclipse.emf.compare.ide.ui.internal.structuremergeviewer.EMFCompareStructureMergeViewerContentProvider.java

private IDifferenceGroupProvider2 getGroupProvider2(TreeNode treeNode) {
    IDifferenceGroupProvider2 result = null;
    Optional<Adapter> searchResult = Iterables.tryFind(treeNode.eAdapters(),
            Predicates.instanceOf(IDifferenceGroupProvider2.class));
    if (searchResult.isPresent()) {
        return (IDifferenceGroupProvider2) searchResult.get();
    }/*from   ww w.j  av  a2  s . c  o  m*/
    return result;
}

From source file:org.killbill.billing.payment.invoice.InvoicePaymentRoutingPluginApi.java

private PluginProperty getPluginProperty(final Iterable<PluginProperty> properties, final String propertyName) {
    return Iterables.tryFind(properties, new Predicate<PluginProperty>() {
        @Override//from   w w  w.j  av  a  2s  .c  o  m
        public boolean apply(final PluginProperty input) {
            return input.getKey().equals(propertyName);
        }
    }).orNull();
}