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

com.hubspot.httpql.internal.CombinedConditionCreator Maven / Gradle / Ivy

The newest version!
package com.hubspot.httpql.internal;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.hubspot.httpql.FieldFactory;
import com.hubspot.httpql.QuerySpec;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.jooq.Condition;
import org.jooq.Operator;
import org.jooq.impl.DSL;

public class CombinedConditionCreator
  implements FilterEntryConditionCreator {

  private Operator operator;

  private List> conditionCreators;

  public CombinedConditionCreator(
    Operator operator,
    Collection> conditionCreators
  ) {
    this.operator = operator;
    this.conditionCreators = Lists.newArrayList(conditionCreators);
  }

  @Override
  public Condition getCondition(QuerySpec value, FieldFactory fieldFactory) {
    return DSL.condition(
      operator,
      conditionCreators
        .stream()
        .map(cc -> cc.getCondition(value, fieldFactory))
        .collect(Collectors.toList())
    );
  }

  @Override
  public Collection> getFlattenedBoundFilterEntries() {
    return conditionCreators
      .stream()
      .map(FilterEntryConditionCreator::getFlattenedBoundFilterEntries)
      .flatMap(Collection::stream)
      .collect(ImmutableList.toImmutableList());
  }

  public void addConditionCreator(FilterEntryConditionCreator conditionCreator) {
    if (!getFlattenedBoundFilterEntries().contains(conditionCreator)) {
      conditionCreators.add(conditionCreator);
    }
  }

  public boolean removeAllFiltersFor(String fieldName) {
    boolean removedFromThis = conditionCreators.removeIf(f ->
      f instanceof BoundFilterEntry &&
      ((BoundFilterEntry) f).getQueryName().equals(fieldName)
    );
    boolean removedFromChildren = conditionCreators
      .stream()
      .filter(cc -> cc instanceof CombinedConditionCreator)
      .map(cc -> ((CombinedConditionCreator) cc).removeAllFiltersFor(fieldName))
      .reduce(Boolean::logicalOr)
      .orElse(false);

    return removedFromThis || removedFromChildren;
  }

  public List> getAllFiltersForFieldName(String fieldName) {
    return getFlattenedBoundFilterEntries()
      .stream()
      .filter(bfe -> bfe.getQueryName().equals(fieldName))
      .collect(Collectors.toList());
  }

  public List> getConditionCreators() {
    return conditionCreators;
  }

  public void addConditionCreators(
    FilterEntryConditionCreator filterEntryConditionCreator
  ) {
    conditionCreators.add(filterEntryConditionCreator);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy