1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.basepom.mojo.duplicatefinder;
15
16 import java.util.Collection;
17 import java.util.EnumMap;
18 import java.util.EnumSet;
19 import java.util.Map;
20 import java.util.SortedSet;
21 import java.util.stream.Collectors;
22
23 import com.google.common.base.Joiner;
24 import com.google.common.collect.ImmutableMap;
25 import com.google.common.collect.ListMultimap;
26 import com.google.common.collect.Multimap;
27 import com.google.common.collect.MultimapBuilder.SetMultimapBuilder;
28 import com.google.common.collect.Multimaps;
29 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
30
31 import static com.google.common.base.Preconditions.checkNotNull;
32
33 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
34 public class ResultCollector {
35
36 private final EnumSet<ConflictState> printState;
37 private final EnumSet<ConflictState> failState;
38
39 private final EnumMap<ConflictType, EnumSet<ConflictState>> seenResults = new EnumMap<>(ConflictType.class);
40
41 private final ListMultimap<String, ConflictResult> results = SetMultimapBuilder.linkedHashKeys()
42 .arrayListValues()
43 .build();
44
45 private ConflictState conflictState = ConflictState.NO_CONFLICT;
46
47 ResultCollector(final EnumSet<ConflictState> printState, final EnumSet<ConflictState> failState) {
48 this.printState = printState;
49 this.failState = failState;
50
51 for (ConflictType conflictType : ConflictType.values()) {
52 seenResults.put(conflictType, EnumSet.noneOf(ConflictState.class));
53 }
54 }
55
56 public ConflictState getConflictState() {
57 return conflictState;
58 }
59
60 public boolean isFailed() {
61 return failState.contains(conflictState);
62 }
63
64 public boolean hasConflictsFor(ConflictType type, ConflictState state) {
65 return seenResults.get(type).contains(state);
66 }
67
68 public void addConflict(ConflictType type, String name, SortedSet<ClasspathElement> conflictingClasspathElements, boolean excepted,
69 final ConflictState state) {
70 if (!excepted) {
71 this.conflictState = ConflictState.max(this.conflictState, state);
72
73
74 seenResults.get(type).add(state);
75 }
76
77 ConflictResult conflictResult = new ConflictResult(type, name, conflictingClasspathElements, excepted, state);
78
79 results.put(conflictResult.getConflictName(), conflictResult);
80 }
81
82 public Map<String, Collection<ConflictResult>> getResults(final ConflictType type, final ConflictState state) {
83 Multimap<String, ConflictResult> result = Multimaps.filterValues(results, conflictResult -> {
84 checkNotNull(conflictResult, "conflictResult is null");
85 return conflictResult.getConflictState() == state
86 && conflictResult.getType() == type
87 && !conflictResult.isExcepted();
88 });
89
90 return ImmutableMap.copyOf(result.asMap());
91 }
92
93 Map<String, Collection<ConflictResult>> getAllResults() {
94 return ImmutableMap.copyOf(results.asMap());
95 }
96
97 private static String buildConflictName(final SortedSet<ClasspathElement> conflictArtifactNames) {
98 return Joiner.on(", ").join(conflictArtifactNames.stream()
99 .map(ClasspathElement.getNameFunction())
100 .collect(Collectors.toList()));
101 }
102
103 public class ConflictResult {
104
105 private final ConflictType type;
106 private final String name;
107 private final SortedSet<ClasspathElement> classpathElements;
108 private final boolean excepted;
109 private final ConflictState conflictState;
110 private final String conflictName;
111
112 ConflictResult(final ConflictType type,
113 final String name,
114 final SortedSet<ClasspathElement> classpathElements,
115 final boolean excepted,
116 final ConflictState conflictState) {
117 this.type = type;
118 this.name = name;
119 this.classpathElements = classpathElements;
120 this.excepted = excepted;
121 this.conflictState = conflictState;
122 this.conflictName = ResultCollector.buildConflictName(classpathElements);
123 }
124
125 public String getName() {
126 return name;
127 }
128
129 public ConflictType getType() {
130 return type;
131 }
132
133 public SortedSet<ClasspathElement> getClasspathElements() {
134 return classpathElements;
135 }
136
137 public boolean isExcepted() {
138 return excepted;
139 }
140
141 public boolean isPrinted() {
142 return !excepted && printState.contains(conflictState);
143 }
144
145 public boolean isFailed() {
146 return !excepted && failState.contains(conflictState);
147 }
148
149 public ConflictState getConflictState() {
150 return conflictState;
151 }
152
153 private String getConflictName() {
154 return conflictName;
155 }
156 }
157 }