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

io.weaviate.client.v1.graphql.query.argument.WhereArgument Maven / Gradle / Ivy

There is a newer version: 4.9.0
Show newest version
// Generated by delombok at Thu Dec 07 10:07:46 UTC 2023
package io.weaviate.client.v1.graphql.query.argument;

import io.weaviate.client.v1.filters.WhereFilter;
import io.weaviate.client.v1.graphql.query.util.Serializer;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

public class WhereArgument implements Argument {
  private final WhereFilter filter;

  @Override
  public String build() {
    String whereStr = filter != null ? buildNestedFilter(filter) : "";
    return String.format("where:{%s}", whereStr);
  }

  private String buildNestedFilter(WhereFilter f) {
    Set args = new LinkedHashSet<>();
    if (ArrayUtils.isNotEmpty(f.getOperands())) {
      if (f.getOperator() != null) {
        args.add(buildArg("operator", Serializer.escape(f.getOperator())));
      }
      args.add(buildArg("operands", Serializer.array(f.getOperands(), o -> String.format("{%s}", buildNestedFilter(o)))));
    } else {
      if (ArrayUtils.isNotEmpty(f.getPath())) {
        args.add(buildArg("path", Serializer.arrayWithQuotes(f.getPath())));
      }
      addArgSingleOrArray("valueBoolean", f.getValueBoolean(), f.getValueBooleanArray(), args::add);
      addArgSingleOrArray("valueInt", f.getValueInt(), f.getValueIntArray(), args::add);
      addArgSingleOrArray("valueNumber", f.getValueNumber(), f.getValueNumberArray(), args::add);
      addArgSingleOrArray("valueString", f.getValueString(), f.getValueStringArray(), args::add, Serializer::quote);
      addArgSingleOrArray("valueText", f.getValueText(), f.getValueTextArray(), args::add, Serializer::quote);
      addArgSingleOrArray("valueDate", f.getValueDate(), f.getValueDateArray(), args::add, date -> {
        String dateString = DateFormatUtils.format(date, "yyyy-MM-dd\'T\'HH:mm:ssZZZZZ");
        return Serializer.quote(dateString);
      });
      if (f.getValueGeoRange() != null) {
        args.add(buildArg("valueGeoRange", buildGeoRange(f.getValueGeoRange())));
      }
      if (f.getOperator() != null) {
        args.add(buildArg("operator", Serializer.escape(f.getOperator())));
      }
    }
    return String.join(" ", args);
  }

  private String buildGeoRange(WhereFilter.GeoRange geoRange) {
    WhereFilter.GeoCoordinates geoCoordinates = geoRange.getGeoCoordinates();
    WhereFilter.GeoDistance distance = geoRange.getDistance();
    if (ObjectUtils.allNotNull(geoCoordinates, geoCoordinates.getLatitude(), geoCoordinates.getLongitude(), distance, distance.getMax())) {
      return String.format("{geoCoordinates:{latitude:%s,longitude:%s},distance:{max:%s}}", geoCoordinates.getLatitude(), geoCoordinates.getLongitude(), distance.getMax());
    }
    return "";
  }

  private String buildArg(String name, Object value) {
    return String.format("%s:%s", name, value);
  }

  private  void addArgSingleOrArray(String valueName, T value, T[] values, Function add, Function valueMapper) {
    String valAsString = null;
    if (Objects.nonNull(value)) {
      valAsString = valueMapper.apply(value);
    } else if (ArrayUtils.isNotEmpty(values)) {
      valAsString = Serializer.array(values, valueMapper);
    }
    if (Objects.nonNull(valAsString)) {
      add.apply(buildArg(valueName, valAsString));
    }
  }

  private  void addArgSingleOrArray(String valueName, T value, T[] values, Function add) {
    addArgSingleOrArray(valueName, value, values, add, Objects::toString);
  }

  @java.lang.SuppressWarnings("all")
  WhereArgument(final WhereFilter filter) {
    this.filter = filter;
  }


  @java.lang.SuppressWarnings("all")
  public static class WhereArgumentBuilder {
    @java.lang.SuppressWarnings("all")
    private WhereFilter filter;

    @java.lang.SuppressWarnings("all")
    WhereArgumentBuilder() {
    }

    /**
     * @return {@code this}.
     */
    @java.lang.SuppressWarnings("all")
    public WhereArgument.WhereArgumentBuilder filter(final WhereFilter filter) {
      this.filter = filter;
      return this;
    }

    @java.lang.SuppressWarnings("all")
    public WhereArgument build() {
      return new WhereArgument(this.filter);
    }

    @java.lang.Override
    @java.lang.SuppressWarnings("all")
    public java.lang.String toString() {
      return "WhereArgument.WhereArgumentBuilder(filter=" + this.filter + ")";
    }
  }

  @java.lang.SuppressWarnings("all")
  public static WhereArgument.WhereArgumentBuilder builder() {
    return new WhereArgument.WhereArgumentBuilder();
  }

  @java.lang.SuppressWarnings("all")
  public WhereFilter getFilter() {
    return this.filter;
  }

  @java.lang.Override
  @java.lang.SuppressWarnings("all")
  public java.lang.String toString() {
    return "WhereArgument(filter=" + this.getFilter() + ")";
  }

  @java.lang.Override
  @java.lang.SuppressWarnings("all")
  public boolean equals(final java.lang.Object o) {
    if (o == this) return true;
    if (!(o instanceof WhereArgument)) return false;
    final WhereArgument other = (WhereArgument) o;
    if (!other.canEqual((java.lang.Object) this)) return false;
    final java.lang.Object this$filter = this.getFilter();
    final java.lang.Object other$filter = other.getFilter();
    if (this$filter == null ? other$filter != null : !this$filter.equals(other$filter)) return false;
    return true;
  }

  @java.lang.SuppressWarnings("all")
  protected boolean canEqual(final java.lang.Object other) {
    return other instanceof WhereArgument;
  }

  @java.lang.Override
  @java.lang.SuppressWarnings("all")
  public int hashCode() {
    final int PRIME = 59;
    int result = 1;
    final java.lang.Object $filter = this.getFilter();
    result = result * PRIME + ($filter == null ? 43 : $filter.hashCode());
    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy