package javakurs.musterloesung.uebung08;

/**
 * {@link Edge} represents an edge in the directed graph This is just a
 * container object, kept as small as possible.
 *
 * @param <EdgeInfo>
 *          the kind of info assigned to this edge
 *
 * @author Bernd Kiefer, DFKI
 * @author Joerg Steffen, DFKI
 * @version $Id: Edge.java,v 1.1 2011/01/04 17:52:54 steffen Exp $
 */
public class Edge<EdgeInfo> {

  /** This contains the info assigned to the edge. */
  private EdgeInfo _info;

  /** This contains the start vertex of the edge. */
  private int _from;

  /** This contains the end vertex of the edge. */
  private int _to;


  /**
   * This creates a new instance of {@link Edge} and adds it to the appropriate
   * vertex's edge lists. This constructor has default visibility because only
   * {@link DirectedGraphBase#newEdge(Object, int, int)} should be able to create a
   * new edge instance.
   *
   * @param info
   *          the info assigned to this edge
   */
  Edge(EdgeInfo info) {

    _info = info;
  }


  /**
   * Returns the info assigned to this edge.
   *
   * @return the edge info
   */
  public EdgeInfo getEdgeInfo() {

    return _info;
  }


  /**
   * Sets the info assigned to this edge.
   *
   * @param info
   *          the edge info
   */
  public void setEdgeInfo(EdgeInfo info) {

    _info = info;
  }


  /**
   * Returns the start vertex of the edge.
   *
   * @return the start vertex
   */
  public int getStartVertex() {

    return _from;
  }


  /**
   * Sets the start vertex of the edge. Only for package (friends).
   *
   * @param startVertex
   *          the start vertex
   */
  void setStartVertex(int startVertex) {

    _from = startVertex;
  }


  /**
   * Returns the end vertex of the edge.
   *
   * @return the end vertex
   */
  public int getEndVertex() {

    return _to;
  }


  /**
   * Sets the end vertex of the edge. Only for package (friends).
   *
   * @param endVertex
   *          the end vertex
   */
  void setEndVertex(int endVertex) {

    _to = endVertex;
  }


  /**
   * {@inheritDoc}
   */
  @Override
  public String toString() {

    StringBuilder strRep = new StringBuilder();
    strRep.append(_from).append("--").append(_info).append("-->").append(_to);
    return strRep.toString();
  }


  /**
   * This returns a string representation using the given vertex names
   *
   * @param vertexNames
   *          a property map with the vertex names
   * @return the string representation
   */
  public String toString(VertexPropertyMap<?> vertexNames) {

    if (vertexNames == null) {
      return toString();
    }
    StringBuilder strRep = new StringBuilder();
    strRep.append(vertexNames.get(_from)).append("--")
      .append(_info).append("-->").append(vertexNames.get(_to));
    return strRep.toString();
  }
}
