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

org.opentripplanner.graph_builder.linking.DisposableEdgeCollection Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package org.opentripplanner.graph_builder.linking;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.opentripplanner.routing.graph.Edge;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.Vertex;

/**
 * This class is used to keep track of temporary edges added to the graph, so that they can be
 * removed from the graph when no longer needed.
 */
public class DisposableEdgeCollection {

  private final Graph graph;

  private final Scope scope;

  private final Set edges = new HashSet<>();

  public DisposableEdgeCollection(Graph graph) {
    this(graph, null);
  }

  public DisposableEdgeCollection(Graph graph, Scope scope) {
    this.graph = graph;
    this.scope = scope;
  }

  public void addEdge(Edge edge) {
    this.edges.add(edge);
  }

  /**
   * Removes all the edges in this collection from the graph.
   */
  public void disposeEdges() {
    if (scope == Scope.REALTIME) {
      for (Edge e : edges) {
        graph.getLinker().removeEdgeFromIndex(e, scope);
      }
    }
    Collection vertices = new ArrayList<>();
    for (Edge e : edges) {
      vertices.add(e.getFromVertex());
      vertices.add(e.getToVertex());
      graph.removeEdge(e);
    }
    for (Vertex v : vertices) {
      graph.removeIfUnconnected(v);
    }
    edges.clear();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy