View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      //--- Constant(s) ---
32  
33      //--- Data field(s) ---
34  	
35  	private DependencyGraph graph;
36  	private boolean requireInstalled;
37  
38      //--- Constructor(s) ---
39  
40  	public DefaultResolver(DependencyGraph graph, boolean requireInstalled)
41      {
42  		this.graph = graph;
43  		this.requireInstalled = requireInstalled;
44      }
45  
46      //--- Method(s) ---
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