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:co.cask.cdap.common.namespace.InMemoryNamespaceClient.java

@Override
public NamespaceMeta get(final Id.Namespace namespaceId) throws Exception {
    Iterable<NamespaceMeta> filtered = Iterables.filter(namespaces, new Predicate<NamespaceMeta>() {
        @Override/*from  w w w .j av a 2 s .c  o m*/
        public boolean apply(NamespaceMeta input) {
            return input.getName().equals(namespaceId.getId());
        }
    });
    if (Iterables.size(filtered) == 0) {
        throw new NamespaceNotFoundException(namespaceId);
    }
    return filtered.iterator().next();
}

From source file:io.druid.server.http.InventoryViewUtils.java

public static Set<DruidDataSource> getSecuredDataSources(InventoryView inventoryView,
        final AuthorizationInfo authorizationInfo) {
    if (authorizationInfo == null) {
        throw new ISE("Invalid to call a secured method with null AuthorizationInfo!!");
    } else {/*w w  w. j  a v  a  2 s . c o m*/
        final Map<Pair<Resource, Action>, Access> resourceAccessMap = new HashMap<>();
        return ImmutableSet
                .copyOf(Iterables.filter(getDataSources(inventoryView), new Predicate<DruidDataSource>() {
                    @Override
                    public boolean apply(DruidDataSource input) {
                        Resource resource = new Resource(input.getName(), ResourceType.DATASOURCE);
                        Action action = Action.READ;
                        Pair<Resource, Action> key = new Pair<>(resource, action);
                        if (resourceAccessMap.containsKey(key)) {
                            return resourceAccessMap.get(key).isAllowed();
                        } else {
                            Access access = authorizationInfo.isAuthorized(key.lhs, key.rhs);
                            resourceAccessMap.put(key, access);
                            return access.isAllowed();
                        }
                    }
                }));
    }
}

From source file:com.facebook.presto.connector.system.jdbc.FilterUtil.java

public static <T> Iterable<T> filter(Iterable<T> items, Optional<T> filter) {
    if (!filter.isPresent()) {
        return items;
    }/*w w w  .j  a v a2 s.  c o  m*/
    return Iterables.filter(items, Predicates.equalTo(filter.get()));
}

From source file:org.eclipse.sirius.diagram.sequence.ui.tool.internal.util.EditPartsTreeIterator.java

/**
 * {@inheritDoc}/*from   w  w w. j  a  v  a 2s .  c  o  m*/
 */
@Override
protected Iterator<? extends IGraphicalEditPart> getChildren(Object object) {
    if (object instanceof IGraphicalEditPart) {
        Iterable<IGraphicalEditPart> children = Iterables.filter(((IGraphicalEditPart) object).getChildren(),
                IGraphicalEditPart.class);
        return children.iterator();
    } else {
        return Iterators.emptyIterator();
    }
}

From source file:org.vclipse.constraint.validation.ConstraintJavaValidator.java

@Check(CheckType.FAST)
public void checkConstraint(ConstraintSource source) {
    if (source != null) {
        List<ConstraintRestriction> restrictions = source.getRestrictions();
        int size = Iterables.size(Iterables.filter(restrictions, ConditionalConstraintRestriction.class));
        if (size > 0 && restrictions.size() > size) {
            error("Mix of conditional and unconditional restrictions is not allowed in a constraint",
                    VcmlPackage.Literals.CONSTRAINT_SOURCE__RESTRICTIONS);
            // TODO possible quickfix: split this constraint
        }/*ww w. j  a v  a2 s.  c  o  m*/
    }
}

From source file:com.openlattice.mail.services.MailRenderer.java

