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.apache.druid.indexer.path.DatasourcePathSpec.java

@Override
public Job addInputPaths(HadoopDruidIndexerConfig config, Job job) throws IOException {
    if (segments == null || segments.isEmpty()) {
        if (ingestionSpec.isIgnoreWhenNoSegments()) {
            logger.warn("No segments found for ingestionSpec [%s]", ingestionSpec);
            return job;
        } else {/*  ww  w .  j av a 2 s.co m*/
            throw new ISE("No segments found for ingestion spec [%s]", ingestionSpec);
        }
    }

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

    DatasourceIngestionSpec updatedIngestionSpec = ingestionSpec;
    if (updatedIngestionSpec.getDimensions() == null) {
        List<String> dims;
        if (config.getParser().getParseSpec().getDimensionsSpec().hasCustomDimensions()) {
            dims = config.getParser().getParseSpec().getDimensionsSpec().getDimensionNames();
        } else {
            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) {
            if (useNewAggs) {
                for (AggregatorFactory col : cols) {
                    metrics.addAll(col.requiredFields());
                }
            } else {
                for (AggregatorFactory col : cols) {
                    metrics.add(col.getName());
                }
            }

        }
        updatedIngestionSpec = updatedIngestionSpec.withMetrics(Lists.newArrayList(metrics));
    }

    updatedIngestionSpec = updatedIngestionSpec
            .withQueryGranularity(config.getGranularitySpec().getQueryGranularity());

    // propagate in the transformSpec from the overall job config
    updatedIngestionSpec = updatedIngestionSpec
            .withTransformSpec(config.getSchema().getDataSchema().getTransformSpec());

    DatasourceInputFormat.addDataSource(job.getConfiguration(), updatedIngestionSpec, segments, maxSplitSize);
    MultipleInputs.addInputPath(job, new Path("/dummy/tobe/ignored"), DatasourceInputFormat.class);
    return job;
}

From source file:org.dishevelled.venn.model.VennModelImpl.java

/**
 * Create and return the map of exclusive set views keyed by bit set.
 *
 * @return the map of exclusive set views keyed by bit set
 *//*from w  w w .  j  av  a  2s  . com*/
private Map<ImmutableBitSet, Set<E>> createExclusives() {
    Map<ImmutableBitSet, Set<E>> map = Maps.newHashMapWithExpectedSize((int) Math.pow(2, this.sets.size()) - 1);

    // construct a set containing 0...n integers
    Set<Integer> input = Sets.newHashSet();
    for (int i = 0, size = size(); i < size; i++) {
        input.add(Integer.valueOf(i));
    }

    // calculate the power set (note n > 30 will overflow int)
    Set<Set<Integer>> powerSet = Sets.powerSet(ImmutableSet.copyOf(input));
    for (Set<Integer> set : powerSet) {
        if (!set.isEmpty()) {
            // intersect all in set
            Iterator<Integer> indices = set.iterator();
            Set<E> view = sets.get(indices.next());
            while (indices.hasNext()) {
                view = Sets.intersection(view, sets.get(indices.next()));
            }

            // subtract all in input not in set
            for (Integer index : input) {
                if (!set.contains(index)) {
                    view = Sets.difference(view, sets.get(index));
                }
            }

            // add to exclusives map
            map.put(toImmutableBitSet(set), view);
        }
    }

    // make an immutable copy?
    return map;
}

From source file:org.onosproject.routing.Router.java

/**
 * Changes the router configuration./*from  w w w. j a  v a 2s.  co m*/
 *
 * @param newConfig new configuration
 * @param forceUnprovision true if we want to force unprovision the device when it goes offline
 */
public void changeConfiguration(RouterInfo newConfig, boolean forceUnprovision) {
    if (forceUnprovision) {
        asyncDeviceFetcher.registerCallback(info.deviceId(), this::provision, this::forceUnprovision);
    } else {
        asyncDeviceFetcher.registerCallback(info.deviceId(), this::provision, null);
    }

    Set<String> oldConfiguredInterfaces = info.interfaces();
    info = newConfig;
    Set<String> newConfiguredInterfaces = info.interfaces();

    if (newConfiguredInterfaces.isEmpty() && !oldConfiguredInterfaces.isEmpty()) {
        // Reverted to using all interfaces. Provision interfaces that
        // weren't previously in the configured list
        getInterfacesForDevice(info.deviceId()).filter(intf -> !oldConfiguredInterfaces.contains(intf.name()))
                .forEach(this::provision);
    } else if (!newConfiguredInterfaces.isEmpty() && oldConfiguredInterfaces.isEmpty()) {
        // Began using an interface list. Unprovision interfaces that
        // are not in the new interface list.
        getInterfacesForDevice(info.deviceId()).filter(intf -> !newConfiguredInterfaces.contains(intf.name()))
                .forEach(this::unprovision);
    } else {
        // The existing interface list was changed.
        Set<String> toUnprovision = Sets.difference(oldConfiguredInterfaces, newConfiguredInterfaces);
        Set<String> toProvision = Sets.difference(newConfiguredInterfaces, oldConfiguredInterfaces);

        toUnprovision.forEach(name -> getInterfacesForDevice(info.deviceId())
                .filter(intf -> intf.name().equals(name)).findFirst().ifPresent(this::unprovision));

        toProvision.forEach(name -> getInterfacesForDevice(info.deviceId())
                .filter(intf -> intf.name().equals(name)).findFirst().ifPresent(this::provision));
    }
}

