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