Example usage for com.google.common.collect MapDifference entriesDiffering

List of usage examples for com.google.common.collect MapDifference entriesDiffering

Introduction

In this page you can find the example usage for com.google.common.collect MapDifference entriesDiffering.

Prototype

Map<K, ValueDifference<V>> entriesDiffering();

Source Link

Document

Returns an unmodifiable map describing keys that appear in both maps, but with different values.

Usage

From source file:org.nuxeo.tools.esync.checker.TypeCardinalityChecker.java

@Override
void check() {//  w w  w.ja  v a  2s.  c o m
    Map<String, Long> esTypes = es.getTypeCardinality();
    Map<String, Long> dbTypes = db.getTypeCardinality();
    MapDifference<String, Long> diff = Maps.difference(dbTypes, esTypes);
    if (diff.areEqual()) {
        postMessage("Found same types cardinality");
        return;
    }
    postMessage("Difference found in types cardinality.");
    for (String key : diff.entriesOnlyOnLeft().keySet()) {
        postError(String.format("Missing type on ES: %s, expected: %d", key, dbTypes.get(key)));
    }
    for (String key : diff.entriesOnlyOnRight().keySet()) {
        postError(String.format("Spurious type in ES: %s, actual: %d", key, esTypes.get(key)));
    }
    for (String key : diff.entriesDiffering().keySet()) {
        long esCount = 0;
        long dbCount = 0;
        if (esTypes.containsKey(key)) {
            esCount = esTypes.get(key);
        }
        if (dbTypes.containsKey(key)) {
            dbCount = dbTypes.get(key);
        }
        postError(String.format("Document type %s (including versions), expected: %d, actual: %d, diff: %d",
                key, dbCount, esCount, dbCount - esCount));
        post(new DiffTypeEvent(key, "diff"));
    }
}

From source file:org.apache.abdera2.activities.extra.Extra.java

/**
 * Two ASObject's are considered equivalent in identity if 
 * they share the same objectType and id property
 * values. Note: This implementation does not yet take 
 * the downstreamDuplicates and upstreamDuplicates properties
 * into account when determining equivalence.
 *//*from   w ww  .  j av  a  2s.  c  o m*/
public static <X extends ASObject> Equivalence<X> identity() {
    return new Equivalence<X>() {
        protected boolean doEquivalent(X a, X b) {
            Selector<Map.Entry<String, Object>> filter = ASBase.withFields("id", "alias", "objectType",
                    "displayName");
            Map<String, Object> map1 = Maps.transformEntries(a.toMap(filter), lower_val);
            Map<String, Object> map2 = Maps.transformEntries(b.toMap(filter), lower_val);
            MapDifference<String, Object> diff = Maps.difference(map1, map2);
            return ((diff.entriesInCommon().containsKey("alias") || diff.entriesInCommon().containsKey("id")
                    || diff.entriesInCommon().containsKey("displayName"))
                    && !diff.entriesDiffering().containsKey("objectType")
                    && !diff.entriesDiffering().containsKey("id") && !diff.entriesOnlyOnLeft().containsKey("id")
                    && !diff.entriesOnlyOnRight().containsKey("id"));
        }

        protected int doHash(ASObject t) {
            return MoreFunctions.genHashCode(1, t.getId(), t.getProperty("alias"), t.getObjectType());
        }
    };
}

From source file:org.locationtech.geogig.storage.TransactionRefDatabase.java

/**
 * The names of the refs that either have changed from their original value or didn't exist at
 * the time this method is called//from   ww  w .j  a  v a 2 s  .c om
 */
public ImmutableSet<String> getChangedRefs() {
    Map<String, String> externalOriginals;
    Map<String, String> externalChanged;
    {
        Map<String, String> originals = refDb.getAll(this.txOrigNamespace);
        Map<String, String> changed = refDb.getAll(this.txNamespace);

        externalOriginals = toExternal(originals);
        externalChanged = toExternal(changed);
    }
    MapDifference<String, String> difference;
    difference = Maps.difference(externalOriginals, externalChanged);

    Map<String, String> changes = new HashMap<>();
    // include all new refs
    changes.putAll(difference.entriesOnlyOnRight());

    // include all changed refs, with the new values
    for (Map.Entry<String, ValueDifference<String>> e : difference.entriesDiffering().entrySet()) {
        String name = e.getKey();
        ValueDifference<String> valueDifference = e.getValue();
        String newValue = valueDifference.rightValue();
        changes.put(name, newValue);
    }
    return ImmutableSet.copyOf(changes.keySet());
}

