|
18 | 18 | import java.io.File;
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.nio.file.Files;
|
| 21 | +import java.util.ArrayList; |
21 | 22 | import java.util.Iterator;
|
22 | 23 | import java.util.LinkedHashMap;
|
23 | 24 | import java.util.List;
|
24 |
| -import java.util.Objects; |
25 | 25 |
|
26 | 26 | import javax.annotation.Nullable;
|
27 | 27 |
|
28 | 28 | public class LintState {
|
29 | 29 | private final DirtyState dirtyState;
|
30 |
| - private @Nullable List<List<Lint>> lintsPerStep; |
| 30 | + private final @Nullable List<List<Lint>> lintsPerStep; |
31 | 31 |
|
32 | 32 | LintState(DirtyState dirtyState, @Nullable List<List<Lint>> lintsPerStep) {
|
33 | 33 | this.dirtyState = dirtyState;
|
@@ -64,36 +64,41 @@ public LinkedHashMap<String, List<Lint>> getLintsByStep(Formatter formatter) {
|
64 | 64 | return result;
|
65 | 65 | }
|
66 | 66 |
|
67 |
| - public void removeSuppressedLints(Formatter formatter, String relativePath, List<LintSuppression> suppressions) { |
| 67 | + public LintState withRemovedSuppressions(Formatter formatter, String relativePath, List<LintSuppression> suppressions) { |
68 | 68 | if (lintsPerStep == null) {
|
69 |
| - return; |
| 69 | + return this; |
70 | 70 | }
|
71 | 71 | if (formatter.getSteps().size() != lintsPerStep.size()) {
|
72 | 72 | throw new IllegalStateException("LintState was created with a different formatter!");
|
73 | 73 | }
|
| 74 | + boolean changed = false; |
| 75 | + ValuePerStep<List<Lint>> perStepFiltered = new ValuePerStep<>(formatter); |
74 | 76 | for (int i = 0; i < lintsPerStep.size(); i++) {
|
75 | 77 | FormatterStep step = formatter.getSteps().get(i);
|
76 |
| - List<Lint> lints = lintsPerStep.get(i); |
77 |
| - if (lints != null) { |
| 78 | + List<Lint> lintsOriginal = lintsPerStep.get(i); |
| 79 | + if (lintsOriginal != null) { |
| 80 | + List<Lint> lints = new ArrayList<>(lintsOriginal); |
78 | 81 | Iterator<Lint> iter = lints.iterator();
|
79 | 82 | while (iter.hasNext()) {
|
80 | 83 | Lint lint = iter.next();
|
81 | 84 | for (LintSuppression suppression : suppressions) {
|
82 | 85 | if (suppression.suppresses(relativePath, step, lint)) {
|
| 86 | + changed = true; |
83 | 87 | iter.remove();
|
84 | 88 | break;
|
85 | 89 | }
|
86 | 90 | }
|
87 | 91 | }
|
88 |
| - if (lints.isEmpty()) { |
89 |
| - lintsPerStep.set(i, null); |
90 |
| - if (lintsPerStep.stream().allMatch(Objects::isNull)) { |
91 |
| - lintsPerStep = null; |
92 |
| - return; |
93 |
| - } |
| 92 | + if (!lints.isEmpty()) { |
| 93 | + perStepFiltered.set(i, lints); |
94 | 94 | }
|
95 | 95 | }
|
96 | 96 | }
|
| 97 | + if (changed) { |
| 98 | + return new LintState(dirtyState, perStepFiltered.indexOfFirstValue() == -1 ? null : perStepFiltered); |
| 99 | + } else { |
| 100 | + return this; |
| 101 | + } |
97 | 102 | }
|
98 | 103 |
|
99 | 104 | public String asStringDetailed(File file, Formatter formatter) {
|
|
0 commit comments