View Javadoc

1   package org.apache.maven.artifact.versioning;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *  http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  /**
24   * Describes a restriction in versioning.
25   *
26   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
27   * @version $Id: Restriction.java 640549 2008-03-24 20:05:11Z bentmann $
28   */
29  public class Restriction
30  {
31      private final ArtifactVersion lowerBound;
32  
33      private final boolean lowerBoundInclusive;
34  
35      private final ArtifactVersion upperBound;
36  
37      private final boolean upperBoundInclusive;
38  
39      static final Restriction EVERYTHING = new Restriction( null, false, null, false );
40  
41      public Restriction( ArtifactVersion lowerBound, boolean lowerBoundInclusive, ArtifactVersion upperBound,
42                          boolean upperBoundInclusive )
43      {
44          this.lowerBound = lowerBound;
45          this.lowerBoundInclusive = lowerBoundInclusive;
46          this.upperBound = upperBound;
47          this.upperBoundInclusive = upperBoundInclusive;
48      }
49  
50      public ArtifactVersion getLowerBound()
51      {
52          return lowerBound;
53      }
54  
55      public boolean isLowerBoundInclusive()
56      {
57          return lowerBoundInclusive;
58      }
59  
60      public ArtifactVersion getUpperBound()
61      {
62          return upperBound;
63      }
64  
65      public boolean isUpperBoundInclusive()
66      {
67          return upperBoundInclusive;
68      }
69  
70      public boolean containsVersion( ArtifactVersion version )
71      {
72          if ( lowerBound != null )
73          {
74              int comparison = lowerBound.compareTo( version );
75              if ( comparison == 0 && !lowerBoundInclusive )
76              {
77                  return false;
78              }
79              if ( comparison > 0 )
80              {
81                  return false;
82              }
83          }
84          if ( upperBound != null )
85          {
86              int comparison = upperBound.compareTo( version );
87              if ( comparison == 0 && !upperBoundInclusive )
88              {
89                  return false;
90              }
91              if ( comparison < 0 )
92              {
93                  return false;
94              }
95          }
96          return true;
97      }
98  
99  
100     public int hashCode()
101     {
102         int result = 13;
103 
104         if ( lowerBound == null )
105         {
106             result += 1;
107         }
108         else
109         {
110             result += lowerBound.hashCode();
111         }
112 
113         result *= lowerBoundInclusive ? 1 : 2;
114 
115         if ( upperBound == null )
116         {
117             result -= 3;
118         }
119         else
120         {
121             result -= upperBound.hashCode();
122         }
123 
124         result *= upperBoundInclusive ? 2 : 3;
125 
126         return result;
127     }
128 
129     public boolean equals( Object other )
130     {
131         if ( this == other )
132         {
133             return true;
134         }
135 
136         if ( !(other instanceof Restriction ) )
137         {
138             return false;
139         }
140 
141         Restriction restriction = (Restriction) other;
142         if ( lowerBound != null )
143         {
144             if ( !lowerBound.equals( restriction.lowerBound ) )
145             {
146                 return false;
147             }
148         }
149         else if ( restriction.lowerBound != null )
150         {
151             return false;
152         }
153 
154         if ( lowerBoundInclusive != restriction.lowerBoundInclusive )
155         {
156             return false;
157         }
158 
159         if ( upperBound != null )
160         {
161             if ( !upperBound.equals( restriction.upperBound ) )
162             {
163                 return false;
164             }
165         }
166         else if ( restriction.upperBound != null )
167         {
168             return false;
169         }
170 
171         if ( upperBoundInclusive != restriction.upperBoundInclusive )
172         {
173             return false;
174         }
175 
176         return true;
177     }
178 
179     public String toString()
180     {
181         StringBuffer buf = new StringBuffer();
182 
183         buf.append( isLowerBoundInclusive() ? "[" : "(" );
184         if ( getLowerBound() != null )
185         {
186             buf.append( getLowerBound().toString() );
187         }
188         buf.append( "," );
189         if ( getUpperBound() != null )
190         {
191             buf.append( getUpperBound().toString() );
192         }
193         buf.append( isUpperBoundInclusive() ? "]" : ")" );
194 
195         return buf.toString();
196     }
197 }