Skip to main content
summaryrefslogtreecommitdiffstats
blob: ae358d85b272d12561c6695671a4fa6ded3822ae (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
 * Copyright (c) 2008, 2009 Code 9 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: 
 *   Code 9 - initial API and implementation
 *   IBM - ongoing development
 ******************************************************************************/
package org.eclipse.equinox.p2.publisher;

import java.util.*;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils;
import org.eclipse.equinox.p2.metadata.*;
import org.eclipse.equinox.p2.metadata.query.InstallableUnitQuery;
import org.eclipse.equinox.p2.query.*;

public class PublisherResult implements IPublisherResult {

	final Map<String, Set<IInstallableUnit>> rootIUs = new HashMap<String, Set<IInstallableUnit>>();
	final Map<String, Set<IInstallableUnit>> nonRootIUs = new HashMap<String, Set<IInstallableUnit>>();

	public void addIU(IInstallableUnit iu, String type) {
		if (type == ROOT)
			addIU(rootIUs, iu.getId(), iu);
		if (type == NON_ROOT)
			addIU(nonRootIUs, iu.getId(), iu);
	}

	public void addIUs(Collection<IInstallableUnit> ius, String type) {
		for (IInstallableUnit iu : ius)
			addIU(iu, type);
	}

	private void addIU(Map<String, Set<IInstallableUnit>> map, String id, IInstallableUnit iu) {
		Set<IInstallableUnit> ius = map.get(id);
		if (ius == null) {
			ius = new HashSet<IInstallableUnit>(11);
			map.put(id, ius);
		}
		ius.add(iu);
	}

	public IInstallableUnit getIU(String id, Version version, String type) {
		if (type == null || type == ROOT) {
			Collection<IInstallableUnit> ius = rootIUs.get(id);
			for (IInstallableUnit iu : ius) {
				if (iu.getVersion().equals(version))
					return iu;
			}
		}
		if (type == null || type == NON_ROOT) {
			Collection<IInstallableUnit> ius = nonRootIUs.get(id);
			for (IInstallableUnit iu : ius) {
				if (iu.getVersion().equals(version))
					return iu;
			}
		}
		return null;
	}

	// TODO this method really should not be needed as it just returns the first
	// matching IU non-deterministically.
	public IInstallableUnit getIU(String id, String type) {
		if (type == null || type == ROOT) {
			Collection<IInstallableUnit> ius = rootIUs.get(id);
			if (ius != null && ius.size() > 0)
				return ius.iterator().next();
		}
		if (type == null || type == NON_ROOT) {
			Collection<IInstallableUnit> ius = nonRootIUs.get(id);
			if (ius != null && ius.size() > 0)
				return ius.iterator().next();
		}
		return null;
	}

	/**
	 * Returns the IUs in this result with the given id.
	 */
	public Collection<IInstallableUnit> getIUs(String id, String type) {
		if (type == null) {
			ArrayList<IInstallableUnit> result = new ArrayList<IInstallableUnit>();
			result.addAll(id == null ? flatten(rootIUs.values()) : getIUs(rootIUs, id));
			result.addAll(id == null ? flatten(nonRootIUs.values()) : getIUs(nonRootIUs, id));
			return result;
		}
		if (type == ROOT)
			return id == null ? flatten(rootIUs.values()) : rootIUs.get(id);
		if (type == NON_ROOT)
			return id == null ? flatten(nonRootIUs.values()) : nonRootIUs.get(id);
		return null;
	}

	private Collection<IInstallableUnit> getIUs(Map<String, Set<IInstallableUnit>> ius, String id) {
		Collection<IInstallableUnit> result = ius.get(id);
		return result == null ? CollectionUtils.<IInstallableUnit> emptyList() : result;
	}

	protected List<IInstallableUnit> flatten(Collection<Set<IInstallableUnit>> values) {
		ArrayList<IInstallableUnit> result = new ArrayList<IInstallableUnit>();
		for (Set<IInstallableUnit> iuSet : values)
			for (IInstallableUnit iu : iuSet)
				result.add(iu);
		return result;
	}

	public void merge(IPublisherResult result, int mode) {
		if (mode == MERGE_MATCHING) {
			addIUs(result.getIUs(null, ROOT), ROOT);
			addIUs(result.getIUs(null, NON_ROOT), NON_ROOT);
		} else if (mode == MERGE_ALL_ROOT) {
			addIUs(result.getIUs(null, ROOT), ROOT);
			addIUs(result.getIUs(null, NON_ROOT), ROOT);
		} else if (mode == MERGE_ALL_NON_ROOT) {
			addIUs(result.getIUs(null, ROOT), NON_ROOT);
			addIUs(result.getIUs(null, NON_ROOT), NON_ROOT);
		}
	}

	class QueryableMap implements IQueryable<IInstallableUnit> {
		private Map<String, Set<IInstallableUnit>> map;

		public QueryableMap(Map<String, Set<IInstallableUnit>> map) {
			this.map = map;
		}

		public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IProgressMonitor monitor) {
			return query.perform(flatten(this.map.values()).iterator());
		}
	}

	/**
	 * Queries both the root and non root IUs
	 */
	public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IProgressMonitor monitor) {
		//optimize for installable unit query
		if (query instanceof InstallableUnitQuery)
			return queryIU((InstallableUnitQuery) query, monitor);
		IQueryable<IInstallableUnit> nonRootQueryable = new QueryableMap(nonRootIUs);
		IQueryable<IInstallableUnit> rootQueryable = new QueryableMap(rootIUs);
		return new CompoundQueryable<IInstallableUnit>(nonRootQueryable, rootQueryable).query(query, monitor);
	}

	private IQueryResult<IInstallableUnit> queryIU(InstallableUnitQuery query, IProgressMonitor monitor) {
		Collector<IInstallableUnit> result = new Collector<IInstallableUnit>();
		Collection<IInstallableUnit> matches = getIUs(query.getId(), null);
		VersionRange queryRange = query.getRange();
		for (IInstallableUnit match : matches) {
			if (queryRange == null || queryRange.isIncluded(match.getVersion()))
				if (!result.accept(match))
					break;
		}
		return result;
	}
}

Back to the top