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