View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2010, 2012 Sonatype, Inc.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *    Sonatype, Inc. - initial API and implementation
10   *******************************************************************************/
11  package org.eclipse.aether.version;
12  
13  /**
14   * A range of versions.
15   */
16  public interface VersionRange
17  {
18  
19      /**
20       * Determines whether the specified version is contained within this range.
21       * 
22       * @param version The version to test, must not be {@code null}.
23       * @return {@code true} if this range contains the specified version, {@code false} otherwise.
24       */
25      boolean containsVersion( Version version );
26  
27      /**
28       * Gets a lower bound (if any) for this range. If existent, this range does not contain any version smaller than its
29       * lower bound. Note that complex version ranges might exclude some versions even within their bounds.
30       * 
31       * @return A lower bound for this range or {@code null} is there is none.
32       */
33      Bound getLowerBound();
34  
35      /**
36       * Gets an upper bound (if any) for this range. If existent, this range does not contain any version greater than
37       * its upper bound. Note that complex version ranges might exclude some versions even within their bounds.
38       * 
39       * @return An upper bound for this range or {@code null} is there is none.
40       */
41      Bound getUpperBound();
42  
43      /**
44       * A bound of a version range.
45       */
46      static final class Bound
47      {
48  
49          private final Version version;
50  
51          private final boolean inclusive;
52  
53          /**
54           * Creates a new bound with the specified properties.
55           * 
56           * @param version The bounding version, must not be {@code null}.
57           * @param inclusive A flag whether the specified version is included in the range or not.
58           */
59          public Bound( Version version, boolean inclusive )
60          {
61              if ( version == null )
62              {
63                  throw new IllegalArgumentException( "version missing" );
64              }
65              this.version = version;
66              this.inclusive = inclusive;
67          }
68  
69          /**
70           * Gets the bounding version.
71           * 
72           * @return The bounding version, never {@code null}.
73           */
74          public Version getVersion()
75          {
76              return version;
77          }
78  
79          /**
80           * Indicates whether the bounding version is included in the range or not.
81           * 
82           * @return {@code true} if the bounding version is included in the range, {@code false} if not.
83           */
84          public boolean isInclusive()
85          {
86              return inclusive;
87          }
88  
89          @Override
90          public boolean equals( Object obj )
91          {
92              if ( obj == this )
93              {
94                  return true;
95              }
96              else if ( obj == null || !getClass().equals( obj.getClass() ) )
97              {
98                  return false;
99              }
100 
101             Bound that = (Bound) obj;
102             return inclusive == that.inclusive && version.equals( that.version );
103         }
104 
105         @Override
106         public int hashCode()
107         {
108             int hash = 17;
109             hash = hash * 31 + version.hashCode();
110             hash = hash * 31 + ( inclusive ? 1 : 0 );
111             return hash;
112         }
113 
114         @Override
115         public String toString()
116         {
117             return String.valueOf( version );
118         }
119 
120     }
121 
122 }