com.google.common.collect.MapDifference.ValueDifference Java Examples

The following examples show how to use com.google.common.collect.MapDifference.ValueDifference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HashMapComparisonUnitTest.java    From tutorials with MIT License 7 votes vote down vote up
@Test
public void givenDifferentMaps_whenGetDiffUsingGuava_thenSuccess() {
    Map<String, String> asia1 = new HashMap<String, String>();
    asia1.put("Japan", "Tokyo");
    asia1.put("South Korea", "Seoul");
    asia1.put("India", "New Delhi");

    Map<String, String> asia2 = new HashMap<String, String>();
    asia2.put("Japan", "Tokyo");
    asia2.put("China", "Beijing");
    asia2.put("India", "Delhi");

    MapDifference<String, String> diff = Maps.difference(asia1, asia2);
    Map<String, ValueDifference<String>> entriesDiffering = diff.entriesDiffering();
    
    assertFalse(diff.areEqual());
    assertEquals(1, entriesDiffering.size());
    assertThat(entriesDiffering, hasKey("India"));
    assertEquals("New Delhi", entriesDiffering.get("India").leftValue());
    assertEquals("Delhi", entriesDiffering.get("India").rightValue());
}
 
Example #2
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static <K, V> void doDifference(
    Map<? extends K, ? extends V> left,
    Map<? extends K, ? extends V> right,
    Equivalence<? super V> valueEquivalence,
    Map<K, V> onlyOnLeft,
    Map<K, V> onlyOnRight,
    Map<K, V> onBoth,
    Map<K, MapDifference.ValueDifference<V>> differences) {
  for (Entry<? extends K, ? extends V> entry : left.entrySet()) {
    K leftKey = entry.getKey();
    V leftValue = entry.getValue();
    if (right.containsKey(leftKey)) {
      V rightValue = onlyOnRight.remove(leftKey);
      if (valueEquivalence.equivalent(leftValue, rightValue)) {
        onBoth.put(leftKey, leftValue);
      } else {
        differences.put(leftKey, ValueDifferenceImpl.create(leftValue, rightValue));
      }
    } else {
      onlyOnLeft.put(leftKey, leftValue);
    }
  }
}
 
Example #3
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static <K, V> void doDifference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence, Map<K, V> onlyOnLeft, Map<K, V> onlyOnRight, Map<K, V> onBoth, Map<K, MapDifference.ValueDifference<V>> differences) {
  for (Entry<? extends K, ? extends V> entry : left.entrySet()) {
    K leftKey = entry.getKey();
    V leftValue = entry.getValue();
    if (right.containsKey(leftKey)) {
      V rightValue = onlyOnRight.remove(leftKey);
      if (valueEquivalence.equivalent(leftValue, rightValue)) {
        onBoth.put(leftKey, leftValue);
      } else {
        differences.put(leftKey, ValueDifferenceImpl.create(leftValue, rightValue));
      }
    } else {
      onlyOnLeft.put(leftKey, leftValue);
    }
  }
}
 
Example #4
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static <K, V> void doDifference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence, Map<K, V> onlyOnLeft, Map<K, V> onlyOnRight, Map<K, V> onBoth, Map<K, MapDifference.ValueDifference<V>> differences) {
  for (Entry<? extends K, ? extends V> entry : left.entrySet()) {
    K leftKey = entry.getKey();
    V leftValue = entry.getValue();
    if (right.containsKey(leftKey)) {
      V rightValue = onlyOnRight.remove(leftKey);
      if (valueEquivalence.equivalent(leftValue, rightValue)) {
        onBoth.put(leftKey, leftValue);
      } else {
        differences.put(leftKey, ValueDifferenceImpl.create(leftValue, rightValue));
      }
    } else {
      onlyOnLeft.put(leftKey, leftValue);
    }
  }
}
 
Example #5
Source File: XtextBuildPreferenceEvaluator.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean hasBuildAffectingChanges(Entry<String, ValueDifference<String>> preference) {
	String key = preference.getKey();
	if (key.startsWith(PREFIX)) {
		String keyName = key.substring(key.lastIndexOf(PreferenceConstants.SEPARATOR) + 1);
		switch (keyName) {
			case OUTPUT_CREATE_DIRECTORY:
			case OUTPUT_OVERRIDE:
			case OUTPUT_DERIVED:
			case OUTPUT_CLEANUP_DERIVED:
			case OUTPUT_CLEAN_DIRECTORY:
			case OUTPUT_KEEP_LOCAL_HISTORY:
				return false;
		}
	}
	return true;
}
 
