1 2 package org.quilt.graph; 3 4 import org.quilt.exception.QuiltException; 5 6 12 13 public class Edge { 14 15 protected Vertex source_; 16 protected Vertex target_; 17 18 21 public Edge(final Vertex s, final Vertex t) { 22 if ( s == null || t == null ) { 23 throw new IllegalArgumentException ("null source or target"); 24 } 25 if (s.getGraph() != t.getGraph() 26 && ! (s instanceof Exit || t instanceof Entry ) ) { 27 throw new IllegalArgumentException ("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 36 public Edge ( final Edge e ) { 37 checkForNull (e, "edge"); 38 source_ = e.getSource(); 39 target_ = e.getTarget(); 40 } 41 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 ( 49 "source and target must be in same graph"); 50 } 51 source_ = v; 52 } 53 public Vertex getTarget() { 54 return target_; 55 } 56 61 public void setTarget (Vertex v) { 62 checkForNull(v, "target"); 63 if ( !(source_ instanceof Exit || v instanceof Entry) 64 && (v.getGraph() != source_.getGraph())) { 65 System.out.println("* WARNING * Edge {" + toString() 70 + "}\n being retargeted to vertex " + v); 71 } 74 target_ = v; 75 } 76 public static void checkForNull(Object o, String what) { 78 if (o == null) { 79 throw new IllegalArgumentException ("null " + what); 80 } 81 } 82 85 public Directed getGraph() { 86 return source_.getGraph(); 87 } 88 93 public void insert (Vertex v) { 94 checkForNull (v, "vertex"); 95 if ( ! (source_.getGraph() == v.getGraph()) ) { 96 throw new IllegalArgumentException ( 97 "vertex is in another graph"); 98 } 99 Connector vConn = v.getConnector(); 100 if (vConn == null) { 101 throw new IllegalArgumentException ( 102 "internal error: vertex has null connector"); 103 } 104 Vertex oldTarget = target_; 105 target_ = v; vConn.setTarget(oldTarget); 107 } 108 109 110 public String toString () { 111 String s = source_.toString() + " ---> " + target_; 112 return s; 113 } 114 } 115 | Popular Tags |