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