KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > resolver > Eclipse21SelectionPolicy


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.osgi.internal.resolver;
13
14 import java.util.*;
15 import org.eclipse.core.internal.dependencies.*;
16
17 public class Eclipse21SelectionPolicy implements ISelectionPolicy {
18     public Set selectMultiple(ElementSet elementSet) {
19         Set selected = new HashSet();
20         for (Iterator requiringIter = elementSet.getRequiring().iterator(); requiringIter.hasNext();) {
21             ElementSet requiringNode = (ElementSet) requiringIter.next();
22             Collection requiringNodeSelectedVersions = requiringNode.getSelected();
23             // loop through the selected versions (one if it is a non-library plug-in) of this requiring plug-in
24
for (Iterator requiringVersionsIter = requiringNodeSelectedVersions.iterator(); requiringVersionsIter.hasNext();) {
25                 Element requiringSelectedVersion = (Element) requiringVersionsIter.next();
26                 // the selected version may not require this element set (but it still can be selected)
27
Dependency requiringSelectedVersionDependency = requiringSelectedVersion.getDependency(elementSet.getId());
28                 if (requiringSelectedVersionDependency == null)
29                     continue;
30                 // find the best version for this pre-requisite
31
Element bestVersion = null;
32                 for (Iterator satisfiedIter = elementSet.getSatisfied().iterator(); satisfiedIter.hasNext();) {
33                     Element satisfiedVersion = (Element) satisfiedIter.next();
34                     boolean satisfiesDependency = requiringSelectedVersionDependency.getMatchRule().isSatisfied(requiringSelectedVersionDependency.getUserObject(), satisfiedVersion.getVersionId());
35                     if (satisfiesDependency) {
36                         boolean betterThanBest = bestVersion == null || elementSet.getSystem().compare(satisfiedVersion.getVersionId(), bestVersion.getVersionId()) > 0;
37                         if (betterThanBest)
38                             bestVersion = satisfiedVersion;
39                     }
40                 }
41                 if (bestVersion != null)
42                     selected.add(bestVersion);
43             }
44         }
45         // if none of the versions are required (or satisfy any selected re-requisites), pick the highest
46
if (selected.isEmpty()) {
47             Element bestVersion = null;
48             for (Iterator satisfiedIter = elementSet.getSatisfied().iterator(); satisfiedIter.hasNext();) {
49                 Element satisfiedVersion = (Element) satisfiedIter.next();
50                 boolean betterThanBest = bestVersion == null || elementSet.getSystem().compare(satisfiedVersion.getVersionId(), bestVersion.getVersionId()) > 0;
51                 if (betterThanBest)
52                     bestVersion = satisfiedVersion;
53             }
54             selected = Collections.singleton(bestVersion);
55         }
56         return selected;
57     }
58
59     public Element selectSingle(ElementSet elementSet) {
60         // none of its versions are required by other element sets - so just pick the highest
61
if (elementSet.getRequiring().isEmpty()) {
62             // otherwise, pick the highest version
63
Element highest = null;
64             for (Iterator satisfiedIter = elementSet.getSatisfied().iterator(); satisfiedIter.hasNext();) {
65                 Element satisfiedVersion = (Element) satisfiedIter.next();
66                 if (highest == null || elementSet.getSystem().compare(satisfiedVersion.getVersionId(), highest.getVersionId()) > 0)
67                     highest = satisfiedVersion;
68             }
69             return highest;
70         }
71
72         // let's pick the highest that satisfies all or the highest required
73
Element highest = null;
74         int highestStatus = 0; // 0 - not required, 1 - satisfy some, 2 - satisfy all mandatory, 3 - satisfy all
75
for (Iterator satisfiedIter = elementSet.getSatisfied().iterator(); satisfiedIter.hasNext();) {
76             boolean satisfiesAllMandatory = true;
77             boolean satisfiesAll = true;
78             boolean isRequired = false;
79             Element satisfiedVersion = (Element) satisfiedIter.next();
80             for (Iterator requiringIter = elementSet.getRequiring().iterator(); requiringIter.hasNext();) {
81                 ElementSet requiringNode = (ElementSet) requiringIter.next();
82                 Collection requiringNodeSelectedVersions = requiringNode.getSelected();
83                 // loop through the selected versions (one if it is a non-library plug-in) of this requiring plug-in
84
for (Iterator requiringVersionsIter = requiringNodeSelectedVersions.iterator(); requiringVersionsIter.hasNext();) {
85                     Element requiringSelectedVersion = (Element) requiringVersionsIter.next();
86                     // the selected version may not require this element set (but it still can be selected)
87
Dependency requiringSelectedVersionDep = requiringSelectedVersion.getDependency(elementSet.getId());
88                     if (requiringSelectedVersionDep != null) {
89                         boolean satisfiesDependency = requiringSelectedVersionDep.getMatchRule().isSatisfied(requiringSelectedVersionDep.getUserObject(), satisfiedVersion.getVersionId());
90                         isRequired |= satisfiesDependency;
91                         satisfiesAll &= satisfiesDependency;
92                         satisfiesAllMandatory &= (satisfiesDependency | requiringSelectedVersionDep.isOptional());
93                     }
94                 }
95             }
96             int status = satisfiesAll ? 3 : (satisfiesAllMandatory ? 2 : (isRequired ? 1 : 0));
97             boolean higherThanHighest = highest == null || elementSet.getSystem().compare(satisfiedVersion.getVersionId(), highest.getVersionId()) > 0;
98             if (status > highestStatus || (status == highestStatus && higherThanHighest)) {
99                 highest = satisfiedVersion;
100                 highestStatus = status;
101             }
102         }
103         return highest;
104     }
105
106 }
Popular Tags