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

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

Introduction

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

Prototype

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

Source Link

Document

Returns an unmodifiable view of the difference of two sets.

Usage

From source file:org.opendaylight.unimgr.mef.nrp.ovs.util.VlanUtils.java

private Integer generateVid(String serviceName)
        throws VlanPoolExhaustedException, InterruptedException, ExecutionException {
    Set<Integer> difference = Sets.difference(POSSIBLE_VLANS, usedVlans);
    if (difference.isEmpty()) {
        LOG.warn(VLAN_POOL_EXHAUSTED_ERROR_MESSAGE);
        throw new VlanPoolExhaustedException(VLAN_POOL_EXHAUSTED_ERROR_MESSAGE);
    }//w w w.j  a  v a 2s. c  o  m
    return updateNodeNewServiceVLAN(serviceName, difference.iterator().next());
}

From source file:fr.inria.maestro.lga.clustering.analysis.impl.PrecisionQuantity.java

public Pair<Double, Double> getPrecisionRecall(final ClusteringPreprocessor analysisClustering) {
    final Set<String> expertClusterNames = Sets.newHashSet();
    final Set<String> expertClusterEntries = Sets.newHashSet();

    for (final ICluster cluster : expertFormingClustering.getClustering().getClusters()) {
        expertClusterNames.add(cluster.getName());
        expertClusterEntries.addAll(cluster.getEntriesNames());

        final ICluster judgementCluster = analysisClustering.getClusterByName(cluster.getName());
        if (judgementCluster == null) {
            throw new IllegalArgumentException("can't find analog for cluster: " + cluster.getName());
        }//from w ww  .j a v  a  2  s . c o m
    }

    int tp = 0;
    int fp = 0;
    int fn = 0;

    for (final ICluster cluster : expertFormingClustering.getClustering().getClusters()) {
        final Set<String> expertDocuments = cluster.getEntriesNames();

        final ICluster analysisCluster = analysisClustering.getClusterByName(cluster.getName());
        if (analysisCluster == null) {
            throw new IllegalArgumentException("can't find analog for cluster: " + cluster.getName());
        }
        final Set<String> analysisDocuments = Sets.intersection(analysisCluster.getEntriesNames(),
                expertClusterEntries);

        tp += Sets.intersection(analysisDocuments, expertDocuments).size();
        fp += Sets.difference(analysisDocuments, expertDocuments).size();
        fn += Sets.difference(expertDocuments, analysisDocuments).size();
    }

    return Pair.create(tp / (1. * (tp + fp)), tp / (1. * (tp + fn)));
}

From source file:com.facebook.buck.tools.dxanalysis.MutabilityAnalyzer.java

private void go() {
    while (true) {
        madeProgress = false;/*www.j  av a  2 s .  co m*/
        for (ClassNode klass : allClasses.values()) {
            analyzeClass(klass);
        }
        if (!madeProgress) {
            break;
        }
    }

    immutableClasses = ImmutableSet.copyOf(Sets.difference(allClasses.keySet(),
            Sets.union(trulyMutableClasses, classesWithMutableDescendents)));
}

From source file:com.facebook.buck.tools.consistency.RuleKeyDiffer.java

private void printDiff(ParsedRuleKeyFile originalFile, RuleKeyNode originalRuleKey, ParsedRuleKeyFile newFile,
        RuleKeyNode newRuleKey, int visitId) throws MaxDifferencesException, GraphTraversalException {
    if (originalRuleKey.ruleKey.key.equals(newRuleKey.ruleKey.key)) {
        return;//from ww w  .  j  a  v a 2 s  .c  o  m
    }

    if (originalRuleKey.lastVisitId == visitId && newRuleKey.lastVisitId == visitId) {
        return;
    }

    originalRuleKey.lastVisitId = visitId;
    newRuleKey.lastVisitId = visitId;

    Set<String> originalRuleKeyProperties = originalRuleKey.ruleKey.values.keySet();
    Set<String> newRuleKeyProperties = newRuleKey.ruleKey.values.keySet();
    Set<String> onlyInOriginalKey = Sets.difference(originalRuleKeyProperties, newRuleKeyProperties);
    Set<String> onlyInNewKey = Sets.difference(newRuleKeyProperties, originalRuleKeyProperties);
    Set<String> inBoth = Sets.intersection(originalRuleKeyProperties, newRuleKeyProperties);

    String target = RuleKeyDiffPrinter.getRuleKeyName(originalFile, originalRuleKey.ruleKey);

    try (TargetScope targetScope = printer.addTarget(target, originalRuleKey.ruleKey.key,
            newRuleKey.ruleKey.key)) {
        try (PropertyScope rootProperty = targetScope.addProperty("")) {
            for (String key : onlyInOriginalKey) {
                try (PropertyScope propertyScope = rootProperty.addNestedProperty(key)) {
                    propertyScope.removed(originalFile, originalRuleKey.ruleKey.values.get(key));
                }
            }
            for (String key : onlyInNewKey) {
                try (PropertyScope propertyScope = rootProperty.addNestedProperty(key)) {
                    propertyScope.added(newFile, newRuleKey.ruleKey.values.get(key));
                }
            }
            for (String key : inBoth) {
                try (PropertyScope propertyScope = rootProperty.addNestedProperty(key)) {
                    printDiff(propertyScope, originalFile, originalRuleKey.ruleKey.values.get(key), newFile,
                            newRuleKey.ruleKey.values.get(key));
                }
            }
        }
    } catch (MaxDifferencesException e) {
        throw e;
    } catch (Exception e) {
        throw new GraphTraversalException(e, "Unexpected error while traversing rule key graph");
    }
}

