KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > dependencies > SatisfactionVisitor


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.core.internal.dependencies;
13
14 import java.util.*;
15
16 public class SatisfactionVisitor implements IElementSetVisitor {
17     private int order;
18
19     public SatisfactionVisitor(int order) {
20         this.order = order;
21     }
22
23     public Collection getAncestors(ElementSet elementSet) {
24         return elementSet.getRequired();
25     }
26
27     public Collection getDescendants(ElementSet elementSet) {
28         return elementSet.getRequiring();
29     }
30
31     public int getOrder() {
32         return order;
33     }
34
35     public void update(ElementSet elementSet) {
36         // if there are no pre-requisites, all available versions are satisfied
37
if (elementSet.getRequired().isEmpty()) {
38             elementSet.setSatisfied(elementSet.getAvailable());
39             return;
40         }
41
42         Set satisfied = new HashSet();
43         for (Iterator elementsIter = elementSet.getAvailable().iterator(); elementsIter.hasNext();) {
44             Element element = (Element) elementsIter.next();
45             Dependency[] dependencies = element.getDependencies();
46             boolean versionSatisfied = true;
47             for (int i = 0; i < dependencies.length; i++) {
48                 // optional pre-requisites are not relevant for satisfaction
49
if (dependencies[i].isOptional())
50                     continue;
51
52                 ElementSet requiredNode = elementSet.getSystem().getElementSet(dependencies[i].getRequiredObjectId());
53
54                 Collection requiredNodeSatisfiedVersions = requiredNode.getSatisfied();
55                 boolean depSatisfied = false;
56                 for (Iterator requiredNodeSatisfiedVersionsIter = requiredNodeSatisfiedVersions.iterator(); requiredNodeSatisfiedVersionsIter.hasNext();) {
57                     Element requiredSatisfiedVersion = (Element) requiredNodeSatisfiedVersionsIter.next();
58                     if (dependencies[i].getMatchRule().isSatisfied(dependencies[i].getUserObject(), requiredSatisfiedVersion.getVersionId())) {
59                         depSatisfied = true;
60                         break;
61                     }
62                 }
63                 if (!depSatisfied) {
64                     versionSatisfied = false;
65                     break;
66                 }
67             }
68             if (versionSatisfied)
69                 satisfied.add(element);
70         }
71         elementSet.setSatisfied(satisfied);
72     }
73
74 }
Popular Tags