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