From source file:com.barchart.proto.util.diff.ProtoDiff.java

private void diffRepeatedObject(FieldDescriptor fieldDescriptor) {
    ImmutableSet<Object> expectedObjects = ImmutableSet
            .copyOf((Collection<Object>) expected.getField(fieldDescriptor));
    ImmutableSet<Object> actualObjects = ImmutableSet
            .copyOf((Collection<Object>) actual.getField(fieldDescriptor));
    Set<Object> missingInExpected = Sets.difference(actualObjects, expectedObjects);
    Set<Object> missingInActual = Sets.difference(expectedObjects, actualObjects);
    if (missingInExpected.size() != missingInActual.size()) {
        Difference difference = new ListLengthDifference(fieldDescriptor, missingInExpected, missingInActual);
        differences.add(difference);/* w  ww  . j  a  v a 2s .  c om*/
    }

}

From source file:com.google.api.client.discovery.RestMethod.java

/**
 * Returns type descriptions for each of the parameters that are named but not
 * listed as required. There is no guaranteed ordering of these parameters.
 *///from w w  w .  ja  v a  2  s. c  o m
public Collection<Parameter> getOptionalParameters() {
    Set<String> required = Sets.newHashSet(methodNode.getParameterOrder());
    Set<String> allParameters = methodNode.getParameters().keySet();
    Set<String> optional = Sets.difference(allParameters, required);

    return createParameterList(Lists.newArrayList(optional));
}

From source file:org.apache.aurora.scheduler.scheduling.TaskSchedulerImpl.java

private Map<String, IAssignedTask> fetchTasks(StoreProvider store, Set<String> ids) {
    Map<String, IAssignedTask> tasks = store.getTaskStore().fetchTasks(Query.taskScoped(ids).byStatus(PENDING))
            .stream().map(IScheduledTask::getAssignedTask)
            .collect(Collectors.toMap(IAssignedTask::getTaskId, Function.identity()));

    if (ids.size() != tasks.size()) {
        LOG.warn("Failed to look up tasks {}", Joiner.on(", ").join(Sets.difference(ids, tasks.keySet())));
    }//  w w w. j a v  a  2 s  . co m
    return tasks;
}

From source file:org.sonar.server.computation.task.projectanalysis.filemove.FileMoveDetectionStep.java

@Override
public void execute() {
    // do nothing if no files in db (first analysis)
    Analysis baseProjectAnalysis = analysisMetadataHolder.getBaseAnalysis();
    if (baseProjectAnalysis == null) {
        LOG.debug("First analysis. Do nothing.");
        return;//from   w w w .  j a  v a2  s .c  o m
    }

    Map<String, DbComponent> dbFilesByKey = getDbFilesByKey();
    if (dbFilesByKey.isEmpty()) {
        LOG.debug("Previous snapshot has no file. Do nothing.");
        return;
    }

    Map<String, Component> reportFilesByKey = getReportFilesByKey(this.rootHolder.getRoot());
    if (reportFilesByKey.isEmpty()) {
        LOG.debug("No files in report. Do nothing.");
        return;
    }

    Set<String> addedFileKeys = ImmutableSet
            .copyOf(Sets.difference(reportFilesByKey.keySet(), dbFilesByKey.keySet()));
    Set<String> removedFileKeys = ImmutableSet
            .copyOf(Sets.difference(dbFilesByKey.keySet(), reportFilesByKey.keySet()));

    // can find matches if at least one of the added or removed files groups is empty => abort
    if (addedFileKeys.isEmpty() || removedFileKeys.isEmpty()) {
        LOG.debug("Either no files added or no files removed. Do nothing.");
        return;
    }

    // retrieve file data from report
    Map<String, File> reportFileSourcesByKey = getReportFileSourcesByKey(reportFilesByKey, addedFileKeys);

    // compute score matrix
    ScoreMatrix scoreMatrix = computeScoreMatrix(dbFilesByKey, removedFileKeys, reportFileSourcesByKey);
    printIfDebug(scoreMatrix);

    // not a single match with score higher than MIN_REQUIRED_SCORE => abort
    if (scoreMatrix.getMaxScore() < MIN_REQUIRED_SCORE) {
        LOG.debug("max score in matrix is less than min required score (%s). Do nothing.", MIN_REQUIRED_SCORE);
        return;
    }

    MatchesByScore matchesByScore = MatchesByScore.create(scoreMatrix);

    ElectedMatches electedMatches = electMatches(removedFileKeys, reportFileSourcesByKey, matchesByScore);

    registerMatches(dbFilesByKey, reportFilesByKey, electedMatches);
}

