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   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
25   */
26  public class Restriction {
27      private final ArtifactVersion lowerBound;
28  
29      private final boolean lowerBoundInclusive;
30  
31      private final ArtifactVersion upperBound;
32  
33      private final boolean upperBoundInclusive;
34  
35      public static final Restriction EVERYTHING = new Restriction(null, false, null, false);
36  
37      public Restriction(
38              ArtifactVersion lowerBound,
39              boolean lowerBoundInclusive,
40              ArtifactVersion upperBound,
41              boolean upperBoundInclusive) {
42          this.lowerBound = lowerBound;
43          this.lowerBoundInclusive = lowerBoundInclusive;
44          this.upperBound = upperBound;
45          this.upperBoundInclusive = upperBoundInclusive;
46      }
47  
48      public ArtifactVersion getLowerBound() {
49          return lowerBound;
50      }
51  
52      public boolean isLowerBoundInclusive() {
53          return lowerBoundInclusive;
54      }
55  
56      public ArtifactVersion getUpperBound() {
57          return upperBound;
58      }
59  
60      public boolean isUpperBoundInclusive() {
61          return upperBoundInclusive;
62      }
63  
64      public boolean containsVersion(ArtifactVersion version) {
65          if (lowerBound != null) {
66              int comparison = lowerBound.compareTo(version);
67  
68              if ((comparison == 0) && !lowerBoundInclusive) {
69                  return false;
70              }
71              if (comparison > 0) {
72                  return false;
73              }
74          }
75          if (upperBound != null) {
76              int comparison = upperBound.compareTo(version);
77  
78              if ((comparison == 0) && !upperBoundInclusive) {
79                  return false;
80              }
81              return comparison >= 0;
82          }
83  
84          return true;
85      }
86  
87      @Override
88      public int hashCode() {
89          int result = 13;
90  
91          if (lowerBound == null) {
92              result += 1;
93          } else {
94              result += lowerBound.hashCode();
95          }
96  
97          result *= lowerBoundInclusive ? 1 : 2;
98  
99          if (upperBound == null) {
100             result -= 3;
101         } else {
102             result -= upperBound.hashCode();
103         }
104 
105         result *= upperBoundInclusive ? 2 : 3;
106 
107         return result;
108     }
109 
110     @Override
111     public boolean equals(Object other) {
112         if (this == other) {
113             return true;
114         }
115 
116         if (!(other instanceof Restriction)) {
117             return false;
118         }
119 
120         Restriction restriction = (Restriction) other;
121         if (lowerBound != null) {
122             if (!lowerBound.equals(restriction.lowerBound)) {
123                 return false;
124             }
125         } else if (restriction.lowerBound != null) {
126             return false;
127         }
128 
129         if (lowerBoundInclusive != restriction.lowerBoundInclusive) {
130             return false;
131         }
132 
133         if (upperBound != null) {
134             if (!upperBound.equals(restriction.upperBound)) {
135                 return false;
136             }
137         } else if (restriction.upperBound != null) {
138             return false;
139         }
140 
141         return upperBoundInclusive == restriction.upperBoundInclusive;
142     }
143 
144     public String toString() {
145         StringBuilder buf = new StringBuilder();
146 
147         buf.append(isLowerBoundInclusive() ? '[' : '(');
148         if (getLowerBound() != null) {
149             buf.append(getLowerBound().toString());
150         }
151         buf.append(',');
152         if (getUpperBound() != null) {
153             buf.append(getUpperBound().toString());
154         }
155         buf.append(isUpperBoundInclusive() ? ']' : ')');
156 
157         return buf.toString();
158     }
159 }