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.eclipse.aether.version;
20
21 import static java.util.Objects.requireNonNull;
22
23 /**
24 * A range of versions.
25 */
26 public interface VersionRange {
27
28 /**
29 * Determines whether the specified version is contained within this range.
30 *
31 * @param version The version to test, must not be {@code null}.
32 * @return {@code true} if this range contains the specified version, {@code false} otherwise.
33 */
34 boolean containsVersion(Version version);
35
36 /**
37 * Gets a lower bound (if any) for this range. If existent, this range does not contain any version smaller than its
38 * lower bound. Note that complex version ranges might exclude some versions even within their bounds.
39 *
40 * @return A lower bound for this range or {@code null} is there is none.
41 */
42 Bound getLowerBound();
43
44 /**
45 * Gets an upper bound (if any) for this range. If existent, this range does not contain any version greater than
46 * its upper bound. Note that complex version ranges might exclude some versions even within their bounds.
47 *
48 * @return An upper bound for this range or {@code null} is there is none.
49 */
50 Bound getUpperBound();
51
52 /**
53 * A bound of a version range.
54 */
55 final class Bound {
56
57 private final Version version;
58
59 private final boolean inclusive;
60
61 /**
62 * Creates a new bound with the specified properties.
63 *
64 * @param version The bounding version, must not be {@code null}.
65 * @param inclusive A flag whether the specified version is included in the range or not.
66 */
67 public Bound(Version version, boolean inclusive) {
68 this.version = requireNonNull(version, "version cannot be null");
69 this.inclusive = inclusive;
70 }
71
72 /**
73 * Gets the bounding version.
74 *
75 * @return The bounding version, never {@code null}.
76 */
77 public Version getVersion() {
78 return version;
79 }
80
81 /**
82 * Indicates whether the bounding version is included in the range or not.
83 *
84 * @return {@code true} if the bounding version is included in the range, {@code false} if not.
85 */
86 public boolean isInclusive() {
87 return inclusive;
88 }
89
90 @Override
91 public boolean equals(Object obj) {
92 if (obj == this) {
93 return true;
94 } else if (obj == null || !getClass().equals(obj.getClass())) {
95 return false;
96 }
97
98 Bound that = (Bound) obj;
99 return inclusive == that.inclusive && version.equals(that.version);
100 }
101
102 @Override
103 public int hashCode() {
104 int hash = 17;
105 hash = hash * 31 + version.hashCode();
106 hash = hash * 31 + (inclusive ? 1 : 0);
107 return hash;
108 }
109
110 @Override
111 public String toString() {
112 return String.valueOf(version);
113 }
114 }
115 }