1
1
/*
2
- * Copyright 2016-2023 DiffPlug
2
+ * Copyright 2016-2024 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.
@@ -33,6 +33,7 @@ final class ImportSorterImpl {
33
33
private static final String SUBGROUP_SEPARATOR = "|" ;
34
34
35
35
private final List <ImportsGroup > importsGroups ;
36
+ private final Set <String > knownGroupings = new HashSet <>();
36
37
private final Map <String , List <String >> matchingImports = new HashMap <>();
37
38
private final List <String > notMatching = new ArrayList <>();
38
39
private final Set <String > allImportOrderItems = new HashSet <>();
@@ -44,10 +45,12 @@ private static class ImportsGroup {
44
45
45
46
private final List <String > subGroups ;
46
47
47
- public ImportsGroup (String importOrder ) {
48
+ public ImportsGroup (String importOrder , Set < String > knownGroupings ) {
48
49
this .subGroups = Stream .of (importOrder .split ("\\ " + SUBGROUP_SEPARATOR , -1 ))
49
50
.map (this ::normalizeStatic )
51
+ .filter (group -> !knownGroupings .contains (group ))
50
52
.collect (Collectors .toList ());
53
+ knownGroupings .addAll (this .subGroups );
51
54
}
52
55
53
56
private String normalizeStatic (String subgroup ) {
@@ -80,7 +83,7 @@ private List<String> sort(List<String> imports, String lineFormat) {
80
83
81
84
private ImportSorterImpl (List <String > importOrder , boolean wildcardsLast , boolean semanticSort ,
82
85
Set <String > treatAsPackage , Set <String > treatAsClass ) {
83
- importsGroups = importOrder .stream ().filter (Objects ::nonNull ).map (ImportsGroup :: new ).collect (Collectors .toList ());
86
+ importsGroups = importOrder .stream ().filter (Objects ::nonNull ).map (order -> new ImportsGroup ( order , knownGroupings ) ).collect (Collectors .toList ());
84
87
putStaticItemIfNotExists (importsGroups );
85
88
putCatchAllGroupIfNotExists (importsGroups );
86
89
@@ -107,13 +110,13 @@ private void putStaticItemIfNotExists(List<ImportsGroup> importsGroups) {
107
110
indexOfFirstStatic = i ;
108
111
}
109
112
}
110
- importsGroups .add (indexOfFirstStatic , new ImportsGroup (STATIC_KEYWORD ));
113
+ importsGroups .add (indexOfFirstStatic , new ImportsGroup (STATIC_KEYWORD , this . knownGroupings ));
111
114
}
112
115
113
116
private void putCatchAllGroupIfNotExists (List <ImportsGroup > importsGroups ) {
114
117
boolean catchAllSubGroupExist = importsGroups .stream ().anyMatch (group -> group .getSubGroups ().contains (CATCH_ALL_SUBGROUP ));
115
118
if (!catchAllSubGroupExist ) {
116
- importsGroups .add (new ImportsGroup (CATCH_ALL_SUBGROUP ));
119
+ importsGroups .add (new ImportsGroup (CATCH_ALL_SUBGROUP , this . knownGroupings ));
117
120
}
118
121
}
119
122
0 commit comments