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.dvc.model;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import org.basepom.mojo.dvc.QualifiedName;
20  import org.basepom.mojo.dvc.QualifiedNameMatcher;
21  
22  import java.util.Objects;
23  
24  import com.google.common.base.MoreObjects;
25  import org.apache.maven.artifact.versioning.ComparableVersion;
26  import org.eclipse.aether.graph.DependencyNode;
27  
28  /**
29   * Mojo model, adds an exclude to the checker.
30   */
31  public class VersionCheckExcludes {
32  
33      private String dependency = "";
34      private ComparableVersion expected;
35      private ComparableVersion resolved;
36  
37      public void setDependency(final String dependency) {
38          this.dependency = checkNotNull(dependency, "dependency is null").trim();
39      }
40  
41      public void setExpected(final String version) {
42          checkNotNull(version, "version is null");
43          this.expected = new ComparableVersion(version);
44      }
45  
46      public void setResolved(final String version) {
47          checkNotNull(version, "version is null");
48          this.resolved = new ComparableVersion(version);
49      }
50  
51      public boolean isValid() {
52          return (expected != null)
53                  && (resolved != null);
54      }
55  
56      public boolean matches(final DependencyNode dependencyNode, final ComparableVersion expectedVersion, final ComparableVersion resolvedVersion) {
57          checkNotNull(dependencyNode, "dependencyNode is null");
58          checkNotNull(expectedVersion, "expectedVersion is null");
59          checkNotNull(resolvedVersion, "resolvedVersion is null");
60  
61          final QualifiedNameMatcher matcher = new QualifiedNameMatcher(dependency);
62          final QualifiedName artifactName = QualifiedName.fromDependencyNode(dependencyNode);
63  
64          return matcher.matches(artifactName)
65                  && this.expected.equals(expectedVersion)
66                  && this.resolved.equals(resolvedVersion);
67      }
68  
69      @Override
70      public String toString() {
71          return MoreObjects.toStringHelper(this)
72                  .add("dependency", dependency)
73                  .add("expected", expected)
74                  .add("resolved", resolved)
75                  .toString();
76      }
77  
78      @Override
79      public boolean equals(final Object o) {
80          if (this == o) {
81              return true;
82          }
83          if (o == null || getClass() != o.getClass()) {
84              return false;
85          }
86          VersionCheckExcludes that = (VersionCheckExcludes) o;
87          return Objects.equals(dependency, that.dependency)
88                  && Objects.equals(expected, that.expected)
89                  && Objects.equals(resolved, that.resolved);
90      }
91  
92      @Override
93      public int hashCode() {
94          return Objects.hash(dependency, expected, resolved);
95      }
96  }