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:com.axemblr.provisionr.commands.DestroyPoolCommand.java

@Override
protected Object doExecute() throws Exception {
    Optional<Provisionr> service = Iterables.tryFind(services, ProvisionrPredicates.withId(id));

    if (service.isPresent()) {
        service.get().destroyPool(businessKey);
    } else {//from w  w w  . j av a2  s.c  om
        throw new NoSuchElementException("No provisioning service found with id: " + id);
    }

    return null;
}

From source file:org.jclouds.openstack.marconi.v1.domain.MessageStream.java

@Override
public Optional<Object> nextMarker() {
    Optional<Link> nextMarkerLink = Iterables.tryFind(getLinks(), IS_NEXT_LINK);
    return nextMarkerLink.transform(TO_LIST_OPTIONS);
}

From source file:org.estatio.dom.communicationchannel.EmailAddresses.java

@Programmatic
public EmailAddress findByEmailAddress(final CommunicationChannelOwner owner, final String emailAddress) {

    final List<CommunicationChannelOwnerLink> links = communicationChannelOwnerLinks
            .findByOwnerAndCommunicationChannelType(owner, CommunicationChannelType.EMAIL_ADDRESS);
    final Iterable<EmailAddress> emailAddresses = Iterables.transform(links,
            CommunicationChannelOwnerLink.Functions.communicationChannel(EmailAddress.class));
    final Optional<EmailAddress> emailAddressIfFound = Iterables.tryFind(emailAddresses,
            EmailAddress.Predicates.equalTo(emailAddress));
    return emailAddressIfFound.orNull();
}

From source file:org.apache.drill.exec.store.AbstractRecordReader.java

public static boolean isStarQuery(Collection<SchemaPath> projected) {
    return Iterables
            .tryFind(Preconditions.checkNotNull(projected, COL_NULL_ERROR), new Predicate<SchemaPath>() {
                @Override//from  ww  w.  j  a  v a2s.com
                public boolean apply(SchemaPath path) {
                    return Preconditions.checkNotNull(path).equals(STAR_COLUMN);
                }
            }).isPresent();
}

From source file:net.seedboxer.core.logic.ContentManager.java

public void updateContents(User user, List<Content> toUpdate) {
    List<Content> userContents = contentDao.getAllContents(user);
    for (Content content : toUpdate) {
        Optional<Content> find = Iterables.tryFind(userContents, Predicates.equalTo(content));
        if (!find.isPresent()) {
            content.setUser(user);//from w w w  .ja  va 2  s  . co m
            contentDao.save(content);
            LOGGER.debug("New content {} for user {}", content.getName(), user.getId());
        }
    }
}

From source file:org.apache.brooklyn.entity.messaging.kafka.KafkaSupport.java

/**
 * Send a message to the {@link KafkaCluster} on the given topic.
 *///  w w  w. j a  v  a 2  s .  com
public void sendMessage(String topic, String message) {
    Optional<Entity> anyBrokerNodeInCluster = Iterables.tryFind(cluster.getCluster().getChildren(),
            Predicates.and(Predicates.instanceOf(KafkaBroker.class),
                    EntityPredicates.attributeEqualTo(KafkaBroker.SERVICE_UP, true)));
    if (anyBrokerNodeInCluster.isPresent()) {
        KafkaBroker broker = (KafkaBroker) anyBrokerNodeInCluster.get();

        Properties props = new Properties();

        props.put("metadata.broker.list",
                format("%s:%d", broker.getAttribute(KafkaBroker.HOSTNAME), broker.getKafkaPort()));
        props.put("bootstrap.servers",
                format("%s:%d", broker.getAttribute(KafkaBroker.HOSTNAME), broker.getKafkaPort()));
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(props);
        ((KafkaZooKeeper) cluster.getZooKeeper()).createTopic(topic);

        ProducerRecord<String, String> data = new ProducerRecord<>(topic, message);
        producer.send(data);
        producer.close();
    } else {
        throw new InvalidParameterException("No kafka broker node found");
    }
}

From source file:org.isisaddons.app.kitchensink.dom.hierarchy.HierarchyObjects.java

@Action(semantics = SemanticsOf.SAFE)
@MemberOrder(sequence = "10")
public ParentObject findParent(@ParameterLayout(named = "Title") final String title) {
    final Optional<ParentObject> parentObjectIfAny = Iterables.tryFind(parentObjects.listAll(),
            input -> container.titleOf(input).contains(title));
    return parentObjectIfAny.orNull();
}

From source file:io.druid.query.topn.NumericTopNMetricSpec.java

@Override
public void verifyPreconditions(List<AggregatorFactory> aggregatorSpecs,
        List<PostAggregator> postAggregatorSpecs) {
    Preconditions.checkNotNull(metric, "metric can't be null");
    Preconditions.checkNotNull(aggregatorSpecs, "aggregations cannot be null");
    Preconditions.checkArgument(aggregatorSpecs.size() > 0, "Must have at least one AggregatorFactory");

    final AggregatorFactory aggregator = Iterables.tryFind(aggregatorSpecs, new Predicate<AggregatorFactory>() {
        @Override//from  w ww .  j av  a  2  s .  c o  m
        public boolean apply(AggregatorFactory input) {
            return input.getName().equals(metric);
        }
    }).orNull();

    final PostAggregator postAggregator = Iterables
            .tryFind(postAggregatorSpecs, new Predicate<PostAggregator>() {
                @Override
                public boolean apply(PostAggregator input) {
                    return input.getName().equals(metric);
                }
            }).orNull();

    Preconditions.checkArgument(aggregator != null || postAggregator != null,
            "Must have an AggregatorFactory or PostAggregator for metric[%s], gave[%s] and [%s]", metric,
            aggregatorSpecs, postAggregatorSpecs);
}

From source file:org.ow2.petals.cloud.manager.commands.paas.BaseCommand.java

/**
 * Get the deployment provider from its type
 *
 * @param type/*from  ww w.j a v a  2  s  .  c o m*/
 * @return
 */
protected DeploymentProvider getDeploymentProvider(final String type) {
    checkNotNull(type);
    Optional<DeploymentProvider> support = Iterables.tryFind(supportedRuntimes,
            new Predicate<DeploymentProvider>() {
                public boolean apply(DeploymentProvider input) {
                    return type.equalsIgnoreCase(input.getType());
                }
            });
    return support.orNull();
}

From source file:org.apache.brooklyn.cloudfoundry.location.domain.VcapServiceRegistry.java

private Optional<VcapService> tryFindVcapDescription(String instanceName) {
    return Iterables.tryFind(vcapServices.values(), new Predicate<VcapService>() {
        @Override/*  ww  w.ja  v a  2  s  . c  o  m*/
        public boolean apply(VcapService input) {
            return instanceName.equals(input.getInstanceName());
        }
    });
}