1 7 8 package javax.imageio.spi; 9 10 import java.io.Serializable ; 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.Set ; 14 15 25 class DigraphNode implements Cloneable , Serializable { 26 27 28 protected Object data; 29 30 34 protected Set outNodes = new HashSet (); 35 36 37 protected int inDegree = 0; 38 39 43 private Set inNodes = new HashSet (); 44 45 public DigraphNode(Object data) { 46 this.data = data; 47 } 48 49 50 public Object getData() { 51 return data; 52 } 53 54 58 public Iterator getOutNodes() { 59 return outNodes.iterator(); 60 } 61 62 71 public boolean addEdge(DigraphNode node) { 72 if (outNodes.contains(node)) { 73 return false; 74 } 75 76 outNodes.add(node); 77 node.inNodes.add(this); 78 node.incrementInDegree(); 79 return true; 80 } 81 82 90 public boolean hasEdge(DigraphNode node) { 91 return outNodes.contains(node); 92 } 93 94 101 public boolean removeEdge(DigraphNode node) { 102 if (!outNodes.contains(node)) { 103 return false; 104 } 105 106 outNodes.remove(node); 107 node.inNodes.remove(this); 108 node.decrementInDegree(); 109 return true; 110 } 111 112 116 public void dispose() { 117 Object [] inNodesArray = inNodes.toArray(); 118 for(int i=0; i<inNodesArray.length; i++) { 119 DigraphNode node = (DigraphNode ) inNodesArray[i]; 120 node.removeEdge(this); 121 } 122 123 Object [] outNodesArray = outNodes.toArray(); 124 for(int i=0; i<outNodesArray.length; i++) { 125 DigraphNode node = (DigraphNode ) outNodesArray[i]; 126 removeEdge(node); 127 } 128 } 129 130 131 public int getInDegree() { 132 return inDegree; 133 } 134 135 136 private void incrementInDegree() { 137 ++inDegree; 138 } 139 140 141 private void decrementInDegree() { 142 --inDegree; 143 } 144 } 145 | Popular Tags |