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

@Nullable
public static <T> T find(Iterable<? extends T> iterable, Predicate<? super T> predicate,
        @Nullable T defaultValue) 

Source Link

Document

Returns the first element in iterable that satisfies the given predicate, or defaultValue if none found.

Usage

From source file:org.polarsys.reqcycle.traceability.cache.emfbased.CacheTraceabilityEngine.java

private AnalyzedResource getResource(final Reachable traceable) {
    return Iterables.find(theModel.getResources(), new Predicate<AnalyzedResource>() {
        @Override//from w  w w  .j a v  a  2s .  co  m
        public boolean apply(AnalyzedResource a) {
            return a.getUri().equals(traceable.trimFragment().toString());
        }
    }, null);
}

From source file:com.eucalyptus.cluster.callback.VmRunCallback.java

private Address getAddress() {
    final PublicIPResource publicIPResource = (PublicIPResource) Iterables.find(
            VmRunCallback.this.token.getAttribute(NetworkResourceVmInstanceLifecycleHelper.NetworkResourcesKey),
            Predicates.instanceOf(PublicIPResource.class), null);
    return publicIPResource != null && publicIPResource.getValue() != null
            ? Addresses.getInstance().lookup(publicIPResource.getValue())
            : null;//from  w  ww. j  av  a  2 s  .c  om
}

From source file:org.jfrog.build.api.builder.BuildInfoMavenBuilder.java

private Artifact findArtifact(List<Artifact> existingArtifacts, final String artifactKey) {
    return Iterables.find(existingArtifacts, new Predicate<Artifact>() {
        public boolean apply(Artifact input) {
            return input.getName().equals(artifactKey);
        }//from w  w  w.j a v  a2s.c  o  m
    }, null);
}

From source file:msi.gama.metamodel.population.GamaPopulation.java

@Override
public T getAgent(final Integer index) {
    return Iterables.find(this, each -> each.getIndex() == index, null);
}

From source file:io.crate.types.DataTypes.java

/**
 * Returns the first data type that is not {@link UNDEFINED}, or {@code UNDEFINED} if none found.
 *///w  w w  .  j  a v  a  2s .  c om
public static DataType tryFindNotNullType(Iterable<? extends DataType> dataTypes) {
    return Iterables.find(dataTypes, NOT_NULL_TYPE_FILTER, UNDEFINED);
}

From source file:org.killbill.billing.plugin.analytics.dao.factory.BusinessInvoiceFactory.java

private BusinessInvoiceItemBaseModelDao createBusinessInvoiceItem(
        final BusinessContextFactory businessContextFactory, final Account account, final Invoice invoice,
        final InvoiceItem invoiceItem, final Collection<InvoiceItem> otherInvoiceItems,
        final Map<UUID, SubscriptionBundle> bundles, final CurrencyConverter currencyConverter,
        final AuditLog creationAuditLog, final Long accountRecordId, final Long tenantRecordId,
        @Nullable final ReportGroup reportGroup) throws AnalyticsRefreshException {
    // For convenience, populate empty columns using the linked item
    final InvoiceItem linkedInvoiceItem = Iterables.find(otherInvoiceItems, new Predicate<InvoiceItem>() {
        @Override//from   www . ja  va 2  s .  c o m
        public boolean apply(final InvoiceItem input) {
            return invoiceItem.getLinkedItemId() != null && invoiceItem.getLinkedItemId().equals(input.getId());
        }
    }, null);

    SubscriptionBundle bundle = null;
    // Subscription and bundle could be null for e.g. credits or adjustments
    if (invoiceItem.getBundleId() != null) {
        bundle = bundles.get(invoiceItem.getBundleId());
    }
    if (bundle == null && linkedInvoiceItem != null && linkedInvoiceItem.getBundleId() != null) {
        bundle = bundles.get(linkedInvoiceItem.getBundleId());
    }

    Plan plan = null;
    if (Strings.emptyToNull(invoiceItem.getPlanName()) != null) {
        plan = businessContextFactory.getPlanFromInvoiceItem(invoiceItem);
    }
    if (plan == null && linkedInvoiceItem != null
            && Strings.emptyToNull(linkedInvoiceItem.getPlanName()) != null) {
        plan = businessContextFactory.getPlanFromInvoiceItem(linkedInvoiceItem);
    }

    PlanPhase planPhase = null;

    if (invoiceItem.getSubscriptionId() != null && Strings.emptyToNull(invoiceItem.getPhaseName()) != null
            && bundle != null) {
        final LocalDate subscriptionStartDate = getSubscriptionStartDate(invoiceItem, bundle);
        if (subscriptionStartDate != null) {
            planPhase = businessContextFactory.getPlanPhaseFromInvoiceItem(invoiceItem, subscriptionStartDate);
        }
    }
    if (planPhase == null && linkedInvoiceItem != null && linkedInvoiceItem.getSubscriptionId() != null
            && Strings.emptyToNull(linkedInvoiceItem.getPhaseName()) != null && bundle != null) {
        final LocalDate subscriptionStartDate = getSubscriptionStartDate(linkedInvoiceItem, bundle);
        if (subscriptionStartDate != null) {
            planPhase = businessContextFactory.getPlanPhaseFromInvoiceItem(linkedInvoiceItem,
                    subscriptionStartDate);
        }
    }

    final Long invoiceItemRecordId = invoiceItem.getId() != null
            ? businessContextFactory.getInvoiceItemRecordId(invoiceItem.getId())
            : null;

    return createBusinessInvoiceItem(account, invoice, invoiceItem, otherInvoiceItems, bundle, plan, planPhase,
            invoiceItemRecordId, currencyConverter, creationAuditLog, accountRecordId, tenantRecordId,
            reportGroup);
}

