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.gogrid.compute.functions.ServerToNodeMetadata.java

protected Image parseImage(Server from) {
    Image image = null;//w w w .  j a v a2s .  c o  m
    try {
        image = Iterables.find(images.get(), new FindImageForServer(from));
    } catch (NoSuchElementException e) {
        logger.debug("could not find a matching image for server %s", from);
    }
    return image;
}

From source file:org.eclipse.osee.orcs.core.internal.relation.order.OrderParser.java

private void process(XMLStreamReader reader, HasOrderData hasOrderData) throws OseeCoreException {
    int eventType = reader.getEventType();
    switch (eventType) {
    case XMLStreamConstants.START_ELEMENT:
        String localName = reader.getLocalName();
        String uri = reader.getNamespaceURI();
        if (START_TAG.equals(localName)) {
            final String relationTypeName = reader.getAttributeValue(uri, RELATION_TYPE_TAG);
            String orderType = reader.getAttributeValue(uri, ORDER_TYPE_TAG);
            String relationSide = reader.getAttributeValue(uri, SIDE_TAG);
            String rawList = reader.getAttributeValue(uri, LIST_TAG);
            if (relationTypeName != null && orderType != null && relationSide != null) {
                List<String> list = Collections.emptyList();
                if (rawList != null) {
                    list = new ArrayList<String>();
                    StringTokenizer tokenizer = new StringTokenizer(rawList, ",");
                    while (tokenizer.hasMoreTokens()) {
                        list.add(tokenizer.nextToken());
                    }//from   ww  w . j  av  a2s  . c om
                }

                // TODO don't store relation type by name - use type UUID
                IRelationType type = Iterables.find(relationCache.getAll(), new Predicate<IRelationType>() {
                    @Override
                    public boolean apply(IRelationType type) {
                        return type.getName().equalsIgnoreCase(relationTypeName);
                    }
                });

                RelationSide side = RelationSide.fromString(relationSide);
                IRelationTypeSide typeSide = TokenFactory.createRelationTypeSide(side, type.getGuid(),
                        type.getName());
                IRelationSorterId sorterId = RelationOrderBaseTypes.getFromGuid(orderType);
                OrderData orderData = new OrderData(sorterId, list);
                hasOrderData.add(typeSide, orderData);
            }
        }
        break;
    default:
        break;
    }
}

From source file:org.eclipselabs.spray.xtext.validation.SprayJavaValidator.java

@Check
public void checkDuplicateConnectionReferences(final MetaClass element) {
    MetaReference found = null;/*from w  ww.j  a v a2  s.co m*/
    for (MetaReference ref : element.getReferences()) {
        found = Iterables.find(Arrays.asList(element.getReferences()),
                getDuplicateConnectionReferenceFilter(ref));
        if (found != null && found != ref) {
            String referenceName = getReferenceName(ref);
            error("Duplicate connection reference: " + referenceName, element,
                    SprayPackage.Literals.META_CLASS__REFERENCES, IssueCodes.DUPLICATE_CONNECTION_REFERENCE,
                    referenceName);
        }
    }
}

From source file:org.apache.brooklyn.entity.database.mysql.MySqlClusterTestHelper.java

public static void test(TestApplication app, Location location, EntitySpec<MySqlCluster> clusterSpec)
        throws Exception {
    MySqlCluster cluster = initCluster(app, location, clusterSpec);
    MySqlNode master = (MySqlNode) cluster.getAttribute(MySqlCluster.FIRST);
    MySqlNode slave = (MySqlNode) Iterables.find(cluster.getMembers(),
            Predicates.not(Predicates.<Entity>equalTo(master)));
    assertEquals(cluster.getMembers().size(), 2);
    assertEquals(cluster.getAttribute(MySqlCluster.SLAVE_DATASTORE_URL_LIST).size(), 1);
    assertEquals(cluster.getAttribute(MySqlNode.DATASTORE_URL), master.getAttribute(MySqlNode.DATASTORE_URL));
    assertReplication(master, slave);//from www.  j a  va 2  s  . c om
}

From source file:com.marvelution.bamboo.plugins.sonar.tasks.web.contextproviders.SonarConfigurationContextProvider.java

/**
 * Get the {@link SonarConfiguration} object from the given {@link Job}s {@link List}
 * //from ww  w . jav  a  2s . c  o m
 * @param jobs the {@link Job} {@link List} to get the {@link SonarConfiguration} from
 * @return the {@link SonarConfiguration}
 */
