View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.version;
20  
21  import java.util.Objects;
22  
23  import org.eclipse.aether.version.Version;
24  import org.eclipse.aether.version.VersionConstraint;
25  import org.eclipse.aether.version.VersionRange;
26  
27  import static java.util.Objects.requireNonNull;
28  
29  /**
30   * A constraint on versions for a dependency.
31   * <p>
32   * Despite its name, this class is generic in a sense it works with any {@link Version}.
33   */
34  final class GenericVersionConstraint implements VersionConstraint {
35  
36      private final VersionRange range;
37  
38      private final Version version;
39  
40      /**
41       * Creates a version constraint from the specified version range.
42       *
43       * @param range The version range, must not be {@code null}.
44       */
45      GenericVersionConstraint(VersionRange range) {
46          this.range = requireNonNull(range, "version range cannot be null");
47          this.version = null;
48      }
49  
50      /**
51       * Creates a version constraint from the specified version.
52       *
53       * @param version The version, must not be {@code null}.
54       */
55      GenericVersionConstraint(Version version) {
56          this.version = requireNonNull(version, "version cannot be null");
57          this.range = null;
58      }
59  
60      @Override
61      public VersionRange getRange() {
62          return range;
63      }
64  
65      @Override
66      public Version getVersion() {
67          return version;
68      }
69  
70      @Override
71      public boolean containsVersion(Version version) {
72          if (range == null) {
73              return version.equals(this.version);
74          } else {
75              return range.containsVersion(version);
76          }
77      }
78  
79      @Override
80      public String toString() {
81          return String.valueOf((range == null) ? version : range);
82      }
83  
84      @Override
85      public boolean equals(Object obj) {
86          if (this == obj) {
87              return true;
88          }
89          if (obj == null || !getClass().equals(obj.getClass())) {
90              return false;
91          }
92  
93          VersionConstraint that = (VersionConstraint) obj;
94  
95          return Objects.equals(range, that.getRange()) && Objects.equals(version, that.getVersion());
96      }
97  
98      @Override
99      public int hashCode() {
100         int hash = 17;
101         hash = hash * 31 + hash(getRange());
102         hash = hash * 31 + hash(getVersion());
103         return hash;
104     }
105 
106     private static int hash(Object obj) {
107         return obj != null ? obj.hashCode() : 0;
108     }
109 }