From source file:org.opentestsystem.delivery.testreg.service.impl.StudentGroupServiceImpl.java

private StudentGroup findStudentGroupInACollection(final List<T> studentGroups, final StudentGroup domainObj) {
    if (CollectionUtils.isEmpty(studentGroups)) {
        return null;
    }/*www  .  j a v  a2s. com*/
    final StudentGroup sg = Iterables.find(studentGroups, new Predicate<StudentGroup>() {
        @Override
        public boolean apply(final StudentGroup studentGroup) {
            return com.google.common.base.Objects.equal(studentGroup.getStudentGroupName(),
                    domainObj.getStudentGroupName())
                    && com.google.common.base.Objects.equal(studentGroup.getInstitutionIdentifier(),
                            domainObj.getInstitutionIdentifier())
                    && com.google.common.base.Objects.equal(studentGroup.getStateAbbreviation(),
                            domainObj.getStateAbbreviation());
        }
    }, null);
    return sg;
}

From source file:com.github.nethad.clustermeister.provisioning.ec2.AmazonNodeManager.java

private AmazonNode getDriverForNode(final AmazonNode node) {
    managedNodesMonitor.enter();//from   w w  w  .j  a v  a  2 s .  com
    try {
        return Iterables.find(drivers, new Predicate<AmazonNode>() {
            @Override
            public boolean apply(AmazonNode driver) {
                String driverAddress = Iterables.getFirst(driver.getPrivateAddresses(), null);
                checkNotNull(driverAddress);
                return node.getDriverAddress().equals(driverAddress);
            }
        }, null);
    } finally {
        managedNodesMonitor.leave();
    }
}

From source file:org.solovyev.android.messenger.realms.sms.SmsAccountConnection.java

@Nullable
private static User findContactByPhone(@Nonnull final String phone, @Nonnull List<User> contacts) {
    final SamePhonePredicate predicate = new SamePhonePredicate(newPhoneNumber(phone));

    return Iterables.find(contacts, new Predicate<User>() {
        @Override/*from   w  w w  .  j  a  va  2s  . com*/
        public boolean apply(@Nullable User contact) {
            if (contact != null) {
                // first try to find by default phone property
                if (predicate.apply(contact.getPropertyValueByName(PROPERTY_PHONE))) {
                    return true;
                } else {
                    return any(contact.getPhoneNumbers(), predicate);
                }
            } else {
                return false;
            }
        }
    }, null);
}

From source file:org.polarsys.reqcycle.typesmodel.handler.IniManagerRegistry.java

private boolean fileDeleted(IFile resource) {
    boolean result = false;
    final String resourceName = getMethodName(resource);
    FileType ft = Iterables.find(typeModel.getFileTypes(), new Predicate<FileType>() {
        @Override/*from   www. j  a v a 2 s  . c om*/
        public boolean apply(FileType arg0) {
            return Objects.equal(resourceName, arg0.getName());
        }
    }, null);
    if (ft != null) {
        EcoreUtil.delete(ft);
    }
    return result;
}