Skip to main content
summaryrefslogtreecommitdiffstats
blob: a227dbb0d94ad291afebffdf62774504bca34ddd (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*******************************************************************************
 * Copyright (c) 2015 Google Inc and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     John Glassmyer <jogl@google.com> - import group sorting is broken - https://bugs.eclipse.org/430303
 *     Lars Vogel <Lars.Vogel@vogella.com> - Contributions for
 *     						Bug 473178
 *******************************************************************************/
package org.eclipse.jdt.internal.core.dom.rewrite.imports;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

/**
 * Sorts imports according to the order of import groups defined on the Organize Imports preference
 * page. Considers equal any two imports matching the same import group.
 */
final class ImportGroupComparator implements Comparator<ImportName>{
	private static final class ImportGroup {
		private final String name;
		private final int index;
		private final ImportGroup prefix;

		public ImportGroup(String name, int index, ImportGroup prefix) {
			this.name = name;
			this.index = index;
			this.prefix = prefix;
		}

		@Override
		public String toString() {
			return String.format("ImportGroup(%d:%s)", getIndex(), getName()); //$NON-NLS-1$
		}

		String getName() {
			return this.name;
		}

		int getIndex() {
			return this.index;
		}

		ImportGroup getPrefix() {
			return this.prefix;
		}
	}

	private static final class IndexedImportGroups {
		final NavigableMap<String, ImportGroup> typeImportGroupsByName;
		final NavigableMap<String, ImportGroup> staticImportGroupByName;
//{ObjectTeams: base added:
/* orig:

		IndexedImportGroups(
				NavigableMap<String, ImportGroup> typeImportGroupsByName,
				NavigableMap<String, ImportGroup> staticImportGroupsByName) {
			this.typeImportGroupsByName = typeImportGroupsByName;
			this.staticImportGroupByName = staticImportGroupsByName;
  giro: */
		final NavigableMap<String, ImportGroup> baseImportGroupByName; // OT
		
		IndexedImportGroups(
				NavigableMap<String, ImportGroup> typeImportGroupsByName,
				NavigableMap<String, ImportGroup> staticImportGroupsByName,
				NavigableMap<String, ImportGroup> baseImportGroupsByName) { // OT
			this.typeImportGroupsByName = typeImportGroupsByName;
			this.staticImportGroupByName = staticImportGroupsByName;
			this.baseImportGroupByName = baseImportGroupsByName; // OT
// SH}
		}
	}

	private static final String MATCH_ALL = ""; //$NON-NLS-1$
	private static final String STATIC_PREFIX = "#"; //$NON-NLS-1$
	private static final String STATIC_MATCH_ALL = STATIC_PREFIX + MATCH_ALL;
//{ObjectTeams: base
	private static final String BASE_PREFIX = "%"; //$NON-NLS-1$
	private static final String BASE_MATCH_ALL = BASE_PREFIX + MATCH_ALL;
// SH}

	private static List<String> memoizedImportOrder = null;
	private static IndexedImportGroups memoizedIndexedImportGroups = null;

	private static List<String> includeMatchAllImportGroups(List<String> importOrder) {
		boolean needsTypeMatchAll = !importOrder.contains(MATCH_ALL);
		boolean needsStaticMatchAll = !importOrder.contains(STATIC_MATCH_ALL);
//{ObjectTeams: base
/* orig:

		if (!needsTypeMatchAll && !needsStaticMatchAll) {
  :giro */
		boolean needsBaseMatchAll = !importOrder.contains(BASE_MATCH_ALL);
		
		if (!needsTypeMatchAll && !needsStaticMatchAll) {
// SH}
			return importOrder;
		}

		List<String> augmentedOrder = new ArrayList<>(importOrder.size() + 2);

		if (needsStaticMatchAll) {
			augmentedOrder.add(STATIC_MATCH_ALL);
		}

		augmentedOrder.addAll(importOrder);

		if (needsTypeMatchAll) {
			augmentedOrder.add(MATCH_ALL);
		}

//{ObjectTeams: by default, base imports come last:
		if (needsBaseMatchAll) {
			augmentedOrder.add(BASE_MATCH_ALL);
		}
// SH}
		return augmentedOrder;
	}

	private static synchronized IndexedImportGroups indexImportOrder(List<String> importOrder) {
		if (importOrder.equals(memoizedImportOrder)) {
			return memoizedIndexedImportGroups;
		}

		Map<String, Integer> typeGroupsAndIndices = new HashMap<>();
		Map<String, Integer> staticGroupsAndIndices = new HashMap<>();
//{ObjectTeams: base
		Map<String, Integer> baseGroupsAndIndices = new HashMap<>();
// SH}
		for (int i = 0; i < importOrder.size(); i++) {
			String importGroupString = importOrder.get(i);

			final Map<String, Integer> groupsAndIndices;
			if (importGroupString.startsWith(STATIC_PREFIX)) {
				groupsAndIndices = staticGroupsAndIndices;
				importGroupString = importGroupString.substring(1);
//{ObjectTeams: base
			} else if (importGroupString.startsWith(BASE_PREFIX)) {
				groupsAndIndices = baseGroupsAndIndices;
				importGroupString = importGroupString.substring(1);
// SH}
			} else {
				groupsAndIndices = typeGroupsAndIndices;
			}

			groupsAndIndices.put(importGroupString, i);
		}

		memoizedImportOrder = importOrder;

		memoizedIndexedImportGroups = new IndexedImportGroups(
				mapImportGroups(typeGroupsAndIndices),
//{ObjectTeams: base
/* orig:
				mapImportGroups(staticGroupsAndIndices));
  giro: */
				mapImportGroups(staticGroupsAndIndices),
				mapImportGroups(baseGroupsAndIndices));
// SH}

		return memoizedIndexedImportGroups;
	}

	private static NavigableMap<String, ImportGroup> mapImportGroups(Map<String, Integer> importGroupNamesAndIndices) {
		if (importGroupNamesAndIndices.isEmpty()) {
			importGroupNamesAndIndices = Collections.singletonMap(MATCH_ALL, 0);
		}

		List<String> sortedNames = new ArrayList<>(importGroupNamesAndIndices.keySet());
		Collections.sort(sortedNames);

		ArrayList<ImportGroup> importGroups = new ArrayList<>(sortedNames.size());

		Deque<ImportGroup> prefixingGroups = new ArrayDeque<>();
		for (String name : sortedNames) {
			while (!prefixingGroups.isEmpty()
					&& !isWholeSegmentPrefix(prefixingGroups.getLast().getName(), name)) {
				prefixingGroups.removeLast();
			}
			ImportGroup prefix = prefixingGroups.peekLast();

			ImportGroup group = new ImportGroup(name, importGroupNamesAndIndices.get(name), prefix);

			importGroups.add(group);

			prefixingGroups.addLast(group);
		}

		NavigableMap<String, ImportGroup> groupsByName = new TreeMap<>();
		for (ImportGroup group : importGroups) {
			groupsByName.put(group.getName(), group);
		}

		return groupsByName;
	}

	private static boolean isWholeSegmentPrefix(String prefix, String name) {
		if (!name.startsWith(prefix)) {
			return false;
		}

		return prefix.isEmpty() || name.length() == prefix.length() || name.charAt(prefix.length()) == '.';
	}

	private final IndexedImportGroups indexedImportGroups;

	ImportGroupComparator(List<String> importOrder) {
		List<String> importOrderWithMatchAllGroups = includeMatchAllImportGroups(importOrder);
		this.indexedImportGroups = indexImportOrder(importOrderWithMatchAllGroups);
	}

	@Override
	public int compare(ImportName o1, ImportName o2) {
		return determineSortPosition(o1) - determineSortPosition(o2);
	}

	private int determineSortPosition(ImportName importName) {
		String name = (importName.isOnDemand() ? importName.containerName : importName.qualifiedName);

		NavigableMap<String, ImportGroup> groupsByName = importName.isStatic
				? this.indexedImportGroups.staticImportGroupByName
						: this.indexedImportGroups.typeImportGroupsByName;
//{ObjectTeams: base
		if (importName.isBase)
			groupsByName = this.indexedImportGroups.baseImportGroupByName;
// SH}

		ImportGroup prefixingGroup = groupsByName.floorEntry(name).getValue();
		while (!isWholeSegmentPrefix(prefixingGroup.getName(), name)) {
			prefixingGroup = prefixingGroup.getPrefix();
		}

		return prefixingGroup.getIndex();
	}
}

Back to the top