Java Code Examples for com.google.common.collect.MapDifference#ValueDifference

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: MapDifferenceSupplier.java    From kafka-connect-solr with Apache License 2.0 6 votes vote down vote up
@Override
public String get() {
  StringBuilder builder = new StringBuilder();
  if (!difference.entriesDiffering().isEmpty()) {
    builder.append("Differing:\n");
    for (Map.Entry<String, MapDifference.ValueDifference<SolrInputField>> diff : difference.entriesDiffering().entrySet()) {
      builder.append("  ");
      builder.append(diff.getKey());
      builder.append('\n');
      builder.append("  left  : ");
      builder.append(diff.getValue().leftValue());
      builder.append('\n');
      builder.append("  right : ");
      builder.append(diff.getValue().rightValue());
      builder.append('\n');
    }
  }

  return builder.toString();
}
 
Example 2
Source File: AbstractResourceUpdateHandler.java    From syndesis with Apache License 2.0 5 votes vote down vote up
protected List<LeveledMessage> computePropertiesDiffMessages(
    Supplier<LeveledMessage.Builder> supplier, Map<String, ConfigurationProperty> left, Map<String, ConfigurationProperty> right) {

    final List<LeveledMessage> messages = new ArrayList<>();
    final MapDifference<String, ConfigurationProperty> diff = Maps.difference(left, right);

    for (Map.Entry<String, MapDifference.ValueDifference<ConfigurationProperty>> entry: diff.entriesDiffering().entrySet()) {
        final MapDifference.ValueDifference<ConfigurationProperty> value = entry.getValue();
        final ConfigurationProperty leftValue = value.leftValue();
        final ConfigurationProperty rightValue = value.rightValue();

        // Special handling because of dynamic metadata
        if (!equals(leftValue, rightValue)) {
            messages.add(
                supplier.get()
                    .level(LeveledMessage.Level.INFO)
                    .code(LeveledMessage.Code.SYNDESIS001).build()
            );

            break;
        }
    }

    if (!diff.entriesOnlyOnLeft().isEmpty() || !diff.entriesOnlyOnRight().isEmpty()) {
        messages.add(
            supplier.get()
                .level(LeveledMessage.Level.WARN)
                .code(LeveledMessage.Code.SYNDESIS002).build()
        );
    }

    return messages;
}
 
Example 3
Source File: AbstractRegistry.java    From styx with Apache License 2.0 5 votes vote down vote up
protected static <T extends Identifiable> Changes<T> changes(Iterable<T> newResources, Iterable<T> currentResources) {
    Map<Id, T> newIdsToResource = mapById(newResources);
    Map<Id, T> currentIdsToResource = mapById(currentResources);

    MapDifference<Id, T> diff = difference(newIdsToResource, currentIdsToResource);

    Map<Id, MapDifference.ValueDifference<T>> diffs = diff.entriesDiffering();
    return new Changes.Builder<T>()
            .added(diff.entriesOnlyOnLeft().values())
            .removed(diff.entriesOnlyOnRight().values())
            .updated(filterKeys(newIdsToResource, diffs::containsKey).values())
            .build();
}
 
Example 4
Source File: GenericAssertions.java    From kafka-connect-transform-common with Apache License 2.0 5 votes vote down vote up
@Override
public String get() {
  try (Writer w = new StringWriter()) {
    try (BufferedWriter writer = new BufferedWriter(w)) {
      writer.append(String.format("Map for actual.%s() does not match expected.%s().", this.method, this.method));
      writer.newLine();
      Map<?, ? extends MapDifference.ValueDifference<?>> differences = mapDifference.entriesDiffering();
      if (!differences.isEmpty()) {
        writer.append("Keys with Differences");
        writer.newLine();
        for (Map.Entry<?, ? extends MapDifference.ValueDifference<?>> kvp : differences.entrySet()) {
          writer.append("  ");
          writer.append(kvp.getKey().toString());
          writer.newLine();

          writer.append("    expected:");
          writer.append(kvp.getValue().leftValue().toString());
          writer.newLine();

          writer.append("    actual:");
          writer.append(kvp.getValue().rightValue().toString());
          writer.newLine();
        }
      }

      Map<?, ?> entries = mapDifference.entriesOnlyOnLeft();
      writeEntries(writer, "Only in expected map", entries);

      Map<?, ?> onlyInActual = mapDifference.entriesOnlyOnRight();
      writeEntries(writer, "Only in actual map", onlyInActual);
    }
    return w.toString();
  } catch (IOException ex) {
    throw new IllegalStateException(ex);
  }
}
 
