001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.dvc.model;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import org.basepom.mojo.dvc.QualifiedName;
020import org.basepom.mojo.dvc.QualifiedNameMatcher;
021
022import java.util.Objects;
023
024import com.google.common.base.MoreObjects;
025import org.apache.maven.artifact.versioning.ComparableVersion;
026import org.eclipse.aether.graph.DependencyNode;
027
028/**
029 * Mojo model, adds an exclude to the checker.
030 */
031public class VersionCheckExcludes {
032
033    private String dependency = "";
034    private ComparableVersion expected;
035    private ComparableVersion resolved;
036
037    public void setDependency(final String dependency) {
038        this.dependency = checkNotNull(dependency, "dependency is null").trim();
039    }
040
041    public void setExpected(final String version) {
042        checkNotNull(version, "version is null");
043        this.expected = new ComparableVersion(version);
044    }
045
046    public void setResolved(final String version) {
047        checkNotNull(version, "version is null");
048        this.resolved = new ComparableVersion(version);
049    }
050
051    public boolean isValid() {
052        return (expected != null)
053                && (resolved != null);
054    }
055
056    public boolean matches(final DependencyNode dependencyNode, final ComparableVersion expectedVersion, final ComparableVersion resolvedVersion) {
057        checkNotNull(dependencyNode, "dependencyNode is null");
058        checkNotNull(expectedVersion, "expectedVersion is null");
059        checkNotNull(resolvedVersion, "resolvedVersion is null");
060
061        final QualifiedNameMatcher matcher = new QualifiedNameMatcher(dependency);
062        final QualifiedName artifactName = QualifiedName.fromDependencyNode(dependencyNode);
063
064        return matcher.matches(artifactName)
065                && this.expected.equals(expectedVersion)
066                && this.resolved.equals(resolvedVersion);
067    }
068
069    @Override
070    public String toString() {
071        return MoreObjects.toStringHelper(this)
072                .add("dependency", dependency)
073                .add("expected", expected)
074                .add("resolved", resolved)
075                .toString();
076    }
077
078    @Override
079    public boolean equals(final Object o) {
080        if (this == o) {
081            return true;
082        }
083        if (o == null || getClass() != o.getClass()) {
084            return false;
085        }
086        VersionCheckExcludes that = (VersionCheckExcludes) o;
087        return Objects.equals(dependency, that.dependency)
088                && Objects.equals(expected, that.expected)
089                && Objects.equals(resolved, that.resolved);
090    }
091
092    @Override
093    public int hashCode() {
094        return Objects.hash(dependency, expected, resolved);
095    }
096}