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.jackrabbit.oak.jcr.session.NodeImpl.java

@Override
@Nonnull/* w ww.j a  v  a2s  .c  om*/
public NodeIterator getNodes(final String namePattern) throws RepositoryException {
    return perform(new NodeOperation<NodeIterator>(dlg, "getNodes") {
        @Nonnull
        @Override
        public NodeIterator perform() throws RepositoryException {
            Iterator<NodeDelegate> children = Iterators.filter(node.getChildren(),
                    new Predicate<NodeDelegate>() {
                        @Override
                        public boolean apply(NodeDelegate state) {
                            // TODO: use Oak names
                            return ItemNameMatcher.matches(toJcrPath(state.getName()), namePattern);
                        }
                    });
            return new NodeIteratorAdapter(nodeIterator(children));
        }
    });
}

From source file:fr.obeo.intent.specification.parser.SpecificationParser.java

private Parameter getParameter(final String parameterName, final ScenarioElement element) {
    UnmodifiableIterator<Parameter> it = Iterators.filter(element.getParameters().iterator(),
            new Predicate<Parameter>() {
                public boolean apply(Parameter parameter) {
                    if (parameter != null && parameter instanceof Parameter) {
                        return parameterName.equals(((NamedElement) parameter).getName());
                    }/*from  www .j  a  v  a  2 s  .c o  m*/
                    return false;
                }
            });
    if (it.hasNext()) {
        return (Parameter) it.next();
    }
    return null;
}

From source file:org.eclipse.sirius.business.internal.session.danalysis.DAnalysisSessionImpl.java

@Override
public void moveRepresentation(final DAnalysis newContainer, final DRepresentation representation) {
    final IPermissionAuthority authority = PermissionAuthorityRegistry.getDefault()
            .getPermissionAuthority(representation.eContainer());
    IProgressMonitor pm = new NullProgressMonitor();
    if (!authority.canDeleteInstance(representation)) {
        throw new LockedInstanceException(representation);
    }// www  .  j  a va2 s.  c  om
    final EObject semantic;
    if (representation.eContainer() instanceof DView
            && !((DView) representation.eContainer()).getModels().isEmpty()) {
        semantic = ((DView) representation.eContainer()).getModels().iterator().next();
    } else {
        semantic = null;
    }
    Viewpoint viewpoint = ((DView) representation.eContainer()).getViewpoint();
    DView receiver = findViewForRepresentation(viewpoint, newContainer);
    if (receiver == null) {
        final IPermissionAuthority analysisAuthority = PermissionAuthorityRegistry.getDefault()
                .getPermissionAuthority(newContainer);
        if (analysisAuthority.canCreateIn(newContainer)) {
            createView(viewpoint, Lists.newArrayList(semantic), false, pm);
            receiver = findViewForRepresentation(viewpoint, newContainer);
        } else {
            throw new LockedInstanceException(newContainer);
        }
    }
    final IPermissionAuthority receiverAuthority = PermissionAuthorityRegistry.getDefault()
            .getPermissionAuthority(receiver);
    if (receiverAuthority.canCreateIn(receiver)) {
        receiver.getOwnedRepresentations().add(representation);
        // Add all semantic root elements pointed by the target of all
        // DSemanticDecorator of this representation (except of this root is
        // a root of a referencedAnalysis)
        if (receiver.eContainer() instanceof DAnalysis) {
            DAnalysisSessionHelper.updateModelsReferences((DAnalysis) receiver.eContainer(),
                    Iterators.filter(representation.eAllContents(), DSemanticDecorator.class));
        }
    } else {
        throw new LockedInstanceException(receiver);
    }
    for (EObject object : getServices().getCustomData(CustomDataConstants.GMF_DIAGRAMS, representation)) {
        getServices().putCustomData(CustomDataConstants.GMF_DIAGRAMS, representation, object);
    }
}

From source file:org.obeonetwork.dsl.uml2.design.services.ActivityServices.java

/**
 * Get all the operations available in the semantic resources.
 * //from   w ww  . jav  a  2 s  .co m
 * @param eObj
 *            Semantic element
 * @return All the operations
 */
public List<EObject> getAllOperations(Element element) {
    List<EObject> operations = Lists.newArrayList();
    UMLServices umlServices = new UMLServices();
    List<org.eclipse.uml2.uml.Package> rootPkgs = umlServices.getAllAvailableRootPackages(element);
    final Predicate<EObject> predicate = new Predicate<EObject>() {
        public boolean apply(EObject eObj) {
            return eObj instanceof Operation
                    && (((Operation) eObj).getMethods() == null || ((Operation) eObj).getMethods().size() == 0);
        }
    };
    for (org.eclipse.uml2.uml.Package pkg : rootPkgs) {
        Iterators.addAll(operations, Iterators.filter(pkg.eAllContents(), predicate));
    }

    return operations;
}

From source file:org.apache.jackrabbit.oak.jcr.session.NodeImpl.java

