1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.pkg;
21
22 import java.util.Iterator;
23 import java.util.LinkedList;
24
25 /***
26 *
27 */
28 public class DefaultResolver
29 {
30
31
32
33
34
35 private DependencyGraph graph;
36 private boolean requireInstalled;
37
38
39
40 public DefaultResolver(DependencyGraph graph, boolean requireInstalled)
41 {
42 this.graph = graph;
43 this.requireInstalled = requireInstalled;
44 }
45
46
47
48 public void resolve() throws UnsatisfiedDependenciesException
49 {
50 graph.getRoot().require(requireInstalled);
51 }
52
53 /***
54 * Traverses the graph and returns all required packages.
55 */
56 public PackageInfo[] getRequired()
57 {
58 LinkedList required = new LinkedList();
59
60 for (Iterator i = graph.preorderIterator(); i.hasNext();) {
61 DependencyNode node = (DependencyNode)i.next();
62 if (node instanceof PackageDependencyNode && node.isRequired()) {
63 required.add(((PackageDependencyNode)node).getPackage());
64 }
65 }
66
67 return (PackageInfo[])required.toArray(new PackageInfo[0]);
68 }
69
70 /***
71 * Traverses the graph and returns all required packages that are
72 * not installed.
73 */
74 public PackageInfo[] getRequiredUninstalled()
75 {
76 LinkedList required = new LinkedList();
77
78 for (Iterator i = graph.preorderIterator(); i.hasNext();) {
79 DependencyNode node = (DependencyNode)i.next();
80 if (node instanceof PackageDependencyNode && node.isRequired()) {
81 PackageDependencyNode p = (PackageDependencyNode)node;
82 if (!p.getPackage().isInstalled()) {
83 required.add(p.getPackage());
84 }
85 }
86 }
87
88 return (PackageInfo[])required.toArray(new PackageInfo[0]);
89 }
90
91 }
92