Example #6
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static <K, V> void doDifference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence, Map<K, V> onlyOnLeft, Map<K, V> onlyOnRight, Map<K, V> onBoth, Map<K, MapDifference.ValueDifference<V>> differences) {
  for (Entry<? extends K, ? extends V> entry : left.entrySet()) {
    K leftKey = entry.getKey();
    V leftValue = entry.getValue();
    if (right.containsKey(leftKey)) {
      V rightValue = onlyOnRight.remove(leftKey);
      if (valueEquivalence.equivalent(leftValue, rightValue)) {
        onBoth.put(leftKey, leftValue);
      } else {
        differences.put(leftKey, ValueDifferenceImpl.create(leftValue, rightValue));
      }
    } else {
      onlyOnLeft.put(leftKey, leftValue);
    }
  }
}
 
Example #7
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static <K, V> void doDifference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence, Map<K, V> onlyOnLeft, Map<K, V> onlyOnRight, Map<K, V> onBoth, Map<K, MapDifference.ValueDifference<V>> differences) {
  for (Entry<? extends K, ? extends V> entry : left.entrySet()) {
    K leftKey = entry.getKey();
    V leftValue = entry.getValue();
    if (right.containsKey(leftKey)) {
      V rightValue = onlyOnRight.remove(leftKey);
      if (valueEquivalence.equivalent(leftValue, rightValue)) {
        onBoth.put(leftKey, leftValue);
      } else {
        differences.put(leftKey, ValueDifferenceImpl.create(leftValue, rightValue));
      }
    } else {
      onlyOnLeft.put(leftKey, leftValue);
    }
  }
}
 
Example #8
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
    public boolean equals(@Nullable Object object) {
      if (object instanceof MapDifference.ValueDifference) {
        MapDifference.ValueDifference<?> that = (MapDifference.ValueDifference<?>) object;
        return Objects.equal(this.left, that.leftValue())
&& Objects.equal(this.right, that.rightValue());
      }
      return false;
    }
 
Example #9
Source File: AbstractN4JSPreferenceStoreAccessor.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param outputName
 *            the output name that identifies the output configuration but in our case also the registered compiler.
 * @param compilerDescriptor
 *            the compiler descriptor holding the values either stored last time this details page was used or the
 *            default values for the compiler
 */
public void populateCompilerConfiguration(String outputName, T compilerDescriptor) {
	// defaults
	preferenceInitializer.initialize(scopedAccessor);

	boolean initialLoad = false;

	ComponentDescriptor currentlyStoredCompilerDescriptor = compilerDescriptor
			.getCurrentlyStoredComponentDescriptor();
	if (currentlyStoredCompilerDescriptor == null) {
		currentlyStoredCompilerDescriptor = compilerDescriptor; // default configuration
		initialLoad = true;
	}

	Map<String, String> originalSettings = currentlyStoredCompilerDescriptor.fillMap(outputName);

	IPreferenceStore preferenceStore = getPreferenceStore();
	// populate
	// use a copy here has we don't want to change the default values provided by the registered compilers
	@SuppressWarnings("unchecked")
	T newCompilerDescriptor = (T) compilerDescriptor.copy();
	for (IComponentProperties<T> prop : getComponentPropertiesValues()) {
		if (prop.getType() == Boolean.class) {
			prop.setValueInCompilerDescriptor(newCompilerDescriptor, outputName,
					preferenceStore.getBoolean(prop.getKey(outputName)));
		} else {
			prop.setValueInCompilerDescriptor(newCompilerDescriptor, outputName,
					preferenceStore.getString(prop.getKey(outputName)));
		}
	}

	Map<String, String> currentSettings = newCompilerDescriptor.fillMap(outputName);

	Map<String, ValueDifference<String>> changes = getPreferenceChanges(originalSettings, currentSettings);

	if (!initialLoad) {
		compilerDescriptor.setChanges(changes);
	}
	compilerDescriptor.setCurrentlyStoredComponentDescriptor(newCompilerDescriptor);
}
 
