1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.artifact.versioning;
20
21
22
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 }