1 22 package org.jboss.util.graph; 23 24 import java.util.ArrayList ; 25 26 30 public class Vertex<T> 31 { 32 private ArrayList <Edge<T>> incomingEdges; 33 private ArrayList <Edge<T>> outgoingEdges; 34 private String name; 35 private boolean mark; 36 private int markState; 37 private T data; 38 39 42 public Vertex() 43 { 44 this(null, null); 45 } 46 50 public Vertex(String n) 51 { 52 this(null, null); 53 } 54 public Vertex(String n, T data) 55 { 56 incomingEdges = new ArrayList <Edge<T>>(); 57 outgoingEdges = new ArrayList <Edge<T>>(); 58 name = new String (n); 59 mark = false; 60 this.data = data; 61 } 62 63 public String getName() 64 { 65 return name; 66 } 67 68 71 public T getData() 72 { 73 return this.data; 74 } 75 78 public void setData(T data) 79 { 80 this.data = data; 81 } 82 83 88 public boolean addEdge(Edge<T> e) 89 { 90 if (e.getFrom() == this) 91 outgoingEdges.add(e); 92 else if (e.getTo() == this) 93 incomingEdges.add(e); 94 else 95 return false; 96 return true; 97 } 98 99 104 public boolean hasEdge(Edge<T> e) 105 { 106 if (e.getFrom() == this) 107 return incomingEdges.contains(e); 108 else if (e.getTo() == this) 109 return outgoingEdges.contains(e); 110 else 111 return false; 112 } 113 114 public boolean remove(Edge<T> e) 116 { 117 if (e.getFrom() == this) 118 incomingEdges.remove(e); 119 else if (e.getTo() == this) 120 outgoingEdges.remove(e); 121 else 122 return false; 123 return true; 124 } 125 126 public int getIncomingEdgeCount() 127 { 128 return incomingEdges.size(); 129 } 130 131 public Edge<T> getIncomingEdge(int i) 132 { 133 Edge<T> e = incomingEdges.get(i); 134 return e; 135 } 136 137 public int getOutgoingEdgeCount() 138 { 139 return outgoingEdges.size(); 140 } 141 public Edge<T> getOutgoingEdge(int i) 142 { 143 Edge<T> e = outgoingEdges.get(i); 144 return e; 145 } 146 147 public Edge<T> findEdge(Vertex<T> dest) 149 { 150 for (int i = 0; i < incomingEdges.size(); i++) 151 { 152 Edge<T> e = incomingEdges.get(i); 153 if (e.getTo() == dest) 154 return e; 155 } 156 return null; 157 } 158 159 public Edge<T> findEdge(Edge<T> e) 161 { 162 if (incomingEdges.contains(e)) 163 return e; 164 else 165 return null; 166 } 167 168 174 public int cost(Vertex<T> dest) 175 { 176 if (dest == this) 177 return 0; 178 179 Edge<T> e = findEdge(dest); 180 if (e != null) 181 return e.getCost(); 182 else 183 return Integer.MAX_VALUE; 184 } 185 186 public boolean hasEdge(Vertex<T> dest) 188 { 189 return (findEdge(dest) != null); 190 } 191 192 public boolean visited() 194 { 195 return mark; 196 } 197 198 public void mark() 199 { 200 mark = true; 201 } 202 public void setMarkState(int state) 203 { 204 markState = state; 205 } 206 public int getMarkState() 207 { 208 return markState; 209 } 210 211 public void visit() 212 { 213 mark(); 214 } 215 216 public void clearMark() 218 { 219 mark = false; 220 } 221 222 public String toString() 223 { 224 StringBuffer tmp = new StringBuffer ("Vertex("); 225 tmp.append(name); 226 tmp.append("), in:["); 227 for (int i = 0; i < incomingEdges.size(); i++) 228 { 229 Edge<T> e = incomingEdges.get(i); 230 if( i > 0 ) 231 tmp.append(','); 232 tmp.append('{'); 233 tmp.append(e.getFrom().name); 234 tmp.append(','); 235 tmp.append(e.getCost()); 236 tmp.append('}'); 237 } 238 tmp.append("], out:["); 239 for (int i = 0; i < outgoingEdges.size(); i++) 240 { 241 Edge<T> e = outgoingEdges.get(i); 242 if( i > 0 ) 243 tmp.append(','); 244 tmp.append('{'); 245 tmp.append(e.getTo().name); 246 tmp.append(','); 247 tmp.append(e.getCost()); 248 tmp.append('}'); 249 } 250 tmp.append(']'); 251 return tmp.toString(); 252 } 253 } 254 | Popular Tags |