Example usage for com.google.common.collect Sets intersection

List of usage examples for com.google.common.collect Sets intersection

Introduction

In this page you can find the example usage for com.google.common.collect Sets intersection.

Prototype

public static <E> SetView<E> intersection(final Set<E> set1, final Set<?> set2) 

Source Link

Document

Returns an unmodifiable view of the intersection of two sets.

Usage

From source file:com.olacabs.fabric.compute.builder.impl.JarScanner.java

private List<ScanResult> scanForProcessors(ClassLoader classLoader, URL[] downloadedUrls) throws Exception {
    Reflections reflections = new Reflections(new ConfigurationBuilder().addClassLoader(classLoader)
            .addScanners(new SubTypesScanner(), new TypeAnnotationsScanner()).addUrls(downloadedUrls));
    Set<Class<?>> processors = Sets.intersection(reflections.getTypesAnnotatedWith(Processor.class),
            reflections.getSubTypesOf(ProcessorBase.class));

    return processors.stream().map(processor -> {
        Processor processorInfo = processor.getAnnotation(Processor.class);
        ComponentMetadata metadata = ComponentMetadata.builder().type(ComponentType.PROCESSOR)
                .namespace(processorInfo.namespace()).name(processorInfo.name())
                .version(processorInfo.version()).description(processorInfo.description())
                .cpu(processorInfo.cpu()).memory(processorInfo.memory())
                .processorType(processorInfo.processorType())
                .requiredProperties(ImmutableList.copyOf(processorInfo.requiredProperties()))
                .optionalProperties(ImmutableList.copyOf(processorInfo.optionalProperties())).build();

        return ScanResult.builder().metadata(metadata).componentClass(processor).build();
    }).collect(Collectors.toCollection(ArrayList::new));
}

From source file:grakn.core.graql.reasoner.rule.RuleUtils.java

/**
 *
 * @return true if the rule subgraph is stratifiable (doesn't contain cycles with negation)
 *///w w  w .j a va  2 s . c o  m
public static List<Set<Type>> negativeCycles(TransactionOLTP tx) {
    HashMultimap<Type, Type> typeGraph = typeGraph(tx);
    return new TarjanSCC<>(
            typeGraph)
                    .getCycles().stream().filter(
                            cycle -> cycle.stream()
                                    .anyMatch(
                                            type -> type.whenRules()
                                                    .filter(rule -> rule.whenNegativeTypes()
                                                            .anyMatch(ntype -> ntype.equals(type)))
                                                    .anyMatch(rule -> !Sets
                                                            .intersection(cycle,
                                                                    rule.thenTypes().collect(toSet()))
                                                            .isEmpty())))
                    .collect(toList());
}

From source file:com.facebook.buck.model.BuildTargets.java

/**
 * Propagate flavors represented by the given {@link FlavorDomain} objects from a parent
 * target to its dependencies.//from w w  w.j a  va  2 s . c o m
 */
public static ImmutableSortedSet<BuildTarget> propagateFlavorDomains(BuildTarget target,
        Iterable<FlavorDomain<?>> domains, Iterable<BuildTarget> deps) {

    Set<Flavor> flavors = Sets.newHashSet();

    // For each flavor domain, extract the corresponding flavor from the parent target and
    // verify that each dependency hasn't already set this flavor.
    for (FlavorDomain<?> domain : domains) {

        // Now extract all relevant domain flavors from our parent target.
        ImmutableSet<Flavor> flavorSet = Sets.intersection(domain.getFlavors(), target.getFlavors())
                .immutableCopy();

        if (flavorSet.isEmpty()) {
            throw new HumanReadableException("%s: no flavor for \"%s\"", target, domain.getName());
        }
        flavors.addAll(flavorSet);

        // First verify that our deps are not already flavored for our given domains.
        for (BuildTarget dep : deps) {
            if (domain.getFlavor(dep).isPresent()) {
                throw new HumanReadableException("%s: dep %s already has flavor for \"%s\" : %s", target, dep,
                        domain.getName(), flavorSet.toString());
            }
        }
    }

    ImmutableSortedSet.Builder<BuildTarget> flavoredDeps = ImmutableSortedSet.naturalOrder();

    // Now flavor each dependency with the relevant flavors.
    for (BuildTarget dep : deps) {
        flavoredDeps.add(BuildTarget.builder(dep).addAllFlavors(flavors).build());
    }

    return flavoredDeps.build();
}

From source file:com.google.api.auth.Authenticator.java

/**
 * Authenticate the current HTTP request.
 *
 * @param httpServletRequest is the incoming HTTP request object.
 * @param authInfo contains authentication configurations of the API method being called.
 * @param serviceName is the name of this service.
 * @return a constructed {@link UserInfo} object representing the identity of the caller.
 *//*from w w w  .  j  ava2  s .  c o m*/
