Skip to content

Commit 7b65d9a

Browse files
committed
Swap key and value in map
Use Map.entry() instead of AbstractMap.SimpleEntry()
1 parent 0f2e67f commit 7b65d9a

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.nio.charset.StandardCharsets;
2323
import java.nio.file.Files;
2424
import java.nio.file.Path;
25-
import java.util.AbstractMap;
2625
import java.util.List;
2726
import java.util.ListIterator;
2827
import java.util.Map;
@@ -237,19 +236,19 @@ private void addIntendedLine(String indent, String line) {
237236
* sequence (\n, \r, \r\n).
238237
*/
239238
private String diff(File file) throws IOException {
240-
return diff(formatter, file).getKey();
239+
return diff(formatter, file).getValue();
241240
}
242241

243242
/**
244-
* Returns a map entry with key being a git-style diff between the contents of the given file and what those contents would
243+
* Returns a map entry with value being a git-style diff between the contents of the given file and what those contents would
245244
* look like if formatted using the given formatter. Does not end with any newline
246-
* sequence (\n, \r, \r\n). The value of the map entry is the line where the first difference occurred.
245+
* sequence (\n, \r, \r\n). The key of the map entry is the 0-based line where the first difference occurred.
247246
*/
248-
public static Map.Entry<String, Integer> diff(Formatter formatter, File file) throws IOException {
247+
public static Map.Entry<Integer, String> diff(Formatter formatter, File file) throws IOException {
249248
return diff(new CleanProviderFormatter(formatter), file);
250249
}
251250

252-
private static Map.Entry<String, Integer> diff(CleanProvider formatter, File file) throws IOException {
251+
private static Map.Entry<Integer, String> diff(CleanProvider formatter, File file) throws IOException {
253252
String raw = new String(Files.readAllBytes(file.toPath()), formatter.getEncoding());
254253
String rawUnix = LineEnding.toUnix(raw);
255254
String formatted = formatter.getFormatted(file, rawUnix);
@@ -264,13 +263,13 @@ private static Map.Entry<String, Integer> diff(CleanProvider formatter, File fil
264263
}
265264

266265
/**
267-
* Returns a map entry with key being a git-style diff between the two unix strings and value being the line of the first difference (in the dirty string)
266+
* Returns a map entry with value being a git-style diff between the two unix strings and key being the 0-based line of the first difference (in the dirty string)
268267
* <p>
269268
* Output has no trailing newlines.
270269
* <p>
271270
* Boolean args determine whether whitespace or line endings will be visible.
272271
*/
273-
private static Map.Entry<String, Integer> diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
272+
private static Map.Entry<Integer, String> diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
274273
dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings);
275274
clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings);
276275

@@ -287,7 +286,7 @@ private static Map.Entry<String, Integer> diffWhitespaceLineEndings(String dirty
287286

288287
// we don't need the diff to show this, since we display newlines ourselves
289288
formatted = formatted.replace("\\ No newline at end of file\n", "");
290-
return new AbstractMap.SimpleEntry<>(NEWLINE_MATCHER.trimTrailingFrom(formatted), getLineOfFirstDifference(edits));
289+
return Map.entry(getLineOfFirstDifference(edits), NEWLINE_MATCHER.trimTrailingFrom(formatted));
291290
}
292291

293292
private static int getLineOfFirstDifference(EditList edits) {

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
5757
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
5858
problemFiles.add(file);
5959
if (buildContext.isIncremental()) {
60-
Map.Entry<String, Integer> diffEntry = DiffMessageFormatter.diff(formatter, file);
61-
buildContext.addMessage(file, diffEntry.getValue() + 1, 0, diffEntry.getKey(), BuildContext.SEVERITY_ERROR, null);
60+
Map.Entry<Integer, String> diffEntry = DiffMessageFormatter.diff(formatter, file);
61+
buildContext.addMessage(file, diffEntry.getKey() + 1, 0, diffEntry.getValue(), BuildContext.SEVERITY_ERROR, null);
6262
}
6363
counter.cleaned();
6464
} else {

0 commit comments

Comments
 (0)