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.eucalyptus.tokens.ws.TokensRequestLoggingFilter.java

@Override
public Collection<String> apply(final Collection<String> parametersOrBody) {
    if (Iterables.tryFind(ACTION_NVPS, Predicates.in(parametersOrBody)).isPresent()) {
        return parametersOrBody.stream()
                .map(parameterAndValue -> parameterAndValue.startsWith(WEB_ID_TOKEN_PARAMETER)
                        ? WEB_ID_TOKEN_PARAMETER_REDACTED
                        : parameterAndValue)
                .collect(Collectors.toList());
    }//from w w  w.j  a  v a2  s .c  o  m
    return parametersOrBody;
}

From source file:clocker.docker.networking.entity.OpenStackVirtualNetworkImpl.java

public NetworkProvisioningExtension findNetworkProvisioner(Collection<? extends Location> locations) {
    Optional<? extends Location> found = Iterables.tryFind(locations, new Predicate<Location>() {
        @Override//  ww w .j  a  v  a 2 s.c  o  m
        public boolean apply(Location input) {
            return input instanceof JcloudsLocation
                    && ((JcloudsLocation) input).getProvider().startsWith("openstack");
        }
    });
    if (!found.isPresent()) {
        throw new IllegalStateException(
                "Cannot find openstack location: " + Iterables.toString(getLocations()));
    }
    JcloudsLocation provisioner = (JcloudsLocation) found.get();
    NetworkProvisioningExtension extension = new OpenStackNetworkProvisioner(provisioner);
    return extension;
}

From source file:springfox.documentation.service.ObjectVendorExtension.java

public void replaceProperty(VendorExtension property) {
    Optional<VendorExtension> vendorProperty = Iterables.tryFind(properties, withName(property.getName()));
    if (vendorProperty.isPresent()) {
        properties.remove(vendorProperty.get());
    }/*  www  .  j a va2  s .com*/
    properties.add(property);
}

From source file:com.github.pmerienne.cf.block.BlockLocker.java

public synchronized Block getAndLockRandomBlock() {
    Block unlockedBlock = Iterables.tryFind(this.toProcesses, new Predicate<Block>() {
        @Override/*from ww  w  . j a va2 s . c  om*/
        public boolean apply(Block block) {
            return isUnlocked(block);
        }
    }).orNull();

    if (unlockedBlock != null) {
        this.lock(unlockedBlock);
    }

    return unlockedBlock;
}

From source file:com.palantir.giraffe.internal.SchemeProviderFinder.java

public Optional<P> find(String scheme) {
    MatchesScheme predicate = new MatchesScheme(scheme);

    Optional<P> provider = Iterables.tryFind(providers, predicate);
    if (!provider.isPresent() && fallbackLoader != null) {
        ServiceLoader<P> loader = ServiceLoader.load(providerClass, fallbackLoader);
        provider = Iterables.tryFind(loader, predicate);
    }//  w  w  w .jav  a 2 s . c  om

    return provider;
}

From source file:org.killbill.billing.plugin.api.PluginProperties.java

public static String findPluginPropertyValue(final String pluginPropertyName,
        @Nullable final Iterable<PluginProperty> properties) {
    if (properties == null) {
        return null;
    }//  ww  w. j a va 2  s  . c  o m

    final PluginProperty pluginProperty = Iterables.tryFind(properties, new Predicate<PluginProperty>() {
        @Override
        public boolean apply(final PluginProperty input) {
            return pluginPropertyName.equals(input.getKey());
        }
    }).orNull();

    if (pluginProperty == null || pluginProperty.getValue() == null) {
        return null;
    }
    final String pluginPropertyString = String.valueOf(pluginProperty.getValue());
    return Strings.isNullOrEmpty(pluginPropertyString) ? null : pluginPropertyString;
}

From source file:com.bennavetta.vetinari.build.internal.phase.RenderPhase.java

private Renderer getRenderer(Page page, Site site) {
    Renderer renderer = null;/*from ww w .  j a  v  a  2  s .c o m*/
    // The last file extension is always the renderer, if anything.
    final String extension = getFileExtension(page.getPath().toString());
    Optional<Renderer> rendererFromExtension = Iterables.tryFind(renderers,
            r -> Iterables.contains(r.getFileExtensions(), extension));
    if (rendererFromExtension.isPresent()) {
        renderer = rendererFromExtension.get();
    } else if (page.getMetadata().hasPath("renderer")) {
        final String rendererName = page.getMetadata().getString("renderer");
        renderer = Iterables.find(renderers, r -> rendererName.equals(r.getName()));
    } else {
        renderer = Iterables.find(renderers, r -> site.getDefaultRenderer().equals(r.getName()));
    }
    return renderer;
}

From source file:pt.ist.maidSyncher.domain.dsi.DSIMilestone.java

public GHMilestone getGhMilestone(final GHRepository ghRepository) {
    Optional<GHMilestone> optionalMilestone = Iterables.tryFind(getGhMilestonesSet(),
            new Predicate<GHMilestone>() {
                @Override/*  www . j  a  va 2 s  . c o  m*/
                public boolean apply(GHMilestone input) {
                    if (input == null)
                        return false;
                    return ObjectUtils.equals(input.getRepository(), ghRepository);
                }
            });
    return optionalMilestone.orNull();
}

From source file:greensopinion.swagger.jaxrsgen.mock.model.ServerError.java

private String computeMessage(Throwable t) {
    Optional<Throwable> found = Iterables.tryFind(Throwables.getCausalChain(t), new Predicate<Throwable>() {
        @Override/*from  w w w .jav a2s  .c o  m*/
        public boolean apply(Throwable input) {
            return input != null && !Strings.isNullOrEmpty(input.getMessage());
        }
    });
    if (found.isPresent()) {
        return found.get().getMessage();
    }
    return null;
}

From source file:com.github.jeluard.guayaba.base.PartialFunctions.java

/**
 * Find first {@link PartialFunction} applicable on `element`.
 *
 * @param <I>// w  w w  .  java 2  s.  c  om
 * @param <O>
 * @param <PF>
 * @param partialFunctions
 * @param element
 * @return result of first matching {@link PartialFunction}
 */
public static <I, O, PF extends PartialFunction<I, ? extends O>> Optional<PF> tryFind(
        final Iterable<PF> partialFunctions, final I element) {
    Preconditions.checkNotNull(partialFunctions, "null partialFunctions");
    Preconditions.checkNotNull(element, "null element");

    return Iterables.tryFind(partialFunctions, new DefinedPredicate(element));
}