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

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

Introduction

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

Prototype

public static <K, V> SortedMapDifference<K, V> difference(SortedMap<K, ? extends V> left,
        Map<? extends K, ? extends V> right) 

Source Link

Document

Computes the difference between two sorted maps, using the comparator of the left map, or Ordering.natural() if the left map uses the natural ordering of its elements.

Usage

From source file:com.github.jcustenborder.kafka.connect.cdc.ChangeAssertions.java

static void assertMap(Map<String, ?> expected, Map<String, ?> actual, String message) {
    if (null == expected && null == actual) {
        return;//from  ww  w  .  ja va  2  s  .c o  m
    }

    String prefix = Strings.isNullOrEmpty(message) ? "" : message + ": ";
    assertNotNull(expected, prefix + "expected cannot be null");
    assertNotNull(actual, prefix + "actual cannot be null");
    MapDifference<String, ?> mapDifference = Maps.difference(expected, actual);
    assertTrue(mapDifference.areEqual(), new MapDifferenceSupplier(mapDifference, prefix));
}

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

@Override
void check() {/*from ww  w  .  j a  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:jetbrains.buildServer.agentsDiff.BuildAgentsDiffCalculator.java

public BuildAgentsDiffBean calculateDiff(BuildAgentEx agentA, BuildAgentEx agentB) {
    final Map<String, String> configParamsA = agentA.getAvailableParameters();
    final Map<String, String> configParamsB = agentB.getAvailableParameters();

    final List<BuildAgentsDiffEntry> entries = new LinkedList<BuildAgentsDiffEntry>();

    final MapDifference<String, String> mapDifference = Maps.difference(configParamsA, configParamsB);
    if (!mapDifference.areEqual()) {
        final Map<String, MapDifference.ValueDifference<String>> stringValueDifferenceMap = mapDifference
                .entriesDiffering();//from  w w  w. j av  a 2 s. c o  m
        for (String key : stringValueDifferenceMap.keySet()) {
            final MapDifference.ValueDifference<String> stringValueDifference = stringValueDifferenceMap
                    .get(key);
            entries.add(new BuildAgentsDiffEntry(BuildAgentsDiffEntryType.PARAMETER_VALUE, key,
                    stringValueDifference.leftValue(), stringValueDifference.rightValue()));
        }

        Map<String, String> map = mapDifference.entriesOnlyOnLeft();
        for (String key : map.keySet()) {
            entries.add(
                    new BuildAgentsDiffEntry(BuildAgentsDiffEntryType.PARAMETER_NAME, key, map.get(key), null));
        }

        map = mapDifference.entriesOnlyOnRight();
        for (String key : map.keySet()) {
            entries.add(
                    new BuildAgentsDiffEntry(BuildAgentsDiffEntryType.PARAMETER_NAME, key, null, map.get(key)));
        }
    }
    Collections.sort(entries, new Comparator<BuildAgentsDiffEntry>() {
        public int compare(BuildAgentsDiffEntry o1, BuildAgentsDiffEntry o2) {
            return o1.getPropertyName().compareToIgnoreCase(o2.getPropertyName());
        }
    });
    return new BuildAgentsDiffBean(agentA, agentB, entries);
}

From source file:jetbrains.buildServer.agentsDiff.BuildDiffCalculator.java

public BuildDiffBean calculateDiff(SBuild buildA, SBuild buildB) {
    final Map<String, String> configParamsA = buildA.getBuildOwnParameters();
    final Map<String, String> configParamsB = buildB.getBuildOwnParameters();

    final List<BuildAgentsDiffEntry> entries = new LinkedList<BuildAgentsDiffEntry>();

    final MapDifference<String, String> mapDifference = Maps.difference(configParamsA, configParamsB);
    if (!mapDifference.areEqual()) {
        final Map<String, MapDifference.ValueDifference<String>> stringValueDifferenceMap = mapDifference
                .entriesDiffering();//from  w w w .  ja  v a2  s . c  o  m
        for (String key : stringValueDifferenceMap.keySet()) {
            final MapDifference.ValueDifference<String> stringValueDifference = stringValueDifferenceMap
                    .get(key);
            entries.add(new BuildAgentsDiffEntry(BuildAgentsDiffEntryType.PARAMETER_VALUE, key,
                    stringValueDifference.leftValue(), stringValueDifference.rightValue()));
        }

        Map<String, String> map = mapDifference.entriesOnlyOnLeft();
        for (String key : map.keySet()) {
            entries.add(
                    new BuildAgentsDiffEntry(BuildAgentsDiffEntryType.PARAMETER_NAME, key, map.get(key), null));
        }

        map = mapDifference.entriesOnlyOnRight();
        for (String key : map.keySet()) {
            entries.add(
                    new BuildAgentsDiffEntry(BuildAgentsDiffEntryType.PARAMETER_NAME, key, null, map.get(key)));
        }
    }
    Collections.sort(entries, new Comparator<BuildAgentsDiffEntry>() {
        public int compare(BuildAgentsDiffEntry o1, BuildAgentsDiffEntry o2) {
            return o1.getPropertyName().compareToIgnoreCase(o2.getPropertyName());
        }
    });
    return new BuildDiffBean(buildA, buildB, entries);
}

From source file:com.google.copybara.git.FetchResult.java

FetchResult(ImmutableMap<String, GitReference> before, ImmutableMap<String, GitReference> after) {
    MapDifference<String, GitReference> diff = Maps.difference(before, after);
    deleted = ImmutableMap.copyOf(diff.entriesOnlyOnLeft());
    inserted = ImmutableMap.copyOf(diff.entriesOnlyOnRight());
    updated = ImmutableMap/* w w w .  j a v a 2 s.c om*/
            .copyOf(diff.entriesDiffering().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
                    v -> new RefUpdate(v.getValue().leftValue(), v.getValue().rightValue()))));
}

