1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.duplicatefinder;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18 import static com.google.common.base.Preconditions.checkState;
19 import static com.google.common.base.Strings.nullToEmpty;
20 import static org.basepom.mojo.duplicatefinder.artifact.ArtifactHelper.isJarArtifact;
21 import static org.basepom.mojo.duplicatefinder.artifact.ArtifactHelper.isTestArtifact;
22
23 import java.io.File;
24 import java.util.Objects;
25 import java.util.function.Function;
26
27 import com.google.common.base.Joiner;
28 import com.google.common.collect.Ordering;
29 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
30 import org.apache.maven.artifact.Artifact;
31
32
33
34
35
36 public abstract class ClasspathElement implements Comparable<ClasspathElement> {
37
38 public abstract String getName();
39
40 public File getFile() {
41 throw new UnsupportedOperationException();
42 }
43
44 public Artifact getArtifact() {
45 throw new UnsupportedOperationException();
46 }
47
48 public boolean isLocalFolder() {
49 return false;
50 }
51
52 public boolean hasArtifact() {
53 return false;
54 }
55
56 @Override
57 public int compareTo(ClasspathElement element) {
58 return Ordering.natural().compare(this.getName(), element.getName());
59 }
60
61 public static final class ClasspathArtifact extends ClasspathElement {
62
63 private final Artifact artifact;
64
65 public ClasspathArtifact(final Artifact artifact) {
66 this.artifact = checkNotNull(artifact, "artifact is null");
67 }
68
69 @Override
70 public String getName() {
71 return Joiner.on(':').skipNulls().join(artifact.getGroupId(),
72 artifact.getArtifactId(),
73 artifact.getVersion(),
74 getType(artifact),
75 getClassifier(artifact));
76 }
77
78 private String getType(final Artifact artifact) {
79 if (isJarArtifact(artifact)) {
80
81
82
83 return nullToEmpty(artifact.getClassifier()).isEmpty() ? null : "jar";
84 }
85
86 return artifact.getType();
87 }
88
89 private String getClassifier(final Artifact artifact) {
90 if (nullToEmpty(artifact.getClassifier()).isEmpty()) {
91 return null;
92 } else if (isTestArtifact(artifact)) {
93 return "tests";
94 }
95
96 return artifact.getClassifier();
97 }
98
99 @Override
100 public boolean hasArtifact() {
101 return true;
102 }
103
104 @Override
105 @SuppressFBWarnings("EI_EXPOSE_REP")
106 public Artifact getArtifact() {
107 return artifact;
108 }
109
110 @Override
111 public int hashCode() {
112 return Objects.hash(artifact);
113 }
114
115 @Override
116 public boolean equals(Object o) {
117 if (o == null || o.getClass() != this.getClass()) {
118 return false;
119 }
120
121 if (o == this) {
122 return true;
123 }
124
125 ClasspathArtifact that = (ClasspathArtifact) o;
126
127 return Objects.equals(this.artifact, that.artifact);
128 }
129 }
130
131 public static final class ClasspathLocalFolder extends ClasspathElement {
132
133 private final File localFolder;
134
135 public ClasspathLocalFolder(final File localFolder) {
136 this.localFolder = checkNotNull(localFolder, "localFolder is null");
137 checkState(localFolder.isDirectory(), "localFolder must be a directory");
138 }
139
140 @Override
141 public String getName() {
142 return localFolder.getAbsolutePath();
143 }
144
145 @Override
146 public boolean isLocalFolder() {
147 return true;
148 }
149
150 @Override
151 public File getFile() {
152 return localFolder;
153 }
154
155 @Override
156 public int hashCode() {
157 return Objects.hashCode(localFolder);
158 }
159
160 @Override
161 public boolean equals(Object o) {
162 if (o == null || o.getClass() != this.getClass()) {
163 return false;
164 }
165
166 if (o == this) {
167 return true;
168 }
169
170 ClasspathLocalFolder that = (ClasspathLocalFolder) o;
171
172 return Objects.equals(this.localFolder, that.localFolder);
173 }
174 }
175
176 public static Function<ClasspathElement, String> getNameFunction() {
177 return ClasspathElement::getName;
178 }
179 }