public UserInfo authenticate(HttpServletRequest httpServletRequest, AuthInfo authInfo, String serviceName) {

    Preconditions.checkNotNull(httpServletRequest);
    Preconditions.checkNotNull(authInfo);

    Optional<String> maybeAuthToken = extractAuthToken(httpServletRequest);
    if (!maybeAuthToken.isPresent()) {
        throw new UnauthenticatedException("No auth token is contained in the HTTP request");
    }

    JwtClaims jwtClaims = this.authTokenDecoder.decode(maybeAuthToken.get());
    UserInfo userInfo = toUserInfo(jwtClaims);
    String issuer = userInfo.getIssuer();

    if (!this.issuersToProviderIds.containsKey(issuer)) {
        throw new UnauthenticatedException("Unknown issuer: " + issuer);
    }
    String providerId = this.issuersToProviderIds.get(issuer);

    // Check whether the provider id is allowed.
    if (!authInfo.isProviderIdAllowed(providerId)) {
        String message = "The requested method does not allowed this provider id: " + providerId;
        throw new UnauthenticatedException(message);
    }

    checkJwtClaims(jwtClaims);

    // Check the audiences decoded from the auth token. The auth token is allowed when
    // 1) an audience is equal to the service name,
    // or 2) at least one audience is allowed in the method configuration.
    Set<String> audiences = userInfo.getAudiences();
    boolean hasServiceName = audiences.contains(serviceName);
    Set<String> allowedAudiences = authInfo.getAudiencesForProvider(providerId);
    if (!hasServiceName && Sets.intersection(audiences, allowedAudiences).isEmpty()) {
        throw new UnauthenticatedException("Audiences not allowed");
    }

    return userInfo;
}

From source file:org.apache.rya.indexing.external.matching.FlattenedOptional.java

/**
 *
 * @param te/*from  ww  w  . j a  v a 2s  . com*/
 *            - {@link TupleExpr} to be added to leftArg of LeftJoin
 * @return - true if adding TupleExpr does not affect unbound variables and
 *         returns false otherwise
 */
public boolean canAddTuple(TupleExpr te) {
    // can only add LeftJoin if rightArg varNames do not intersect
    // unbound vars
    if (te instanceof FlattenedOptional) {
        FlattenedOptional lj = (FlattenedOptional) te;
        if (Sets.intersection(lj.rightArg.getBindingNames(), unboundVars).size() > 0) {
            return false;
        } else {
            return true;
        }
    }

    return Sets.intersection(te.getBindingNames(), unboundVars).size() == 0;
}

From source file:org.obm.provisioning.processing.impl.users.AbstractUserOperationProcessor.java

protected void validateUserEmail(ObmUser user) {
    if (!user.isEmailAvailable()) {
        return;/* ww w  .jav a 2s.  c o  m*/
    }
    try {
        final ImmutableSet<String> userEmails = ImmutableSet.<String>builder().add(user.getEmail())
                .addAll(user.getEmailAlias()).build();
        final Set<String> allEmails = userDao.getAllEmailsFrom(user.getDomain(), user.getExtId());

        final SetView<String> matchingEmails = Sets.intersection(allEmails, userEmails);
        if (!matchingEmails.isEmpty()) {
            throw new ProcessingException(String.format(
                    "Cannot create/modify user because similar emails have been found : %s", matchingEmails));
        }
    } catch (SQLException e) {
        throw new ProcessingException(e);
    }
}

From source file:com.facebook.presto.type.setdigest.SetDigest.java

public static long exactIntersectionCardinality(SetDigest a, SetDigest b) {
    checkState(a.isExact(), "exact intersection cannot operate on approximate sets");
    checkArgument(b.isExact(), "exact intersection cannot operate on approximate sets");

    return Sets.intersection(a.minhash.keySet(), b.minhash.keySet()).size();
}

From source file:com.google.cloud.tools.eclipse.appengine.localserver.launching.LaunchHelper.java

/**
 * Look for servers that may match.//from w  ww  .  ja  va2 s  .c om
 * 
 * @param modules the web modules to search for
 * @param narrow if true, look for exact module match
 * @return an existing server
 */
@VisibleForTesting
public Collection<IServer> findExistingServers(IModule[] modules, boolean exact, SubMonitor progress) {
    if (modules.length == 1) {
        IServer defaultServer = ServerCore.getDefaultServer(modules[0]);
        if (defaultServer != null
                && LocalAppEngineServerDelegate.SERVER_TYPE_ID.equals(defaultServer.getServerType().getId())) {
            return Collections.singletonList(defaultServer);
        }
    }
    Set<IModule> myModules = ImmutableSet.copyOf(modules);
    List<IServer> matches = new ArrayList<>();
    // Look for servers that contain these modules
    // Could prioritize servers that have *exactly* these modules,
    // or that have the smallest overlap
    for (IServer server : ServerCore.getServers()) {
        if (!LocalAppEngineServerDelegate.SERVER_TYPE_ID.equals(server.getServerType().getId())) {
            continue;
        }
        Set<IModule> serverModules = ImmutableSet.copyOf(server.getModules());
        SetView<IModule> overlap = Sets.intersection(myModules, serverModules);
        if (overlap.size() == myModules.size() && (!exact || overlap.size() == serverModules.size())) {
            matches.add(server);
        }
    }
    return matches;
}

