1 43 44 package org.jfree.base.modules; 45 46 import java.util.ArrayList ; 47 import java.util.Arrays ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 52 import org.jfree.util.Log; 53 54 65 public final class PackageSorter 66 { 67 74 private static class SortModule implements Comparable 75 { 76 77 private int position; 78 79 private final PackageState state; 80 81 private ArrayList dependSubsystems; 82 85 91 public SortModule(final PackageState state) 92 { 93 this.position = -1; 94 this.state = state; 95 } 96 97 103 public ArrayList getDependSubsystems() 104 { 105 return this.dependSubsystems; 106 } 107 108 114 public void setDependSubsystems(final ArrayList dependSubsystems) 115 { 116 this.dependSubsystems = dependSubsystems; 117 } 118 119 126 public int getPosition() 127 { 128 return this.position; 129 } 130 131 137 public void setPosition(final int position) 138 { 139 this.position = position; 140 } 141 142 147 public PackageState getState() 148 { 149 return this.state; 150 } 151 152 159 public String toString () 160 { 161 final StringBuffer buffer = new StringBuffer (); 162 buffer.append("SortModule: "); 163 buffer.append(this.position); 164 buffer.append(" "); 165 buffer.append(this.state.getModule().getName()); 166 buffer.append(" "); 167 buffer.append(this.state.getModule().getModuleClass()); 168 return buffer.toString(); 169 } 170 171 182 public int compareTo(final Object o) 183 { 184 final SortModule otherModule = (SortModule) o; 185 if (this.position > otherModule.position) 186 { 187 return +1; 188 } 189 if (this.position < otherModule.position) 190 { 191 return -1; 192 } 193 return 0; 194 } 195 } 196 197 200 private PackageSorter() { 201 } 203 204 212 public static void sort (final List modules) 213 { 214 final HashMap moduleMap = new HashMap (); 215 final ArrayList errorModules = new ArrayList (); 216 final ArrayList weightModules = new ArrayList (); 217 218 for (int i = 0; i < modules.size(); i++) 219 { 220 final PackageState state = (PackageState) modules.get(i); 221 if (state.getState() == PackageState.STATE_ERROR) 222 { 223 errorModules.add (state); 224 } 225 else 226 { 227 final SortModule mod = new SortModule(state); 228 weightModules.add (mod); 229 moduleMap.put(state.getModule().getModuleClass(), mod); 230 } 231 } 232 233 final SortModule[] weigths = (SortModule[]) 234 weightModules.toArray(new SortModule[weightModules.size()]); 235 236 for (int i = 0; i < weigths.length; i++) 237 { 238 final SortModule sortMod = weigths[i]; 239 sortMod.setDependSubsystems 240 (collectSubsystemModules(sortMod.getState().getModule(), 241 moduleMap)); 242 } 243 244 245 boolean doneWork = true; 251 while (doneWork) 252 { 253 doneWork = false; 254 for (int i = 0; i < weigths.length; i++) 255 { 256 final SortModule mod = weigths[i]; 257 final int position = searchModulePosition(mod, moduleMap); 258 if (position != mod.getPosition()) 259 { 260 mod.setPosition(position); 261 doneWork = true; 262 } 263 } 264 } 265 266 Arrays.sort(weigths); 267 modules.clear(); 268 for (int i = 0; i < weigths.length; i++) 269 { 270 modules.add (weigths[i].getState()); 271 } 272 for (int i = 0; i < errorModules.size(); i++) 273 { 274 modules.add (errorModules.get(i)); 275 } 276 } 277 278 287 private static int searchModulePosition 288 (final SortModule smodule, final HashMap moduleMap) 289 { 290 final Module module = smodule.getState().getModule(); 291 int position = 0; 292 293 ModuleInfo[] modInfo = module.getOptionalModules(); 297 for (int modPos = 0; modPos < modInfo.length; modPos++) 298 { 299 final String moduleName = modInfo[modPos].getModuleClass(); 300 final SortModule reqMod = (SortModule) moduleMap.get(moduleName); 301 if (reqMod == null) 302 { 303 continue; 304 } 305 if (reqMod.getPosition() >= position) 306 { 307 position = reqMod.getPosition() + 1; 308 } 309 } 310 311 modInfo = module.getRequiredModules(); 316 for (int modPos = 0; modPos < modInfo.length; modPos++) 317 { 318 final String moduleName = modInfo[modPos].getModuleClass(); 319 final SortModule reqMod = (SortModule) moduleMap.get(moduleName); 320 if (reqMod.getPosition() >= position) 321 { 322 position = reqMod.getPosition() + 1; 323 } 324 } 325 326 final String subSystem = module.getSubSystem(); 330 final Iterator it = moduleMap.values().iterator(); 331 while (it.hasNext()) 332 { 333 final SortModule mod = (SortModule) it.next(); 334 if (mod.getState().getModule() == module) 336 { 337 continue; 339 } 340 final Module subSysMod = mod.getState().getModule(); 341 if (subSystem.equals(subSysMod.getSubSystem())) 345 { 346 continue; 348 } 349 350 if (smodule.getDependSubsystems().contains(subSysMod.getSubSystem())) 355 { 356 if (isBaseModule(subSysMod, module) == false) 360 { 361 if (mod.getPosition() >= position) 362 { 363 position = mod.getPosition() + 1; 364 } 365 } 366 } 367 } 368 return position; 369 } 370 371 379 private static boolean isBaseModule(final Module mod, final ModuleInfo mi) 380 { 381 ModuleInfo[] info = mod.getRequiredModules(); 382 for (int i = 0; i < info.length; i++) 383 { 384 if (info[i].getModuleClass().equals(mi.getModuleClass())) 385 { 386 return true; 387 } 388 } 389 info = mod.getOptionalModules(); 390 for (int i = 0; i < info.length; i++) 391 { 392 if (info[i].getModuleClass().equals(mi.getModuleClass())) 393 { 394 return true; 395 } 396 } 397 return false; 398 } 399 400 401 408 private static ArrayList collectSubsystemModules 409 (final Module childMod, final HashMap moduleMap) 410 { 411 final ArrayList collector = new ArrayList (); 412 ModuleInfo[] info = childMod.getRequiredModules(); 413 for (int i = 0; i < info.length; i++) 414 { 415 final SortModule dependentModule = (SortModule) 416 moduleMap.get(info[i].getModuleClass()); 417 if (dependentModule == null) 418 { 419 Log.warn 420 (new Log.SimpleMessage 421 ("A dependent module was not found in the list of known modules.", 422 info[i].getModuleClass())); 423 continue; 424 } 425 426 collector.add (dependentModule.getState().getModule().getSubSystem()); 427 } 428 429 info = childMod.getOptionalModules(); 430 for (int i = 0; i < info.length; i++) 431 { 432 final Module dependentModule = (Module) 433 moduleMap.get(info[i].getModuleClass()); 434 if (dependentModule == null) 435 { 436 Log.warn ("A dependent module was not found in the list of known modules."); 437 continue; 438 } 439 collector.add (dependentModule.getSubSystem()); 440 } 441 return collector; 442 } 443 } 444 | Popular Tags |