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