From source file:com.thinkbiganalytics.metadata.rest.model.nifi.NiFiFlowCacheSync.java

public Map<String, NiFiFlowCacheConnectionData> getConnectionIdToConnectionUpdatedSinceLastSync(
        Map<String, String> latestConnectionIdToNameMap,
        Map<String, NiFiFlowCacheConnectionData> latestConnectionDataMap) {
    MapDifference<String, String> diff = Maps.difference(snapshot.getConnectionIdToConnectionName(),
            latestConnectionIdToNameMap);
    Map<String, NiFiFlowCacheConnectionData> differences = new HashMap<>();
    Map<String, String> diffs = diff.entriesOnlyOnRight();
    if (diffs != null && !diffs.isEmpty()) {
        for (String connId : diffs.keySet()) {
            differences.put(connId, latestConnectionDataMap.get(connId));
        }//ww w .j av  a 2s .  c  o  m
    }

    Set<String> updates = diff.entriesDiffering().keySet();
    if (updates != null) {
        for (String key : updates) {
            differences.put(key, latestConnectionDataMap.get(key));
        }
    }

    return differences;
}

From source file:com.github.sevntu.checkstyle.domain.BaseCheckTestSupport.java

protected void verify(Checker checker, File[] processedFiles, Map<String, List<String>> expectedViolations)
        throws Exception {
    stream.flush();/* ww w.j a va 2 s .c om*/
    final List<File> theFiles = Lists.newArrayList();
    Collections.addAll(theFiles, processedFiles);
    final int errs = checker.process(theFiles);

    // process each of the lines
    final Map<String, List<String>> actualViolations = getActualViolations(errs);
    final Map<String, List<String>> realExpectedViolations = Maps.filterValues(expectedViolations,
            new Predicate<List<String>>() {
                @Override
                public boolean apply(List<String> input) {
                    return !input.isEmpty();
                }
            });
    final MapDifference<String, List<String>> violationDifferences = Maps.difference(realExpectedViolations,
            actualViolations);

    final Map<String, List<String>> missingViolations = violationDifferences.entriesOnlyOnLeft();
    final Map<String, List<String>> unexpectedViolations = violationDifferences.entriesOnlyOnRight();
    final Map<String, ValueDifference<List<String>>> differingViolations = violationDifferences
            .entriesDiffering();

    final StringBuilder message = new StringBuilder();
    if (!missingViolations.isEmpty()) {
        message.append("missing violations: ").append(missingViolations);
    }
    if (!unexpectedViolations.isEmpty()) {
        if (message.length() > 0) {
            message.append('\n');
        }
        message.append("unexpected violations: ").append(unexpectedViolations);
    }
    if (!differingViolations.isEmpty()) {
        if (message.length() > 0) {
            message.append('\n');
        }
        message.append("differing violations: ").append(differingViolations);
    }

    assertTrue(message.toString(),
            missingViolations.isEmpty() && unexpectedViolations.isEmpty() && differingViolations.isEmpty());

    checker.destroy();
}

From source file:org.locationtech.geogig.storage.impl.TransactionRefDatabase.java

/**
 * The names of the refs that either have changed from their original value or didn't exist at
 * the time this method is called/*  www . j  ava2s  . c o m*/
 */
public ImmutableSet<String> getChangedRefs() {
    Map<String, String> externalOriginals;
    Map<String, String> externalChanged;
    {
        Map<String, String> originals = refDb.getAll(this.txOrigNamespace);
        Map<String, String> changed = refDb.getAll(this.txChangedNamespace);

        externalOriginals = toExternal(originals);
        externalChanged = toExternal(changed);
    }
    MapDifference<String, String> difference;
    difference = Maps.difference(externalOriginals, externalChanged);

    Map<String, String> changes = new HashMap<>();
    // include all new refs
    changes.putAll(difference.entriesOnlyOnRight());

    // include all changed refs, with the new values
    for (Map.Entry<String, ValueDifference<String>> e : difference.entriesDiffering().entrySet()) {
        String name = e.getKey();
        ValueDifference<String> valueDifference = e.getValue();
        String newValue = valueDifference.rightValue();
        changes.put(name, newValue);
    }
    return ImmutableSet.copyOf(changes.keySet());
}

From source file:eu.numberfour.n4js.ui.preferences.AbstractN4JSPreferencePage.java