From source file:com.urswolfer.intellij.plugin.gerrit.ui.changesbrowser.CommitDiffBuilder.java

private void removedFiles() throws VcsException {
    Sets.SetView<String> removedFiles = Sets.difference(baseChanges.keySet(), changes.keySet());
    for (String removedFile : removedFiles) {
        Change baseChange = baseChanges.get(removedFile);
        ContentRevision afterRevision = null;
        if (baseChange.getType().equals(Change.Type.MODIFICATION)) {
            ContentRevision baseChangeBeforeRevision = baseChange.getBeforeRevision();
            assert baseChangeBeforeRevision != null;
            afterRevision = new SimpleContentRevision(baseChangeBeforeRevision.getContent(),
                    baseChangeBeforeRevision.getFile(), hash);
        }/*  w  w w. ja  va  2  s .c o m*/
        diff.add(new Change(baseChange.getAfterRevision(), afterRevision));
    }
}

From source file:org.apache.storm.zookeeper.Zookeeper.java

public static LeaderLatchListener leaderLatchListenerImpl(final Map<String, Object> conf,
        final CuratorFramework zk, final BlobStore blobStore, final LeaderLatch leaderLatch, final TopoCache tc)
        throws UnknownHostException {
    final String hostName = InetAddress.getLocalHost().getCanonicalHostName();
    return new LeaderLatchListener() {
        final String STORM_JAR_SUFFIX = "-stormjar.jar";
        final String STORM_CODE_SUFFIX = "-stormcode.ser";
        final String STORM_CONF_SUFFIX = "-stormconf.ser";

        @Override//from  w ww . j  a v  a 2 s.c om
        public void isLeader() {
            Set<String> activeTopologyIds = new TreeSet<>(ClientZookeeper.getChildren(zk,
                    conf.get(Config.STORM_ZOOKEEPER_ROOT) + ClusterUtils.STORMS_SUBTREE, false));

            Set<String> activeTopologyBlobKeys = populateTopologyBlobKeys(activeTopologyIds);
            Set<String> activeTopologyCodeKeys = filterTopologyCodeKeys(activeTopologyBlobKeys);
            Set<String> allLocalBlobKeys = Sets.newHashSet(blobStore.listKeys());
            Set<String> allLocalTopologyBlobKeys = filterTopologyBlobKeys(allLocalBlobKeys);

            // this finds all active topologies blob keys from all local topology blob keys
            Sets.SetView<String> diffTopology = Sets.difference(activeTopologyBlobKeys,
                    allLocalTopologyBlobKeys);
            LOG.info("active-topology-blobs [{}] local-topology-blobs [{}] diff-topology-blobs [{}]",
                    generateJoinedString(activeTopologyIds), generateJoinedString(allLocalTopologyBlobKeys),
                    generateJoinedString(diffTopology));

            if (diffTopology.isEmpty()) {
                Set<String> activeTopologyDependencies = getTopologyDependencyKeys(activeTopologyCodeKeys);

                // this finds all dependency blob keys from active topologies from all local blob keys
                Sets.SetView<String> diffDependencies = Sets.difference(activeTopologyDependencies,
                        allLocalBlobKeys);
                LOG.info("active-topology-dependencies [{}] local-blobs [{}] diff-topology-dependencies [{}]",
                        generateJoinedString(activeTopologyDependencies),
                        generateJoinedString(allLocalBlobKeys), generateJoinedString(diffDependencies));

                if (diffDependencies.isEmpty()) {
                    LOG.info(
                            "Accepting leadership, all active topologies and corresponding dependencies found locally.");
                    tc.clear();
                } else {
                    LOG.info(
                            "Code for all active topologies is available locally, but some dependencies are not found locally, "
                                    + "giving up leadership.");
                    closeLatch();
                }
            } else {
                LOG.info("code for all active topologies not available locally, giving up leadership.");
                closeLatch();
            }
        }

        @Override
        public void notLeader() {
            LOG.info("{} lost leadership.", hostName);
            //Just to be sure
            tc.clear();
        }

        private String generateJoinedString(Set<String> activeTopologyIds) {
            return Joiner.on(",").join(activeTopologyIds);
        }

        private Set<String> populateTopologyBlobKeys(Set<String> activeTopologyIds) {
            Set<String> activeTopologyBlobKeys = new TreeSet<>();
            for (String activeTopologyId : activeTopologyIds) {
                activeTopologyBlobKeys.add(activeTopologyId + STORM_JAR_SUFFIX);
                activeTopologyBlobKeys.add(activeTopologyId + STORM_CODE_SUFFIX);
                activeTopologyBlobKeys.add(activeTopologyId + STORM_CONF_SUFFIX);
            }
            return activeTopologyBlobKeys;
        }

        private Set<String> filterTopologyBlobKeys(Set<String> blobKeys) {
            Set<String> topologyBlobKeys = new HashSet<>();
            for (String blobKey : blobKeys) {
                if (blobKey.endsWith(STORM_JAR_SUFFIX) || blobKey.endsWith(STORM_CODE_SUFFIX)
                        || blobKey.endsWith(STORM_CONF_SUFFIX)) {
                    topologyBlobKeys.add(blobKey);
                }
            }
            return topologyBlobKeys;
        }

        private Set<String> filterTopologyCodeKeys(Set<String> blobKeys) {
            Set<String> topologyCodeKeys = new HashSet<>();
            for (String blobKey : blobKeys) {
                if (blobKey.endsWith(STORM_CODE_SUFFIX)) {
                    topologyCodeKeys.add(blobKey);
                }
            }
            return topologyCodeKeys;
        }

        private Set<String> getTopologyDependencyKeys(Set<String> activeTopologyCodeKeys) {
            Set<String> activeTopologyDependencies = new TreeSet<>();
            Subject subject = ReqContext.context().subject();

            for (String activeTopologyCodeKey : activeTopologyCodeKeys) {
                try {
                    InputStreamWithMeta blob = blobStore.getBlob(activeTopologyCodeKey, subject);
                    byte[] blobContent = IOUtils.readFully(blob, new Long(blob.getFileLength()).intValue());
                    StormTopology stormCode = Utils.deserialize(blobContent, StormTopology.class);
                    if (stormCode.is_set_dependency_jars()) {
                        activeTopologyDependencies.addAll(stormCode.get_dependency_jars());
                    }
                    if (stormCode.is_set_dependency_artifacts()) {
                        activeTopologyDependencies.addAll(stormCode.get_dependency_artifacts());
                    }
                } catch (AuthorizationException | KeyNotFoundException | IOException e) {
                    LOG.error("Exception occurs while reading blob for key: " + activeTopologyCodeKey
                            + ", exception: " + e, e);
                    throw new RuntimeException("Exception occurs while reading blob for key: "
                            + activeTopologyCodeKey + ", exception: " + e, e);
                }
            }
            return activeTopologyDependencies;
        }

        private void closeLatch() {
            try {
                leaderLatch.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:org.apache.james.mailbox.inmemory.InMemoryMessageIdManager.java

@Override
public void setInMailboxes(MessageId messageId, List<MailboxId> mailboxIds, MailboxSession mailboxSession)
        throws MailboxException {
    List<MessageResult> messages = getMessages(ImmutableList.of(messageId), FetchGroupImpl.MINIMAL,
            mailboxSession);/*from  w w  w .  j  av a 2  s  . c  o m*/

    filterOnMailboxSession(mailboxIds, mailboxSession);

    if (!messages.isEmpty()) {
        ImmutableSet<MailboxId> currentMailboxes = currentMailboxes(messages).toSet();

        HashSet<MailboxId> targetMailboxes = Sets.newHashSet(mailboxIds);
        List<MailboxId> mailboxesToRemove = ImmutableList
                .copyOf(Sets.difference(currentMailboxes, targetMailboxes));
        SetView<MailboxId> mailboxesToAdd = Sets.difference(targetMailboxes, currentMailboxes);

        MessageResult referenceMessage = Iterables.getLast(messages);
        for (MailboxId mailboxId : mailboxesToAdd) {
            MessageRange messageRange = referenceMessage.getUid().toRange();
            mailboxManager.copyMessages(messageRange, referenceMessage.getMailboxId(), mailboxId,
                    mailboxSession);
            mailboxManager.getMailbox(mailboxId, mailboxSession).setFlags(referenceMessage.getFlags(),
                    FlagsUpdateMode.REPLACE, messageRange, mailboxSession);
        }

        for (MessageResult message : messages) {
            delete(message.getMessageId(), mailboxesToRemove, mailboxSession);
        }
    }
}