Skip to content

Commit 5d365b3

Browse files
committed
Made ImportSorter* ready for incremental builds.
1 parent 24e93f6 commit 5d365b3

File tree

18 files changed

+256
-109
lines changed

18 files changed

+256
-109
lines changed

lib/src/main/java/com/diffplug/spotless/generic/CustomReplaceRegexStep.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private static final class State implements Serializable {
3434
private final Pattern regex;
3535
private final String replacement;
3636

37-
private State(Pattern regex, String replacement) {
37+
State(Pattern regex, String replacement) {
3838
this.regex = regex;
3939
this.replacement = replacement;
4040
}

lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ public static FormatterStep create(Type type, int numSpacesPerTab) {
4141
new State(type, numSpacesPerTab), State::toFormatter);
4242
}
4343

44-
static class State implements Serializable {
44+
private static class State implements Serializable {
4545
private static final long serialVersionUID = 1L;
4646

4747
final Type type;
4848
final int numSpacesPerTab;
4949

50-
public State(Type type, int numSpacesPerTab) {
50+
State(Type type, int numSpacesPerTab) {
5151
this.type = type;
5252
this.numSpacesPerTab = numSpacesPerTab;
5353
}
5454

55-
public FormatterFunc toFormatter() {
55+
FormatterFunc toFormatter() {
5656
return new Runtime(this)::format;
5757
}
5858
}

lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public final class LicenseHeaderStep implements Serializable {
3131
private static final long serialVersionUID = 1L;
3232

3333
// TODO: Make private and only allow access through a public static method?
34-
public static final String NAME = "LicenseHeader";
34+
public static final String NAME = "licenseHeader";
3535

3636
private final String licenseHeader;
3737
private final Pattern delimiterPattern;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.java;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.Serializable;
21+
import java.io.UncheckedIOException;
22+
import java.nio.file.Files;
23+
import java.util.AbstractMap.SimpleImmutableEntry;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Objects;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
30+
31+
import com.diffplug.spotless.FormatterFunc;
32+
import com.diffplug.spotless.FormatterStep;
33+
34+
public final class ImportOrderStep {
35+
private ImportOrderStep() {}
36+
37+
public static FormatterStep createFromOrder(List<String> importOrder) {
38+
Objects.requireNonNull(importOrder);
39+
return importOrder instanceof Serializable
40+
? createFromOrderImpl(importOrder)
41+
: createFromOrderImpl(new ArrayList<>(importOrder));
42+
}
43+
44+
public static FormatterStep createFromFile(File importsFile) {
45+
Objects.requireNonNull(importsFile);
46+
return createFromOrderImpl(getImportOrder(importsFile));
47+
}
48+
49+
private static FormatterStep createFromOrderImpl(List<String> importOrder) {
50+
return FormatterStep.createLazy("importOrder",
51+
() -> new State(importOrder),
52+
State::toFormatter);
53+
}
54+
55+
private static List<String> getImportOrder(File importsFile) {
56+
try (Stream<String> lines = Files.lines(importsFile.toPath())) {
57+
return lines.filter(line -> !line.startsWith("#"))
58+
// parse 0=input
59+
.map(ImportOrderStep::splitIntoIndexAndName)
60+
.sorted(Map.Entry.comparingByKey())
61+
.map(Map.Entry::getValue)
62+
.collect(Collectors.toCollection(ArrayList::new));
63+
} catch (IOException e) {
64+
throw new UncheckedIOException(e);
65+
}
66+
}
67+
68+
private static Map.Entry<Integer, String> splitIntoIndexAndName(String line) {
69+
String[] pieces = line.split("=");
70+
Integer index = Integer.valueOf(pieces[0]);
71+
String name = pieces.length == 2 ? pieces[1] : "";
72+
return new SimpleImmutableEntry<>(index, name);
73+
}
74+
75+
private static final class State implements Serializable {
76+
private static final long serialVersionUID = 1L;
77+
78+
private final List<String> importOrder;
79+
80+
State(List<String> importOrder) {
81+
this.importOrder = importOrder;
82+
}
83+
84+
FormatterFunc toFormatter() {
85+
return raw -> new ImportSorter(importOrder).format(raw);
86+
}
87+
}
88+
}

lib/src/main/java/com/diffplug/spotless/java/ImportSorterStep.java renamed to lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java

+4-40
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,25 @@
1515
*/
1616
package com.diffplug.spotless.java;
1717

18-
import static java.util.stream.Collectors.toCollection;
19-
20-
import java.io.File;
21-
import java.io.IOException;
22-
import java.nio.file.Files;
23-
import java.util.AbstractMap.SimpleImmutableEntry;
2418
import java.util.ArrayList;
2519
import java.util.List;
26-
import java.util.Map;
27-
import java.util.Objects;
2820
import java.util.Scanner;
29-
import java.util.stream.Stream;
3021

3122
/**
32-
* From https://github.com/krasa/EclipseCodeFormatter
33-
*
3423
* @author Vojtech Krasa
3524
*/
36-
public final class ImportSorterStep {
37-
// TODO: Make private and only allow access through a public static method?
38-
public static final String NAME = "ImportSorter";
39-
25+
// Based on ImportSorterStep from https://github.com/krasa/EclipseCodeFormatter,
26+
// which itself is licensed under the Apache 2.0 license.
27+
final class ImportSorter {
4028
private static final int START_INDEX_OF_IMPORTS_PACKAGE_DECLARATION = 7;
4129
static final String N = "\n";
4230

4331
private final List<String> importsOrder;
4432

45-
public static ImportSorterStep of(List<String> importsOrder) {
46-
return new ImportSorterStep(Objects.requireNonNull(importsOrder));
47-
}
48-
49-
public static ImportSorterStep fromFile(File importsFile) throws IOException {
50-
Objects.requireNonNull(importsFile);
51-
try (Stream<String> lines = Files.lines(importsFile.toPath())) {
52-
List<String> importsOrder = lines.filter(line -> !line.startsWith("#"))
53-
// parse 0=input
54-
.map(ImportSorterStep::splitIntoIndexAndName)
55-
.sorted(Map.Entry.comparingByKey())
56-
.map(Map.Entry::getValue)
57-
.collect(toCollection(ArrayList::new));
58-
return new ImportSorterStep(importsOrder);
59-
}
60-
}
61-
62-
private ImportSorterStep(List<String> importsOrder) {
33+
ImportSorter(List<String> importsOrder) {
6334
this.importsOrder = new ArrayList<>(importsOrder);
6435
}
6536

66-
private static Map.Entry<Integer, String> splitIntoIndexAndName(String line) {
67-
String[] pieces = line.split("=");
68-
Integer index = Integer.valueOf(pieces[0]);
69-
String name = pieces.length == 2 ? pieces[1] : "";
70-
return new SimpleImmutableEntry<>(index, name);
71-
}
72-
7337
public String format(String raw) {
7438
// parse file
7539
Scanner scanner = new Scanner(raw);

lib/src/main/java/com/diffplug/spotless/java/ImportSorterImpl.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
import java.util.Map;
2424
import java.util.Set;
2525

26-
// From https://github.com/krasa/EclipseCodeFormatter
2726
/*not thread safe*/
27+
// Based on ImportSorterImpl from https://github.com/krasa/EclipseCodeFormatter,
28+
// which itself is licensed under the Apache 2.0 license.
2829
final class ImportSorterImpl {
2930

3031
private final List<String> template = new ArrayList<>();
@@ -136,7 +137,7 @@ private void mergeNotMatchingItems(boolean staticItems) {
136137
// no order is specified
137138
if (template.size() > 0 && (template.get(template.size() - 1).startsWith("static"))) {
138139
// insert N after last static import
139-
template.add(ImportSorterStep.N);
140+
template.add(ImportSorter.N);
140141
}
141142
template.add(notMatchingItem);
142143
} else {
@@ -194,23 +195,23 @@ private void mergeMatchingItems() {
194195
// replace order item by matching import statements
195196
// this is a mess and it is only a luck that it works :-]
196197
template.remove(i);
197-
if (i != 0 && !template.get(i - 1).equals(ImportSorterStep.N)) {
198-
template.add(i, ImportSorterStep.N);
198+
if (i != 0 && !template.get(i - 1).equals(ImportSorter.N)) {
199+
template.add(i, ImportSorter.N);
199200
i++;
200201
}
201-
if (i + 1 < template.size() && !template.get(i + 1).equals(ImportSorterStep.N)
202-
&& !template.get(i).equals(ImportSorterStep.N)) {
203-
template.add(i, ImportSorterStep.N);
202+
if (i + 1 < template.size() && !template.get(i + 1).equals(ImportSorter.N)
203+
&& !template.get(i).equals(ImportSorter.N)) {
204+
template.add(i, ImportSorter.N);
204205
}
205206
template.addAll(i, matchingItems);
206-
if (i != 0 && !template.get(i - 1).equals(ImportSorterStep.N)) {
207-
template.add(i, ImportSorterStep.N);
207+
if (i != 0 && !template.get(i - 1).equals(ImportSorter.N)) {
208+
template.add(i, ImportSorter.N);
208209
}
209210

210211
}
211212
}
212213
// if there is \n on the end, remove it
213-
if (template.size() > 0 && template.get(template.size() - 1).equals(ImportSorterStep.N)) {
214+
if (template.size() > 0 && template.get(template.size() - 1).equals(ImportSorter.N)) {
214215
template.remove(template.size() - 1);
215216
}
216217
}
@@ -219,10 +220,10 @@ private List<String> getResult() {
219220
List<String> strings = new ArrayList<>();
220221

221222
for (String s : template) {
222-
if (s.equals(ImportSorterStep.N)) {
223+
if (s.equals(ImportSorter.N)) {
223224
strings.add(s);
224225
} else {
225-
strings.add("import " + s + ";" + ImportSorterStep.N);
226+
strings.add("import " + s + ";" + ImportSorter.N);
226227
}
227228
}
228229
return strings;

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import com.diffplug.spotless.extra.java.EclipseFormatterStep;
2727
import com.diffplug.spotless.generic.LicenseHeaderStep;
2828
import com.diffplug.spotless.java.GoogleJavaFormatStep;
29-
import com.diffplug.spotless.java.ImportSorterStep;
29+
import com.diffplug.spotless.java.ImportOrderStep;
3030

3131
public class JavaExtension extends FormatExtension {
3232
public static final String NAME = "java";
@@ -46,11 +46,11 @@ public void licenseHeaderFile(Object licenseHeaderFile) {
4646
}
4747

4848
public void importOrder(List<String> importOrder) {
49-
customLazy(ImportSorterStep.NAME, () -> ImportSorterStep.of(importOrder)::format);
49+
addStep(ImportOrderStep.createFromOrder(importOrder));
5050
}
5151

5252
public void importOrderFile(Object importOrderFile) {
53-
customLazy(ImportSorterStep.NAME, () -> ImportSorterStep.fromFile(getProject().file(importOrderFile))::format);
53+
addStep(ImportOrderStep.createFromFile(getProject().file(importOrderFile)));
5454
}
5555

5656
public void eclipseFormatFile(Object eclipseFormatFile) {

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ImportSorterStepTest.java

-49
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless;
17+
18+
import java.util.AbstractList;
19+
import java.util.Arrays;
20+
import java.util.Objects;
21+
22+
public final class NonSerializableList extends AbstractList<String> {
23+
private final String[] elements;
24+
25+
public static NonSerializableList of(String... elements) {
26+
Objects.requireNonNull(elements);
27+
return new NonSerializableList(elements);
28+
}
29+
30+
private NonSerializableList(String[] elements) {
31+
this.elements = Arrays.copyOf(elements, elements.length);
32+
}
33+
34+
@Override
35+
public String get(int index) {
36+
if (index < 0 || index >= elements.length) {
37+
throw new IndexOutOfBoundsException(String.format("index: %s, size: %s", index, elements.length));
38+
}
39+
return elements[index];
40+
}
41+
42+
@Override
43+
public int size() {
44+
return elements.length;
45+
}
46+
}

0 commit comments

Comments
 (0)