From source file:org.apache.sentry.provider.db.generic.SentryGenericProviderBackend.java

@Override
public ImmutableSet<String> getRoles(Set<String> groups, ActiveRoleSet roleSet) {
    if (!initialized) {
        throw new IllegalStateException("SentryGenericProviderBackend has not been properly initialized");
    }//from  www . j a v a2s.  c om
    SentryGenericServiceClient client = null;
    try {
        Set<TSentryRole> tRoles = Sets.newHashSet();
        client = getClient();
        //get the roles according to group
        String requestor = UserGroupInformation.getCurrentUser().getShortUserName();
        for (String group : groups) {
            tRoles.addAll(client.listRolesByGroupName(requestor, group, getComponentType()));
        }
        Set<String> roles = Sets.newHashSet();
        for (TSentryRole tRole : tRoles) {
            roles.add(tRole.getRoleName());
        }
        return ImmutableSet.copyOf(roleSet.isAll() ? roles : Sets.intersection(roles, roleSet.getRoles()));
    } catch (SentryUserException e) {
        String msg = "Unable to obtain roles from server: " + e.getMessage();
        LOGGER.error(msg, e);
    } catch (Exception e) {
        String msg = "Unable to obtain client:" + e.getMessage();
        LOGGER.error(msg, e);
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return ImmutableSet.of();
}

From source file:co.cask.cdap.etl.planner.PipelinePlanner.java

/**
 * Create an execution plan for the given logical pipeline. This is used for batch pipelines.
 * Though it may eventually be useful to mark windowing points for realtime pipelines.
 *
 * A plan consists of one or more phases, with connections between phases.
 * A connection between a phase indicates control flow, and not necessarily
 * data flow. This class assumes that it receives a valid pipeline spec.
 * That is, the pipeline has no cycles, all its nodes have unique names,
 * sources don't have any input, sinks don't have any output,
 * everything else has both an input and an output, etc.
 *
 * We start by inserting connector nodes into the logical dag,
 * which are used to mark boundaries between mapreduce jobs.
 * Each connector represents a node where we will need to write to a local dataset.
 *
 * Next, the logical pipeline is broken up into phases,
 * using the connectors as sinks in one phase, and a source in another.
 * After this point, connections between phases do not indicate data flow, but control flow.
 *
 * @param spec the pipeline spec, representing a logical pipeline
 * @return the execution plan/*  ww  w. j av  a  2s. c  om*/
 */
public PipelinePlan plan(PipelineSpec spec) {
    // go through the stages and examine their plugin type to determine which stages are reduce stages
    Set<String> reduceNodes = new HashSet<>();
    Set<String> isolationNodes = new HashSet<>();
    Map<String, StageSpec> specs = new HashMap<>();
    for (StageSpec stage : spec.getStages()) {
        if (reduceTypes.contains(stage.getPlugin().getType())) {
            reduceNodes.add(stage.getName());
        }
        if (isolationTypes.contains(stage.getPlugin().getType())) {
            isolationNodes.add(stage.getName());
        }
        specs.put(stage.getName(), stage);
    }

    // insert connector stages into the logical pipeline
    ConnectorDag cdag = ConnectorDag.builder().addConnections(spec.getConnections()).addReduceNodes(reduceNodes)
            .addIsolationNodes(isolationNodes).build();
    cdag.insertConnectors();
    Set<String> connectorNodes = cdag.getConnectors();

    // now split the logical pipeline into pipeline phases, using the connectors as split points
    Map<String, Dag> subdags = new HashMap<>();
    // assign some name to each subdag
    for (Dag subdag : cdag.splitOnConnectors()) {
        String name = getPhaseName(subdag.getSources(), subdag.getSinks());
        subdags.put(name, subdag);
    }

    // build connections between phases
    Set<Connection> phaseConnections = new HashSet<>();
    for (Map.Entry<String, Dag> subdagEntry1 : subdags.entrySet()) {
        String dag1Name = subdagEntry1.getKey();
        Dag dag1 = subdagEntry1.getValue();

        for (Map.Entry<String, Dag> subdagEntry2 : subdags.entrySet()) {
            String dag2Name = subdagEntry2.getKey();
            Dag dag2 = subdagEntry2.getValue();
            if (dag1Name.equals(dag2Name)) {
                continue;
            }

            // if dag1 has any sinks that are a source in dag2, add a connection between the dags
            if (Sets.intersection(dag1.getSinks(), dag2.getSources()).size() > 0) {
                phaseConnections.add(new Connection(dag1Name, dag2Name));
            }
        }
    }

    // convert to objects the programs expect.
    Map<String, PipelinePhase> phases = new HashMap<>();
    for (Map.Entry<String, Dag> dagEntry : subdags.entrySet()) {
        phases.put(dagEntry.getKey(), dagToPipeline(dagEntry.getValue(), connectorNodes, specs));
    }
    return new PipelinePlan(phases, phaseConnections);
}