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

ai.stapi.graphoperations.graphLoader.inmemory.InMemorySearchResolvingContext Maven / Gradle / Ivy

There is a newer version: 0.3.2
Show newest version
package ai.stapi.graphoperations.graphLoader.inmemory;

import ai.stapi.graph.inMemoryGraph.InMemoryGraphRepository;
import ai.stapi.graph.traversableGraphElements.TraversableGraphElement;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.positive.PositiveGraphDescription;
import ai.stapi.graphoperations.graphLoader.search.ResolvedQueryPart;
import ai.stapi.graphoperations.graphLoader.search.SearchResolvingContext;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable;

public class InMemorySearchResolvingContext implements SearchResolvingContext, ResolvedQueryPart {

  private Stream graphElements;

  @Nullable
  private Comparator comparator;

  private final InMemoryGraphRepository contextGraph;
  
  private final PositiveGraphDescription lastDescription;

  public InMemorySearchResolvingContext(
      Stream graphElements,
      InMemoryGraphRepository contextGraph,
      PositiveGraphDescription lastDescription
  ) {
    this.graphElements = graphElements;
    this.contextGraph = contextGraph;
    this.lastDescription = lastDescription;
  }

  public InMemorySearchResolvingContext setSearchOption(SearchOptionSetter setter) {
    this.graphElements = setter.set(this.graphElements);
    return this;
  }

  public > InMemorySearchResolvingContext setAscSort(
      Function keyExtractor
  ) {
    if (this.comparator == null) {
      this.comparator = Comparator.comparing(keyExtractor);
    } else {
      this.comparator = this.comparator.thenComparing(keyExtractor);
    }
    return this;
  }

  public > InMemorySearchResolvingContext setDescSort(
      Function keyExtractor
  ) {
    if (this.comparator == null) {
      this.comparator = Comparator.comparing(keyExtractor, Comparator.reverseOrder());
    } else {
      this.comparator = this.comparator.thenComparing(keyExtractor, Comparator.reverseOrder());
    }
    return this;
  }

  public void applySort() {
    if (this.comparator == null) {
      return;
    }
    this.graphElements = this.graphElements.sorted(this.comparator);
  }

  public List getGraphElements() {
    return graphElements.toList();
  }

  public InMemoryGraphRepository getContextGraph() {
    return contextGraph;
  }

  public PositiveGraphDescription getLastDescription() {
    return lastDescription;
  }

  public interface SearchOptionSetter {
    Stream set(Stream graphElements);
  }
}