class ValuePropertiesUtils {

  public static ValueProperties.Builder addAllOptional(final ValueProperties originalProperties, final ValueProperties propertiesToAdd) {
    ArgumentChecker.notNull(originalProperties, "original properties");
    if (propertiesToAdd == null || propertiesToAdd.isEmpty()) {
      return originalProperties.copy();
    }
    final ValueProperties.Builder newProperties = originalProperties.copy();
    for (final String propertyName : propertiesToAdd.getProperties()) {
      final Set<String> propertyValue = propertiesToAdd.getValues(propertyName);
      if (originalProperties.getValues(propertyName) != null && !propertyValue.equals(originalProperties.getValues(propertyName))) {
        throw new IllegalStateException("Property " + propertyName + " already present, but value " + 
            propertyValue + " not equal to " + originalProperties.getValues(propertyName));
      }
      if (propertyValue == null || propertyValue.isEmpty()) {
        newProperties.withAny(propertyName).withOptional(propertyName);
      } else {
        newProperties.with(propertyName, propertyValue).withOptional(propertyName);
      }
    }
    return newProperties;
  }

}