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