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