1 package org.apache.maven.artifact.versioning;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class Restriction
28 {
29 private final ArtifactVersion lowerBound;
30
31 private final boolean lowerBoundInclusive;
32
33 private final ArtifactVersion upperBound;
34
35 private final boolean upperBoundInclusive;
36
37 public static final Restriction EVERYTHING = new Restriction( null, false, null, false );
38
39 public Restriction( ArtifactVersion lowerBound, boolean lowerBoundInclusive, ArtifactVersion upperBound,
40 boolean upperBoundInclusive )
41 {
42 this.lowerBound = lowerBound;
43 this.lowerBoundInclusive = lowerBoundInclusive;
44 this.upperBound = upperBound;
45 this.upperBoundInclusive = upperBoundInclusive;
46 }
47
48 public ArtifactVersion getLowerBound()
49 {
50 return lowerBound;
51 }
52
53 public boolean isLowerBoundInclusive()
54 {
55 return lowerBoundInclusive;
56 }
57
58 public ArtifactVersion getUpperBound()
59 {
60 return upperBound;
61 }
62
63 public boolean isUpperBoundInclusive()
64 {
65 return upperBoundInclusive;
66 }
67
68 public boolean containsVersion( ArtifactVersion version )
69 {
70 if ( lowerBound != null )
71 {
72 int comparison = lowerBound.compareTo( version );
73
74 if ( ( comparison == 0 ) && !lowerBoundInclusive )
75 {
76 return false;
77 }
78 if ( comparison > 0 )
79 {
80 return false;
81 }
82 }
83 if ( upperBound != null )
84 {
85 int comparison = upperBound.compareTo( version );
86
87 if ( ( comparison == 0 ) && !upperBoundInclusive )
88 {
89 return false;
90 }
91 if ( comparison < 0 )
92 {
93 return false;
94 }
95 }
96
97 return true;
98 }
99
100 @Override
101 public int hashCode()
102 {
103 int result = 13;
104
105 if ( lowerBound == null )
106 {
107 result += 1;
108 }
109 else
110 {
111 result += lowerBound.hashCode();
112 }
113
114 result *= lowerBoundInclusive ? 1 : 2;
115
116 if ( upperBound == null )
117 {
118 result -= 3;
119 }
120 else
121 {
122 result -= upperBound.hashCode();
123 }
124
125 result *= upperBoundInclusive ? 2 : 3;
126
127 return result;
128 }
129
130 @Override
131 public boolean equals( Object other )
132 {
133 if ( this == other )
134 {
135 return true;
136 }
137
138 if ( !( other instanceof Restriction ) )
139 {
140 return false;
141 }
142
143 Restriction restriction = (Restriction) other;
144 if ( lowerBound != null )
145 {
146 if ( !lowerBound.equals( restriction.lowerBound ) )
147 {
148 return false;
149 }
150 }
151 else if ( restriction.lowerBound != null )
152 {
153 return false;
154 }
155
156 if ( lowerBoundInclusive != restriction.lowerBoundInclusive )
157 {
158 return false;
159 }
160
161 if ( upperBound != null )
162 {
163 if ( !upperBound.equals( restriction.upperBound ) )
164 {
165 return false;
166 }
167 }
168 else if ( restriction.upperBound != null )
169 {
170 return false;
171 }
172
173 if ( upperBoundInclusive != restriction.upperBoundInclusive )
174 {
175 return false;
176 }
177
178 return true;
179 }
180
181 public String toString()
182 {
183 StringBuilder buf = new StringBuilder();
184
185 buf.append( isLowerBoundInclusive() ? "[" : "(" );
186 if ( getLowerBound() != null )
187 {
188 buf.append( getLowerBound().toString() );
189 }
190 buf.append( "," );
191 if ( getUpperBound() != null )
192 {
193 buf.append( getUpperBound().toString() );
194 }
195 buf.append( isUpperBoundInclusive() ? "]" : ")" );
196
197 return buf.toString();
198 }
199 }