KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quilt > graph > Edge


1 /* Edge.java */
2 package org.quilt.graph;
3
4 import org.quilt.exception.QuiltException;
5
6 /**
7  * An edge in a Quilt graph. This is seen as a source/target pair,
8  * XXX but in fact the source field may be unnecessary.
9  *
10  * @author < a HREF="jddixon@users.sourceforge.net">Jim Dixon</a>
11  */

12
13 public class Edge {
14
15     protected Vertex source_;
16     protected Vertex target_;
17  
18     /**
19      * An edge in a directed graph.
20      */

21     public Edge(final Vertex s, final Vertex t) {
22         if ( s == null || t == null ) {
23             throw new IllegalArgumentException JavaDoc("null source or target");
24         }
25         if (s.getGraph() != t.getGraph()
26                 && ! (s instanceof Exit || t instanceof Entry ) ) {
27             throw new IllegalArgumentException JavaDoc("source " + s
28                     + " and target " + t +
29                     " of edge constructor are not in the same graph");
30         }
31         source_ = s;
32         target_ = t;
33     }
34
35     /** Copy constructor. */
36     public Edge ( final Edge e ) {
37         checkForNull (e, "edge");
38         source_ = e.getSource();
39         target_ = e.getTarget();
40     }
41     // ACCESSOR METHODS /////////////////////////////////////////////
42
public Vertex getSource() {
43         return source_;
44     }
45     public void setSource (Vertex v) {
46         checkForNull(v, "source");
47         if (target_ != null && v.getGraph() != source_.getGraph()) {
48             throw new IllegalArgumentException JavaDoc(
49                     "source and target must be in same graph");
50         }
51         source_ = v;
52     }
53     public Vertex getTarget() {
54         return target_;
55     }
56     /**
57      * Change the target of this edge. XXX Wasn't public before;
58      * made it so to allow cl.SortedBlocks to retarget to existing
59      * vertex.
60      */

61     public void setTarget (Vertex v) {
62         checkForNull(v, "target");
63         if ( !(source_ instanceof Exit || v instanceof Entry)
64                             && (v.getGraph() != source_.getGraph())) {
65             /////////////////////////////////////////////////////////
66
// DEBUG -- this is a real problem but needs some thought.
67
// Fix it and put the exception back.
68
// //////////////////////////////////////////////////////
69
System.out.println("* WARNING * Edge {" + toString()
70                 + "}\n being retargeted to vertex " + v);
71             // END
72
//throw new IllegalArgumentException ("target in different graph");
73
}
74         target_ = v;
75     }
76     // OTHER METHODS ////////////////////////////////////////////////
77
public static void checkForNull(Object JavaDoc o, String JavaDoc what) {
78         if (o == null) {
79             throw new IllegalArgumentException JavaDoc ("null " + what);
80         }
81     }
82     /**
83      * @return the graph that this edge is in.
84      */

85     public Directed getGraph() {
86         return source_.getGraph();
87     }
88     /**
89      * Insert a vertex with a UnaryConnector into an edge.
90      *
91      * @param v The Vertex being inserted.
92      */

93     public void insert (Vertex v) {
94         checkForNull (v, "vertex");
95         if ( ! (source_.getGraph() == v.getGraph()) ) {
96             throw new IllegalArgumentException JavaDoc (
97                     "vertex is in another graph");
98         }
99         Connector vConn = v.getConnector();
100         if (vConn == null) {
101             throw new IllegalArgumentException JavaDoc(
102                     "internal error: vertex has null connector");
103         }
104         Vertex oldTarget = target_;
105         target_ = v; // retarget this edge to v
106
vConn.setTarget(oldTarget);
107     }
108
109     /** @return A String description of the edge, NOT newline-terminated. */
110     public String JavaDoc toString () {
111         String JavaDoc s = source_.toString() + " ---> " + target_;
112         return s;
113     }
114 }
115
Popular Tags