Example #10
Source File: OptionsConfigurationBlock.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public Map<String, ValueDifference<String>> getPreferenceChanges() {
	Map<String, String> currentSettings = Maps.newHashMapWithExpectedSize(keys.length);
	for (String key : keys) {
		currentSettings.put(key, preferenceStore.getString(key));
	}
	MapDifference<String, String> mapDifference = Maps.difference(currentSettings, originalSettings);
	Map<String, ValueDifference<String>> entriesDiffering = mapDifference.entriesDiffering();
	return entriesDiffering;
}
 
Example #11
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
MapDifferenceImpl(
    Map<K, V> onlyOnLeft,
    Map<K, V> onlyOnRight,
    Map<K, V> onBoth,
    Map<K, ValueDifference<V>> differences) {
  this.onlyOnLeft = unmodifiableMap(onlyOnLeft);
  this.onlyOnRight = unmodifiableMap(onlyOnRight);
  this.onBoth = unmodifiableMap(onBoth);
  this.differences = unmodifiableMap(differences);
}
 
Example #12
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean equals(@Nullable Object object) {
  if (object instanceof MapDifference.ValueDifference) {
    MapDifference.ValueDifference<?> that = (MapDifference.ValueDifference<?>) object;
    return Objects.equal(this.left, that.leftValue())
    && Objects.equal(this.right, that.rightValue());
  }
  return false;
}
 
Example #13
Source File: N4JSBuilderPreferencePage.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This method has been copied from org.eclipse.xtext.builder.preferences.BuilderPreferencePage.
 */
private void scheduleCleanerJobIfNecessary(IPreferencePageContainer preferencePageContainer) {
	Map<String, ValueDifference<String>> changes = getPreferenceChanges();
	for (String key : changes.keySet()) {
		if (key.matches("^" + CompilerProperties.OUTPUT_PREFERENCE_TAG + "\\.\\w+\\."
				+ CompilerProperties.OUTPUT_PREFERENCE_TAG + "$")) {
			ValueDifference<String> difference = changes.get(key);
			scheduleCleanerJob(preferencePageContainer, difference.rightValue());
		}
	}
}
 
Example #14
Source File: BuilderPreferencePage.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void scheduleCleanerJobIfNecessary(IPreferencePageContainer preferencePageContainer) {
	Map<String, ValueDifference<String>> changes = builderConfigurationBlock.getPreferenceChanges();
	for (String key : changes.keySet()) {
		if (key.matches("^" + EclipseOutputConfigurationProvider.OUTPUT_PREFERENCE_TAG + "\\.\\w+\\."
				+ EclipseOutputConfigurationProvider.OUTPUT_DIRECTORY + "$")) {
			ValueDifference<String> difference = changes.get(key);
			scheduleCleanerJob(preferencePageContainer, difference.rightValue());
		}
	}
}
 
Example #15
Source File: ClaimsListShard.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private static final void loadAndCompareCloudSqlList(ClaimsListShard datastoreList) {
  Optional<ClaimsList> maybeCloudSqlList = ClaimsListDao.getLatestRevision();
  if (maybeCloudSqlList.isPresent()) {
    ClaimsList cloudSqlList = maybeCloudSqlList.get();
    MapDifference<String, String> diff =
        Maps.difference(datastoreList.labelsToKeys, cloudSqlList.getLabelsToKeys());
    if (!diff.areEqual()) {
      if (diff.entriesDiffering().size() > 10) {
        logger.atWarning().log(
            String.format(
                "Unequal claims lists detected, Cloud SQL list with revision id %d has %d"
                    + " different records than the current Datastore list.",
                cloudSqlList.getRevisionId(), diff.entriesDiffering().size()));
      } else {
        StringBuilder diffMessage = new StringBuilder("Unequal claims lists detected:\n");
        diff.entriesDiffering().entrySet().stream()
            .forEach(
                entry -> {
                  String label = entry.getKey();
                  ValueDifference<String> valueDiff = entry.getValue();
                  diffMessage.append(
                      String.format(
                          "Domain label %s has key %s in Datastore and key %s in Cloud"
                              + " SQL.\n",
                          label, valueDiff.leftValue(), valueDiff.rightValue()));
                });
        logger.atWarning().log(diffMessage.toString());
      }
    }
  } else {
    logger.atWarning().log("Claims list in Cloud SQL is empty.");
  }
}
 
