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.bennavetta.vetinari.build.internal.phase.PathGenerationPhase.java

private PathGenerator getPathGenerator(Page page) {
    final String generatorName = page.getMetadata().hasPath("pathGenerator")
            ? page.getMetadata().getString("pathGenerator")
            : context.getSiteConfig().getString("defaultPathGenerator");

    return Iterables.find(pathGenerators, generator -> generatorName.equals(generator.getName()));
}

From source file:gov.nih.nci.caarray.magetab.sdrf.SdrfColumns.java

/**
 * Return the column most closely preceding the given column that is a node column.
 * /*from  w w w .  j  ava2  s . c  o  m*/
 * @param column the column for which to find the preceding node column
 * @return the preceding node column, or null if there is none
 */
public SdrfColumn getPreviousNodeColumn(SdrfColumn column) {
    int columnIndex = columns.indexOf(column);
    if (columnIndex < 1) {
        return null;
    }

    try {
        return Iterables.find(Iterables.reverse(columns.subList(0, columnIndex)), new Predicate<SdrfColumn>() {
            public boolean apply(SdrfColumn c) {
                return c.getType().isNode();
            }
        });
    } catch (NoSuchElementException e) {
        return null;
    }
}

From source file:org.richfaces.cdk.apt.SourceUtilsTestBase.java

protected Element findElement(RoundEnvironment roundEnvironment, final String name) {
    Set<? extends Element> elements = roundEnvironment.getRootElements();
    return Iterables.find(elements, new Predicate<Element>() {
        @Override/*from ww  w  .  j a v  a2  s . c om*/
        public boolean apply(Element input) {
            return name.equals(input.getSimpleName().toString());
        }
    });
}

From source file:org.apache.brooklyn.core.mgmt.rebind.RebindTestFixtureWithApp.java

@Override
protected TestApplication rebind(RebindOptions options) throws Exception {
    if (options.applicationChooserOnRebind == null) {
        // Some sub-classes will have added additional apps before rebind. We must return an
        // app of type "TestApplication"; otherwise we'll get a class-cast exception.
        options = RebindOptions.create(options);
        options.applicationChooserOnRebind(new Function<Collection<Application>, Application>() {
            @Override/*from w w w.  j a va  2  s  .com*/
            public Application apply(Collection<Application> input) {
                return Iterables.find(input, Predicates.instanceOf(TestApplication.class));
            }
        });
    }
    return super.rebind(options);
}

From source file:org.jclouds.vcloud.director.v1_5.functions.OrgNameCatalogNameItemNameToEndpoint.java

@SuppressWarnings("unchecked")
public URI apply(Object from) {
    Iterable<Object> orgCatalog = (Iterable<Object>) checkNotNull(from, "args");
    Object org = Iterables.get(orgCatalog, 0);
    Object catalog = Iterables.get(orgCatalog, 1);
    Object catalogItem = Iterables.get(orgCatalog, 2);
    if (org == null)
        org = defaultOrg.get().getName();
    if (catalog == null)
        catalog = defaultCatalog.get().getName();
    try {/*from w  w  w  .jav a2  s. c  o m*/
        Map<String, Catalog> catalogs = checkNotNull(orgCatalogMap.get().get(org));
        return Iterables.find(catalogs.get(catalog).getCatalogItems(), nameEquals((String) catalogItem))
                .getHref();
    } catch (NullPointerException e) {
        throw new NoSuchElementException(
                org + "/" + catalog + "/" + catalogItem + " not found in " + orgCatalogMap.get());
    }
}

From source file:org.jclouds.vcloud.director.v1_5.functions.OrgNameAndVdcNameToEndpoint.java

