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.inline.mojo;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  import static com.google.common.base.Preconditions.checkState;
19  
20  import java.util.List;
21  import java.util.Objects;
22  import java.util.Optional;
23  
24  import com.google.common.base.Splitter;
25  import org.apache.maven.artifact.Artifact;
26  import org.eclipse.aether.graph.Dependency;
27  import org.eclipse.aether.graph.DependencyNode;
28  
29  public final class ArtifactIdentifier {
30  
31      private final String artifactId;
32      private final String groupId;
33  
34      public ArtifactIdentifier(String artifact) {
35          checkNotNull(artifact, "artifact is null");
36          List<String> elements = Splitter.on(':').trimResults().splitToList(artifact);
37          checkState(elements.size() == 2, "artifact format is <groupId>:<artifactId> (got %s)", artifact);
38          this.groupId = elements.get(0);
39          this.artifactId = elements.get(1);
40      }
41  
42      public ArtifactIdentifier(Dependency dependency) {
43          checkNotNull(dependency, "dependency is null");
44  
45          this.groupId = dependency.getArtifact().getGroupId();
46          this.artifactId = dependency.getArtifact().getArtifactId();
47      }
48  
49      public ArtifactIdentifier(Artifact artifact) {
50          this.groupId = artifact.getGroupId();
51          this.artifactId = artifact.getArtifactId();
52      }
53  
54      public ArtifactIdentifier(DependencyNode dependencyNode) {
55          checkNotNull(dependencyNode, "dependencyNode is null");
56  
57          this.groupId = dependencyNode.getArtifact().getGroupId();
58          this.artifactId = dependencyNode.getArtifact().getArtifactId();
59      }
60  
61      public String getArtifactId() {
62          return artifactId;
63      }
64  
65      public String getGroupId() {
66          return groupId;
67      }
68  
69      public boolean matchDependency(Dependency dependency) {
70          return Optional.ofNullable(dependency)
71                  .map(Dependency::getArtifact)
72                  .map(a -> match(a.getGroupId(), a.getArtifactId()))
73                  .orElse(false);
74      }
75  
76      public boolean matchArtifact(Artifact artifact) {
77          return Optional.of(artifact)
78                  .map(a -> match(a.getGroupId(), a.getArtifactId()))
79                  .orElse(false);
80      }
81  
82      private boolean match(String groupId, String artifactId) {
83          return (getArtifactId().equals("*") || getArtifactId().equals(artifactId))
84                  && (getGroupId().equals("*") || getGroupId().equals(groupId));
85      }
86  
87      @Override
88      public boolean equals(Object o) {
89          if (this == o) {
90              return true;
91          }
92          if (o == null || getClass() != o.getClass()) {
93              return false;
94          }
95          ArtifactIdentifier that = (ArtifactIdentifier) o;
96          return artifactId.equals(that.artifactId) && groupId.equals(that.groupId);
97      }
98  
99      @Override
100     public int hashCode() {
101         return Objects.hash(artifactId, groupId);
102     }
103 
104     @Override
105     public String toString() {
106         return String.format("%s:%s", groupId, artifactId);
107     }
108 }