Skip to main content
summaryrefslogtreecommitdiffstats
blob: b390daa40a200da24e5eac20a0b1a7274b90ee49 (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
/*******************************************************************************
 * Copyright (c) 2016 Red Hat Inc. 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:
 *   Mickael Istria (Red Hat Inc.) - initial implementation
 *******************************************************************************/
package org.eclipse.ui.internal.genericeditor;

import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.Assert;
import org.eclipse.ui.internal.genericeditor.TextHoverRegistry.TextHoverExtension;

/**
 * A comparator that allows to sort elements according to their relative
 * placement (isBefore and isAfter)
 *
 */
class OrderedExtensionComparator implements Comparator<TextHoverExtension> {

	private Map<String, TextHoverExtension> extensionsById;

	public OrderedExtensionComparator(Collection<TextHoverExtension> extensions) {
		Assert.isNotNull(extensions);
		this.extensionsById = extensions.stream().collect(Collectors.toMap(TextHoverExtension::getId, Function.identity()));
	}

	@Override
	public int compare(TextHoverExtension arg0, TextHoverExtension arg1) {
		if (isDeclaredAsBefore(arg0, arg1) || isDeclaredAsAfter(arg1, arg0)) {
			return -1;
		}
		if (isDeclaredAsAfter(arg0, arg1) || isDeclaredAsBefore(arg1, arg0)) {
			return +1;
		}
		return arg0.toString().compareTo(arg1.toString());
	}

	private boolean isDeclaredAsBefore(TextHoverExtension arg0, TextHoverExtension arg1) {
		String before0 = arg0.getIsBefore();
		if (before0 == null) {
			return false;
		}
		if ("*".equals(before0)) {
			return true;
		}
		String id1 = arg1.getId();
		if (id1 == null) {
			return false; 
		}
		if (before0.equals(id1)) {
			return true;
		}
		String after1 = arg1.getIsAfter();
		if (after1 == null) {
			return false;
		}
		return isDeclaredAsAfter(arg0, this.extensionsById.get(after1));
	}

	private boolean isDeclaredAsAfter(TextHoverExtension arg0, TextHoverExtension arg1) {
		String after0 = arg0.getIsAfter();
		if (after0 == null) {
			return false;
		}
		if ("*".equals(after0)) {
			return true;
		}
		String id1 = arg1.getId();
		if (id1 == null) {
			return false; 
		}
		if (after0.equals(id1)) {
			return true;
		}
		String before1 = arg1.getIsBefore();
		if (before1 == null) {
			return false;
		}
		return isDeclaredAsAfter(arg0, this.extensionsById.get(before1));
	}

}

Back to the top