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:com.googlecode.efactory.util.Resources.java

public static <T extends EObject> T getRoot(Resource resource, final Class<T> type) {
    if (resource != null) {
        return type.cast(Iterables.find(resource.getContents(), new Predicate<EObject>() {

            public boolean apply(EObject input) {
                return type.isInstance(input);
            }//w  w w  .j a  v a2 s  . c o  m
        }));
    }
    throw new IllegalArgumentException("Resource must not be null");
}

From source file:org.eclipse.sirius.tests.unit.contribution.Freezer.java

public static void thaw(EObject obj) {
    try {/* w w  w  .  j a  v a2s .co m*/
        Adapter f = Iterables.find(obj.eAdapters(), Predicates.instanceOf(Freezer.class));
        obj.eAdapters().remove(f);
    } catch (NoSuchElementException nsee) {
        // Ignore.
    }
}

From source file:org.jclouds.karaf.recipe.RecipeProviders.java

/**
 * Returns the {@link RecipeProvider} with id.
 * @param id/* w  w  w. j  ava 2 s  .  c o  m*/
 * @return
 * @throws NoSuchElementException
 */
public static RecipeProvider withId(String id) throws NoSuchElementException {
    return Iterables.find(all(), RecipeProviderPredicates.id(id));
}

From source file:org.opendaylight.protocol.bgp.rib.impl.IdentifierUtils.java

private static NodeIdentifierWithPredicates firstKeyOf(final YangInstanceIdentifier id,
        final Predicate<PathArgument> match) {
    final PathArgument ret = Iterables.find(id.getPathArguments(), match);
    Preconditions.checkArgument(ret instanceof NodeIdentifierWithPredicates, "Non-key peer identifier %s", ret);
    return (NodeIdentifierWithPredicates) ret;
}

From source file:com.atlassian.jira.rest.client.domain.EntityHelper.java

public static <T extends NamedEntity> T findEntityByName(Iterable<T> entities, final String name) {
    return Iterables.find(entities, HasNamePredicate.forName(name));
}

From source file:io.joynr.generator.loading.Query.java

public <T extends EObject> T find(String name, Class<T> type) {
    return Iterables.find(all(type), name(name));
}

From source file:com.google.jenkins.plugins.dsl.YamlHistoryAction.java

/**
 * Search the actions of the {@link YamlBuild} for a {@link YamlHistoryAction}
 *
 * @return the action, or null if not found (or no build was passed)
 *///from   w w  w  .  j a  v a 2 s . co m
@Nullable
public static YamlHistoryAction of(YamlBuild build) {
    if (build == null) {
        return null;
    }
    try {
        return (YamlHistoryAction) Iterables.find(build.getRawActions(),
                Predicates.instanceOf(YamlHistoryAction.class));
    } catch (NoSuchElementException e) {
        return null;
    }
}

From source file:org.jclouds.chef.util.ChefUtils.java

/**
 * //w w w.  j  a  v a2 s.  co  m
 * @return NoSuchElementException if no element in the runList is a role.
 */
public static String findRoleInRunList(List<String> runList) {
    final Pattern pattern = Pattern.compile("^role\\[(.*)\\]$");
    String roleToParse = Iterables.find(runList, new Predicate<String>() {

        @Override
        public boolean apply(String input) {
            return pattern.matcher(input).matches();
        }

    });
    Matcher matcher = pattern.matcher(roleToParse);
    matcher.find();
    return matcher.group(1);
}

From source file:org.yakindu.sct.generator.core.extensions.FileExtensions.java

public static String getFileExtension(final String generatorId) {
    FileExtensionDescriptor descriptor = Iterables.find(getFileExtensions(),
            new Predicate<FileExtensionDescriptor>() {
                public boolean apply(FileExtensionDescriptor input) {
                    return generatorId.equals(input.getGeneratorId());
                }// ww w .  ja  v  a 2 s. c om
            });
    return descriptor.getExtension();
}

From source file:org.apache.beam.runners.spark.aggregators.metrics.sink.InMemoryMetrics.java

@SuppressWarnings({ "unchecked", "WeakerAccess" })
public static <T> T valueOf(final String name) {
    final T retVal;

    // this might fail in case we have multiple aggregators with the same suffix after
    // the last dot, but it should be good enough for tests.
    if (extendedMetricsRegistry != null && Iterables.any(extendedMetricsRegistry.getGauges().keySet(),
            Predicates.containsPattern(name + "$"))) {
        String key = Iterables.find(extendedMetricsRegistry.getGauges().keySet(),
                Predicates.containsPattern(name + "$"));
        retVal = (T) extendedMetricsRegistry.getGauges().get(key).getValue();
    } else {//from  w  ww. j a  v a  2s.c  o m
        retVal = null;
    }

    return retVal;
}