From source file:org.sosy_lab.cpachecker.cpa.bdd.BDDCompressExpressionVisitor.java

/** This function creates a mapping of intEqual partitions to a mapping of number to bitvector.
 * This allows to compress big numbers to a small number of bits in the BDD. */
private Map<BigInteger, Region[]> initMappingIntToRegions(final Partition partition) {

    if (!INT_REGIONS_MAP.containsKey(partition)) {
        final Map<BigInteger, Region[]> currentMapping = new HashMap<>();

        // special handling of One and Zero,
        // because they can appear as result of an equality-check.
        // this allows us to check expressions as "((a==0)==5)" with varClass intEQ
        currentMapping.put(BigInteger.ZERO, bvmgr.makeNumber(BigInteger.valueOf(0), size));
        currentMapping.put(BigInteger.ONE, bvmgr.makeNumber(BigInteger.valueOf(1), size));

        int i = 2;
        for (BigInteger num : Sets.difference(partition.getValues(),
                Sets.newHashSet(BigInteger.ZERO, BigInteger.ONE))) {
            currentMapping.put(num, bvmgr.makeNumber(BigInteger.valueOf(i), size));
            i++;//from w w w  .j  a  v  a 2  s.c om
        }
        INT_REGIONS_MAP.put(partition, currentMapping);
    }
    return INT_REGIONS_MAP.get(partition);
}

From source file:com.facebook.presto.execution.scheduler.SourcePartitionedScheduler.java

@Override
public synchronized ScheduleResult schedule() {
    // Acquire a future for the next state change before doing calculations.
    ////from   w  w w  .j a  va2  s . com
    // This code may need to return a future when the workers are full, and
    // it is critical that this future is notified of any changes that occur
    // during this calculation (to avoid starvation).
    CompletableFuture<?> taskStateChange = stage.getTaskStateChange();

    // try to get the next batch if necessary
    if (pendingSplits.isEmpty()) {
        if (batchFuture == null) {
            if (splitSource.isFinished()) {
                // no more splits
                splitSource.close();
                return new ScheduleResult(true, ImmutableSet.of(), CompletableFuture.completedFuture(null));
            }

            batchFuture = splitSource.getNextBatch(splitBatchSize);
            long start = System.nanoTime();
            batchFuture.thenRun(() -> stage.recordGetSplitTime(start));
        }

        if (!batchFuture.isDone()) {
            // wrap batch future in unmodifiable future so cancellation is not propagated
            CompletableFuture<List<Split>> blocked = unmodifiableFuture(batchFuture);
            return new ScheduleResult(false, ImmutableSet.of(), blocked);
        }
        pendingSplits = ImmutableSet.copyOf(getFutureValue(batchFuture));
        batchFuture = null;
    }

    // assign the splits
    Multimap<Node, Split> splitAssignment = splitPlacementPolicy.computeAssignments(pendingSplits);
    Set<RemoteTask> newTasks = assignSplits(splitAssignment);

    // remove assigned splits
    pendingSplits = ImmutableSet
            .copyOf(Sets.difference(pendingSplits, ImmutableSet.copyOf(splitAssignment.values())));

    // if not all splits were consumed, return a partial result
    if (!pendingSplits.isEmpty()) {
        newTasks = ImmutableSet.<RemoteTask>builder().addAll(newTasks).addAll(finalizeTaskCreationIfNecessary())
                .build();

        return new ScheduleResult(false, newTasks, taskStateChange);
    }

    // all splits assigned - check if the source is finished
    boolean finished = splitSource.isFinished();
    if (finished) {
        splitSource.close();
    }
    return new ScheduleResult(finished, newTasks, CompletableFuture.completedFuture(null));
}

From source file:com.facebook.buck.android.AndroidTransitiveDependencyGraph.java

/**
 * @param uberRDotJava that may have produced {@code R.class} files that need to be dexed.
 *//*from  ww w  . j a  v  a  2 s  . c o m*/
