List of usage examples for com.google.common.collect MapDifference entriesOnlyOnLeft
Map<K, V> entriesOnlyOnLeft();
From source file:org.openengsb.core.util.ConfigUtils.java
/** * apply the differences to the given map (3-way-merge) * * @throws MergeException if the changes cannot be applied because of merge-conflicts *///from w w w. j a v a 2 s .c om public static <K, V> Map<K, V> updateMap(final Map<K, V> original, MapDifference<K, V> diff) throws MergeException { Map<K, V> result = new HashMap<K, V>(original); if (diff.areEqual()) { return result; } for (Entry<K, V> entry : diff.entriesOnlyOnLeft().entrySet()) { V originalValue = original.get(entry.getKey()); if (ObjectUtils.equals(originalValue, entry.getValue())) { result.remove(entry.getKey()); } } for (Entry<K, V> entry : diff.entriesOnlyOnRight().entrySet()) { K key = entry.getKey(); if (original.containsKey(key)) { if (ObjectUtils.notEqual(original.get(key), entry.getValue())) { throw new MergeException( String.format("tried to introduce a new value, but it was already there: %s (%s,%s)", key, original.get(key), entry.getValue())); } } result.put(entry.getKey(), entry.getValue()); } for (Entry<K, ValueDifference<V>> entry : diff.entriesDiffering().entrySet()) { K key = entry.getKey(); V originalValue = original.get(entry.getKey()); if (ObjectUtils.equals(originalValue, entry.getValue().leftValue())) { // Map changed in diff only result.put(key, entry.getValue().rightValue()); } else if (ObjectUtils.equals(originalValue, entry.getValue().rightValue())) { // Diff would change value to value already in original Map result.put(key, originalValue); } else { // Merge conflict, got 3 different Values String errorMessage = String.format( "Changes could not be applied, because original value differes from left-side of the" + "MapDifference: %s (%s,%s)", entry.getKey(), original.get(entry.getKey()), entry.getValue()); throw new MergeException(errorMessage); } } return result; }
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);/* ww w.j av a 2 s . c om*/ 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:org.sonarsource.sonarlint.core.container.connected.update.check.ModuleStorageUpdateChecker.java
private static void checkForQualityProfilesUpdates(DefaultStorageUpdateCheckResult result, ModuleConfiguration serverModuleConfiguration, ModuleConfiguration storageModuleConfiguration) { MapDifference<String, String> qProfileDiff = Maps.difference( storageModuleConfiguration.getQprofilePerLanguageMap(), serverModuleConfiguration.getQprofilePerLanguageMap()); if (!qProfileDiff.areEqual()) { for (Map.Entry<String, String> entry : qProfileDiff.entriesOnlyOnLeft().entrySet()) { LOG.debug("Quality profile for language '{}' removed", entry.getKey()); }//from ww w. j av a 2 s. c om for (Map.Entry<String, String> entry : qProfileDiff.entriesOnlyOnRight().entrySet()) { LOG.debug("Quality profile for language '{}' added with value '{}'", entry.getKey(), entry.getValue()); } for (Map.Entry<String, ValueDifference<String>> entry : qProfileDiff.entriesDiffering().entrySet()) { LOG.debug("Quality profile for language '{}' changed from '{}' to '{}'", entry.getKey(), entry.getValue().leftValue(), entry.getValue().rightValue()); } // Don't report update when QP removed since this is harmless for the analysis if (!qProfileDiff.entriesOnlyOnRight().isEmpty() || !qProfileDiff.entriesDiffering().isEmpty()) { result.appendToChangelog("Quality profiles configuration changed"); } } }
From source file:cpw.mods.fml.client.modloader.ModLoaderClientHelper.java
public static void handleFinishLoadingFor(ModLoaderModContainer mc, Minecraft game) { FMLLog.log(mc.getModId(), Level.FINE, "Handling post startup activities for ModLoader mod %s", mc.getModId());//from w ww . jav a2s. c o m BaseMod mod = (BaseMod) mc.getMod(); Map<Class<? extends Entity>, Render> renderers = Maps.newHashMap(RenderManager.field_78727_a.field_78729_o); try { FMLLog.log(mc.getModId(), Level.FINEST, "Requesting renderers from basemod %s", mc.getModId()); mod.addRenderer(renderers); FMLLog.log(mc.getModId(), Level.FINEST, "Received %d renderers from basemod %s", renderers.size(), mc.getModId()); } catch (Exception e) { FMLLog.log(mc.getModId(), Level.SEVERE, e, "A severe problem was detected with the mod %s during the addRenderer call. Continuing, but expect odd results", mc.getModId()); } MapDifference<Class<? extends Entity>, Render> difference = Maps .difference(RenderManager.field_78727_a.field_78729_o, renderers, Equivalence.identity()); for (Entry<Class<? extends Entity>, Render> e : difference.entriesOnlyOnLeft().entrySet()) { FMLLog.log(mc.getModId(), Level.WARNING, "The mod %s attempted to remove an entity renderer %s from the entity map. This will be ignored.", mc.getModId(), e.getKey().getName()); } for (Entry<Class<? extends Entity>, Render> e : difference.entriesOnlyOnRight().entrySet()) { FMLLog.log(mc.getModId(), Level.FINEST, "Registering ModLoader entity renderer %s as instance of %s", e.getKey().getName(), e.getValue().getClass().getName()); RenderingRegistry.registerEntityRenderingHandler(e.getKey(), e.getValue()); } for (Entry<Class<? extends Entity>, ValueDifference<Render>> e : difference.entriesDiffering().entrySet()) { FMLLog.log(mc.getModId(), Level.FINEST, "Registering ModLoader entity rendering override for %s as instance of %s", e.getKey().getName(), e.getValue().rightValue().getClass().getName()); RenderingRegistry.registerEntityRenderingHandler(e.getKey(), e.getValue().rightValue()); } try { mod.registerAnimation(game); } catch (Exception e) { FMLLog.log(mc.getModId(), Level.SEVERE, e, "A severe problem was detected with the mod %s during the registerAnimation call. Continuing, but expect odd results", mc.getModId()); } }
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 ww w.j a va2 s. co 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: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);/*w ww.j a v a 2 s.co m*/ 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:org.jfrog.hudson.plugins.artifactory.util.ExtractorUtils.java
private static void addEnvVars(Map<String, String> env, AbstractBuild<?, ?> build, ArtifactoryClientConfiguration configuration, IncludesExcludes envVarsPatterns) { IncludeExcludePatterns patterns = new IncludeExcludePatterns(envVarsPatterns.getIncludePatterns(), envVarsPatterns.getExcludePatterns()); // Add only the Hudson specific environment variables MapDifference<String, String> envDifference = Maps.difference(env, System.getenv()); Map<String, String> filteredEnvDifference = envDifference.entriesOnlyOnLeft(); configuration.info.addBuildVariables(filteredEnvDifference, patterns); // Add Hudson build variables Map<String, String> buildVariables = build.getBuildVariables(); MapDifference<String, String> buildVarDifference = Maps.difference(buildVariables, System.getenv()); Map<String, String> filteredBuildVarDifferences = buildVarDifference.entriesOnlyOnLeft(); configuration.info.addBuildVariables(filteredBuildVarDifferences, patterns); // Write all the deploy (matrix params) properties. configuration.fillFromProperties(buildVariables, patterns); for (Map.Entry<String, String> entry : buildVariables.entrySet()) { if (entry.getKey().startsWith(ClientProperties.PROP_DEPLOY_PARAM_PROP_PREFIX)) { configuration.publisher.addMatrixParam(entry.getKey(), entry.getValue()); }/* w ww .j a va 2 s . c om*/ } }
From source file:org.sonarsource.sonarlint.core.container.connected.update.check.ModuleStorageUpdateChecker.java
private static void checkForSettingsUpdates(DefaultStorageUpdateCheckResult result, ModuleConfiguration serverModuleConfiguration, ModuleConfiguration storageModuleConfiguration) { MapDifference<String, String> propDiff = Maps.difference( GlobalSettingsUpdateChecker.filter(storageModuleConfiguration.getPropertiesMap()), GlobalSettingsUpdateChecker.filter(serverModuleConfiguration.getPropertiesMap())); if (!propDiff.areEqual()) { result.appendToChangelog("Project settings updated"); for (Map.Entry<String, String> entry : propDiff.entriesOnlyOnLeft().entrySet()) { LOG.debug("Property '{}' removed", entry.getKey()); }/* w w w. ja va2 s.c o m*/ for (Map.Entry<String, String> entry : propDiff.entriesOnlyOnRight().entrySet()) { LOG.debug("Property '{}' added with value '{}'", entry.getKey(), GlobalSettingsUpdateChecker.formatValue(entry.getKey(), entry.getValue())); } for (Map.Entry<String, ValueDifference<String>> entry : propDiff.entriesDiffering().entrySet()) { LOG.debug("Value of property '{}' changed from '{}' to '{}'", entry.getKey(), GlobalSettingsUpdateChecker.formatLeftDiff(entry.getKey(), entry.getValue().leftValue(), entry.getValue().rightValue()), GlobalSettingsUpdateChecker.formatRightDiff(entry.getKey(), entry.getValue().leftValue(), entry.getValue().rightValue())); } } }
From source file:org.apache.fluo.recipes.core.map.it.DocumentObserver.java
private static Map<String, Long> calculateChanges(Map<String, Long> newCounts, Map<String, Long> currCounts) { Map<String, Long> changes = new HashMap<>(); // guava Maps class MapDifference<String, Long> diffs = Maps.difference(currCounts, newCounts); // compute the diffs for words that changed changes.putAll(//from w w w . jav a2 s .c om Maps.transformValues(diffs.entriesDiffering(), vDiff -> vDiff.rightValue() - vDiff.leftValue())); // add all new words changes.putAll(diffs.entriesOnlyOnRight()); // subtract all words no longer present changes.putAll(Maps.transformValues(diffs.entriesOnlyOnLeft(), l -> l * -1)); return changes; }
From source file:org.eclipse.mdht.uml.common.util.ModelCompare.java
static public void compare(NamedElement element1, NamedElement element2, CompareResultVisitor compareResults) { compareResults.startElement(element1); HashMap<String, Element> umlPackage1HashMap = createElementHashMap(element1); HashMap<String, Element> umlPackage2HashMap = createElementHashMap(element2); MapDifference<String, Element> diff = Maps.difference(umlPackage1HashMap, umlPackage2HashMap, ValueEquivalence.ElementEquivalence); ArrayList<Element> leftElements = getSortedElements(diff.entriesOnlyOnLeft().values()); for (Element eee : leftElements) { compareResults.addedElement(element1, eee); if (eee instanceof Package || eee instanceof org.eclipse.uml2.uml.Class) { compare((NamedElement) eee, null, compareResults); }/*from w w w . j a v a 2 s . c om*/ } ArrayList<Element> rightElements = getSortedElements(diff.entriesOnlyOnRight().values()); for (Element eee : rightElements) { compareResults.deletedElement(element1, eee); } ArrayList<ValueDifference<Element>> differences = getSortedValueDifference( diff.entriesDiffering().values()); for (ValueDifference<Element> valueDifference : differences) { compareResults.changedElement(element1, valueDifference.leftValue(), valueDifference.rightValue()); if ((valueDifference.leftValue() instanceof Class) || (valueDifference.leftValue() instanceof Package)) { compare((NamedElement) valueDifference.leftValue(), (NamedElement) valueDifference.rightValue(), compareResults); } } compareResults.endElement(element1); }