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              if (comparison < 0) {
82                  return false;
83              }
84          }
85  
86          return true;
87      }
88  
89      @Override
90      public int hashCode() {
91          int result = 13;
92  
93          if (lowerBound == null) {
94              result += 1;
95          } else {
96              result += lowerBound.hashCode();
97          }
98  
99          result *= lowerBoundInclusive ? 1 : 2;
100 
101         if (upperBound == null) {
102             result -= 3;
103         } else {
104             result -= upperBound.hashCode();
105         }
106 
107         result *= upperBoundInclusive ? 2 : 3;
108 
109         return result;
110     }
111 
112     @Override
113     public boolean equals(Object other) {
114         if (this == other) {
115             return true;
116         }
117 
118         if (!(other instanceof Restriction)) {
119             return false;
120         }
121 
122         Restriction restriction = (Restriction) other;
123         if (lowerBound != null) {
124             if (!lowerBound.equals(restriction.lowerBound)) {
125                 return false;
126             }
127         } else if (restriction.lowerBound != null) {
128             return false;
129         }
130 
131         if (lowerBoundInclusive != restriction.lowerBoundInclusive) {
132             return false;
133         }
134 
135         if (upperBound != null) {
136             if (!upperBound.equals(restriction.upperBound)) {
137                 return false;
138             }
139         } else if (restriction.upperBound != null) {
140             return false;
141         }
142 
143         return upperBoundInclusive == restriction.upperBoundInclusive;
144     }
145 
146     public String toString() {
147         StringBuilder buf = new StringBuilder();
148 
149         buf.append(isLowerBoundInclusive() ? '[' : '(');
150         if (getLowerBound() != null) {
151             buf.append(getLowerBound().toString());
152         }
153         buf.append(',');
154         if (getUpperBound() != null) {
155             buf.append(getUpperBound().toString());
156         }
157         buf.append(isUpperBoundInclusive() ? ']' : ')');
158 
159         return buf.toString();
160     }
161 }