1 /* Vertex.java */
2
3 package org.quilt.graph;
4
5 /***
6 * A vertex in a directed graph.
7 *
8 * @author < a href="jddixon@users.sourceforge.net">Jim Dixon</a>
9 */
10 public class Vertex {
11
12 /*** Unique non-negative assigned to the Vertex; -1 means 'unassigned' */
13 protected int index = -1;
14 /*** The graph this vertex belongs to. */
15 protected Directed graph = null;
16 /*** Connects this vertex to one or more other vertices. */
17 protected Connector connector = null;
18 /*** Optional label. */
19 protected String label_ = null;
20
21 /*** Creates a vertex without an index and belonging to no graph. */
22 protected Vertex () {
23 }
24 /***
25 * Creates a vertex belonging to a graph, assigns an index unique
26 * within this graph.
27 * @param g The graph the vertex belongs to.
28 */
29 public Vertex (Directed g) {
30 checkForNull(g, "graph");
31 graph = g;
32 index = g.anotherVertex(this);
33 }
34 // ACCESSOR METHODS /////////////////////////////////////////////
35 public Connector getConnector () {
36 return connector;
37 }
38 public void setConnector (Connector c) {
39 checkForNull (c, "connector");
40 connector = c;
41 }
42 public Edge getEdge () {
43 if (connector == null) {
44 return null;
45 } else {
46 return connector.getEdge();
47 }
48 }
49 public Vertex getTarget() {
50 if (connector == null) {
51 return null;
52 } else {
53 return connector.getTarget();
54 }
55 }
56 /*** Get the graph this vertex is in. */
57 public Directed getGraph() {
58 return graph;
59 }
60
61 /*** @return Vertex index, a non-negative integer. */
62 public int getIndex() {
63 return index;
64 }
65 /*** @return String label or null */
66 public String getLabel() {
67 return label_;
68 }
69 /*** Assign a label to the Vertex. */
70 public void setLabel (String s) {
71 label_ = s;
72 }
73 // CONNECTOR CONVERTERS //////////////////////////////////////////
74 /***
75 * Convert the existing connector to a BinaryConnector.
76 *
77 * @return The 'other' edge created.
78 */
79 public Edge makeBinary() {
80 Edge otherEdge = new Edge(this, graph.getExit());
81 connector = new BinaryConnector( connector, otherEdge );
82 return otherEdge;
83 }
84 /***
85 * Convert the exiting connector to a ComplexConnector, using the
86 * existing Edge as seed.
87 */
88 public ComplexConnector makeComplex(int n) {
89 // rely on range check in constructor;
90 connector = new ComplexConnector ( connector, n);
91 return (ComplexConnector) connector;
92 }
93
94 /***
95 * Convert the exiting connector to a MultiConnector, using the
96 * existing Edge as seed.
97 */
98 public MultiConnector makeMulti(int n) {
99 // rely on range check in constructor;
100 connector = new MultiConnector ( connector, n);
101 return (MultiConnector) connector;
102 }
103
104 // UTILITY FUNCTIONS ////////////////////////////////////////////
105
106 /***
107 * Is the graph a parent, grandparent of this vertex?
108 *
109 * @param g Candidate progenitor.
110 * @return True if match is found.
111 */
112 public boolean above (final Directed g) {
113 // DEBUG
114 System.out.println("above: checking whether graph " + g.getIndex()
115 + " is above vertex " + toString ()
116 + " whose parent is graph " + getGraph().getParent().getIndex()
117 );
118 // END
119 if (g == null || g == graph) {
120 return false;
121 }
122
123 // search upward through parent graphs
124 for ( Directed pop = graph.getParent(); pop != null;
125 pop = pop.getParent() ) {
126 // DEBUG
127 System.out.println(" checking whether graph " + g.getIndex()
128 + " is the same as graph " + pop.getIndex());
129 // END
130 if ( pop == g ) {
131 return true;
132 }
133 }
134 return false;
135 }
136 /***
137 * Throw an exception if the argument is null.
138 *
139 * @param o Argument being checked
140 * @param what What it is - for error message.
141 */
142 public static void checkForNull( Object o, String what) {
143 if (o == null) {
144 throw new IllegalArgumentException ("null " + what);
145 }
146 }
147 /***
148 * @return A String in parent-index:my-index form.
149 */
150 public String toString () {
151 StringBuffer sb = new StringBuffer()
152 .append(graph.getIndex())
153 .append(":")
154 .append(index);
155 return sb.toString();
156 }
157 }
This page was automatically generated by Maven