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.version;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import org.basepom.mojo.dvc.QualifiedName;
020
021import java.util.Objects;
022
023import com.google.common.base.MoreObjects;
024import org.apache.maven.artifact.versioning.ComparableVersion;
025
026public final class VersionResolution {
027
028    /**
029     * The dependencies that requests this specific version resolution.
030     */
031    private final VersionResolutionElement requestingDependency;
032
033    /**
034     * The version expected by the requesting dependency.
035     */
036    private final ComparableVersion expectedVersion;
037
038    public static VersionResolution forDirectDependency(final QualifiedName requestingDependency,
039            final ComparableVersion expectedVersion,
040            final boolean managedDependency) {
041        return new VersionResolution(requestingDependency, expectedVersion, managedDependency, true);
042    }
043
044    public static VersionResolution forTransitiveDependency(final QualifiedName requestingDependency,
045            final ComparableVersion expectedVersion,
046            final boolean managedDependency) {
047        return new VersionResolution(requestingDependency, expectedVersion, managedDependency, false);
048    }
049
050    private VersionResolution(
051            final QualifiedName requestingDependency,
052            final ComparableVersion expectedVersion,
053            final boolean manageDependency,
054            final boolean directDependency) {
055        checkNotNull(requestingDependency, "requestingDependencyName is null");
056        this.requestingDependency = new VersionResolutionElement(requestingDependency, manageDependency, directDependency);
057        this.expectedVersion = checkNotNull(expectedVersion, "expectedVersion is null");
058    }
059
060    public VersionResolutionElement getRequestingDependency() {
061        return requestingDependency;
062    }
063
064    public ComparableVersion getExpectedVersion() {
065        return expectedVersion;
066    }
067
068    public void conflict() {
069        requestingDependency.conflict();
070    }
071
072    @Override
073    public boolean equals(final Object o) {
074        if (this == o) {
075            return true;
076        }
077        if (o == null || getClass() != o.getClass()) {
078            return false;
079        }
080        final VersionResolution that = (VersionResolution) o;
081        return requestingDependency.equals(that.requestingDependency)
082                && expectedVersion.equals(that.expectedVersion);
083    }
084
085    @Override
086    public int hashCode() {
087        return Objects.hash(requestingDependency, expectedVersion);
088    }
089
090    @Override
091    public String toString() {
092        return MoreObjects.toStringHelper(this)
093                .add("requestingDependency", requestingDependency)
094                .add("expectedVersion", expectedVersion)
095                .toString();
096    }
097}