VersionCheckExcludes.java

  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.dvc.model;

  15. import static com.google.common.base.Preconditions.checkNotNull;

  16. import org.basepom.mojo.dvc.QualifiedName;
  17. import org.basepom.mojo.dvc.QualifiedNameMatcher;

  18. import java.util.Objects;

  19. import com.google.common.base.MoreObjects;
  20. import org.apache.maven.artifact.versioning.ComparableVersion;
  21. import org.eclipse.aether.graph.DependencyNode;

  22. /**
  23.  * Mojo model, adds an exclude to the checker.
  24.  */
  25. public class VersionCheckExcludes {

  26.     private String dependency = "";
  27.     private ComparableVersion expected;
  28.     private ComparableVersion resolved;

  29.     public void setDependency(final String dependency) {
  30.         this.dependency = checkNotNull(dependency, "dependency is null").trim();
  31.     }

  32.     public void setExpected(final String version) {
  33.         checkNotNull(version, "version is null");
  34.         this.expected = new ComparableVersion(version);
  35.     }

  36.     public void setResolved(final String version) {
  37.         checkNotNull(version, "version is null");
  38.         this.resolved = new ComparableVersion(version);
  39.     }

  40.     public boolean isValid() {
  41.         return (expected != null)
  42.                 && (resolved != null);
  43.     }

  44.     public boolean matches(final DependencyNode dependencyNode, final ComparableVersion expectedVersion, final ComparableVersion resolvedVersion) {
  45.         checkNotNull(dependencyNode, "dependencyNode is null");
  46.         checkNotNull(expectedVersion, "expectedVersion is null");
  47.         checkNotNull(resolvedVersion, "resolvedVersion is null");

  48.         final QualifiedNameMatcher matcher = new QualifiedNameMatcher(dependency);
  49.         final QualifiedName artifactName = QualifiedName.fromDependencyNode(dependencyNode);

  50.         return matcher.matches(artifactName)
  51.                 && this.expected.equals(expectedVersion)
  52.                 && this.resolved.equals(resolvedVersion);
  53.     }

  54.     @Override
  55.     public String toString() {
  56.         return MoreObjects.toStringHelper(this)
  57.                 .add("dependency", dependency)
  58.                 .add("expected", expected)
  59.                 .add("resolved", resolved)
  60.                 .toString();
  61.     }

  62.     @Override
  63.     public boolean equals(final Object o) {
  64.         if (this == o) {
  65.             return true;
  66.         }
  67.         if (o == null || getClass() != o.getClass()) {
  68.             return false;
  69.         }
  70.         VersionCheckExcludes that = (VersionCheckExcludes) o;
  71.         return Objects.equals(dependency, that.dependency)
  72.                 && Objects.equals(expected, that.expected)
  73.                 && Objects.equals(resolved, that.resolved);
  74.     }

  75.     @Override
  76.     public int hashCode() {
  77.         return Objects.hash(dependency, expected, resolved);
  78.     }
  79. }