@Override
@Nonnull//  ww  w.  j  a v  a2 s  .  com
public NodeIterator getNodes(final String[] nameGlobs) throws RepositoryException {
    return perform(new NodeOperation<NodeIterator>(dlg, "getNodes") {
        @Nonnull
        @Override
        public NodeIterator perform() throws RepositoryException {
            Iterator<NodeDelegate> children = Iterators.filter(node.getChildren(),
                    new Predicate<NodeDelegate>() {
                        @Override
                        public boolean apply(NodeDelegate state) {
                            // TODO: use Oak names
                            return ItemNameMatcher.matches(toJcrPath(state.getName()), nameGlobs);
                        }
                    });
            return new NodeIteratorAdapter(nodeIterator(children));
        }
    });
}

From source file:org.obeonetwork.dsl.uml2.design.services.ActivityServices.java

/**
 * Get all the behaviors available in the semantic resources.
 * //from   w  w  w  .j  a  va2 s  .c o m
 * @param eObj
 *            Semantic element
 * @return All the behaviors
 */
public List<EObject> getAllBehaviors(Element element) {
    List<EObject> behaviors = Lists.newArrayList();
    UMLServices umlServices = new UMLServices();
    List<org.eclipse.uml2.uml.Package> rootPkgs = umlServices.getAllAvailableRootPackages(element);
    for (org.eclipse.uml2.uml.Package pkg : rootPkgs) {
        Iterators.addAll(behaviors,
                Iterators.filter(pkg.eAllContents(), Predicates.instanceOf(Behavior.class)));
    }

    return behaviors;
}

From source file:org.locationtech.geogig.geotools.data.reader.FeatureReaderBuilder.java

private List<String> resolveFidFilter(Filter filter) {
    List<String> pathFilters = ImmutableList.of();
    if (filter instanceof Id) {
        final Set<Identifier> identifiers = ((Id) filter).getIdentifiers();
        Iterator<FeatureId> featureIds = Iterators
                .filter(Iterators.filter(identifiers.iterator(), FeatureId.class), notNull());
        Preconditions.checkArgument(featureIds.hasNext(), "Empty Id filter");

        pathFilters = Lists.newArrayList(Iterators.transform(featureIds, (fid) -> fid.getID()));
    }//from  w  w w  . j  a  va 2 s . c om

    return pathFilters;
}

From source file:org.eclipse.sirius.tests.sample.migration.design.Draw2dToSiriusModelTransformer.java

@SuppressWarnings("unchecked")
private ContainerRepresentation getMigrationContainerRepresentation(
        IDiagramContainerEditPart diagramContainerEditPart) {
    ContainerRepresentation containerRepresentation = MigrationmodelerFactory.eINSTANCE
            .createContainerRepresentation();
    containerRepresentation.setMappingId(getMappingId(diagramContainerEditPart));
    org.eclipse.sirius.tests.sample.migration.migrationmodeler.ContainerStyle containerStyle = MigrationmodelerFactory.eINSTANCE
            .createContainerStyle();//  w w  w  . j  av  a2  s.c  o m
    containerRepresentation.setOwnedStyle(containerStyle);
    updateLabelStyle(containerStyle, diagramContainerEditPart);
    updateBorderedStyle(containerStyle, diagramContainerEditPart);
    Object model = diagramContainerEditPart.getModel();
    if (model instanceof org.eclipse.gmf.runtime.notation.Node) {
        org.eclipse.gmf.runtime.notation.Node node = (org.eclipse.gmf.runtime.notation.Node) model;
        if (node.getLayoutConstraint() instanceof Size) {
            Size size = (Size) node.getLayoutConstraint();
            if (size.getWidth() == -1 || size.getHeight() == -1) {
                containerRepresentation.setAutoSized(true);
            }
        }
    }
    updateLayout(containerRepresentation, diagramContainerEditPart.getFigure());
    List<?> children = new ArrayList<Object>(diagramContainerEditPart.getChildren());
    Iterator<ResizableCompartmentEditPart> compart = Iterators
            .filter(diagramContainerEditPart.getChildren().iterator(), ResizableCompartmentEditPart.class);
    if (compart.hasNext()) {
        ResizableCompartmentEditPart compartmentEditPart = compart.next();
        children.addAll(compartmentEditPart.getChildren());
    }
    Iterable<IAbstractDiagramNodeEditPart> filter = Iterables.filter(children,
            IAbstractDiagramNodeEditPart.class);
    for (IAbstractDiagramNodeEditPart childEditPart : filter) {
        EObject targetSemanticElement = childEditPart.resolveTargetSemanticElement();
        if (targetSemanticElement instanceof Node && childEditPart instanceof IDiagramNodeEditPart) {
            Node subNode = (Node) targetSemanticElement;
            IDiagramNodeEditPart childNodeEditPart = (IDiagramNodeEditPart) childEditPart;
            NodeRepresentation nodeRepresentation = getMigrationNodeRepresentation(childNodeEditPart);
            subNode.getNodeRepresentations().add(nodeRepresentation);
        } else if (targetSemanticElement instanceof Bordered) {
            Bordered subBordered = (Bordered) targetSemanticElement;
            BorderedRepresentation borderedRepresentation = getMigrationBorderedRepresentation(childEditPart);
            subBordered.getBorderedRepresentations().add(borderedRepresentation);
        } else if (targetSemanticElement instanceof Container) {
            Container subContainer = (Container) targetSemanticElement;
            ContainerRepresentation subContainerRepresentation = null;
            if (childEditPart instanceof IDiagramContainerEditPart) {
                subContainerRepresentation = getMigrationContainerRepresentation(
                        (IDiagramContainerEditPart) childEditPart);
            } else if (childEditPart instanceof IDiagramListEditPart) {
                subContainerRepresentation = getMigrationContainerRepresentation(
                        (IDiagramListEditPart) childEditPart);
            }
            if (subContainerRepresentation != null) {
                subContainer.getContainerRepresentations().add(subContainerRepresentation);
            }
        }
    }
    return containerRepresentation;
}

