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