Example usage for com.google.common.collect Iterables filter

List of usage examples for com.google.common.collect Iterables filter

Introduction

In this page you can find the example usage for com.google.common.collect Iterables filter.

Prototype

@GwtIncompatible("Class.isInstance")
@CheckReturnValue
public static <T> Iterable<T> filter(final Iterable<?> unfiltered, final Class<T> desiredType) 

Source Link

Document

Returns all elements in unfiltered that are of the type desiredType .

Usage

From source file:see.functions.functional.Filter.java

/**
 * Choose appropriate filter based on runtime type of collection
 * @param items collection to transform/*from ww w  .j a va 2s  . co m*/
 * @param predicate predicate to match
 * @return lazy filtered collection
 */
private Iterable<?> filter(Iterable<?> items, Predicate<Object> predicate) {
    if (items instanceof Set<?>) {
        return Sets.filter((Set<?>) items, predicate);
    } else if (items instanceof Collection<?>) {
        return Collections2.filter((Collection<?>) items, predicate);
    } else {
        return Iterables.filter(items, predicate);
    }
}

From source file:pl.softech.eav.example.SimpleInMemmoryRepository.java

@Override
public Iterable<E> findAll(Iterable<Long> ids) {

    Iterable<Long> filtered = Iterables.filter(ids, new Predicate<Long>() {

        @Override//from ww  w  .  ja v  a  2s .com
        public boolean apply(Long key) {
            return key2entity.containsKey(key);
        }
    });

    return Iterables.transform(filtered, new Function<Long, E>() {

        @Override
        public E apply(Long input) {
            return key2entity.get(input);
        }
    });
}

From source file:org.obiba.opal.web.project.permissions.ProjectTablePermissionsResource.java

/**
 * Get all table-level permissions of a table in the project.
 *
 * @param domain/* www. j  a  v a  2  s.  c  o m*/
 * @param type
 * @return
 */
@GET
public Iterable<Opal.Acl> getTablePermissions(@QueryParam("type") SubjectType type) {

    // make sure datasource and table exists
    getValueTable();

    Iterable<SubjectAclService.Permissions> permissions = subjectAclService.getNodePermissions(DOMAIN,
            getNode(), type);

    return Iterables.transform(Iterables.filter(permissions, new MagmaPermissionsPredicate()),
            PermissionsToAclFunction.INSTANCE);
}

From source file:com.facebook.presto.sql.planner.plan.DistinctLimitNode.java

public List<Symbol> getDistinctSymbols() {
    if (hashSymbol.isPresent()) {
        return ImmutableList.copyOf(Iterables.filter(getOutputSymbols(), not(hashSymbol.get()::equals)));
    }//from www. java2  s  .  c o m
    return getOutputSymbols();
}

From source file:org.spongepowered.api.util.command.args.PatternMatchingCommandElement.java

@Nullable
@Override/*from   w  w  w.  ja va2 s. co  m*/
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
    final String unformattedPattern = args.next();
    Pattern pattern = getFormattedPattern(unformattedPattern);
    Iterable<String> filteredChoices = Iterables.filter(getChoices(source),
            element -> pattern.matcher(element).find());
    for (String el : filteredChoices) { // Match a single value
        if (el.equalsIgnoreCase(unformattedPattern)) {
            return getValue(el);
        }
    }
    Iterable<Object> ret = Iterables.transform(filteredChoices, this::getValue);

    if (!ret.iterator().hasNext()) {
        throw args.createError(t("No values matching pattern '%s' present for %s!", unformattedPattern,
                getKey() == null ? nullKeyArg : getKey()));
    }
    return ret;
}

From source file:com.metamx.common.guava.FunctionalIterable.java

public <RetType> FunctionalIterable<RetType> keep(Function<T, RetType> fn) {
    return new FunctionalIterable<>(Iterables.filter(Iterables.transform(delegate, fn), Predicates.notNull()));
}

From source file:hu.petabyte.redflags.engine.scope.NoticeRangeScope.java

public NoticeRangeScope(NoticeID first, NoticeID last, MaxNumberDeterminer mnd) {
    checkNotNull(first, "first should not be null.");
    checkNotNull(last, "last should not be null.");
    checkNotNull(mnd, "mnd should not be null.");
    this.noticeRange = new NoticeRangePredicate(first, last);
    List<Integer> years = new ArrayList<Integer>();
    for (int y = first.year(); y <= last.year(); y++) {
        years.add(y);//  ww  w. ja v a 2 s  .  c o m
    }
    this.it = Iterables.filter(new YearListScope(years, mnd), noticeRange).iterator(); // guava magic :-)
}

From source file:edu.umd.cs.psl.evaluation.statistics.DiscretePredictionStatistics.java

public Iterable<Map.Entry<GroundAtom, Double>> getFalsePositives() {
    return Iterables.filter(errors.entrySet(), new Predicate<Entry<GroundAtom, Double>>() {

        @Override//from   w w  w  .  j av  a2s . com
        public boolean apply(Entry<GroundAtom, Double> e) {
            if (e.getValue() > 0.0)
                return true;
            else
                return false;
        }
    });
}

From source file:com.google.devtools.build.lib.concurrent.Sharder.java

@Override
public Iterator<List<T>> iterator() {
    return Iterables.filter(shards, new Predicate<List<T>>() {
        @Override//from   w  ww .j a  v a  2s. c  om
        public boolean apply(List<T> list) {
            return !list.isEmpty();
        }
    }).iterator();
}

From source file:ezbake.services.graph.EzBreadthFirstSearch.java

private static Iterable<Vertex> removeVisited(Iterable<Vertex> collection, final Map<Object, Boolean> visited) {
    return Iterables.filter(collection, new com.google.common.base.Predicate<Vertex>() {
        @Override/* ww w . ja  v  a 2s .co m*/
        public boolean apply(Vertex vertex) {
            // return true that the passed in vertex is not in the visited list
            return visited.get(vertex.getId()) == null;
        }
    });
}