Skip to main content
summaryrefslogtreecommitdiffstats
blob: 908bf866aaf475daf67776b3730f90a458900eb7 (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
/*******************************************************************************
 * Copyright (c) 2004, 2010 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
 *     Danail Nachev -  ProSyst - bug 218625
 *******************************************************************************/
package org.eclipse.osgi.internal.module;

import java.util.*;

public class VersionHashMap extends MappedList implements Comparator {
	private ResolverImpl resolver;

	public VersionHashMap(ResolverImpl resolver) {
		this.resolver = resolver;
	}

	// assumes existing array is sorted
	// finds the index where to insert the new value
	protected int insertionIndex(Object[] existing, Object value) {
		int index = existing.length;
		if (compare(existing[existing.length - 1], value) > 0) {
			index = Arrays.binarySearch(existing, value, this);
			if (index < 0)
				index = -index - 1;
		}
		return index;
	}

	public void put(VersionSupplier[] versionSuppliers) {
		for (int i = 0; i < versionSuppliers.length; i++)
			put(versionSuppliers[i].getName(), versionSuppliers[i]);
	}

	public boolean contains(VersionSupplier vs) {
		return contains(vs, false) != null;
	}

	private VersionSupplier contains(VersionSupplier vs, boolean remove) {
		Object existing = internal.get(vs.getName());
		if (existing == null)
			return null;
		if (existing == vs) {
			if (remove)
				internal.remove(vs.getName());
			return vs;
		}
		if (!existing.getClass().isArray())
			return null;
		Object[] existingValues = (Object[]) existing;
		for (int i = 0; i < existingValues.length; i++)
			if (existingValues[i] == vs) {
				if (remove) {
					if (existingValues.length == 2) {
						internal.put(vs.getName(), existingValues[i == 0 ? 1 : 0]);
						return vs;
					}
					Object[] newExisting = new Object[existingValues.length - 1];
					System.arraycopy(existingValues, 0, newExisting, 0, i);
					if (i + 1 < existingValues.length)
						System.arraycopy(existingValues, i + 1, newExisting, i, existingValues.length - i - 1);
					internal.put(vs.getName(), newExisting);
				}
				return vs;
			}
		return null;
	}

	public Object remove(VersionSupplier toBeRemoved) {
		return contains(toBeRemoved, true);
	}

	public void remove(VersionSupplier[] versionSuppliers) {
		for (int i = 0; i < versionSuppliers.length; i++)
			remove(versionSuppliers[i]);
	}

	// Once we have resolved bundles, we need to make sure that version suppliers
	// from the resolved bundles are ahead of those from unresolved bundles
	void reorder() {
		for (Iterator it = internal.values().iterator(); it.hasNext();) {
			Object existing = it.next();
			if (!existing.getClass().isArray())
				continue;
			Arrays.sort((Object[]) existing, this);
		}
	}

	// Compares two VersionSuppliers for descending ordered sorts.
	// The VersionSuppliers are sorted by the following priorities
	// First the resolution status of the supplying bundle.
	// Second is the supplier version.
	// Third is the bundle id of the supplying bundle.
	public int compare(Object o1, Object o2) {
		if (!(o1 instanceof VersionSupplier) || !(o2 instanceof VersionSupplier))
			throw new IllegalArgumentException();
		VersionSupplier vs1 = (VersionSupplier) o1;
		VersionSupplier vs2 = (VersionSupplier) o2;
		// if the selection policy is set then use that
		if (resolver.getSelectionPolicy() != null)
			return resolver.getSelectionPolicy().compare(vs1.getBaseDescription(), vs2.getBaseDescription());
		String systemBundle = resolver.getSystemBundle();
		if (systemBundle.equals(vs1.getBundle().getSymbolicName()) && !systemBundle.equals(vs2.getBundle().getSymbolicName()))
			return -1;
		else if (!systemBundle.equals(vs1.getBundle().getSymbolicName()) && systemBundle.equals(vs2.getBundle().getSymbolicName()))
			return 1;
		if (vs1.getBundle().isResolved() != vs2.getBundle().isResolved())
			return vs1.getBundle().isResolved() ? -1 : 1;
		int versionCompare = -(vs1.getVersion().compareTo(vs2.getVersion()));
		if (versionCompare != 0)
			return versionCompare;
		return vs1.getBundle().getBundleId() <= vs2.getBundle().getBundleId() ? -1 : 1;
	}
}

Back to the top