All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gov.nasa.pds.api.registry.util.GroupConstraintImpl Maven / Gradle / Ivy

There is a newer version: 4.5.6
Show newest version
package gov.nasa.pds.api.registry.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.errorprone.annotations.Immutable;

import gov.nasa.pds.api.registry.GroupConstraint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Immutable
public class GroupConstraintImpl implements GroupConstraint {

  private static final Logger log = LoggerFactory.getLogger(GroupConstraintImpl.class);

  final private Map> filterToAny;
  final private Map> must;
  final private Map> mustNot;

  private GroupConstraintImpl(Map> must, Map> filterToAny,
                              Map> mustNot) {
    int filterTermsKeysCount = filterToAny.keySet().size();
    if (filterTermsKeysCount > 1) {
      throw new RuntimeException(
              "Filtering on multiple keys is not supported by OpenSearch terms query, so is undefined");
    }

    this.must = Map.copyOf(must);
    this.filterToAny = Map.copyOf(filterToAny);
    this.mustNot = Map.copyOf(mustNot);
  }

  @Override
  public Map> must() {
    return must;
  }

  @Override
  public Map> filterToAny() {
    return filterToAny;
  }

  @Override
  public Map> mustNot() {
    return mustNot;
  }


  /**
   * Perform a union on a pair of a single constraint type (filter, must or mustNot)
   */
  private Map> performConstraintUnion(Map> constraint1, Map> constraint2) {
    Map> unionConstraint = new HashMap<>(constraint1);
    constraint2.forEach(
        (key, value) -> {
          if (unionConstraint.containsKey(key)) {
            unionConstraint.get(key).addAll(value);
            List deduplicatedValue = List.copyOf(Set.copyOf(unionConstraint.get(key)));
            unionConstraint.put(key, deduplicatedValue);
          } else {
            unionConstraint.put(key, List.copyOf(value));
          }
        });

    return unionConstraint;
  }

  private void logMutuallyExclusiveMustConditions(Map> mustConditions) {
    mustConditions.forEach(
        (key, value) -> {
          if (value.size() > 1) {
            log.warn(
                "Multiple values for 'must' condition are mutually-exclusive and will always fail: property='"
                    + key
                    + "', values=['"
                    + String.join("', '", value)
                    + "']");
          }
        });
  }

  @Override
  public GroupConstraint union(GroupConstraint otherGroupConstraint) {
    Map> unionFilter =
        performConstraintUnion(this.filterToAny, otherGroupConstraint.filterToAny());
    Map> unionMust =
        performConstraintUnion(this.must, otherGroupConstraint.must());
    Map> unionMustNot =
        performConstraintUnion(this.mustNot, otherGroupConstraint.mustNot());

    logMutuallyExclusiveMustConditions(unionMust);

    return new GroupConstraintImpl(unionMust, unionFilter, unionMustNot);
  }

  final private static Map> EMPTY = new HashMap>();

  public static GroupConstraint empty() {
    return new GroupConstraintImpl(EMPTY, EMPTY, EMPTY);
  }

  public static GroupConstraint buildAll(Map> map) {
    return new GroupConstraintImpl(map, EMPTY, EMPTY);
  }

  public static GroupConstraint buildAny(Map> map) {
    return new GroupConstraintImpl(EMPTY, map, EMPTY);
  }

  public static GroupConstraint buildNot(Map> map) {
    return new GroupConstraintImpl(EMPTY, EMPTY, map);
  }

  public static GroupConstraint buildAllAny(Map> allmap,
      Map> anymap) {
    return new GroupConstraintImpl(allmap, anymap, EMPTY);
  }

  public static GroupConstraint buildAllNot(Map> allmap,
      Map> notmap) {
    return new GroupConstraintImpl(allmap, EMPTY, notmap);
  }

  public static GroupConstraint buildAnyNot(Map> anymap,
      Map> notmap) {
    return new GroupConstraintImpl(EMPTY, anymap, notmap);
  }

  public static GroupConstraint build(Map> allmap,
      Map> anymap, Map> notmap) {
    return new GroupConstraintImpl(allmap, anymap, notmap);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy