1 19 20 package edu.umd.cs.findbugs.graph; 21 22 30 public class AbstractVertex < 31 EdgeType extends AbstractEdge<EdgeType, ActualVertexType>, 32 ActualVertexType extends AbstractVertex<EdgeType, ActualVertexType> 33 > implements GraphVertex<ActualVertexType> { 34 35 private int id; 36 private int label; 37 EdgeType firstIncomingEdge, lastIncomingEdge; 38 EdgeType firstOutgoingEdge, lastOutgoingEdge; 39 40 void setId(int id) { 41 this.id = id; 42 } 43 44 int getId() { 45 return id; 46 } 47 48 public int getLabel() { 49 return label; 50 } 51 52 public void setLabel(int label) { 53 this.label = label; 54 } 55 56 public int hashCode() { 57 return id; 58 } 59 public boolean equals(Object o) { 60 if (o instanceof AbstractVertex) 61 return id == ((AbstractVertex)o).id; 62 return false; 63 } 64 public int compareTo(ActualVertexType other) { 65 return id - other.id; 66 } 67 68 void addOutgoingEdge(EdgeType edge) { 69 if (firstOutgoingEdge == null) { 70 firstOutgoingEdge = lastOutgoingEdge = edge; 71 } else { 72 lastOutgoingEdge.setNextOutgoingEdge(edge); 73 lastOutgoingEdge = edge; 74 } 75 } 76 77 EdgeType getFirstOutgoingEdge() { 78 return firstOutgoingEdge; 79 } 80 81 void addIncomingEdge(EdgeType edge) { 82 if (firstIncomingEdge == null) { 83 firstIncomingEdge = lastIncomingEdge = edge; 84 } else { 85 lastIncomingEdge.setNextIncomingEdge(edge); 86 lastIncomingEdge = edge; 87 } 88 } 89 90 EdgeType getFirstIncomingEdge() { 91 return firstIncomingEdge; 92 } 93 94 void removeIncomingEdge(EdgeType edge) { 95 EdgeType prev = null, cur = firstIncomingEdge; 96 while (cur != null) { 97 EdgeType next = cur.getNextIncomingEdge(); 98 if (cur.equals(edge)) { 99 if (prev != null) 100 prev.setNextIncomingEdge(next); 101 else 102 firstIncomingEdge = next; 103 return; 104 } 105 prev = cur; 106 cur = next; 107 } 108 throw new IllegalArgumentException ("removing nonexistent edge!"); 109 } 110 111 void removeOutgoingEdge(EdgeType edge) { 112 EdgeType prev = null, cur = firstOutgoingEdge; 113 while (cur != null) { 114 EdgeType next = cur.getNextOutgoingEdge(); 115 if (cur.equals(edge)) { 116 if (prev != null) 117 prev.setNextOutgoingEdge(next); 118 else 119 firstOutgoingEdge = next; 120 return; 121 } 122 prev = cur; 123 cur = cur.getNextOutgoingEdge(); 124 } 125 throw new IllegalArgumentException ("removing nonexistent edge!"); 126 } 127 128 129 } 130 131 | Popular Tags |