Example #16
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
MapDifferenceImpl(
                 Map<K, V> onlyOnLeft,
                 Map<K, V> onlyOnRight,
                 Map<K, V> onBoth,
                 Map<K, ValueDifference<V>> differences) {
  this.onlyOnLeft = unmodifiableMap(onlyOnLeft);
  this.onlyOnRight = unmodifiableMap(onlyOnRight);
  this.onBoth = unmodifiableMap(onBoth);
  this.differences = unmodifiableMap(differences);
}
 
Example #17
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
MapDifferenceImpl(
                 Map<K, V> onlyOnLeft,
                 Map<K, V> onlyOnRight,
                 Map<K, V> onBoth,
                 Map<K, ValueDifference<V>> differences) {
  this.onlyOnLeft = unmodifiableMap(onlyOnLeft);
  this.onlyOnRight = unmodifiableMap(onlyOnRight);
  this.onBoth = unmodifiableMap(onBoth);
  this.differences = unmodifiableMap(differences);
}
 
Example #18
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
    public boolean equals(@Nullable Object object) {
      if (object instanceof MapDifference.ValueDifference) {
        MapDifference.ValueDifference<?> that = (MapDifference.ValueDifference<?>) object;
        return Objects.equal(this.left, that.leftValue())
&& Objects.equal(this.right, that.rightValue());
      }
      return false;
    }
 
Example #19
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
MapDifferenceImpl(
                 Map<K, V> onlyOnLeft,
                 Map<K, V> onlyOnRight,
                 Map<K, V> onBoth,
                 Map<K, ValueDifference<V>> differences) {
  this.onlyOnLeft = unmodifiableMap(onlyOnLeft);
  this.onlyOnRight = unmodifiableMap(onlyOnRight);
  this.onBoth = unmodifiableMap(onBoth);
  this.differences = unmodifiableMap(differences);
}
 
Example #20
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean equals(@Nullable Object object) {
  if (object instanceof MapDifference.ValueDifference) {
    MapDifference.ValueDifference<?> that = (MapDifference.ValueDifference<?>) object;
    return Objects.equal(this.left, that.leftValue())
        && Objects.equal(this.right, that.rightValue());
  }
  return false;
}
 
Example #21
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean equals(@Nullable Object object) {
  if (object instanceof MapDifference.ValueDifference) {
    MapDifference.ValueDifference<?> that = (MapDifference.ValueDifference<?>) object;
    return Objects.equal(this.left, that.leftValue())
    && Objects.equal(this.right, that.rightValue());
  }
  return false;
}
 
Example #22
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
SortedMapDifferenceImpl(
    SortedMap<K, V> onlyOnLeft,
    SortedMap<K, V> onlyOnRight,
    SortedMap<K, V> onBoth,
    SortedMap<K, ValueDifference<V>> differences) {
  super(onlyOnLeft, onlyOnRight, onBoth, differences);
}
 
Example #23
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
MapDifferenceImpl(
                 Map<K, V> onlyOnLeft,
                 Map<K, V> onlyOnRight,
                 Map<K, V> onBoth,
                 Map<K, ValueDifference<V>> differences) {
  this.onlyOnLeft = unmodifiableMap(onlyOnLeft);
  this.onlyOnRight = unmodifiableMap(onlyOnRight);
  this.onBoth = unmodifiableMap(onBoth);
  this.differences = unmodifiableMap(differences);
}
 
Example #24
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Map<K, ValueDifference<V>> entriesDiffering() {
  return differences;
}
 
Example #25
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Map<K, ValueDifference<V>> entriesDiffering() {
  return differences;
}
 
Example #26
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
static <V> ValueDifference<V> create(@Nullable V left, @Nullable V right) {
  return new ValueDifferenceImpl<V>(left, right);
}
 
Example #27
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
static <V> ValueDifference<V> create(@Nullable V left, @Nullable V right) {
  return new ValueDifferenceImpl<V>(left, right);
}
 
Example #28
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public SortedMap<K, ValueDifference<V>> entriesDiffering() {
  return (SortedMap<K, ValueDifference<V>>) super.entriesDiffering();
}
 
Example #29
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Map<K, ValueDifference<V>> entriesDiffering() {
  return differences;
}
 
Example #30
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public SortedMap<K, ValueDifference<V>> entriesDiffering() {
  return (SortedMap<K, ValueDifference<V>>) super.entriesDiffering();
}