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              return comparison >= 0;
92          }
93  
94          return true;
95      }
96  
97      @Override
98      public int hashCode()
99      {
100         int result = 13;
101 
102         if ( lowerBound == null )
103         {
104             result += 1;
105         }
106         else
107         {
108             result += lowerBound.hashCode();
109         }
110 
111         result *= lowerBoundInclusive ? 1 : 2;
112 
113         if ( upperBound == null )
114         {
115             result -= 3;
116         }
117         else
118         {
119             result -= upperBound.hashCode();
120         }
121 
122         result *= upperBoundInclusive ? 2 : 3;
123 
124         return result;
125     }
126 
127     @Override
128     public boolean equals( Object other )
129     {
130         if ( this == other )
131         {
132             return true;
133         }
134 
135         if ( !( other instanceof Restriction ) )
136         {
137             return false;
138         }
139 
140         Restriction restriction = (Restriction) other;
141         if ( lowerBound != null )
142         {
143             if ( !lowerBound.equals( restriction.lowerBound ) )
144             {
145                 return false;
146             }
147         }
148         else if ( restriction.lowerBound != null )
149         {
150             return false;
151         }
152 
153         if ( lowerBoundInclusive != restriction.lowerBoundInclusive )
154         {
155             return false;
156         }
157 
158         if ( upperBound != null )
159         {
160             if ( !upperBound.equals( restriction.upperBound ) )
161             {
162                 return false;
163             }
164         }
165         else if ( restriction.upperBound != null )
166         {
167             return false;
168         }
169 
170         return upperBoundInclusive == restriction.upperBoundInclusive;
171 
172     }
173 
174     public String toString()
175     {
176         StringBuilder buf = new StringBuilder();
177 
178         buf.append( isLowerBoundInclusive() ? '[' : '(' );
179         if ( getLowerBound() != null )
180         {
181             buf.append( getLowerBound().toString() );
182         }
183         buf.append( ',' );
184         if ( getUpperBound() != null )
185         {
186             buf.append( getUpperBound().toString() );
187         }
188         buf.append( isUpperBoundInclusive() ? ']' : ')' );
189 
190         return buf.toString();
191     }
192 }