From source file:org.terasology.identity.storageServiceClient.SyncIdentitiesAction.java

@Override
public void perform(StorageServiceWorker worker) {
    if (worker.hasConflictingIdentities()) {
        worker.logMessage(true, "${engine:menu#storage-service-sync-previous-conflicts}");
    } else {//from   w  ww  .j a va 2  s  . c  o m
        try {
            Map<PublicIdentityCertificate, ClientIdentity> local = worker.securityConfig.getAllIdentities();
            Map<PublicIdentityCertificate, ClientIdentity> remote = worker.sessionInstance.getAllIdentities();
            MapDifference<PublicIdentityCertificate, ClientIdentity> diff = Maps.difference(local, remote);
            //upload the "local only" ones
            for (Map.Entry<PublicIdentityCertificate, ClientIdentity> entry : diff.entriesOnlyOnLeft()
                    .entrySet()) {
                if (entry.getValue().getPlayerPrivateCertificate() != null) { //TODO: find out why sometimes it's null
                    worker.sessionInstance.putIdentity(entry.getKey(), entry.getValue());
                }
            }
            //download the "remote only" ones
            for (Map.Entry<PublicIdentityCertificate, ClientIdentity> entry : diff.entriesOnlyOnRight()
                    .entrySet()) {
                worker.securityConfig.addIdentity(entry.getKey(), entry.getValue());
            }
            //keep track of the conflicting ones for manual resolution
            worker.resetConflicts();
            for (Map.Entry<PublicIdentityCertificate, MapDifference.ValueDifference<ClientIdentity>> entry : diff
                    .entriesDiffering().entrySet()) {
                worker.conflictingRemoteIdentities
                        .addLast(new IdentityBundle(entry.getKey(), entry.getValue().rightValue()));
            }
            worker.saveConfig();
            worker.logMessage(false, "${engine:menu#storage-service-sync-ok}", diff.entriesOnlyOnRight().size(),
                    diff.entriesOnlyOnLeft().size(), diff.entriesDiffering().size());
            if (!diff.entriesDiffering().isEmpty()) {
                worker.logMessage(true, "${engine:menu#storage-service-sync-conflicts}");
            }
        } catch (Exception e) {
            worker.logMessage(true, "${engine:menu#storage-service-sync-fail}", e.getMessage());
        }
    }
    worker.status = StorageServiceWorkerStatus.LOGGED_IN;
}

From source file:co.mitro.core.util.ParallelPhantomLoginMain.java

static synchronized final void addFlattenedSiteData(String key, Map<String, String> data) {
    newData.put(key, data);//from   w  w  w. j  a va2  s.c o m
    if (oldData.containsKey(key)) {
        MapDifference<String, String> differences = Maps.difference(data, oldData.get(key));
        if (differences.areEqual()) {
            safePrintln("data for " + key + " has not changed.");
            return;
        } else {
            safePrintln("data for " + key + " differs:");
            for (String s : differences.entriesOnlyOnLeft().keySet()) {
                safePrintln("\tNew key " + s);
            }
            for (String s : differences.entriesOnlyOnRight().keySet()) {
                safePrintln("\tMissing key " + s);
            }
            for (String s : differences.entriesDiffering().keySet()) {
                safePrintln("\tKey[" + key + "]" + " old:" + differences.entriesDiffering().get(s).rightValue()
                        + " new:" + differences.entriesDiffering().get(s).leftValue());
            }
        }
    } else {
        safePrintln("no old data for " + key);
    }
}

From source file:io.druid.indexing.overlord.setup.FillCapacityWithAffinityConfig.java

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }//from  w  w w .  j a v a2 s . c o  m
    if (o == null || getClass() != o.getClass()) {
        return false;
    }

    FillCapacityWithAffinityConfig that = (FillCapacityWithAffinityConfig) o;

    if (affinity != null ? !Maps.difference(affinity, that.affinity).entriesDiffering().isEmpty()
            : that.affinity != null) {
        return false;
    }

    return true;
}

From source file:io.druid.indexing.overlord.setup.AffinityConfig.java

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }/*from w  w  w .  ja v a  2  s . c  om*/
    if (o == null || getClass() != o.getClass()) {
        return false;
    }

    AffinityConfig that = (AffinityConfig) o;

    if (affinity != null ? !Maps.difference(affinity, that.affinity).entriesDiffering().isEmpty()
            : that.affinity != null) {
        return false;
    }

    return true;
}

From source file:io.fabric8.process.fabric.child.tasks.ApplyConfigurationTask.java

@Override
public void install(ProcessConfig config, String id, File installDir) throws Exception {
    Map<String, String> templates = Maps.filterKeys(configuration, isTemplate);
    Map<String, String> plainFiles = Maps.difference(configuration, templates).entriesOnlyOnLeft();
    Map<String, String> renderedTemplates = Maps.transformValues(templates,
            new MvelTemplateRendering(variables));
    File baseDir = ProcessUtils.findInstallDir(installDir);
    applyTemplates(renderedTemplates, baseDir);
    applyPlainConfiguration(plainFiles, baseDir);

}