001: /* Edge.java */
002: package org.quilt.graph;
003:
004: import org.quilt.exception.QuiltException;
005:
006: /**
007: * An edge in a Quilt graph. This is seen as a source/target pair,
008: * XXX but in fact the source field may be unnecessary.
009: *
010: * @author < a href="jddixon@users.sourceforge.net">Jim Dixon</a>
011: */
012:
013: public class Edge {
014:
015: protected Vertex source_;
016: protected Vertex target_;
017:
018: /**
019: * An edge in a directed graph.
020: */
021: public Edge(final Vertex s, final Vertex t) {
022: if (s == null || t == null) {
023: throw new IllegalArgumentException("null source or target");
024: }
025: if (s.getGraph() != t.getGraph()
026: && !(s instanceof Exit || t instanceof Entry)) {
027: throw new IllegalArgumentException("source " + s
028: + " and target " + t
029: + " of edge constructor are not in the same graph");
030: }
031: source_ = s;
032: target_ = t;
033: }
034:
035: /** Copy constructor. */
036: public Edge(final Edge e) {
037: checkForNull(e, "edge");
038: source_ = e.getSource();
039: target_ = e.getTarget();
040: }
041:
042: // ACCESSOR METHODS /////////////////////////////////////////////
043: public Vertex getSource() {
044: return source_;
045: }
046:
047: public void setSource(Vertex v) {
048: checkForNull(v, "source");
049: if (target_ != null && v.getGraph() != source_.getGraph()) {
050: throw new IllegalArgumentException(
051: "source and target must be in same graph");
052: }
053: source_ = v;
054: }
055:
056: public Vertex getTarget() {
057: return target_;
058: }
059:
060: /**
061: * Change the target of this edge. XXX Wasn't public before;
062: * made it so to allow cl.SortedBlocks to retarget to existing
063: * vertex.
064: */
065: public void setTarget(Vertex v) {
066: checkForNull(v, "target");
067: if (!(source_ instanceof Exit || v instanceof Entry)
068: && (v.getGraph() != source_.getGraph())) {
069: /////////////////////////////////////////////////////////
070: // DEBUG -- this is a real problem but needs some thought.
071: // Fix it and put the exception back.
072: // //////////////////////////////////////////////////////
073: System.out.println("* WARNING * Edge {" + toString()
074: + "}\n being retargeted to vertex " + v);
075: // END
076: //throw new IllegalArgumentException ("target in different graph");
077: }
078: target_ = v;
079: }
080:
081: // OTHER METHODS ////////////////////////////////////////////////
082: public static void checkForNull(Object o, String what) {
083: if (o == null) {
084: throw new IllegalArgumentException("null " + what);
085: }
086: }
087:
088: /**
089: * @return the graph that this edge is in.
090: */
091: public Directed getGraph() {
092: return source_.getGraph();
093: }
094:
095: /**
096: * Insert a vertex with a UnaryConnector into an edge.
097: *
098: * @param v The Vertex being inserted.
099: */
100: public void insert(Vertex v) {
101: checkForNull(v, "vertex");
102: if (!(source_.getGraph() == v.getGraph())) {
103: throw new IllegalArgumentException(
104: "vertex is in another graph");
105: }
106: Connector vConn = v.getConnector();
107: if (vConn == null) {
108: throw new IllegalArgumentException(
109: "internal error: vertex has null connector");
110: }
111: Vertex oldTarget = target_;
112: target_ = v; // retarget this edge to v
113: vConn.setTarget(oldTarget);
114: }
115:
116: /** @return A String description of the edge, NOT newline-terminated. */
117: public String toString() {
118: String s = source_.toString() + " ---> " + target_;
119: return s;
120: }
121: }
|