protected Set<Email> renderEmail(RenderableEmailRequest emailRequest) {
    Iterable<String> toAddresses = Iterables.filter(Arrays.asList(emailRequest.getTo()),
            (String address) -> isNotBlacklisted(address));
    logger.info("filtered e-mail addresses that are blacklisted.");

    if (Iterables.size(toAddresses) < 1) {
        logger.error("Must include at least one valid e-mail address.");
        return ImmutableSet.of();
    }/*from   ww w . j a  va  2s . c  om*/
    String template;
    try {
        template = TemplateUtils.loadTemplate(emailRequest.getTemplatePath());
    } catch (IOException e) {
        throw new InvalidTemplateException("Invalid Email Template: " + emailRequest.getTemplatePath(), e);
    }
    String templateHtml = TemplateUtils.DEFAULT_TEMPLATE_COMPILER.compile(template)
            .execute(emailRequest.getTemplateObjs().or(new Object()));

    /*
     * when someone invites multiple people, we want to spool an individual invite for each person. as such, we need
     * to create a multiple Email objects instead of one Email object with multiple "To" fields to avoid having
     * multiple emails appear in the "To" field in an email client like Gmail.
     */
    Set<Email> emailSet = Sets.newHashSet();
    for (String toAddress : toAddresses) {

        Email email = Email.create().from(emailRequest.getFrom().or(EmailTemplate.getCourierEmailAddress()))
                .to(toAddress).subject(emailRequest.getSubject().or("")).addHtml(templateHtml);
        if (emailRequest.getByteArrayAttachment().isPresent()) {
            ByteArrayAttachment[] attachments = emailRequest.getByteArrayAttachment().get();
            for (int i = 0; i < attachments.length; i++) {
                email.attach(attachments[i]);
            }
        }

        if (emailRequest.getAttachmentPaths().isPresent()) {
            String[] paths = emailRequest.getAttachmentPaths().get();
            for (int i = 0; i < paths.length; i++) {
                email.attach(EmailAttachment.attachment().file(paths[i]));
            }
        }

        emailSet.add(email);
    }
    return emailSet;
}

From source file:org.vclipse.configscan.vcmlt.imports.Abstract2VcmlTImportTransformation.java

public void init() {
    if (referencedModel != null) {
        VcmlModel vcmlModel = (VcmlModel) referencedModel;
        String materialNumber = "";
        for (Option option : vcmlModel.getOptions()) {
            if (OptionType.UPS == option.getName()) {
                materialNumber = option.getValue();
            }//from w  ww.j av a 2 s . co m
        }
        if (materialNumber.isEmpty()) {
            throw new IllegalArgumentException("Can not create a test case without a material number");
        }
        final String searchString = materialNumber;
        Iterable<Material> materialObjects = Iterables.filter(vcmlModel.getObjects(), Material.class);
        Iterator<Material> foundNamedMaterial = Iterables.filter(materialObjects, new Predicate<Material>() {
            @Override
            public boolean apply(Material input) {
                return input.getName().equals(searchString);
            }
        }).iterator();

        if (foundNamedMaterial.hasNext()) {
            TestCase testCase = VcmlTFactory.eINSTANCE.createTestCase();
            testCase.setItem(foundNamedMaterial.next());
            ((Model) targetModel).setTestcase(testCase);
        }

    }
}

From source file:org.jclouds.openstack.nova.compute.strategy.NovaListNodesStrategy.java

@Override
public Iterable<? extends NodeMetadata> listDetailsOnNodesMatching(Predicate<ComputeMetadata> filter) {
    return Iterables.filter(
            Iterables.transform(client.listServers(ListOptions.Builder.withDetails()), serverToNodeMetadata),
            filter);/*from  w w w  .  j  ava  2 s  .  c o  m*/
}

From source file:org.eclipse.sirius.business.internal.movida.ViewpointResourceOperations.java

/**
 * Unloads the resource, and reset all the proxy URIs for viewpoints elements
 * to the corresponding logical (instead of physical) URI, so that when
 * references to these elements are re-resolved later, they are resolved
 * taking the logical/physical mapping at that time.
 *//* ww  w  .ja  v a 2s .  c  o  m*/
public void unloadAndResetProxyURIs() {
    Map<EObject, URI> logicalURIs = computeLogicalURIs();
    resource.unload();
    for (InternalEObject obj : Iterables.filter(logicalURIs.keySet(), InternalEObject.class)) {
        if (obj.eIsProxy()) {
            obj.eSetProxyURI(logicalURIs.get(obj));
        }
    }
}

From source file:com.isotrol.impe3.api.support.BasicPrincipal.java

public BasicPrincipal(final String username, final String displayName, final Iterable<String> roles,
        final Map<String, String> properties) {
    this.username = username;
    this.displayName = displayName;
    if (roles == null) {
        this.roles = ImmutableSet.of();
    } else {//  w w  w .j a v  a 2s  .  c o  m
        this.roles = ImmutableSet.copyOf(Iterables.filter(roles, Predicates.notNull()));
    }
    if (properties == null) {
        this.properties = ImmutableMap.of();
    } else {
        this.properties = ImmutableMap.copyOf(filterKeys(filterValues(properties, notNull()), notNull()));
    }
}