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:qdg.view.SubMixedGraph.java

@Override
public Iterator<Edge> getOutArcIterator(Node node) {
    return Iterators.filter(g.getOutArcIterator(node), spanningEdgeFilterPredicate);
}

From source file:org.eclipse.sirius.diagram.ui.business.internal.migration.CollapseSupportOnRegionMigrationParticipant.java

@SuppressWarnings("unchecked")
@Override//from  w  w w. j a v a2  s. c om
protected void postLoad(DAnalysis dAnalysis, Version loadedVersion) {
    if (loadedVersion.compareTo(MIGRATION_VERSION) < 0) {
        EClass drawerStyleClass = NotationPackage.eINSTANCE.getDrawerStyle();

        // Step 1: get all view to update
        final Collection<View> allViewsToUpdate = Sets.newLinkedHashSet();
        for (DView dView : dAnalysis.getOwnedViews()) {
            for (DDiagram dDiagram : Iterables.filter(dView.getOwnedRepresentations(), DDiagram.class)) {
                DiagramCreationUtil diagramCreationUtil = new DiagramCreationUtil(dDiagram);
                if (diagramCreationUtil.findAssociatedGMFDiagram()) {
                    Diagram gmfDiagram = diagramCreationUtil.getAssociatedGMFDiagram();
                    for (Node node : Lists.newArrayList(
                            Iterators.filter(Iterators.filter(gmfDiagram.eAllContents(), Node.class),
                                    new IsRegionCompartmentPredicate()))) {
                        Style style = node.getStyle(drawerStyleClass);
                        if (style == null) {
                            allViewsToUpdate.add(node);
                        }
                    }
                }
            }
        }

        // Step 2: update views
        for (View viewToUpdate : allViewsToUpdate) {
            DrawerStyle style = (DrawerStyle) drawerStyleClass.getEPackage().getEFactoryInstance()
                    .create(drawerStyleClass);
            style.setCollapsed(false);
            viewToUpdate.getStyles().add(style);
        }
    }
}

From source file:ch.njol.skript.expressions.ExprFilter.java

@NonNull
@Override/*from ww  w  . j a va 2s  .  co  m*/
public Iterator<?> iterator(Event e) {
    try {
        return Iterators.filter(new ArrayIterator<>(this.objects.getArray(e)), object -> {
            current = object;
            return condition.check(e);
        });
    } finally {
        current = null;
    }
}

From source file:org.kiji.schema.impl.cassandra.RowDecoders.java

/**
 * Create a new column family result set decoder function.
 *
 * @param tableName The Cassandra table that the results are from.
 * @param column The Kiji column name of the family.
 * @param columnRequest The column request defining the request for the family.
 * @param dataRequest The data request defining the request.
 * @param layout The layout of the Kiji table.
 * @param translator A column name translator for the table.
 * @param decoderProvider A cell decoder provider for the table.
 * @param <T> Type of cell values.
 * @return A function to convert a {@link ResultSet} containing a column family to cells.
 *///from  w ww .j a v  a  2s  . c o  m
public static <T> Function<ResultSet, Iterator<KijiCell<T>>> getColumnFamilyDecoderFunction(
        final CassandraTableName tableName, final KijiColumnName column, final Column columnRequest,
        final KijiDataRequest dataRequest, final KijiTableLayout layout,
        final CassandraColumnNameTranslator translator, final CellDecoderProvider decoderProvider) {
    return new Function<ResultSet, Iterator<KijiCell<T>>>() {
        /** {@inheritDoc} */
        @Override
        public Iterator<KijiCell<T>> apply(final ResultSet resultSet) {
            final int mMaxVersions = columnRequest.getMaxVersions();
            final long mMinTimestamp = dataRequest.getMinTimestamp();
            final long mMaxTimestamp = dataRequest.getMaxTimestamp();

            Iterator<Row> rows = resultSet.iterator();

            if (mMinTimestamp != 0) {
                rows = Iterators.filter(rows, new MinTimestampPredicate(mMinTimestamp));
            }
            if (mMaxTimestamp != KConstants.END_OF_TIME) {
                rows = Iterators.filter(rows, new MaxTimestampPredicate(mMaxTimestamp));
            }
            rows = Iterators.filter(rows, new MaxVersionsPredicate(mMaxVersions));

            try {
                if (layout.getFamilyMap().get(column.getFamily()).isMapType()) {
                    // Map-type family
                    final Function<Row, KijiCell<T>> decoder = new MapFamilyDecoder<>(tableName,
                            translator.toCassandraColumnName(column), translator,
                            decoderProvider.<T>getDecoder(column));

                    return Iterators.transform(rows, decoder);
                } else {
                    // Group-type family
                    final Function<Row, KijiCell<T>> decoder = new GroupFamilyDecoder<>(tableName,
                            translator.toCassandraColumnName(column), translator, decoderProvider);

                    // Group family decoder may return nulls, so filter them out
                    return Iterators.filter(Iterators.transform(rows, decoder), Predicates.notNull());
                }
            } catch (NoSuchColumnException e) {
                throw new IllegalStateException(
                        String.format("Column %s does not exist in Kiji table %s.", column, layout.getName()));
            }
        }
    };
}

From source file:org.apache.giraph.block_app.library.VertexSuppliers.java

/**
 * Supplier which extracts and returns Iterator over neighbor IDs
 * that return true for given predicate.
 * Note - iterator returns reused object, so you need to "use" them,
 * before calling next() again./*  w  w w  . j  a v  a  2 s.com*/
 */
