Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 850e15c1c193957ba4dbe9be4820551b5449b9cb (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
/*******************************************************************************
 * Copyright (c) 2000, 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
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.ui.viewers;

import java.net.URI;
import org.eclipse.equinox.internal.p2.ui.ProvUI;
import org.eclipse.equinox.internal.p2.ui.model.*;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.jface.viewers.IElementComparer;

public class ProvElementComparer implements IElementComparer {

	public boolean equals(Object a, Object b) {
		// We treat category elements specially because this
		// is one case where resolving down to an IU will lose identity
		// differences.  (category IU's with the same name and version number cannot be treated the same).
		if (a instanceof CategoryElement || b instanceof CategoryElement)
			return a.equals(b);
		IInstallableUnit iu1 = getIU(a);
		IInstallableUnit iu2 = getIU(b);
		if (iu1 != null && iu2 != null)
			return iu1.equals(iu2);
		String p1 = getProfileId(a);
		String p2 = getProfileId(b);
		if (p1 != null && p2 != null)
			return p1.equals(p2);
		URI r1 = getRepositoryLocation(a);
		URI r2 = getRepositoryLocation(b);
		if (r1 != null && r2 != null)
			return r1.equals(r2);
		return a.equals(b);
	}

	public int hashCode(Object element) {
		if (element instanceof CategoryElement)
			return element.hashCode();
		IInstallableUnit iu = getIU(element);
		if (iu != null)
			return iu.hashCode();
		String profileId = getProfileId(element);
		if (profileId != null)
			return profileId.hashCode();
		URI location = getRepositoryLocation(element);
		if (location != null)
			return location.hashCode();
		return element.hashCode();
	}

	private IInstallableUnit getIU(Object obj) {
		return ProvUI.getAdapter(obj, IInstallableUnit.class);
	}

	private String getProfileId(Object obj) {
		if (obj instanceof ProfileElement)
			return ((ProfileElement) obj).getLabel(obj);
		IProfile profile = ProvUI.getAdapter(obj, IProfile.class);
		if (profile == null)
			return null;
		return profile.getProfileId();
	}

	private URI getRepositoryLocation(Object obj) {
		if (obj instanceof IRepositoryElement<?>)
			return ((IRepositoryElement<?>) obj).getLocation();
		return null;
	}

}

Back to the top