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

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

Introduction

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

Prototype

@SuppressWarnings("unchecked") 
@GwtIncompatible("Class.isInstance")
@CheckReturnValue
public static <T> UnmodifiableIterator<T> filter(Iterator<?> unfiltered, Class<T> desiredType) 

Source Link

Document

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

Usage

From source file:org.apache.marmotta.commons.sesame.repository.ResourceUtils.java

private static Iterable<Resource> listSubjectsInternal(final RepositoryConnection con, final URI property,
        final Value value, final URI context) {
    final Resource[] contexts;
    if (context != null) {
        contexts = new Resource[] { context };
    } else {//  w w w . j  a  v  a 2s  .  c o m
        contexts = new Resource[0];
    }

    return new Iterable<Resource>() {
        @Override
        public Iterator<Resource> iterator() {
            try {
                return Iterators.filter(Iterators.transform(
                        ResultUtils.unwrap(con.getStatements(null, property, value, true, contexts)),
                        new Function<Statement, Resource>() {
                            @Override
                            public Resource apply(Statement input) {
                                return input.getSubject();
                            }
                        }), new Predicate<Resource>() {
                            // filter duplicates by remembering hash codes of visited resources
                            private HashSet<Integer> visited = new HashSet<Integer>();

                            @Override
                            public boolean apply(Resource input) {
                                if (!visited.contains(input.hashCode())) {
                                    visited.add(input.hashCode());
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        });
            } catch (RepositoryException e) {
                ExceptionUtils.handleRepositoryException(e, ResourceUtils.class);
                return Iterators.emptyIterator();
            }
        }
    };

}

From source file:org.apache.calcite.sql.SqlUtil.java

private static Iterator<SqlOperator> lookupSubjectRoutinesByName(SqlOperatorTable opTab, SqlIdentifier funcName,
        final SqlSyntax syntax, SqlFunctionCategory category) {
    final List<SqlOperator> sqlOperators = new ArrayList<>();
    opTab.lookupOperatorOverloads(funcName, category, syntax, sqlOperators);
    switch (syntax) {
    case FUNCTION:
        return Iterators.filter(sqlOperators.iterator(), Predicates.instanceOf(SqlFunction.class));
    default://from   w w  w.j  av  a2s .c om
        return Iterators.filter(sqlOperators.iterator(), new PredicateImpl<SqlOperator>() {
            public boolean test(SqlOperator operator) {
                return operator.getSyntax() == syntax;
            }
        });
    }
}

From source file:org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.java

protected boolean containsUnorderedGroup(Grammar grammar) {
    for (ParserRule rule : GrammarUtil.allParserRules(grammar)) {
        if (Iterators.filter(rule.eAllContents(), UnorderedGroup.class).hasNext()) {
            return true;
        }/*from   w  ww  . j a v  a  2 s .c o  m*/
    }
    return false;
}

From source file:org.obeonetwork.dsl.uml2.core.api.services.ActivityDiagramServices.java

/**
 * Get all the behaviors available in the semantic resources.
 *
 * @param eObj/*  www .  j a v a2s  .co m*/
 *            Semantic element
 * @return All the behaviors
 */
private List<EObject> getAllBehaviors(Element element) {
    final List<EObject> behaviors = Lists.newArrayList();
    final List<org.eclipse.uml2.uml.Package> rootPkgs = getAllAvailableRootPackages(element);
    for (final org.eclipse.uml2.uml.Package pkg : rootPkgs) {
        Iterators.addAll(behaviors,
                Iterators.filter(pkg.eAllContents(), Predicates.instanceOf(Behavior.class)));
    }

    return behaviors;
}

From source file:com.dataart.spreadsheetanalytics.engine.PoiWorkbookConverters.java

public PoiProxyCellIterator(PoiProxyCell[][][][] tile0) {
    final List<Iterator<PoiProxyCell>> iterators = new ArrayList<>(10);
    for (PoiProxyCell[][][] tile1 : tile0) {
        if (tile1 == null) {
            continue;
        }/*  ww  w  .  j  a va  2  s . c o m*/

        for (PoiProxyCell[][] tile2 : tile1) {
            if (tile2 == null) {
                continue;
            }

            for (PoiProxyCell[] tile3 : tile2) {
                if (tile3 == null) {
                    continue;
                }

                iterators.add(Iterators.filter(Iterators.forArray(tile3), Predicates.notNull()));
            }
        }
    }

    this.iterator = Iterators.concat(iterators.iterator());
}

From source file:org.apache.calcite.sql.SqlUtil.java

private static Iterator<SqlOperator> filterRoutinesByParameterCount(Iterator<SqlOperator> routines,
        final List<RelDataType> argTypes) {
    return Iterators.filter(routines, new PredicateImpl<SqlOperator>() {
        public boolean test(SqlOperator operator) {
            SqlOperandCountRange od = operator.getOperandCountRange();
            return od.isValidCount(argTypes.size());
        }//from w  w w .j  a v a  2  s. c o m
    });
}

From source file:org.apache.uima.lucas.indexer.analysis.AnnotationTokenStream.java

protected Iterator<FeatureStructure> createFeatureStructureIterator(Annotation annotation, String featurePath) {
    Collection<FeatureStructure> featureStructures = new LinkedList<FeatureStructure>();
    Collection<FeatureStructure> childs = new LinkedList<FeatureStructure>();

    if (featurePath == null) {
        featureStructures.add(annotation);
        return featureStructures.iterator();
    }/*from w w  w. j  av  a  2  s.  co  m*/

    Type currentType = annotation.getType();
    if (currentType.isArray())
        currentType = currentType.getComponentType();

    String[] pathEntries = featurePath.split("\\.");
    featureStructures.add(annotation);

    for (String pathEntry : pathEntries) {
        Feature feature = currentType.getFeatureByBaseName(pathEntry);
        childs.clear();

        if (feature.getRange().isArray()) {
            for (FeatureStructure featureStructureItem : featureStructures) {
                FSArray fsArray = (FSArray) featureStructureItem.getFeatureValue(feature);
                if (fsArray == null)
                    continue;

                for (int i = 0; i < fsArray.size(); i++)
                    childs.add(fsArray.get(i));
            }
        } else
            for (FeatureStructure featureStructureItem : featureStructures)
                childs.add(featureStructureItem.getFeatureValue(feature));

        currentType = feature.getRange();
        if (currentType.isArray())
            currentType = currentType.getComponentType();

        featureStructures.clear();
        featureStructures.addAll(childs);
    }

    return Iterators.filter(featureStructures.iterator(), new NotNullPredicate<FeatureStructure>());
}

From source file:org.apache.calcite.sql.SqlUtil.java

/**
 * @see Glossary#SQL99 SQL:1999 Part 2 Section 10.4 Syntax Rule 6.b.iii.2.B
 *///w w  w  . j ava 2  s.co m
private static Iterator<SqlOperator> filterRoutinesByParameterType(SqlSyntax syntax,
        final Iterator<SqlOperator> routines, final List<RelDataType> argTypes, final List<String> argNames) {
    if (syntax != SqlSyntax.FUNCTION) {
        return routines;
    }

    //noinspection unchecked
    return (Iterator) Iterators.filter(Iterators.filter(routines, SqlFunction.class),
            new PredicateImpl<SqlFunction>() {
                public boolean test(SqlFunction function) {
                    List<RelDataType> paramTypes = function.getParamTypes();
                    if (paramTypes == null) {
                        // no parameter information for builtins; keep for now
                        return true;
                    }
                    final List<RelDataType> permutedArgTypes;
                    if (argNames != null) {
                        // Arguments passed by name. Make sure that the function has
                        // parameters of all of these names.
                        final Map<Integer, Integer> map = new HashMap<>();
                        for (Ord<String> argName : Ord.zip(argNames)) {
                            final int i = function.getParamNames().indexOf(argName.e);
                            if (i < 0) {
                                return false;
                            }
                            map.put(i, argName.i);
                        }
                        permutedArgTypes = Functions.generate(paramTypes.size(),
                                new Function1<Integer, RelDataType>() {
                                    public RelDataType apply(Integer a0) {
                                        if (map.containsKey(a0)) {
                                            return argTypes.get(map.get(a0));
                                        } else {
                                            return null;
                                        }
                                    }
                                });
                    } else {
                        permutedArgTypes = Lists.newArrayList(argTypes);
                        while (permutedArgTypes.size() < argTypes.size()) {
                            paramTypes.add(null);
                        }
                    }
                    for (Pair<RelDataType, RelDataType> p : Pair.zip(paramTypes, permutedArgTypes)) {
                        final RelDataType argType = p.right;
                        final RelDataType paramType = p.left;
                        if (argType != null && !SqlTypeUtil.canCastFrom(paramType, argType, false)) {
                            return false;
                        }
                    }
                    return true;
                }
            });
}

From source file:org.eclipse.sirius.diagram.ui.tools.internal.palette.PaletteManagerImpl.java

/**
 * Returns the palette entry contained in the given {@link PaletteContainer}
 * with the given id, of the given type. If none found,
 * {@link Options#newNone()} will be returned. If several found, we will log
 * a warning and return only one of the candidates.
 * /*from w  w w.  ja v a 2  s .co  m*/
 * @param <T>
 *            the type of the searched palette entry
 * @param container
 *            the container in which search for this palette entry
 * @param id
 *            the searched id
 * @param type
 *            the expected type
 * @return {@link Options#newNone()} if no matching candidate is found, or
 *         the found candidate (if several found, we will log a warning and
 *         return only one of the candidates).
 */
private <T extends PaletteEntry> Option<T> getPaletteEntry(PaletteContainer container, final String id,
        Class<T> type) {
    Option<T> matchingPaletteEntry = Options.newNone();
    UnmodifiableIterator<T> matchingPaletteEntries = Iterators
            .filter(Iterators.filter(container.getChildren().iterator(), type), new Predicate<T>() {
                @Override
                public boolean apply(T paletteEntry) {
                    return id.equals(paletteEntry.getId());
                }
            });
    try {
        matchingPaletteEntry = Options.newSome(Iterators.getOnlyElement(matchingPaletteEntries));
    } catch (NoSuchElementException e) {
        // Here no matching candidate has been found, we will return
        // Options.newNone
    } catch (IllegalArgumentException e) {
        DiagramPlugin.getDefault().logWarning(MessageFormat
                .format(Messages.PaletteManagerImpl_severalCandidatesInPalette, type.getName(), id));
        // Here no matching candidate has been found, we will return
        // Options.newNone
    }
    return matchingPaletteEntry;
}

From source file:grakn.core.graql.analytics.ShortestPathVertexProgram.java

private List<MessageFromSource> messageFromSource(List<VertexMessage> messages) {
    return IteratorUtils.asList(Iterators.filter(messages.iterator(), e -> e instanceof MessageFromSource));
}