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 return comparison >= 0;
92 }
93
94 return true;
95 }
96
97 @Override
98 public int hashCode()
99 {
100 int result = 13;
101
102 if ( lowerBound == null )
103 {
104 result += 1;
105 }
106 else
107 {
108 result += lowerBound.hashCode();
109 }
110
111 result *= lowerBoundInclusive ? 1 : 2;
112
113 if ( upperBound == null )
114 {
115 result -= 3;
116 }
117 else
118 {
119 result -= upperBound.hashCode();
120 }
121
122 result *= upperBoundInclusive ? 2 : 3;
123
124 return result;
125 }
126
127 @Override
128 public boolean equals( Object other )
129 {
130 if ( this == other )
131 {
132 return true;
133 }
134
135 if ( !( other instanceof Restriction ) )
136 {
137 return false;
138 }
139
140 Restriction restriction = (Restriction) other;
141 if ( lowerBound != null )
142 {
143 if ( !lowerBound.equals( restriction.lowerBound ) )
144 {
145 return false;
146 }
147 }
148 else if ( restriction.lowerBound != null )
149 {
150 return false;
151 }
152
153 if ( lowerBoundInclusive != restriction.lowerBoundInclusive )
154 {
155 return false;
156 }
157
158 if ( upperBound != null )
159 {
160 if ( !upperBound.equals( restriction.upperBound ) )
161 {
162 return false;
163 }
164 }
165 else if ( restriction.upperBound != null )
166 {
167 return false;
168 }
169
170 return upperBoundInclusive == restriction.upperBoundInclusive;
171
172 }
173
174 public String toString()
175 {
176 StringBuilder buf = new StringBuilder();
177
178 buf.append( isLowerBoundInclusive() ? '[' : '(' );
179 if ( getLowerBound() != null )
180 {
181 buf.append( getLowerBound().toString() );
182 }
183 buf.append( ',' );
184 if ( getUpperBound() != null )
185 {
186 buf.append( getUpperBound().toString() );
187 }
188 buf.append( isUpperBoundInclusive() ? ']' : ')' );
189
190 return buf.toString();
191 }
192 }