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.google.cloud.tools.intellij.appengine.cloud.AppEngineArtifactDeploymentSourceType.java

@NotNull
@Override/*from w w  w  .ja  v a 2 s. c  om*/
public AppEngineArtifactDeploymentSource load(@NotNull Element tag, @NotNull Project project) {
    final String artifactName = tag.getAttributeValue(NAME_ATTRIBUTE);
    Element settings = tag.getChild(DeployToServerRunConfiguration.SETTINGS_ELEMENT);

    if (settings != null) {
        Artifact[] artifacts = ArtifactManager.getInstance(project).getArtifacts();
        Optional<Artifact> artifact = Iterables.tryFind(Arrays.asList(artifacts), new Predicate<Artifact>() {
            @Override
            public boolean apply(Artifact artifact) {
                return artifact.getName().equals(artifactName);
            }
        });

        String environment = settings.getAttributeValue(AppEngineDeploymentConfiguration.ENVIRONMENT_ATTRIBUTE);

        if (artifact.isPresent() && environment != null) {
            return new AppEngineArtifactDeploymentSource(AppEngineEnvironment.valueOf(environment),
                    ArtifactPointerManager.getInstance(project).createPointer(artifact.get()));
        }
    }

    return new AppEngineArtifactDeploymentSource(
            ArtifactPointerManager.getInstance(project).createPointer(artifactName));
}

From source file:org.apache.brooklyn.location.jclouds.JcloudsProviderAndApiLoader.java

public static Optional<ProviderMetadata> getProvider(String id) {
    if (LazyServiceLoader.INSTANCE.providers.containsKey(id)) {
        return Optional.of(LazyServiceLoader.INSTANCE.providers.get(id));
    }/*from w  w  w. ja  v a  2s .  co m*/
    return Iterables.tryFind(ProviderRegistry.fromRegistry(), ProviderPredicates.id(id));
}

From source file:org.ow2.play.governance.platform.user.service.rest.AbstractService.java

/**
 * Get a user resource if present in the profile, or null if not found.
 * /*from  w  w  w  . java2  s  . co m*/
 * @param uri
 * @return
 */
protected Resource getUserResource(final String uri, final String name) {
    Optional<Resource> optional = Iterables.tryFind(getUser().resources, new Predicate<Resource>() {
        public boolean apply(Resource input) {
            return input.uri != null && input.uri.equals(uri) && input.name != null && input.name.equals(name);
        }
    });
    if (!optional.isPresent()) {
        return null;
    }
    return optional.get();
}

From source file:com.eucalyptus.cloudformation.ws.CloudFormationRequestLoggingFilter.java

@Override
public Collection<String> apply(final Collection<String> parametersOrBody) {
    if (Iterables.tryFind(ACTION_NVPS, Predicates.in(parametersOrBody)).isPresent()) {
        final Iterable<String> templateBodyNVPs = Iterables.filter(parametersOrBody,
                Predicates.contains(TEMPLATE_BODY_PARAMETER_REGEX));

        final Iterable<String> parametersNVPs = Iterables.filter(parametersOrBody,
                Predicates.contains(PARAMETERS_PARAMETER_REGEX));

        if (!Iterables.isEmpty(templateBodyNVPs) || !Iterables.isEmpty(parametersNVPs)) {
            final ArrayList<String> parametersCopy = Lists.newArrayList(parametersOrBody);
            redactParameters(parametersCopy, templateBodyNVPs, TEMPLATE_BODY_PARAMETER_REGEX);
            redactParameters(parametersCopy, parametersNVPs, PARAMETERS_PARAMETER_REGEX);
            return parametersCopy;
        }/*  w w  w. j  a  v a 2  s .  co m*/
    }

    return parametersOrBody;
}

From source file:org.killbill.billing.invoice.tree.ItemsInterval.java

public Item getCancellingItemIfExists(final UUID targetId) {
    return Iterables.tryFind(items, new Predicate<Item>() {
        @Override//w  w w .  java  2 s .c o m
        public boolean apply(final Item input) {
            return input.getAction() == ItemAction.CANCEL && input.getLinkedId().equals(targetId);
        }
    }).orNull();
}

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

private Optional<PhoneOrFaxNumber> findByPhoneOrFaxNumber(final CommunicationChannelOwner owner,
        final String phoneNumber, final CommunicationChannelType communicationChannelType) {
    final List<CommunicationChannelOwnerLink> links = communicationChannelOwnerLinks
            .findByOwnerAndCommunicationChannelType(owner, communicationChannelType);
    final Iterable<PhoneOrFaxNumber> phoneOrFaxNumbers = Iterables.transform(links,
            CommunicationChannelOwnerLink.Functions.communicationChannel(PhoneOrFaxNumber.class));
    return Iterables.tryFind(phoneOrFaxNumbers,
            PhoneOrFaxNumber.Predicates.equalTo(phoneNumber, communicationChannelType));
}

From source file:com.arcbees.beestore.client.application.CurrentOrderImpl.java

private ShoppingCartItem getItemFromCart(final ShoppingCartItem item) {
    return Iterables.tryFind(items, shoppingCartItem -> shoppingCartItem.equals(item)).orNull();
}

From source file:com.android.tools.idea.databinding.DataBindingComponentClassFinder.java

@Nullable
@Override//from  w  ww  . j a  v  a 2s.  co  m
public PsiClass findClass(@NotNull String qualifiedName, @NotNull final GlobalSearchScope scope) {
    if (!isEnabled() || !SdkConstants.CLASS_DATA_BINDING_COMPONENT.equals(qualifiedName)) {
        return null;
    }
    return Iterables.tryFind(myClasses.getValue(), input -> check(input, scope)).orNull();
}

From source file:org.kuali.rice.kew.impl.document.search.FormFields.java

Field getField(final String name) {
    return Iterables.tryFind(getFields(), new Predicate<Field>() {
        public boolean apply(@Nullable Field input) {
            return StringUtils.equals(name, input.getPropertyName());
        }// www  .j a  v a 2s.c  o  m
    }).orNull();
}

From source file:org.opendaylight.protocol.pcep.auto.bandwidth.extension.PcRptMessageCodec.java

@Override
protected Reports getValidReports(final List<Object> objects, final List<Message> errors) {
    final Optional<Object> find = Iterables.tryFind(objects, Predicates.instanceOf(BandwidthUsage.class));
    final Object object;
    if (find.isPresent()) {
        object = find.get();/*from  w w  w .  j  a  v a2s .  com*/
        objects.remove(object);
    } else {
        object = null;
    }
    final Reports validReports = super.getValidReports(objects, errors);
    if (object != null && validReports != null) {
        final Path path = validReports.getPath();
        if (path != null) {
            return new ReportsBuilder(validReports).setPath(new PathBuilder(path)
                    .setBandwidth(setBandwidthUsage(path.getBandwidth(), (BandwidthUsage) object)).build())
                    .build();
        }
    }
    return validReports;
}