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
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 }