public AndroidDexTransitiveDependencies findDexDependencies(
        ImmutableList<HasAndroidResourceDeps> androidResourceDeps,
        final ImmutableSet<JavaLibrary> buildRulesToExcludeFromDex, final UberRDotJava uberRDotJava) {
    // These are paths that will be dex'ed. They may be either directories of compiled .class files,
    // or paths to compiled JAR files.
    final ImmutableSet.Builder<Path> pathsToDexBuilder = ImmutableSet.builder();

    // Paths to the classfiles to not dex.
    final ImmutableSet.Builder<Path> noDxPathsBuilder = ImmutableSet.builder();

    // These are paths to third-party jars that may contain resources that must be included in the
    // final APK.
    final ImmutableSet.Builder<Path> pathsToThirdPartyJarsBuilder = ImmutableSet.builder();

    AndroidResourceDetails details = findAndroidResourceDetails(androidResourceDeps);

    // Update pathsToDex.
    final ImmutableSetMultimap<JavaLibrary, Path> classpath = Classpaths
            .getClasspathEntries(rulesToTraverseForTransitiveDeps);
    for (Map.Entry<JavaLibrary, Path> entry : classpath.entries()) {
        if (!buildRulesToExcludeFromDex.contains(entry.getKey())) {
            pathsToDexBuilder.add(entry.getValue());
        } else {
            noDxPathsBuilder.add(entry.getValue());
        }
    }
    // Include the directory of compiled R.java files on the classpath.
    final ImmutableSet<String> rDotJavaPackages = details.rDotJavaPackages;
    if (!rDotJavaPackages.isEmpty()) {
        Path pathToCompiledRDotJavaFiles = uberRDotJava.getPathToCompiledRDotJavaFiles();
        pathsToDexBuilder.add(pathToCompiledRDotJavaFiles);
    }

    ImmutableSet<Path> noDxPaths = noDxPathsBuilder.build();

    // Filter out the classpath entries to exclude from dex'ing, if appropriate
    Set<Path> classpathEntries = Sets.difference(pathsToDexBuilder.build(), noDxPaths);

    Supplier<Map<String, HashCode>> classNamesToHashesSupplier = Suppliers
            .memoize(new Supplier<Map<String, HashCode>>() {
                @Override
                public Map<String, HashCode> get() {
                    ImmutableMap.Builder<String, HashCode> builder = ImmutableMap.builder();
                    for (JavaLibrary javaLibrary : classpath.keySet()) {
                        if (!buildRulesToExcludeFromDex.contains(javaLibrary)) {
                            builder.putAll(javaLibrary.getClassNamesToHashes());
                        }
                    }
                    if (!rDotJavaPackages.isEmpty()) {
                        builder.putAll(uberRDotJava.getClassNamesToHashes());
                    }
                    return builder.build();
                }
            });

    // Visit all of the transitive dependencies to populate the above collections.
    new AbstractDependencyVisitor(rulesToTraverseForTransitiveDeps) {
        @Override
        public ImmutableSet<BuildRule> visit(BuildRule rule) {
            // We need to include the transitive closure of the compiled .class files when dex'ing, as
            // well as the third-party jars that they depend on.
            // Update pathsToThirdPartyJars.
            if (rule.getBuildable() instanceof PrebuiltJar) {
                PrebuiltJar prebuiltJar = (PrebuiltJar) rule.getBuildable();
                pathsToThirdPartyJarsBuilder.add(prebuiltJar.getBinaryJar());
            }
            return maybeVisitAllDeps(rule, rule.getProperties().is(LIBRARY));
        }
    }.start();

    // Classpath entries that should be excluded from dexing should also be excluded from
    // pathsToThirdPartyJars because their resources should not end up in main APK. If they do,
    // the pre-dexed library may try to load a resource from the main APK rather than from within
    // the pre-dexed library (even though the resource is available in both locations). This
    // causes a significant performance regression, as the resource may take more than one second
    // longer to load.
    Set<Path> pathsToThirdPartyJars = Sets.difference(pathsToThirdPartyJarsBuilder.build(), noDxPaths);

    return new AndroidDexTransitiveDependencies(classpathEntries, pathsToThirdPartyJars, noDxPaths,
            classNamesToHashesSupplier);
}

From source file:com.facebook.buck.rules.TargetNodeFactory.java

