View Javadoc
1   package org.eclipse.aether.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  /**
25   * A range of versions.
26   */
27  public interface VersionRange
28  {
29  
30      /**
31       * Determines whether the specified version is contained within this range.
32       * 
33       * @param version The version to test, must not be {@code null}.
34       * @return {@code true} if this range contains the specified version, {@code false} otherwise.
35       */
36      boolean containsVersion( Version version );
37  
38      /**
39       * Gets a lower bound (if any) for this range. If existent, this range does not contain any version smaller than its
40       * lower bound. Note that complex version ranges might exclude some versions even within their bounds.
41       * 
42       * @return A lower bound for this range or {@code null} is there is none.
43       */
44      Bound getLowerBound();
45  
46      /**
47       * Gets an upper bound (if any) for this range. If existent, this range does not contain any version greater than
48       * its upper bound. Note that complex version ranges might exclude some versions even within their bounds.
49       * 
50       * @return An upper bound for this range or {@code null} is there is none.
51       */
52      Bound getUpperBound();
53  
54      /**
55       * A bound of a version range.
56       */
57      final class Bound
58      {
59  
60          private final Version version;
61  
62          private final boolean inclusive;
63  
64          /**
65           * Creates a new bound with the specified properties.
66           *
67           * @param version The bounding version, must not be {@code null}.
68           * @param inclusive A flag whether the specified version is included in the range or not.
69           */
70          public Bound( Version version, boolean inclusive )
71          {
72              this.version = requireNonNull( version, "version cannot be null" );
73              this.inclusive = inclusive;
74          }
75  
76          /**
77           * Gets the bounding version.
78           *
79           * @return The bounding version, never {@code null}.
80           */
81          public Version getVersion()
82          {
83              return version;
84          }
85  
86          /**
87           * Indicates whether the bounding version is included in the range or not.
88           * 
89           * @return {@code true} if the bounding version is included in the range, {@code false} if not.
90           */
91          public boolean isInclusive()
92          {
93              return inclusive;
94          }
95  
96          @Override
97          public boolean equals( Object obj )
98          {
99              if ( obj == this )
100             {
101                 return true;
102             }
103             else if ( obj == null || !getClass().equals( obj.getClass() ) )
104             {
105                 return false;
106             }
107 
108             Bound that = (Bound) obj;
109             return inclusive == that.inclusive && version.equals( that.version );
110         }
111 
112         @Override
113         public int hashCode()
114         {
115             int hash = 17;
116             hash = hash * 31 + version.hashCode();
117             hash = hash * 31 + ( inclusive ? 1 : 0 );
118             return hash;
119         }
120 
121         @Override
122         public String toString()
123         {
124             return String.valueOf( version );
125         }
126 
127     }
128 
129 }