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  /**
29   * A constraint on versions for a dependency.
30   */
31  final class GenericVersionConstraint
32      implements VersionConstraint
33  {
34  
35      private final VersionRange range;
36  
37      private final Version version;
38  
39      /**
40       * Creates a version constraint from the specified version range.
41       *
42       * @param range The version range, must not be {@code null}.
43       */
44      public GenericVersionConstraint( VersionRange range )
45      {
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      public GenericVersionConstraint( Version version )
56      {
57          this.version = requireNonNull( version, "version cannot be null" );
58          this.range = null;
59      }
60  
61      public VersionRange getRange()
62      {
63          return range;
64      }
65  
66      public Version getVersion()
67      {
68          return version;
69      }
70  
71      public boolean containsVersion( Version version )
72      {
73          if ( range == null )
74          {
75              return version.equals( this.version );
76          }
77          else
78          {
79              return range.containsVersion( version );
80          }
81      }
82  
83      @Override
84      public String toString()
85      {
86          return String.valueOf( ( range == null ) ? version : range );
87      }
88  
89      @Override
90      public boolean equals( Object obj )
91      {
92          if ( this == obj )
93          {
94              return true;
95          }
96          if ( obj == null || !getClass().equals( obj.getClass() ) )
97          {
98              return false;
99          }
100 
101         GenericVersionConstraint that = (GenericVersionConstraint) obj;
102 
103         return eq( range, that.range ) && eq( version, that.getVersion() );
104     }
105 
106     private static <T> boolean eq( T s1, T s2 )
107     {
108         return s1 != null ? s1.equals( s2 ) : s2 == null;
109     }
110 
111     @Override
112     public int hashCode()
113     {
114         int hash = 17;
115         hash = hash * 31 + hash( getRange() );
116         hash = hash * 31 + hash( getVersion() );
117         return hash;
118     }
119 
120     private static int hash( Object obj )
121     {
122         return obj != null ? obj.hashCode() : 0;
123     }
124 
125 }