@SuppressWarnings("unchecked")
public <T, U extends Description<T>> TargetNode<T, U> create(HashCode rawInputsHashCode, U description,
        T constructorArg, ProjectFilesystem filesystem, BuildTarget buildTarget,
        ImmutableSet<BuildTarget> declaredDeps, ImmutableSet<VisibilityPattern> visibilityPatterns,
        CellPathResolver cellRoots) throws NoSuchBuildTargetException {

    ImmutableSortedSet.Builder<BuildTarget> extraDepsBuilder = ImmutableSortedSet.naturalOrder();
    ImmutableSet.Builder<Path> pathsBuilder = ImmutableSet.builder();

    // Scan the input to find possible BuildTargets, necessary for loading dependent rules.
    T arg = description.createUnpopulatedConstructorArg();
    for (Field field : arg.getClass().getFields()) {
        ParamInfo info = new ParamInfo(typeCoercerFactory, arg.getClass(), field);
        if (info.isDep() && info.isInput()
                && info.hasElementTypes(BuildTarget.class, SourcePath.class, Path.class)) {
            detectBuildTargetsAndPathsForConstructorArg(extraDepsBuilder, pathsBuilder, info, constructorArg);
        }//  w  ww . j a  v a2 s. c o m
    }

    if (description instanceof ImplicitDepsInferringDescription) {
        extraDepsBuilder.addAll(((ImplicitDepsInferringDescription<T>) description)
                .findDepsForTargetFromConstructorArgs(buildTarget, cellRoots, constructorArg));
    }

    if (description instanceof ImplicitInputsInferringDescription) {
        pathsBuilder.addAll(((ImplicitInputsInferringDescription<T>) description)
                .inferInputsFromConstructorArgs(buildTarget.getUnflavoredBuildTarget(), constructorArg));
    }

    return new TargetNode<>(this, rawInputsHashCode, description, constructorArg, filesystem, buildTarget,
            declaredDeps, ImmutableSortedSet.copyOf(Sets.difference(extraDepsBuilder.build(), declaredDeps)),
            visibilityPatterns, pathsBuilder.build(), cellRoots, Optional.empty());
}

From source file:io.druid.indexer.path.DatasourcePathSpec.java

@Override
public Job addInputPaths(HadoopDruidIndexerConfig config, Job job) throws IOException {
    Preconditions.checkArgument(segments != null && !segments.isEmpty(), "no segments provided");

    logger.info("Found total [%d] segments for [%s]  in interval [%s]", segments.size(),
            ingestionSpec.getDataSource(), ingestionSpec.getInterval());

    DatasourceIngestionSpec updatedIngestionSpec = ingestionSpec;
    if (updatedIngestionSpec.getDimensions() == null) {
        List<String> dims;
        if (config.getParser().getParseSpec().getDimensionsSpec().hasCustomDimensions()) {
            dims = config.getParser().getParseSpec().getDimensionsSpec().getDimensions();
        } else {//from w  w w .  j  a v a 2s .co  m
            Set<String> dimSet = Sets.newHashSet(Iterables.concat(
                    Iterables.transform(segments, new Function<WindowedDataSegment, Iterable<String>>() {
                        @Override
                        public Iterable<String> apply(WindowedDataSegment dataSegment) {
                            return dataSegment.getSegment().getDimensions();
                        }
                    })));
            dims = Lists.newArrayList(Sets.difference(dimSet,
                    config.getParser().getParseSpec().getDimensionsSpec().getDimensionExclusions()));
        }
        updatedIngestionSpec = updatedIngestionSpec.withDimensions(dims);
    }

    if (updatedIngestionSpec.getMetrics() == null) {
        Set<String> metrics = Sets.newHashSet();
        final AggregatorFactory[] cols = config.getSchema().getDataSchema().getAggregators();
        if (cols != null) {
            for (AggregatorFactory col : cols) {
                metrics.add(col.getName());
            }
        }
        updatedIngestionSpec = updatedIngestionSpec.withMetrics(Lists.newArrayList(metrics));
    }

    job.getConfiguration().set(DatasourceInputFormat.CONF_DRUID_SCHEMA,
            mapper.writeValueAsString(updatedIngestionSpec));
    job.getConfiguration().set(DatasourceInputFormat.CONF_INPUT_SEGMENTS, mapper.writeValueAsString(segments));
    job.getConfiguration().set(DatasourceInputFormat.CONF_MAX_SPLIT_SIZE, String.valueOf(maxSplitSize));
    MultipleInputs.addInputPath(job, new Path("/dummy/tobe/ignored"), DatasourceInputFormat.class);

    return job;
}

From source file:org.graylog2.indexer.esplugin.IndexChangeMonitor.java

private Set<String> calculateReopenedIndices(ClusterState currentState, @Nullable ClusterState previousState) {
    if (previousState == null || previousState.metaData() == currentState.metaData()) {
        return Collections.emptySet();
    }/*  w  ww  .  ja v  a 2  s . c o  m*/

    final Set<String> currentClosedIndices = getClosedIndices(currentState.getMetaData());
    final Set<String> previousClosedIndices = getClosedIndices(previousState.getMetaData());

    return Sets.difference(previousClosedIndices, currentClosedIndices);
}