Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6428eda8abfd55c576d46ec3b4fb816c79dfd6d9 (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
/*******************************************************************************
 * 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.provisional.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.internal.provisional.p2.metadata.Version;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.*;

public class PublisherResult implements IPublisherResult {

	private static final Collection EMPTY_COLLECTION = new ArrayList(0);
	final Map rootIUs = new HashMap();
	final Map nonRootIUs = new HashMap();

	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 ius, String type) {
		for (Iterator i = ius.iterator(); i.hasNext();)
			addIU((IInstallableUnit) i.next(), type);
	}

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

	public IInstallableUnit getIU(String id, Version version, String type) {
		if (type == null || type == ROOT) {
			Collection ius = (Collection) rootIUs.get(id);
			for (Iterator i = ius.iterator(); i.hasNext();) {
				IInstallableUnit iu = (IInstallableUnit) i.next();
				if (iu.getVersion().equals(version))
					return iu;
			}
		}
		if (type == null || type == NON_ROOT) {
			Collection ius = (Collection) nonRootIUs.get(id);
			for (Iterator i = ius.iterator(); i.hasNext();) {
				IInstallableUnit iu = (IInstallableUnit) i.next();
				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 ius = (Collection) rootIUs.get(id);
			if (ius != null && ius.size() > 0)
				return (IInstallableUnit) ius.iterator().next();
		}
		if (type == null || type == NON_ROOT) {
			Collection ius = (Collection) nonRootIUs.get(id);
			if (ius != null && ius.size() > 0)
				return (IInstallableUnit) ius.iterator().next();
		}
		return null;
	}

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

	private Collection getIUs(Map ius, String id) {
		Collection result = (Collection) ius.get(id);
		return result == null ? EMPTY_COLLECTION : result;
	}

	protected List flatten(Collection values) {
		ArrayList result = new ArrayList();
		for (Iterator i = values.iterator(); i.hasNext();)
			for (Iterator j = ((HashSet) i.next()).iterator(); j.hasNext();)
				result.add(j.next());
		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 {
		private Map map;

		public QueryableMap(Map map) {
			this.map = map;
		}

		public Collector query(Query query, Collector collector, IProgressMonitor monitor) {
			return query.perform(flatten(this.map.values()).iterator(), collector);
		}
	}

	/**
	 * Queries both the root and non root IUs
	 */
	public Collector query(Query query, Collector collector, IProgressMonitor monitor) {
		IQueryable nonRootQueryable = new QueryableMap(nonRootIUs);
		IQueryable rootQueryable = new QueryableMap(rootIUs);
		return new CompoundQueryable(new IQueryable[] {nonRootQueryable, rootQueryable}).query(query, collector, monitor);
	}
}

Back to the top