1 7 8 package javax.imageio.spi; 9 10 import java.util.AbstractSet ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.LinkedList ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 37 class PartiallyOrderedSet extends AbstractSet { 38 39 43 private Map poNodes = new HashMap (); 45 46 private Set nodes = poNodes.keySet(); 48 49 52 public PartiallyOrderedSet() {} 53 54 public int size() { 55 return nodes.size(); 56 } 57 58 public boolean contains(Object o) { 59 return nodes.contains(o); 60 } 61 62 67 public Iterator iterator() { 68 return new PartialOrderIterator(poNodes.values().iterator()); 69 } 70 71 75 public boolean add(Object o) { 76 if (nodes.contains(o)) { 77 return false; 78 } 79 80 DigraphNode node = new DigraphNode (o); 81 poNodes.put(o, node); 82 return true; 83 } 84 85 89 public boolean remove(Object o) { 90 DigraphNode node = (DigraphNode )poNodes.get(o); 91 if (node == null) { 92 return false; 93 } 94 95 poNodes.remove(o); 96 node.dispose(); 97 return true; 98 } 99 100 public void clear() { 101 poNodes.clear(); 102 } 103 104 113 public boolean setOrdering(Object first, Object second) { 114 DigraphNode firstPONode = 115 (DigraphNode )poNodes.get(first); 116 DigraphNode secondPONode = 117 (DigraphNode )poNodes.get(second); 118 119 secondPONode.removeEdge(firstPONode); 120 return firstPONode.addEdge(secondPONode); 121 } 122 123 128 public boolean unsetOrdering(Object first, Object second) { 129 DigraphNode firstPONode = 130 (DigraphNode )poNodes.get(first); 131 DigraphNode secondPONode = 132 (DigraphNode )poNodes.get(second); 133 134 return firstPONode.removeEdge(secondPONode) || 135 secondPONode.removeEdge(firstPONode); 136 } 137 138 142 public boolean hasOrdering(Object preferred, Object other) { 143 DigraphNode preferredPONode = 144 (DigraphNode )poNodes.get(preferred); 145 DigraphNode otherPONode = 146 (DigraphNode )poNodes.get(other); 147 148 return preferredPONode.hasEdge(otherPONode); 149 } 150 } 151 152 class PartialOrderIterator implements Iterator { 153 154 LinkedList zeroList = new LinkedList (); 155 Map inDegrees = new HashMap (); 157 public PartialOrderIterator(Iterator iter) { 158 while (iter.hasNext()) { 160 DigraphNode node = (DigraphNode )iter.next(); 161 int inDegree = node.getInDegree(); 162 inDegrees.put(node, new Integer (inDegree)); 163 164 if (inDegree == 0) { 166 zeroList.add(node); 167 } 168 } 169 } 170 171 public boolean hasNext() { 172 return !zeroList.isEmpty(); 173 } 174 175 public Object next() { 176 DigraphNode first = (DigraphNode )zeroList.removeFirst(); 177 178 Iterator outNodes = first.getOutNodes(); 180 while (outNodes.hasNext()) { 181 DigraphNode node = (DigraphNode )outNodes.next(); 182 int inDegree = ((Integer )inDegrees.get(node)).intValue() - 1; 183 inDegrees.put(node, new Integer (inDegree)); 184 185 if (inDegree == 0) { 187 zeroList.add(node); 188 } 189 } 190 191 return first.getData(); 192 } 193 194 public void remove() { 195 throw new UnsupportedOperationException (); 196 } 197 } 198 | Popular Tags |