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.version;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import org.basepom.mojo.dvc.QualifiedName;
20  
21  import java.util.Objects;
22  
23  import com.google.common.base.MoreObjects;
24  import org.apache.maven.artifact.versioning.ComparableVersion;
25  
26  public final class VersionResolution {
27  
28      /**
29       * The dependencies that requests this specific version resolution.
30       */
31      private final VersionResolutionElement requestingDependency;
32  
33      /**
34       * The version expected by the requesting dependency.
35       */
36      private final ComparableVersion expectedVersion;
37  
38      public static VersionResolution forDirectDependency(final QualifiedName requestingDependency,
39              final ComparableVersion expectedVersion,
40              final boolean managedDependency) {
41          return new VersionResolution(requestingDependency, expectedVersion, managedDependency, true);
42      }
43  
44      public static VersionResolution forTransitiveDependency(final QualifiedName requestingDependency,
45              final ComparableVersion expectedVersion,
46              final boolean managedDependency) {
47          return new VersionResolution(requestingDependency, expectedVersion, managedDependency, false);
48      }
49  
50      private VersionResolution(
51              final QualifiedName requestingDependency,
52              final ComparableVersion expectedVersion,
53              final boolean manageDependency,
54              final boolean directDependency) {
55          checkNotNull(requestingDependency, "requestingDependencyName is null");
56          this.requestingDependency = new VersionResolutionElement(requestingDependency, manageDependency, directDependency);
57          this.expectedVersion = checkNotNull(expectedVersion, "expectedVersion is null");
58      }
59  
60      public VersionResolutionElement getRequestingDependency() {
61          return requestingDependency;
62      }
63  
64      public ComparableVersion getExpectedVersion() {
65          return expectedVersion;
66      }
67  
68      public void conflict() {
69          requestingDependency.conflict();
70      }
71  
72      @Override
73      public boolean equals(final Object o) {
74          if (this == o) {
75              return true;
76          }
77          if (o == null || getClass() != o.getClass()) {
78              return false;
79          }
80          final VersionResolution that = (VersionResolution) o;
81          return requestingDependency.equals(that.requestingDependency)
82                  && expectedVersion.equals(that.expectedVersion);
83      }
84  
85      @Override
86      public int hashCode() {
87          return Objects.hash(requestingDependency, expectedVersion);
88      }
89  
90      @Override
91      public String toString() {
92          return MoreObjects.toStringHelper(this)
93                  .add("requestingDependency", requestingDependency)
94                  .add("expectedVersion", expectedVersion)
95                  .toString();
96      }
97  }