Example 5
Source File: GenericAssertions.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
@Override
public String get() {
  try (Writer w = new StringWriter()) {
    try (BufferedWriter writer = new BufferedWriter(w)) {
      writer.append(String.format("Map for actual.%s() does not match expected.%s().", this.method, this.method));
      writer.newLine();
      Map<?, ? extends MapDifference.ValueDifference<?>> differences = mapDifference.entriesDiffering();
      if (!differences.isEmpty()) {
        writer.append("Keys with Differences");
        writer.newLine();
        for (Map.Entry<?, ? extends MapDifference.ValueDifference<?>> kvp : differences.entrySet()) {
          writer.append("  ");
          writer.append(kvp.getKey().toString());
          writer.newLine();

          writer.append("    expected:");
          writer.append(kvp.getValue().leftValue().toString());
          writer.newLine();

          writer.append("    actual:");
          writer.append(kvp.getValue().rightValue().toString());
          writer.newLine();
        }
      }

      Map<?, ?> entries = mapDifference.entriesOnlyOnLeft();
      writeEntries(writer, "Only in expected map", entries);

      Map<?, ?> onlyInActual = mapDifference.entriesOnlyOnRight();
      writeEntries(writer, "Only in actual map", onlyInActual);
    }
    return w.toString();
  } catch (IOException ex) {
    throw new IllegalStateException(ex);
  }
}
 
Example 6
Source File: GenericAssertions.java    From connect-utils with Apache License 2.0 5 votes vote down vote up
@Override
public String get() {
  try (Writer w = new StringWriter()) {
    try (BufferedWriter writer = new BufferedWriter(w)) {
      writer.append(String.format("Map for actual.%s() does not match expected.%s().", this.method, this.method));
      writer.newLine();
      Map<?, ? extends MapDifference.ValueDifference<?>> differences = mapDifference.entriesDiffering();
      if (!differences.isEmpty()) {
        writer.append("Keys with Differences");
        writer.newLine();
        for (Map.Entry<?, ? extends MapDifference.ValueDifference<?>> kvp : differences.entrySet()) {
          writer.append("  ");
          writer.append(kvp.getKey().toString());
          writer.newLine();

          writer.append("    expected:");
          writer.append(kvp.getValue().leftValue().toString());
          writer.newLine();

          writer.append("    actual:");
          writer.append(kvp.getValue().rightValue().toString());
          writer.newLine();
        }
      }

      Map<?, ?> entries = mapDifference.entriesOnlyOnLeft();
      writeEntries(writer, "Only in expected map", entries);

      Map<?, ?> onlyInActual = mapDifference.entriesOnlyOnRight();
      writeEntries(writer, "Only in actual map", onlyInActual);
    }
    return w.toString();
  } catch (IOException ex) {
    throw new IllegalStateException(ex);
  }
}
 
Example 7
Source File: RemoteConnectionInstanceWorkflow.java    From ecs-cf-service-broker with Apache License 2.0 5 votes vote down vote up
private void validateSettings(ServiceInstance remoteInstance, ServiceDefinitionProxy serviceDef, PlanProxy plan,
                              Map<String, Object> parameters) {
    Map<String, Object> settings = new HashMap<>();
    settings.putAll(parameters);
    settings.putAll(plan.getServiceSettings());
    settings.putAll(serviceDef.getServiceSettings());

    Map<String, MapDifference.ValueDifference<Object>> settingsDiff =
            Maps.difference(settings, remoteInstance.getServiceSettings()).entriesDiffering();
    if (! settingsDiff.isEmpty())
        throw new ServiceBrokerException("service definition must match between local and remote instances");

}
 
Example 8
Source File: PropsUtils.java    From liteflow with Apache License 2.0 4 votes vote down vote up
/**
 * @return the difference between oldProps and newProps.
 */
public static String getPropertyDiff(Props oldProps, Props newProps) {

  final StringBuilder builder = new StringBuilder("");

  // oldProps can not be null during the below comparison process.
  if (oldProps == null) {
    oldProps = new Props();
  }

  if (newProps == null) {
    newProps = new Props();
  }

  final MapDifference<String, String> md =
      Maps.difference(toStringMap(oldProps, false), toStringMap(newProps, false));

  final Map<String, String> newlyCreatedProperty = md.entriesOnlyOnRight();
  if (newlyCreatedProperty != null && newlyCreatedProperty.size() > 0) {
    builder.append("Newly created Properties: ");
    newlyCreatedProperty.forEach((k, v) -> {
      builder.append("[ " + k + ", " + v + "], ");
    });
    builder.append("\n");
  }

  final Map<String, String> deletedProperty = md.entriesOnlyOnLeft();
  if (deletedProperty != null && deletedProperty.size() > 0) {
    builder.append("Deleted Properties: ");
    deletedProperty.forEach((k, v) -> {
      builder.append("[ " + k + ", " + v + "], ");
    });
    builder.append("\n");
  }

  final Map<String, MapDifference.ValueDifference<String>> diffProperties = md.entriesDiffering();
  if (diffProperties != null && diffProperties.size() > 0) {
    builder.append("Modified Properties: ");
    diffProperties.forEach((k, v) -> {
      builder.append("[ " + k + ", " + v.leftValue() + "-->" + v.rightValue() + "], ");
    });
  }
  return builder.toString();
}
 
Example 9
Source File: JobDiff.java    From attic-aurora with Apache License 2.0 4 votes vote down vote up
private static <V> Function<MapDifference.ValueDifference<V>, V> leftValue() {
  return ValueDifference::leftValue;
}