1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.duplicatefinder.classpath;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18 import static java.lang.String.format;
19
20 import org.basepom.mojo.duplicatefinder.artifact.MavenCoordinates;
21
22 import java.util.Collection;
23 import java.util.List;
24
25 import com.google.common.base.Predicate;
26 import com.google.common.collect.ImmutableList;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class MatchArtifactPredicate implements Predicate<Artifact> {
33
34 private static final Logger LOG = LoggerFactory.getLogger(MatchArtifactPredicate.class);
35
36 private final List<MavenCoordinates> mavenCoordinates;
37
38 MatchArtifactPredicate(final Collection<MavenCoordinates> dependencies) {
39 this.mavenCoordinates = ImmutableList.copyOf(checkNotNull(dependencies, "dependencies is null"));
40 }
41
42 @Override
43 public boolean apply(final Artifact artifact) {
44 if (artifact != null) {
45 for (final MavenCoordinates mavenCoordinate : mavenCoordinates) {
46 try {
47 if (mavenCoordinate.matches(artifact)) {
48 LOG.debug(format("Ignoring artifact '%s' (matches %s)", artifact, mavenCoordinate));
49 return true;
50 }
51 } catch (final OverConstrainedVersionException e) {
52 LOG.warn(format("Caught '%s' while comparing '%s' to '%s'", e.getMessage(), mavenCoordinate, artifact));
53 }
54 }
55 }
56 return false;
57 }
58 }