/**
 * @param originalSettings/*  w  w  w  .java 2  s .  co  m*/
 *            the settings before applying the values of the form page
 * @param currentSettings
 *            the settings after collecting the values of the form page
 * @return a map keyed by the preference store key (e.g. outlet.es5.autobuilding for compiler (resp. output
 *         configuration) with name 'es5' and property 'autobuilding') containing old and new value. Only keys whose
 *         values has been changed are included.
 */
private Map<String, ValueDifference<String>> getPreferenceChanges(Map<String, String> originalSettings,
        Map<String, String> currentSettings) {
    MapDifference<String, String> mapDifference = Maps.difference(currentSettings, originalSettings);
    return mapDifference.entriesDiffering();
}

From source file:org.apache.cassandra.schema.LegacySchemaTables.java

private static Set<String> mergeKeyspaces(Map<DecoratedKey, ColumnFamily> before,
        Map<DecoratedKey, ColumnFamily> after) {
    List<Row> created = new ArrayList<>();
    List<String> altered = new ArrayList<>();
    Set<String> dropped = new HashSet<>();

    /*// ww  w.  j a v  a  2s .com
     * - we don't care about entriesOnlyOnLeft() or entriesInCommon(), because only the changes are of interest to us
     * - of all entriesOnlyOnRight(), we only care about ones that have live columns; it's possible to have a ColumnFamily
     *   there that only has the top-level deletion, if:
     *      a) a pushed DROP KEYSPACE change for a keyspace hadn't ever made it to this node in the first place
     *      b) a pulled dropped keyspace that got dropped before it could find a way to this node
     * - of entriesDiffering(), we don't care about the scenario where both pre and post-values have zero live columns:
     *   that means that a keyspace had been recreated and dropped, and the recreated keyspace had never found a way
     *   to this node
     */
    MapDifference<DecoratedKey, ColumnFamily> diff = Maps.difference(before, after);

    for (Map.Entry<DecoratedKey, ColumnFamily> entry : diff.entriesOnlyOnRight().entrySet())
        if (entry.getValue().hasColumns())
            created.add(new Row(entry.getKey(), entry.getValue()));

    for (Map.Entry<DecoratedKey, MapDifference.ValueDifference<ColumnFamily>> entry : diff.entriesDiffering()
            .entrySet()) {
        String keyspaceName = AsciiType.instance.compose(entry.getKey().getKey());

        ColumnFamily pre = entry.getValue().leftValue();
        ColumnFamily post = entry.getValue().rightValue();

        if (pre.hasColumns() && post.hasColumns())
            altered.add(keyspaceName);
        else if (pre.hasColumns())
            dropped.add(keyspaceName);
        else if (post.hasColumns()) // a (re)created keyspace
            created.add(new Row(entry.getKey(), post));
    }

    for (Row row : created)
        Schema.instance.addKeyspace(createKeyspaceFromSchemaPartition(row));
    for (String name : altered)
        Schema.instance.updateKeyspace(name);
    return dropped;
}

From source file:org.locationtech.geogig.remotes.pack.DiffRemoteRefsOp.java

