List of usage examples for com.google.common.collect MapDifference entriesInCommon
Map<K, V> entriesInCommon();
From source file:components.cells.Cells.java
private static <T> Map<IPosition, T> merge(final T initialSymbol, final Map<IPosition, T> left, final Map<IPosition, T> right) { final Builder<IPosition, T> builder = new ImmutableSortedMap.Builder<IPosition, T>(Ordering.natural()); final MapDifference<IPosition, T> difference = Maps.difference(left, right); for (final Entry<IPosition, T> mutation : difference.entriesInCommon().entrySet()) if (!mutation.getValue().equals(initialSymbol)) builder.put(mutation);/*from w ww. j a va 2s .com*/ for (final Entry<IPosition, T> mutation : difference.entriesOnlyOnLeft().entrySet()) if (!mutation.getValue().equals(initialSymbol)) builder.put(mutation); for (final Entry<IPosition, T> mutation : difference.entriesOnlyOnRight().entrySet()) if (!mutation.getValue().equals(initialSymbol)) builder.put(mutation); for (final Entry<IPosition, ValueDifference<T>> mutation : difference.entriesDiffering().entrySet()) { final T rightValue = mutation.getValue().rightValue(); if (!rightValue.equals(initialSymbol)) builder.put(mutation.getKey(), rightValue); } return builder.build(); }
From source file:com.nearinfinity.honeycomb.mysql.Util.java
/** * Retrieve from a list of indices which ones have been changed. * * @param indices Table indices/*from w ww . ja v a2 s . co m*/ * @param oldRecords Old MySQL row * @param newRecords New MySQL row * @return List of changed indices */ public static ImmutableList<IndexSchema> getChangedIndices(Collection<IndexSchema> indices, Map<String, ByteBuffer> oldRecords, Map<String, ByteBuffer> newRecords) { if (indices.isEmpty()) { return ImmutableList.of(); } MapDifference<String, ByteBuffer> diff = Maps.difference(oldRecords, newRecords); Set<String> changedColumns = Sets.difference(Sets.union(newRecords.keySet(), oldRecords.keySet()), diff.entriesInCommon().keySet()); ImmutableList.Builder<IndexSchema> changedIndices = ImmutableList.builder(); for (IndexSchema index : indices) { Set<String> indexColumns = ImmutableSet.copyOf(index.getColumns()); if (!Sets.intersection(changedColumns, indexColumns).isEmpty()) { changedIndices.add(index); } } return changedIndices.build(); }
From source file:org.apache.aurora.scheduler.updater.JobDiff.java
private static JobDiff computeUnscoped(Map<Integer, ITaskConfig> currentState, IJobKey job, Map<Integer, ITaskConfig> proposedState) { requireNonNull(job);//w w w. j a v a2 s. com requireNonNull(proposedState); MapDifference<Integer, ITaskConfig> diff = Maps.difference(currentState, proposedState); Map<Integer, ITaskConfig> removedInstances = ImmutableMap.<Integer, ITaskConfig>builder() .putAll(diff.entriesOnlyOnLeft()) .putAll(Maps.transformValues(diff.entriesDiffering(), JobDiff.leftValue())).build(); Set<Integer> addedInstances = ImmutableSet.<Integer>builder().addAll(diff.entriesOnlyOnRight().keySet()) .addAll(diff.entriesDiffering().keySet()).build(); return new JobDiff(removedInstances, addedInstances, ImmutableMap.copyOf(diff.entriesInCommon())); }
From source file:uk.ac.open.kmi.iserve.discovery.disco.Util.java
/** * Given two sets of matches find out the differences * TODO: Probably useful for others. Place somewhere else. The discovery-api module is an option but it forces to have guava as a dependency, do we want to? * * @param inputMatches//from w w w. j a va 2 s .c o m * @param inputMatchesAlt */ public static void compareResults(Map<URL, MatchResult> inputMatches, Map<URL, MatchResult> inputMatchesAlt) { MapDifference<URL, MatchResult> diff = Maps.difference(inputMatches, inputMatchesAlt); System.out.println("Comparing Match Results Maps"); Map<URL, MatchResult> common = diff.entriesInCommon(); System.out.println("Entries in common"); showMapDetails(common); Map<URL, MatchResult> onlyLeft = diff.entriesOnlyOnLeft(); System.out.println("Entries only in the left map"); showMapDetails(common); Map<URL, MatchResult> onlyRight = diff.entriesOnlyOnRight(); System.out.println("Entries only in the right map"); showMapDetails(common); Map<URL, ValueDifference<MatchResult>> diffValues = diff.entriesDiffering(); System.out.println("Differing values"); for (Entry<URL, ValueDifference<MatchResult>> entry : diffValues.entrySet()) { MatchResult resultLeft = entry.getValue().leftValue(); MatchResult resultRight = entry.getValue().rightValue(); System.out.println("Match " + entry.getKey().toString()); System.out.println("Left value details: "); System.out.println("Match explanation: " + resultLeft.getExplanation()); System.out.println("Right value details: "); System.out.println("Match explanation: " + resultRight.getExplanation()); } }
From source file:org.openhealthtools.mdht.mdmi.CmdLineApp.java
public static String transform(String srcMap, String srcMdl, String srcMsg, String trgMap, String trgMdl, String trgMsg, String cnvElm) { File rootDir = new File(System.getProperties().getProperty("user.dir")); System.out.println(" rootDir = " + rootDir.getAbsolutePath()); System.out.println(""); // initialize the runtime, using the current folder as the roo folder Mdmi.INSTANCE.initialize(rootDir);//from ww w . ja v a 2 s . c o m Mdmi.INSTANCE.start(); String retVal = null; try { // 1. check to make sure the maps and messages exist File f = Mdmi.INSTANCE.fileFromRelPath(srcMap); if (!f.exists() || !f.isFile()) { System.out.println("Source map file '" + srcMap + "' does not exist!"); usage(); } f = Mdmi.INSTANCE.fileFromRelPath(trgMap); if (!f.exists() || !f.isFile()) { System.out.println("Target map file '" + trgMap + "' does not exist!"); usage(); } f = Mdmi.INSTANCE.fileFromRelPath(srcMsg); if (!f.exists() || !f.isFile()) { System.out.println("Source message file '" + srcMsg + "' does not exist!"); usage(); } f = Mdmi.INSTANCE.fileFromRelPath(trgMsg); if (!f.exists() || !f.isFile()) { System.out.println("Target message file '" + trgMsg + "' does not exist!"); usage(); } // 2. make sure the qualified message names are spelled right String[] a = srcMdl.split("\\."); if (a == null || a.length != 2) { System.out .println("Invalid source model '" + srcMdl + "', must be formatted as MapName.MessageName"); usage(); } String srcMapName = a[0]; String srcMsgMdl = a[1]; a = trgMdl.split("\\."); if (a == null || a.length != 2) { System.out .println("Invalid target model '" + trgMdl + "', must be formatted as MapName.MessageName"); usage(); } String trgMapName = a[0]; String trgMsgMdl = a[1]; // 3. parse the elements array final ArrayList<String> elements = new ArrayList<String>(); String[] ss = cnvElm.split(";"); for (String s : ss) { if (s != null && s.trim().length() > 0) { elements.add(s); } } // 4. load the maps into the runtime. Mdmi.INSTANCE.getConfig().putMapInfo(new MdmiConfig.MapInfo(srcMapName, srcMap)); Mdmi.INSTANCE.getConfig().putMapInfo(new MdmiConfig.MapInfo(trgMapName, trgMap)); Mdmi.INSTANCE.getResolver().resolveConfig(Mdmi.INSTANCE.getConfig()); // 5. Construct the parameters to the call based on the values passed in MdmiModelRef sMod = new MdmiModelRef(srcMapName, srcMsgMdl); MdmiMessage sMsg = new MdmiMessage(Mdmi.INSTANCE.fileFromRelPath(srcMsg)); MdmiModelRef tMod = new MdmiModelRef(trgMapName, trgMsgMdl); MdmiMessage tMsg = new MdmiMessage(Mdmi.INSTANCE.fileFromRelPath(trgMsg)); Map<String, MdmiBusinessElementReference> left = sMod.getModel().getBusinessElementHashMap(); Map<String, MdmiBusinessElementReference> right = tMod.getModel().getBusinessElementHashMap(); Equivalence<MdmiBusinessElementReference> valueEquivalence = new Equivalence<MdmiBusinessElementReference>() { @Override protected boolean doEquivalent(MdmiBusinessElementReference a, MdmiBusinessElementReference b) { return a.getUniqueIdentifier().equals(b.getUniqueIdentifier()); } @Override protected int doHash(MdmiBusinessElementReference t) { return t.getUniqueIdentifier().hashCode(); } }; MapDifference<String, MdmiBusinessElementReference> differences = Maps.difference(left, right, valueEquivalence); Predicate<MdmiBusinessElementReference> predicate = new Predicate<MdmiBusinessElementReference>() { @Override public boolean apply(MdmiBusinessElementReference input) { if (!elements.isEmpty()) { for (String element : elements) { if (input.getName().equalsIgnoreCase(element)) { return true; } } return false; } return true; } }; ; ArrayList<MdmiBusinessElementReference> bers = new ArrayList<MdmiBusinessElementReference>(); bers.addAll(Collections2.filter(differences.entriesInCommon().values(), predicate)); MdmiTransferInfo ti = new MdmiTransferInfo(sMod, sMsg, tMod, tMsg, bers); ti.useDictionary = true; // 6. call the runtime Mdmi.INSTANCE.executeTransfer(ti); // 7. set the return value retVal = tMsg.getDataAsString(); saveResults(retVal); } catch (Exception ex) { ex.printStackTrace(); } Mdmi.INSTANCE.stop(); return retVal; // return the target message transformed }
From source file:org.wso2.carbon.governance.comparator.wsdl.WSDLOperationComparator.java
private boolean isDifferent(Map<String, Fault> left, Map<String, Fault> right) { if (left != null && right != null && left.size() != right.size()) { return true; } else {/*from w w w .j a v a 2s .c o m*/ MapDifference<String, Fault> mapDiff = Maps.difference(left, right); if (!mapDiff.areEqual()) { return true; } else { for (String name : mapDiff.entriesInCommon().keySet()) { if (isDifferent(left.get(name), right.get(name))) { return true; } } } } return false; }
From source file:com.facebook.buck.rules.ActionGraphCache.java
/** * Compares the cached ActionGraph with a newly generated from the targetGraph. The comparison * is done by generating and comparing content agnostic RuleKeys. In case of mismatch, the * mismatching BuildRules are printed and the building process is stopped. * @param eventBus Buck's event bus.//from w w w. ja va2 s . co m * @param lastActionGraphAndResolver The cached version of the graph that gets compared. * @param targetGraph Used to generate the actionGraph that gets compared with lastActionGraph. */ private void compareActionGraphs(final BuckEventBus eventBus, final ActionGraphAndResolver lastActionGraphAndResolver, final TargetGraph targetGraph, final int keySeed) { try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("ActionGraphCacheCheck"))) { // We check that the lastActionGraph is not null because it's possible we had a // invalidateCache() between the scheduling and the execution of this task. LOG.info("ActionGraph integrity check spawned."); Pair<TargetGraph, ActionGraphAndResolver> newActionGraph = new Pair<TargetGraph, ActionGraphAndResolver>( targetGraph, createActionGraph(eventBus, new DefaultTargetNodeToBuildRuleTransformer(), targetGraph)); Map<BuildRule, RuleKey> lastActionGraphRuleKeys = getRuleKeysFromBuildRules( lastActionGraphAndResolver.getActionGraph().getNodes(), lastActionGraphAndResolver.getResolver(), keySeed); Map<BuildRule, RuleKey> newActionGraphRuleKeys = getRuleKeysFromBuildRules( newActionGraph.getSecond().getActionGraph().getNodes(), newActionGraph.getSecond().getResolver(), keySeed); if (!lastActionGraphRuleKeys.equals(newActionGraphRuleKeys)) { invalidateCache(); String mismatchInfo = "RuleKeys of cached and new ActionGraph don't match:\n"; MapDifference<BuildRule, RuleKey> mismatchedRules = Maps.difference(lastActionGraphRuleKeys, newActionGraphRuleKeys); mismatchInfo += "Number of nodes in common/differing: " + mismatchedRules.entriesInCommon().size() + "/" + mismatchedRules.entriesDiffering().size() + "\n" + "Entries only in the cached ActionGraph: " + mismatchedRules.entriesOnlyOnLeft().size() + "Entries only in the newly created ActionGraph: " + mismatchedRules.entriesOnlyOnRight().size() + "The rules that did not match:\n"; mismatchInfo += mismatchedRules.entriesDiffering().keySet().toString(); LOG.error(mismatchInfo); throw new RuntimeException(mismatchInfo); } } }
From source file:com.facebook.buck.core.model.actiongraph.computation.ActionGraphProvider.java
/** * Compares the cached ActionGraph with a newly generated from the targetGraph. The comparison is * done by generating and comparing content agnostic RuleKeys. In case of mismatch, the * mismatching BuildRules are printed and the building process is stopped. * * @param lastActionGraphAndBuilder The cached version of the graph that gets compared. * @param targetGraph Used to generate the actionGraph that gets compared with lastActionGraph. * @param fieldLoader/*from w w w .j av a 2 s .co m*/ * @param ruleKeyLogger The logger to use (if any) when computing the new action graph */ private void compareActionGraphs(ActionGraphAndBuilder lastActionGraphAndBuilder, TargetNodeToBuildRuleTransformer transformer, TargetGraph targetGraph, RuleKeyFieldLoader fieldLoader, Optional<ThriftRuleKeyLogger> ruleKeyLogger) { try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("ActionGraphCacheCheck"))) { // We check that the lastActionGraph is not null because it's possible we had a // invalidateCache() between the scheduling and the execution of this task. LOG.info("ActionGraph integrity check spawned."); ActionGraphAndBuilder newActionGraph = createActionGraph(transformer, targetGraph, IncrementalActionGraphMode.DISABLED); Map<BuildRule, RuleKey> lastActionGraphRuleKeys = getRuleKeysFromBuildRules( lastActionGraphAndBuilder.getActionGraph().getNodes(), lastActionGraphAndBuilder.getActionGraphBuilder(), fieldLoader, Optional.empty() /* Only log once, and only for the new graph */); Map<BuildRule, RuleKey> newActionGraphRuleKeys = getRuleKeysFromBuildRules( newActionGraph.getActionGraph().getNodes(), newActionGraph.getActionGraphBuilder(), fieldLoader, ruleKeyLogger); if (!lastActionGraphRuleKeys.equals(newActionGraphRuleKeys)) { actionGraphCache.invalidateCache(); String mismatchInfo = "RuleKeys of cached and new ActionGraph don't match:\n"; MapDifference<BuildRule, RuleKey> mismatchedRules = Maps.difference(lastActionGraphRuleKeys, newActionGraphRuleKeys); mismatchInfo += "Number of nodes in common/differing: " + mismatchedRules.entriesInCommon().size() + "/" + mismatchedRules.entriesDiffering().size() + "\n" + "Entries only in the cached ActionGraph: " + mismatchedRules.entriesOnlyOnLeft().size() + "Entries only in the newly created ActionGraph: " + mismatchedRules.entriesOnlyOnRight().size() + "The rules that did not match:\n"; mismatchInfo += mismatchedRules.entriesDiffering().keySet().toString(); LOG.error(mismatchInfo); throw new RuntimeException(mismatchInfo); } } }
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 ww w.java 2 s .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.lisapark.octopus.core.ProcessorBean.java
/** /*from w w w .ja va2s . co m*/ * * @param bean * @param threshold * @return */ public Double compare(ProcessorBean bean) { Double tolerance = null; if (this.getClassName().equalsIgnoreCase(bean.getClassName())) { Map<String, Object> thisParams = Maps.newHashMap(this.getParams()); Map<String, Object> thatParams = Maps.newHashMap(bean.getParams()); MapDifference<String, Object> diff = Maps.difference(thisParams, thatParams); int different = diff.entriesDiffering().size(); int inCommon = diff.entriesInCommon().size(); tolerance = ((double) different / (double) (thisParams.size() + thatParams.size() - inCommon)); } return tolerance; }