View Javadoc
1   package org.apache.maven.artifact.versioning;
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  /**
23   * Describes a restriction in versioning.
24   *
25   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
26   */
27  public class Restriction
28  {
29      private final ArtifactVersion lowerBound;
30  
31      private final boolean lowerBoundInclusive;
32  
33      private final ArtifactVersion upperBound;
34  
35      private final boolean upperBoundInclusive;
36  
37      public static final Restriction EVERYTHING = new Restriction( null, false, null, false );
38  
39      public Restriction( ArtifactVersion lowerBound, boolean lowerBoundInclusive, ArtifactVersion upperBound,
40                          boolean upperBoundInclusive )
41      {
42          this.lowerBound = lowerBound;
43          this.lowerBoundInclusive = lowerBoundInclusive;
44          this.upperBound = upperBound;
45          this.upperBoundInclusive = upperBoundInclusive;
46      }
47  
48      public ArtifactVersion getLowerBound()
49      {
50          return lowerBound;
51      }
52  
53      public boolean isLowerBoundInclusive()
54      {
55          return lowerBoundInclusive;
56      }
57  
58      public ArtifactVersion getUpperBound()
59      {
60          return upperBound;
61      }
62  
63      public boolean isUpperBoundInclusive()
64      {
65          return upperBoundInclusive;
66      }
67  
68      public boolean containsVersion( ArtifactVersion version )
69      {
70          if ( lowerBound != null )
71          {
72              int comparison = lowerBound.compareTo( version );
73  
74              if ( ( comparison == 0 ) && !lowerBoundInclusive )
75              {
76                  return false;
77              }
78              if ( comparison > 0 )
79              {
80                  return false;
81              }
82          }
83          if ( upperBound != null )
84          {
85              int comparison = upperBound.compareTo( version );
86  
87              if ( ( comparison == 0 ) && !upperBoundInclusive )
88              {
89                  return false;
90              }
91              if ( comparison < 0 )
92              {
93                  return false;
94              }
95          }
96  
97          return true;
98      }
99  
100     @Override
101     public int hashCode()
102     {
103         int result = 13;
104 
105         if ( lowerBound == null )
106         {
107             result += 1;
108         }
109         else
110         {
111             result += lowerBound.hashCode();
112         }
113 
114         result *= lowerBoundInclusive ? 1 : 2;
115 
116         if ( upperBound == null )
117         {
118             result -= 3;
119         }
120         else
121         {
122             result -= upperBound.hashCode();
123         }
124 
125         result *= upperBoundInclusive ? 2 : 3;
126 
127         return result;
128     }
129 
130     @Override
131     public boolean equals( Object other )
132     {
133         if ( this == other )
134         {
135             return true;
136         }
137 
138         if ( !( other instanceof Restriction ) )
139         {
140             return false;
141         }
142 
143         Restriction restriction = (Restriction) other;
144         if ( lowerBound != null )
145         {
146             if ( !lowerBound.equals( restriction.lowerBound ) )
147             {
148                 return false;
149             }
150         }
151         else if ( restriction.lowerBound != null )
152         {
153             return false;
154         }
155 
156         if ( lowerBoundInclusive != restriction.lowerBoundInclusive )
157         {
158             return false;
159         }
160 
161         if ( upperBound != null )
162         {
163             if ( !upperBound.equals( restriction.upperBound ) )
164             {
165                 return false;
166             }
167         }
168         else if ( restriction.upperBound != null )
169         {
170             return false;
171         }
172 
173         return upperBoundInclusive == restriction.upperBoundInclusive;
174 
175     }
176 
177     public String toString()
178     {
179         StringBuilder buf = new StringBuilder();
180 
181         buf.append( isLowerBoundInclusive() ? "[" : "(" );
182         if ( getLowerBound() != null )
183         {
184             buf.append( getLowerBound().toString() );
185         }
186         buf.append( "," );
187         if ( getUpperBound() != null )
188         {
189             buf.append( getUpperBound().toString() );
190         }
191         buf.append( isUpperBoundInclusive() ? "]" : ")" );
192 
193         return buf.toString();
194     }
195 }