public static <I extends WritableComparable, V extends Writable, E extends Writable> SupplierFromVertex<I, V, E, Iterator<I>> vertexNeighborsSupplier(
        final Predicate<I> toSupply) {
    return new SupplierFromVertex<I, V, E, Iterator<I>>() {
        @Override
        public Iterator<I> get(final Vertex<I, V, E> vertex) {
            return Iterators.filter(new TargetVertexIdIterator<>(vertex),
                    new com.google.common.base.Predicate<I>() {
                        @Override
                        public boolean apply(I input) {
                            return toSupply.apply(input);
                        }
                    });
        }
    };
}

From source file:eu.numberfour.n4js.utils.EcoreUtilN4.java

/**
 * Returns all content of a given type, ignoring all elements which are not of the given type. This filters out also
 * elements of the given type, if their container has a different type. The given object itself is neither added to
 * the result nor is it tested against the predicate.
 *
 * @param eobj//from ww  w  .j a v a 2s.  c o m
 *            the root object, may be null
 * @return the tree iterator, may be an empty iterator but never null
 */
@SuppressWarnings("unchecked")
public static <T> TreeIterator<T> getAllDirectlyFoundContentsOfType(EObject eobj, final Class<T> type) {
    if (eobj == null) {
        return emptyTreeIterator();
    }
    return (TreeIterator<T>) new AbstractTreeIterator<EObject>(eobj, false) {
        private static final long serialVersionUID = 1L;

        @Override
        public Iterator<? extends EObject> getChildren(Object element) {
            return (Iterator<? extends EObject>) Iterators.filter(((EObject) element).eContents().iterator(),
                    type);
        }
    };
}

From source file:mysql5.MySQL5PlayerEffectsDAO.java

@Override
public void storePlayerEffects(final Player player) {
    deletePlayerEffects(player);/*from w  w w  . j  a  v a  2  s.  c om*/

    Iterator<Effect> iterator = player.getEffectController().iterator();
    iterator = Iterators.filter(iterator, insertableEffectsPredicate);

    if (!iterator.hasNext()) {
        return;
    }

    Connection con = null;
    PreparedStatement ps = null;
    try {
        con = DatabaseFactory.getConnection();
        con.setAutoCommit(false);
        ps = con.prepareStatement(INSERT_QUERY);

        while (iterator.hasNext()) {
            Effect effect = iterator.next();
            ps.setInt(1, player.getObjectId());
            ps.setInt(2, effect.getSkillId());
            ps.setInt(3, effect.getSkillLevel());
            ps.setInt(4, effect.getRemainingTime());
            ps.setLong(5, effect.getEndTime());
            ps.addBatch();
        }

        ps.executeBatch();
        con.commit();
    } catch (SQLException e) {
        log.error("Exception while saving effects of player " + player.getObjectId(), e);
    } finally {
        DatabaseFactory.close(ps, con);
    }
}

From source file:com.clarkparsia.openrdf.SetGraph.java

/**
 * @inheritDoc/*from   ww w .j  a v a2s  . c om*/
 */
public Iterator<Statement> match(final Resource theSubject, final URI thePredicate, final Value theObject,
        final Resource... theContexts) {
    return Iterators.filter(iterator(), new Predicate<Statement>() {
        @Override
        public boolean apply(final Statement theStatement) {
            if (theSubject != null && !theSubject.equals(theStatement.getSubject())) {
                return false;
            }
            if (thePredicate != null && !thePredicate.equals(theStatement.getPredicate())) {
                return false;
            }
            if (theObject != null && !theObject.equals(theStatement.getObject())) {
                return false;
            }

            if (theContexts == null || theContexts.length == 0) {
                // no context specified, SPO were all equal, so this is equals as null/empty context is a wildcard
                return true;
            } else {
                Resource aContext = theStatement.getContext();

                for (Resource aCxt : theContexts) {
                    if (aCxt == null && aContext == null) {
                        return true;
                    }
                    if (aCxt != null && aCxt.equals(aContext)) {
                        return true;
                    }
                }

                return false;
            }
        }
    });
}

From source file:gobblin.source.workunit.BasicWorkUnitStream.java

/**
 * Apply a filtering function to this stream.
 *///w w w . j av a 2s. c om
public WorkUnitStream filter(Predicate<WorkUnit> predicate) {
    if (this.materializedWorkUnits == null) {
        return new BasicWorkUnitStream(this, Iterators.filter(this.workUnits, predicate), null);
    } else {
        return new BasicWorkUnitStream(this, null,
                Lists.newArrayList(Iterables.filter(this.materializedWorkUnits, predicate)));
    }
}

From source file:org.eclipse.sirius.tree.business.internal.dialect.common.viewpoint.MappingBasedPartition.java

@Override
public EvaluatedSemanticPartition evaluate(EObject root, CreatedOutput parentElement) {
    setTreeElementVariables(parentElement);
    Iterator<EObject> elements = Collections.<EObject>emptyList().iterator();
    if (semanticCandidate.some()) {
        try {// w ww.  j a v  a  2s  . c  o m
            elements = ctx.getInterpreter().evaluateCollection(root, semanticCandidate.get()).iterator();
        } catch (EvaluationException e) {
            ctx.getSpecifierFeedBack().warning(Messages.MappingBasedPartition_semanticCandidateEvaluationError,
                    e, specificationAttachment);
        }
    } else {
        elements = allEObjectsOfTheSession();
    }
    unSetTreeElementVariables();
    return SemanticPartitions
            .eObjectList(ImmutableList.copyOf(Iterators.filter(elements, new Predicate<EObject>() {

                @Override
                public boolean apply(EObject input) {
                    return ctx.getModelAccessor().eInstanceOf(input, domainClass);
                }
            })));

}