1 21 package oracle.toplink.essentials.internal.sessions; 23 24 import java.util.*; 25 import oracle.toplink.essentials.descriptors.ClassDescriptor; 26 27 40 public class CommitOrderCalculator { 41 protected int currentTime; 42 protected Vector nodes; 43 protected Vector orderedDescriptors; 44 protected AbstractSession session; 45 46 49 public CommitOrderCalculator(AbstractSession session) { 50 super(); 51 this.currentTime = 0; 52 this.nodes = new Vector(1); 53 this.session = session; 54 } 55 56 protected void addNode(ClassDescriptor d) { 57 nodes.addElement(new CommitOrderDependencyNode(this, d, session)); 58 } 59 60 public void addNodes(Vector descriptors) { 61 Enumeration descriptorsEnum = descriptors.elements(); 62 while (descriptorsEnum.hasMoreElements()) { 63 ClassDescriptor descriptor = (ClassDescriptor)descriptorsEnum.nextElement(); 64 addNode(descriptor); 65 } 66 } 67 68 71 public void calculateMappingDependencies() { 72 for (Enumeration e = nodes.elements(); e.hasMoreElements();) { 73 CommitOrderDependencyNode node = (CommitOrderDependencyNode)e.nextElement(); 74 node.recordMappingDependencies(); 75 } 76 } 77 78 81 public void calculateSpecifiedDependencies() { 82 for (Enumeration e = nodes.elements(); e.hasMoreElements();) { 83 CommitOrderDependencyNode node = (CommitOrderDependencyNode)e.nextElement(); 84 node.recordSpecifiedDependencies(); 85 } 86 } 87 88 public void depthFirstSearch() { 89 90 97 98 for (Enumeration e = getNodes().elements(); e.hasMoreElements();) { 100 CommitOrderDependencyNode node = (CommitOrderDependencyNode)e.nextElement(); 101 node.markNotVisited(); 102 node.setPredecessor(null); 103 } 104 currentTime = 0; 105 106 for (Enumeration e = getNodes().elements(); e.hasMoreElements();) { 108 CommitOrderDependencyNode node = (CommitOrderDependencyNode)e.nextElement(); 109 if (node.hasNotBeenVisited()) { 110 node.visit(); 111 } 112 } 113 } 114 115 116 119 private static int doCompare(Object o1, Object o2) { 120 int first; 122 123 int second; 125 first = ((CommitOrderDependencyNode)o1).getFinishingTime(); 126 second = ((CommitOrderDependencyNode)o2).getFinishingTime(); 127 if (first >= second) { 128 return 1; 129 } else { 130 return -1; 131 } 132 } 133 134 135 138 private static int doCompare(CommitOrderDependencyNode o1, CommitOrderDependencyNode o2) { 139 int first; 141 142 int second; 144 first = o1.getFinishingTime(); 145 second = o2.getFinishingTime(); 146 if (first >= second) { 147 return 1; 148 } else { 149 return -1; 150 } 151 } 152 153 public int getNextTime() { 154 int result = currentTime; 155 currentTime++; 156 return result; 157 } 158 159 public Vector getNodes() { 160 return nodes; 161 } 162 163 166 public Vector getOrderedClasses() { 167 Vector orderedClasses = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(getOrderedDescriptors().size()); 168 for (Enumeration orderedDescriptorsEnum = getOrderedDescriptors().elements(); 169 orderedDescriptorsEnum.hasMoreElements();) { 170 orderedClasses.addElement(((ClassDescriptor)orderedDescriptorsEnum.nextElement()).getJavaClass()); 171 } 172 173 return orderedClasses; 174 } 175 176 179 public Vector getOrderedDescriptors() { 180 return orderedDescriptors; 181 } 182 183 public CommitOrderDependencyNode nodeFor(Class c) { 184 for (Enumeration e = nodes.elements(); e.hasMoreElements();) { 185 CommitOrderDependencyNode n = (CommitOrderDependencyNode)e.nextElement(); 186 if (n.getDescriptor().getJavaClass() == c) { 187 return n; 188 } 189 } 190 return null; 191 } 192 193 public CommitOrderDependencyNode nodeFor(ClassDescriptor d) { 194 for (Enumeration e = nodes.elements(); e.hasMoreElements();) { 195 CommitOrderDependencyNode n = (CommitOrderDependencyNode)e.nextElement(); 196 if (n.getDescriptor() == d) { 197 return n; 198 } 199 } 200 return null; 201 } 202 203 210 public void orderCommits() { 211 depthFirstSearch(); 212 213 Object [] nodeArray = new Object [nodes.size()]; 214 nodes.copyInto(nodeArray); 215 216 quicksort(nodeArray); 217 Vector result = new Vector(nodes.size()); 218 for (int i = 0; i < nodes.size(); i++) { 219 CommitOrderDependencyNode node = (CommitOrderDependencyNode)nodeArray[i]; 220 result.addElement(node.getDescriptor()); 221 } 222 this.orderedDescriptors = result; 223 } 224 225 228 private static void quicksort(Object [] arr) { 229 quicksort(arr, 0, arr.length - 1); 230 } 231 232 239 private static void quicksort(Object [] arr, int left, int right) { 240 int i; 241 int last; 242 243 if (left >= right) { 244 return; 245 } 246 swap(arr, left, (left + right) / 2); 247 last = left; 248 for (i = left + 1; i <= right; i++) { 249 if (doCompare(arr[i], arr[left]) < 0) { 250 swap(arr, ++last, i); 251 } 252 } 253 swap(arr, left, last); 254 quicksort(arr, left, last - 1); 255 quicksort(arr, last + 1, right); 256 } 257 258 private static void swap(Object [] arr, int i, int j) { 259 Object tmp; 260 261 tmp = arr[i]; 262 arr[i] = arr[j]; 263 arr[j] = tmp; 264 } 265 } 266 | Popular Tags |