From source file:nl.knaw.huygens.timbuctoo.Repository.java

public Iterator<RelationType> getRelationTypes(Class<? extends DomainEntity> sourceType,
        Class<? extends DomainEntity> targetType) throws RepositoryException {

    try {//from  www.  j  a va 2  s  . com
        StorageIterator<RelationType> relationTypes = storage.getSystemEntities(RelationType.class);
        IsRelationTypeBetween isRelationTypeBetween = new IsRelationTypeBetween(sourceType, targetType);

        UnmodifiableIterator<RelationType> filteredRelationTypes = Iterators.filter(relationTypes,
                relationType -> isRelationTypeBetween.test(relationType));

        return filteredRelationTypes;
    } catch (StorageException e) {
        throw new RepositoryException(e);
    }
}

From source file:org.eclipse.emf.compare.merge.AbstractMerger.java

/**
 * Returns the master equivalence for a {@link ReferenceChange} from among its equivalents with the same
 * or {@code eOpposite} reference.//from   w  w w . j  av a  2s  . co  m
 * 
 * @see AbstractMerger#findMasterEquivalence(Diff, boolean)
 * @param diff
 *            The {@link Diff} we need to check the equivalence for a 'master' difference.
 * @param mergeRightToLeft
 *            Direction of the current merging.
 * @return The master difference of {@code diff} and its equivalent diffs. This method may return
 *         <code>null</code> if there is no master diff.
 */
private Diff getMasterEquivalenceOnReference(ReferenceChange diff, final boolean mergeRightToLeft) {
    Diff masterDiff = null;
    /*
     * For the following, we'll only consider diffs that are either on the same reference as "diff", or on
     * its eopposite.
     */
    final Predicate<Diff> candidateFilter = or(isDiffOnEOppositeOf(diff), hasSameReferenceAs(diff));
    final List<Diff> equivalentDiffs = diff.getEquivalence().getDifferences();

    // We need to lookup the first multi-valued addition
    final Optional<Diff> multiValuedAddition = Iterators
            .tryFind(Iterators.filter(equivalentDiffs.iterator(), candidateFilter), new Predicate<Diff>() {
                public boolean apply(Diff input) {
                    return input instanceof ReferenceChange && ((ReferenceChange) input).getReference().isMany()
                            && isAdd((ReferenceChange) input, mergeRightToLeft);
                }
            });

    final Iterator<Diff> candidateDiffs = Iterators.filter(equivalentDiffs.iterator(), candidateFilter);
    if (multiValuedAddition.isPresent()) {
        // We have at least one multi-valued addition. It will take precedence if there is any
        // single-valued reference change or multi-valued deletion
        while (masterDiff == null && candidateDiffs.hasNext()) {
            final ReferenceChange next = (ReferenceChange) candidateDiffs.next();
            if (!next.getReference().isMany() || !isAdd(next, mergeRightToLeft)) {
                masterDiff = multiValuedAddition.get();
            }
        }
    } else {
        // The only diff that could take precedence is a single-valued set, _if_ there is any multi-valued
        // deletion or single-valued unset in the list.
        ReferenceChange candidate = null;
        if (candidateDiffs.hasNext()) {
            candidate = (ReferenceChange) candidateDiffs.next();
        }
        while (masterDiff == null && candidateDiffs.hasNext()) {
            assert candidate != null;
            final ReferenceChange next = (ReferenceChange) candidateDiffs.next();
            if (candidate.getReference().isMany() || isUnset(candidate, mergeRightToLeft)) {
                // candidate is a multi-valued deletion or an unset. Do we have a single-valued set in the
                // list?
                if (!next.getReference().isMany() && isSet(next, mergeRightToLeft)) {
                    masterDiff = next;
                }
            } else if (isSet(candidate, mergeRightToLeft)) {
                // candidate is a set. Is it our master diff?
                if (next.getReference().isMany() || isUnset(next, mergeRightToLeft)) {
                    masterDiff = candidate;
                }
            } else {
                // candidate is a change on a single-valued reference. This has no influence over the
                // 'master' lookup. Go on to the next.
                candidate = next;
            }
        }
    }

    return masterDiff;
}