@Override
protected List<RefDiff> _call() {
    checkState(remote != null, "no remote provided");
    // list of refs/remotes/<remote>/<refname> or refs/heads according to formatAsRemoteRefs
    Map<String, Ref> remotes;
    Map<String, Ref> locals;
    {//from www.j  a v  a2  s .c  om
        // current live remote refs in the remote's local namespace (e.g. refs/heads/<branch>)
        Iterable<Ref> remoteRefs = getRemoteRefs();
        if (formatAsRemoteRefs) {
            // format refs returned by the remote in its local namespaces to our repository's
            // remotes namespace
            remoteRefs = command(MapRef.class)//
                    .setRemote(remote.getInfo())//
                    .convertToRemote()//
                    .addAll(remoteRefs)//
                    .call();
        }
        // current local local copy of the remote refs (e.g. refs/remotes/<remote>/<branch>
        List<Ref> remoteLocalRefs = Lists.newArrayList(getRemoteLocalRefs());
        if (!formatAsRemoteRefs) {
            // format local repository copies of the remote refs to the remote's local namespace
            remoteLocalRefs = command(MapRef.class)//
                    .setRemote(remote.getInfo())//
                    .convertToLocal()//
                    .addAll(remoteLocalRefs)//
                    .call();
        }
        if (this.getTags) {
            Map<String, RevTag> tags = Maps.uniqueIndex(command(TagListOp.class).call(), (t) -> t.getName());
            for (Ref rf : remoteRefs) {
                if (rf.getName().startsWith(Ref.TAGS_PREFIX) && tags.containsKey(rf.localName())) {
                    RevTag tag = tags.get(rf.localName());
                    remoteLocalRefs.add(new Ref(Ref.TAGS_PREFIX + tag.getName(), tag.getId()));
                }
            }
        }
        remotes = Maps.uniqueIndex(remoteRefs, (r) -> r.getName());
        locals = Maps.uniqueIndex(remoteLocalRefs, (r) -> r.getName());
    }
    final boolean mapped = remote.getInfo().getMapped();
    if (mapped) {
        // for a mapped remote, we are only interested in the branch we are mapped to
        final String mappedBranch = remote.getInfo().getMappedBranch();
        checkNotNull(mappedBranch);
        final String mappedBranchName = Ref.localName(mappedBranch);
        remotes = Maps.filterKeys(remotes, (name) -> Ref.localName(name).equals(mappedBranchName));
        locals = Maps.filterKeys(locals, (name) -> Ref.localName(name).equals(mappedBranchName));
    }
    MapDifference<String, Ref> difference = Maps.difference(remotes, locals);

    // refs existing on the remote and not on the local repo
    Collection<Ref> newRemoteRefs = difference.entriesOnlyOnLeft().values();

    // remote refs existing on the local repo and not existing on the remote anymore
    Collection<Ref> removedRemoteRefs = difference.entriesOnlyOnRight().values();

    // refs existing both in local and remote with different objectIds
    Collection<ValueDifference<Ref>> changes = difference.entriesDiffering().values();

    List<RefDiff> diffs = new ArrayList<>();
    newRemoteRefs.forEach((r) -> diffs.add(RefDiff.added(r)));
    removedRemoteRefs.forEach((r) -> diffs.add(RefDiff.removed(r)));
    // v.leftValue() == new (remote copy), v.rightValue() == old (local copy)
    changes.forEach((v) -> diffs.add(RefDiff.updated(v.rightValue(), v.leftValue())));

    return diffs;
}

From source file:org.apache.cassandra.schema.LegacySchemaTables.java

private static void mergeTables(Map<DecoratedKey, ColumnFamily> before, Map<DecoratedKey, ColumnFamily> after) {
    List<CFMetaData> created = new ArrayList<>();
    List<CFMetaData> altered = new ArrayList<>();
    List<CFMetaData> dropped = new ArrayList<>();

    MapDifference<DecoratedKey, ColumnFamily> diff = Maps.difference(before, after);

    for (Map.Entry<DecoratedKey, ColumnFamily> entry : diff.entriesOnlyOnRight().entrySet())
        if (entry.getValue().hasColumns())
            created.addAll(createTablesFromTablesPartition(new Row(entry.getKey(), entry.getValue())).values());

    for (Map.Entry<DecoratedKey, MapDifference.ValueDifference<ColumnFamily>> entry : diff.entriesDiffering()
            .entrySet()) {/*from ww  w  . j av  a2  s .c o  m*/
        String keyspaceName = AsciiType.instance.compose(entry.getKey().getKey());

        ColumnFamily pre = entry.getValue().leftValue();
        ColumnFamily post = entry.getValue().rightValue();

        if (pre.hasColumns() && post.hasColumns()) {
            MapDifference<String, CFMetaData> delta = Maps.difference(
                    Schema.instance.getKSMetaData(keyspaceName).cfMetaData(),
                    createTablesFromTablesPartition(new Row(entry.getKey(), post)));

            dropped.addAll(delta.entriesOnlyOnLeft().values());
            created.addAll(delta.entriesOnlyOnRight().values());
            Iterables.addAll(altered, Iterables.transform(delta.entriesDiffering().values(),
                    new Function<MapDifference.ValueDifference<CFMetaData>, CFMetaData>() {
                        public CFMetaData apply(MapDifference.ValueDifference<CFMetaData> pair) {
                            return pair.rightValue();
                        }
                    }));
        } else if (pre.hasColumns()) {
            dropped.addAll(Schema.instance.getKSMetaData(keyspaceName).cfMetaData().values());
        } else if (post.hasColumns()) {
            created.addAll(createTablesFromTablesPartition(new Row(entry.getKey(), post)).values());
        }
    }

    for (CFMetaData cfm : created)
        Schema.instance.addTable(cfm);
    for (CFMetaData cfm : altered)
        Schema.instance.updateTable(cfm.ksName, cfm.cfName);
    for (CFMetaData cfm : dropped)
        Schema.instance.dropTable(cfm.ksName, cfm.cfName);
}