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 restriction) {
116             if (lowerBound != null) {
117                 if (!lowerBound.equals(restriction.lowerBound)) {
118                     return false;
119                 }
120             } else if (restriction.lowerBound != null) {
121                 return false;
122             }
123 
124             if (lowerBoundInclusive != restriction.lowerBoundInclusive) {
125                 return false;
126             }
127 
128             if (upperBound != null) {
129                 if (!upperBound.equals(restriction.upperBound)) {
130                     return false;
131                 }
132             } else if (restriction.upperBound != null) {
133                 return false;
134             }
135 
136             return upperBoundInclusive == restriction.upperBoundInclusive;
137         } else {
138             return false;
139         }
140     }
141 
142     public String toString() {
143         StringBuilder buf = new StringBuilder();
144 
145         buf.append(isLowerBoundInclusive() ? '[' : '(');
146         if (getLowerBound() != null) {
147             buf.append(getLowerBound().toString());
148         }
149         buf.append(',');
150         if (getUpperBound() != null) {
151             buf.append(getUpperBound().toString());
152         }
153         buf.append(isUpperBoundInclusive() ? ']' : ')');
154 
155         return buf.toString();
156     }
157 }