Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dad73bee179f4dbcd47a8d134c5a19b781723e5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: IBM Corporation - initial API and implementation
 ******************************************************************************/
package org.eclipse.equinox.internal.p2.director;

import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.p2.metadata.MetadataFactory;
import org.eclipse.equinox.p2.metadata.RequiredCapability;
import org.eclipse.osgi.service.resolver.VersionRange;

public class RecommendationDescriptor {
	public static final String TOUCHPOINT_DATA_KEY = "recommendations";
	public static final String KIND = "recommendation";

	private Set recommendations;

	public RecommendationDescriptor(Set recommendations) {
		this.recommendations = recommendations;
	}

	public Set getRecommendations() {
		return recommendations;
	}

	public Recommendation findRecommendation(RequiredCapability toMatch) {
		for (Iterator iterator = recommendations.iterator(); iterator.hasNext();) {
			Recommendation name = (Recommendation) iterator.next();
			if (name.matches(toMatch))
				return name;
		}
		return null;
	}

	public Recommendation findRecommendation(Recommendation toMatch) {
		for (Iterator iterator = recommendations.iterator(); iterator.hasNext();) {
			Recommendation name = (Recommendation) iterator.next();
			if (name.matches(toMatch))
				return name;
		}
		return null;
	}

	//Merge the other descriptor into this one. Return an OK Status is the merged succeeded, otherwise return an INFO Status
	public IStatus merge(RecommendationDescriptor other) {
		MultiStatus result = null;
		for (Iterator it = other.recommendations.iterator(); it.hasNext();) {
			Recommendation otherRecommendation = (Recommendation) it.next();
			Recommendation matchInThis = findRecommendation(otherRecommendation);
			if (matchInThis == null) {
				recommendations.add(otherRecommendation);
				continue;
			}
			Recommendation newRec = otherRecommendation.merge(matchInThis);
			if (newRec != null) {
				recommendations.remove(matchInThis);
				recommendations.add(newRec);
				continue;
			} else {
				if (result == null)
					result = new MultiStatus(DirectorActivator.PI_DIRECTOR, 0, "Conflict between recommendations", null);
				result.add(new Status(IStatus.INFO, DirectorActivator.PI_DIRECTOR, "can't merge " + otherRecommendation + " with " + matchInThis));
			}
		}
		if (result == null)
			return Status.OK_STATUS;
		return result;

	}

	public static RecommendationDescriptor parse(String descriptor) {
		StringTokenizer entries = new StringTokenizer(descriptor, "\n");
		Set recommendations = new HashSet(entries.countTokens());
		while (entries.hasMoreElements()) {
			StringTokenizer oneRec = new StringTokenizer((String) entries.nextElement(), "/");
			if (oneRec.countTokens() != 4) {
				//format error, ignore and continue

				continue;
			}
			String ns = oneRec.nextToken().trim();
			String name = oneRec.nextToken().trim();
			String oldRange = oneRec.nextToken().trim();
			String newRange = oneRec.nextToken().trim();
			recommendations.add(new Recommendation(MetadataFactory.createRequiredCapability(ns, name, new VersionRange(oldRange), null, false, false), MetadataFactory.createRequiredCapability(ns, name, new VersionRange(newRange), null, false, false)));
		}
		return new RecommendationDescriptor(recommendations);
	}

	public static String serialize(RecommendationDescriptor toSerialize) {
		StringBuffer result = new StringBuffer();
		for (Iterator iterator = toSerialize.recommendations.iterator(); iterator.hasNext();) {
			Recommendation entry = (Recommendation) iterator.next();
			result.append(entry.applyOn().getNamespace() + '/' + entry.applyOn().getName() + '/' + entry.applyOn().getRange().toString() + '/' + entry.newValue().getRange().toString() + '\n');
		}
		return result.toString();
	}

	public boolean isCompatible(RecommendationDescriptor other) {
		for (Iterator it = other.recommendations.iterator(); it.hasNext();) {
			Recommendation otherRecommendation = (Recommendation) it.next();
			Recommendation matchInThis = findRecommendation(otherRecommendation);
			if (matchInThis == null) {
				continue;
			}
			if (!otherRecommendation.isCompatible(matchInThis))
				return false;
		}
		return true;

	}
}

Back to the top