private SonarConfiguration getSonarConfigurationFromJobs(List<Job> jobs) {
    SonarConfiguration config = new SonarConfiguration();
    for (Job job : jobs) {
        TaskDefinition taskDefinition = Iterables.find(job.getBuildDefinition().getTaskDefinitions(),
                SonarPredicates.isSonarTask());
        if (taskDefinition != null) {
            // Copy the Sonar Host configuration form the task definition
            config.setHost(taskDefinition.getConfiguration().get(CFG_SONAR_HOST_URL));
            config.setUsername(taskDefinition.getConfiguration().get(CFG_SONAR_HOST_USERNAME));
            config.setPassword(
                    ENCRYPTOR.decrypt(taskDefinition.getConfiguration().get(CFG_SONAR_HOST_PASSWORD)));
            // And get the Sonar project key and name form the job build results
            for (ResultsSummary buildResult : summaryManager
                    .getResultSummaries(new ResultsSummaryCriteria(job.getKey(), false))) {
                LOGGER.debug("Checking result of build: " + buildResult.getBuildKey() + " #"
                        + buildResult.getBuildNumber());
                if (buildResult.getCustomBuildData().containsKey(TRD_SONAR_PROJECT_KEY)) {
                    config.setProjectKey(buildResult.getCustomBuildData().get(TRD_SONAR_PROJECT_KEY));
                    config.setProjectName(buildResult.getCustomBuildData().get(TRD_SONAR_PROJECT_NAME));
                    break;
                }
            }
            LOGGER.debug("Found Configuration " + config.toString());
            break;
        }
    }
    return config;
}

From source file:org.jclouds.rimuhosting.miro.compute.functions.ServerToNodeMetadata.java

protected OperatingSystem parseOperatingSystem(Server from, Location location) {
    try {/*from  w w w  .  j a va 2  s .c  o  m*/
        return Iterables.find(images.get(), new FindImageForServer(location, from)).getOperatingSystem();
    } catch (NoSuchElementException e) {
        logger.warn("could not find a matching image for server %s in location %s", from, location);
    }
    return null;
}

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

protected Hardware parseHardware(Server from) {
    try {// www .j av  a2  s  . c  o  m
        return Iterables.find(hardwares.get(), new FindHardwareForServer(from));
    } catch (NoSuchElementException e) {
        logger.debug("could not find a matching hardware for server %s", from);
    }
    return null;
}

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

protected OperatingSystem parseOperatingSystem(Slice from) {
    try {/*from  www  .j a v a2  s.c  o  m*/
        return Iterables.find(images.get(), new FindImageForSlice(from)).getOperatingSystem();
    } catch (NoSuchElementException e) {
        logger.warn("could not find a matching image for slice %s in location %s", from, location);
    }
    return null;
}

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

protected OperatingSystem parseOperatingSystem(Server from) {
    try {//from w  w w  .j a va2  s. c o m
        return Iterables.find(images.get(), new FindImageForServer(from)).getOperatingSystem();
    } catch (NoSuchElementException e) {
        logger.warn("could not find a matching image for server %s in location %s", from, location);
    }
    return null;
}

From source file:com.infinities.nova.volumes.api.DaseinVolumesApi.java

/**
 * @param volume//from w w  w.ja v  a  2 s. c  o  m
 * @param iterable
 * @return
 */
private Volume toVolume(org.dasein.cloud.compute.Volume volume, Iterable<VolumeProduct> iterable) {
    final Volume ret = new Volume();
    ret.setAvailabilityZone(volume.getProviderDataCenterId());
    Calendar createdAt = Calendar.getInstance();
    createdAt.setTimeInMillis(volume.getCreationTimestamp());
    ret.setCreatedAt(createdAt);
    ret.setDescription(volume.getDescription());
    ret.setId(volume.getProviderVolumeId());
    ret.setMetadata(volume.getTags());
    ret.setName(volume.getName());
    ret.setSize(volume.getSizeInGigabytes());
    ret.setSnapshotId(volume.getProviderSnapshotId());
    ret.setStatus(volume.getCurrentState().name());
    ret.setVolumeType(volume.getProviderProductId());
    try {
        VolumeProduct product = Iterables.find(iterable, new Predicate<VolumeProduct>() {

            @Override
            public boolean apply(VolumeProduct input) {
                return input.getProviderProductId().equals(ret.getVolumeType());
            }
        });
        ret.setVolumeType(product.getName());
    } catch (NoSuchElementException e) {
        ret.setVolumeType(null);
        // ignore
    }

    List<VolumeAttachment> attachments = new ArrayList<VolumeAttachment>();
    if (!Strings.isNullOrEmpty(volume.getProviderVirtualMachineId())
            && !Strings.isNullOrEmpty(volume.getDeviceId())) {
        VolumeAttachment attachment = new VolumeAttachment();
        attachment.setId(volume.getProviderVolumeId());
        attachment.setDevice(volume.getDeviceId());
        attachment.setVolumeId(volume.getProviderVolumeId());
        attachment.setServerId(volume.getProviderVirtualMachineId());
        attachments.add(attachment);
    }
    ret.setAttachments(attachments);

    return ret;
}