@SuppressWarnings("unchecked")
public URI apply(Object from) {
    Iterable<Object> orgVdc = (Iterable<Object>) checkNotNull(from, "args");
    Object org = Iterables.get(orgVdc, 0);
    Object vdc = Iterables.get(orgVdc, 1);
    if (org == null && vdc == null)
        return defaultVdc.get().getHref();
    else if (org == null)
        org = defaultOrg.get().getName();

    try {//from w ww . j av  a2s .  co m
        Set<Reference> vdcs = checkNotNull(orgNameToVdcEndpoint.get().get(org)).getVdcs();
        return vdc == null ? Iterables.getLast(vdcs).getHref()
                : Iterables.find(vdcs, nameEquals((String) vdc)).getHref();
    } catch (NullPointerException e) {
        throw new NoSuchElementException(org + "/" + vdc + " not found in " + orgNameToVdcEndpoint.get());
    }
}

From source file:org.jclouds.vcloud.director.v1_5.functions.OrgNameAndCatalogNameToEndpoint.java

@SuppressWarnings("unchecked")
public URI apply(Object from) {
    Iterable<Object> orgCatalog = (Iterable<Object>) checkNotNull(from, "args");
    Object org = Iterables.get(orgCatalog, 0);
    Object catalog = Iterables.get(orgCatalog, 1);
    if (org == null && catalog == null)
        return defaultCatalog.get().getHref();
    else if (org == null)
        org = defaultOrg.get().getName();

    try {//  w ww  .j ava  2 s  .c  o m
        Set<Reference> catalogs = checkNotNull(orgMap.get().get(org)).getCatalogs();
        return catalog == null ? Iterables.getLast(catalogs).getHref()
                : Iterables.find(catalogs, nameEquals((String) catalog)).getHref();
    } catch (NullPointerException e) {
        throw new NoSuchElementException(org + "/" + catalog + " not found in " + orgMap.get());
    }
}

From source file:org.apache.isis.viewer.wicket.ui.selector.SelectorPanelAbstract.java

private void addUnderlyingViews(final String underlyingId, final T model, final ComponentFactory factory) {
    final List<ComponentFactory> componentFactories = findOtherComponentFactories(model, factory);

    final ComponentFactory selectedComponentFactory = Iterables.find(componentFactories,
            determineInitialFactory(model));
    if (componentFactories.size() > 1) {
        final Model<ComponentFactory> componentFactoryModel = new Model<ComponentFactory>();

        componentFactoryModel.setObject(selectedComponentFactory);

        final WebMarkupContainer views = new WebMarkupContainer(ID_VIEWS);
        final DropDownChoiceComponentFactory viewsDropDown = new DropDownChoiceComponentFactory(
                ID_VIEWS_DROP_DOWN, componentFactoryModel, componentFactories, this, underlyingId, model);
        views.addOrReplace(viewsDropDown);
        addOrReplace(views);//from  w  ww.j a  va  2 s  . co m
    } else {
        permanentlyHide(ID_VIEWS);
    }
    addOrReplace(selectedComponentFactory.createComponent(underlyingId, model));
}

From source file:org.jclouds.location.suppliers.OnlyLocationOrFirstRegionOptionallyMatchingRegionId.java

@Override
@Singleton//  w  w  w.  j a va 2 s  . c  o m
public Location get() {
    Set<? extends Location> locations = locationsSupplier.get();
    if (locationsSupplier.get().size() == 1)
        return getOnlyElement(locationsSupplier.get());
    try {
        Location toReturn = Iterables.find(locations, locationPredicate);
        return toReturn.getScope() == LocationScope.REGION ? toReturn : toReturn.getParent();
    } catch (NoSuchElementException e) {
        throw new NoSuchElementException(String.format("couldn't find region matching %s in %s",
                locationPredicate, transform(locations, ToIdAndScope.INSTANCE)));
    }
}

From source file:org.sonar.updatecenter.common.PluginReferential.java

/**
 * @throws NoSuchElementException if plugin could not be found
 *//* ww w . j  a  v a 2 s .  c  o  m*/
public Plugin findPlugin(final String key) {
    try {
        return Iterables.find(plugins, new Predicate<Plugin>() {
            public boolean apply(Plugin input) {
                return input.getKey().equals(key);
            }
        });
    } catch (NoSuchElementException e) {
        throw new NoSuchElementException("Unable to find plugin with key " + key);
    }
}