1
1
/*
2
- * Copyright 2016 DiffPlug
2
+ * Copyright 2016-2021 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package com .diffplug .spotless .java ;
17
17
18
- import java .util .ArrayList ;
19
- import java .util .Collections ;
20
- import java .util .HashMap ;
21
- import java .util .HashSet ;
22
- import java .util .List ;
23
- import java .util .Map ;
24
- import java .util .Set ;
18
+ import java .util .*;
25
19
26
20
import javax .annotation .Nullable ;
27
21
@@ -34,9 +28,10 @@ final class ImportSorterImpl {
34
28
private final Map <String , List <String >> matchingImports = new HashMap <>();
35
29
private final List <String > notMatching = new ArrayList <>();
36
30
private final Set <String > allImportOrderItems = new HashSet <>();
31
+ private final Comparator <String > ordering ;
37
32
38
- static List <String > sort (List <String > imports , List <String > importsOrder , String lineFormat ) {
39
- ImportSorterImpl importsSorter = new ImportSorterImpl (importsOrder );
33
+ static List <String > sort (List <String > imports , List <String > importsOrder , boolean wildcardsLast , String lineFormat ) {
34
+ ImportSorterImpl importsSorter = new ImportSorterImpl (importsOrder , wildcardsLast );
40
35
return importsSorter .sort (imports , lineFormat );
41
36
}
42
37
@@ -49,11 +44,12 @@ private List<String> sort(List<String> imports, String lineFormat) {
49
44
return getResult (lineFormat );
50
45
}
51
46
52
- private ImportSorterImpl (List <String > importOrder ) {
47
+ private ImportSorterImpl (List <String > importOrder , boolean wildcardsLast ) {
53
48
List <String > importOrderCopy = new ArrayList <>(importOrder );
54
49
normalizeStaticOrderItems (importOrderCopy );
55
50
putStaticItemIfNotExists (importOrderCopy );
56
51
template .addAll (importOrderCopy );
52
+ ordering = new OrderingComparator (wildcardsLast );
57
53
this .allImportOrderItems .addAll (importOrderCopy );
58
54
}
59
55
@@ -119,7 +115,7 @@ private void filterMatchingImports(List<String> imports) {
119
115
* not matching means it does not match any order item, so it will be appended before or after order items
120
116
*/
121
117
private void mergeNotMatchingItems (boolean staticItems ) {
122
- Collections . sort (notMatching );
118
+ sort (notMatching );
123
119
124
120
int firstIndexOfOrderItem = getFirstIndexOfOrderItem (notMatching , staticItems );
125
121
int indexOfOrderItem = 0 ;
@@ -192,7 +188,7 @@ private void mergeMatchingItems() {
192
188
continue ;
193
189
}
194
190
List <String > matchingItems = new ArrayList <>(strings );
195
- Collections . sort (matchingItems );
191
+ sort (matchingItems );
196
192
197
193
// replace order item by matching import statements
198
194
// this is a mess and it is only a luck that it works :-]
@@ -218,6 +214,10 @@ private void mergeMatchingItems() {
218
214
}
219
215
}
220
216
217
+ private void sort (List <String > items ) {
218
+ items .sort (ordering );
219
+ }
220
+
221
221
private List <String > getResult (String lineFormat ) {
222
222
List <String > strings = new ArrayList <>();
223
223
@@ -256,4 +256,28 @@ private List<String> getResult(String lineFormat) {
256
256
return null ;
257
257
}
258
258
259
+ private static class OrderingComparator implements Comparator <String > {
260
+ private final boolean wildcardsLast ;
261
+
262
+ private OrderingComparator (boolean wildcardsLast ) {
263
+ this .wildcardsLast = wildcardsLast ;
264
+ }
265
+
266
+ @ Override
267
+ public int compare (String string1 , String string2 ) {
268
+ int string1WildcardIndex = string1 .indexOf ('*' );
269
+ int string2WildcardIndex = string2 .indexOf ('*' );
270
+ boolean string1IsWildcard = string1WildcardIndex >= 0 ;
271
+ boolean string2IsWildcard = string2WildcardIndex >= 0 ;
272
+ if (string1IsWildcard == string2IsWildcard ) {
273
+ return string1 .compareTo (string2 );
274
+ }
275
+ int prefixLength = string1IsWildcard ? string1WildcardIndex : string2WildcardIndex ;
276
+ boolean samePrefix = string1 .regionMatches (0 , string2 , 0 , prefixLength );
277
+ if (!samePrefix ) {
278
+ return string1 .compareTo (string2 );
279
+ }
280
+ return (string1IsWildcard == wildcardsLast ) ? 1 : -1 ;
281
+ }
282
+ }
259
283
}
0 commit comments