package javakurs.musterloesung.uebung08;

/**
 * {@link CollectTimesVisitor} implements both the {@link DfsGraphVisitor} and
 * {@link BfsGraphVisitor} interfaces for a visitor collecting the discovery and
 * finishing times of the vertices.
 *
 * @param <EdgeInfo>
 *          the kind of information assigned to the edges of the traversed graph
 *
 * @author Bernd Kiefer, DFKI
 * @author Joerg Steffen, DFKI
 * @version $Id: CollectTimesVisitor.java,v 1.3 2011/01/12 16:30:18 steffen Exp $
 */
public class CollectTimesVisitor<EdgeInfo>
    implements DfsGraphVisitor<EdgeInfo>, BfsGraphVisitor<EdgeInfo> {

  /**
   * These contain the discovery and finishing times of the vertices.
   */
  private VertexPropertyMap<Integer> _discovery;
  private VertexPropertyMap<Integer> _finish;


  /**
   * This keeps track of the time counter.
   */
  private int _time;


  /**
   * This creates a new instance of {@link CollectTimesVisitor}.
   */
  public CollectTimesVisitor() {

    _time = 1;
  }


  /**
   * {@inheritDoc}
   */
  @Override
  public void startVertex(int v, DirectedGraphBase<EdgeInfo> g) {

    if (_discovery == null) {
      _discovery = new VertexPropertyMap<Integer>(g);
      _finish = new VertexPropertyMap<Integer>(g);
    }
  }


  /**
   * {@inheritDoc}
   */
  @Override
  public void discoverVertex(int v, DirectedGraphBase<EdgeInfo> g) {

    _discovery.put(v, _time);
    ++_time;
  }


  /**
   * {@inheritDoc}
   */
  @Override
  public void finishVertex(int v, DirectedGraphBase<EdgeInfo> g) {

    _finish.put(v, _time);
    ++_time;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void treeEdge(Edge<EdgeInfo> e, DirectedGraphBase<EdgeInfo> g) {

    // nothing to do here
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void treeEdgeBefore(Edge<EdgeInfo> e, DirectedGraphBase<EdgeInfo> g) {

    // nothing to do here
  }


  /**
   * {@inheritDoc}
   */
  @Override
  public void treeEdgeAfter(Edge<EdgeInfo> e, DirectedGraphBase<EdgeInfo> g) {

    // nothing to do here
  }


  /**
   * {@inheritDoc}
   */
  @Override
  public void nonTreeEdge(Edge<EdgeInfo> e, DirectedGraphBase<EdgeInfo> g) {

    // nothing to do here
  }


  /**
   * Returns the discovery time map.
   *
   * @return the discovery time map
   */
  VertexPropertyMap<Integer> getDiscoveryMap() {

    return _discovery;
  }


  /**
   * Returns the finish time map.
   *
   * @return the finish time map
   */
  VertexPropertyMap<Integer> getFinishMap() {

    return _finish;
  }
}
