Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2010 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2010 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.modules.j2ee.deployment.common.api; |
41 |
|
42 |
import java.util.regex.Matcher; |
43 |
import java.util.regex.Pattern; |
44 |
import org.netbeans.api.annotations.common.CheckForNull; |
45 |
import org.netbeans.api.annotations.common.NonNull; |
46 |
import org.openide.util.Parameters; |
47 |
|
48 |
/** |
49 |
* Represents the generic version. Useful for libraries, products etc. |
50 |
* <p> |
51 |
* This class is <i>Immutable</i>. |
52 |
* |
53 |
* @author Petr Hejl |
54 |
* @since 1.68 |
55 |
*/ |
56 |
// TODO add JBoss notation parsing MAJOR.MINOR.MICRO.QUALIFIER |
57 |
public final class Version { |
58 |
|
59 |
private static final Pattern JSR277_PATTERN = Pattern.compile( |
60 |
"(\\d+)(\\.(\\d+)(\\.(\\d+)(\\.(\\d+))?)?)?(-((\\w|-)+))?"); |
61 |
|
62 |
private final String version; |
63 |
|
64 |
private final Integer majorNumber; |
65 |
|
66 |
private final Integer minorNumber; |
67 |
|
68 |
private final Integer microNumber; |
69 |
|
70 |
private final Integer updateNumber; |
71 |
|
72 |
private final String qualifier; |
73 |
|
74 |
private Version(String version, Integer majorNumber, Integer minorNumber, |
75 |
Integer microNumber, Integer updateNumber, String qualifier) { |
76 |
this.version = version; |
77 |
this.majorNumber = majorNumber; |
78 |
this.minorNumber = minorNumber; |
79 |
this.microNumber = microNumber; |
80 |
this.updateNumber = updateNumber; |
81 |
this.qualifier = qualifier; |
82 |
} |
83 |
|
84 |
/** |
85 |
* Creates the version from the spec version string. |
86 |
* Expected format is <code>MAJOR_NUMBER[.MINOR_NUMBER[.MICRO_NUMBER[.UPDATE_NUMBER]]][-QUALIFIER]</code> |
87 |
* or <code>GENERIC_VERSION_STRING</code>. |
88 |
* |
89 |
* @param version spec version string with the following format: |
90 |
* <code>MAJOR_NUMBER[.MINOR_NUMBER[.MICRO_NUMBER[.UPDATE_NUMBER]]][-QUALIFIER]</code> |
91 |
* or <code>GENERIC_VERSION_STRING</code> |
92 |
*/ |
93 |
public static @NonNull Version fromJsr277NotationWithFallback(@NonNull String version) { |
94 |
Parameters.notNull("version", version); |
95 |
|
96 |
Matcher matcher = JSR277_PATTERN.matcher(version); |
97 |
if (matcher.matches()) { |
98 |
String fragment = matcher.group(1); |
99 |
Integer majorNumber = fragment != null ? Integer.valueOf(fragment) : null; |
100 |
fragment = matcher.group(3); |
101 |
Integer minorNumber = fragment != null ? Integer.valueOf(fragment) : null; |
102 |
fragment = matcher.group(5); |
103 |
Integer microNumber = fragment != null ? Integer.valueOf(fragment) : null; |
104 |
fragment = matcher.group(7); |
105 |
Integer updateNumber = fragment != null ? Integer.valueOf(fragment) : null; |
106 |
String qualifier = matcher.group(9); |
107 |
|
108 |
return new Version(version, majorNumber, minorNumber, |
109 |
microNumber, updateNumber, qualifier); |
110 |
} else { |
111 |
return new Version(version, null, null, null, null, null); |
112 |
} |
113 |
} |
114 |
|
115 |
/** |
116 |
* Returns the major number. May return <code>null</code>. |
117 |
* |
118 |
* @return the major number; may return <code>null</code> |
119 |
*/ |
120 |
@CheckForNull |
121 |
public Integer getMajor() { |
122 |
return majorNumber; |
123 |
} |
124 |
|
125 |
/** |
126 |
* Returns the minor number. May return <code>null</code>. |
127 |
* |
128 |
* @return the minor number; may return <code>null</code> |
129 |
*/ |
130 |
@CheckForNull |
131 |
public Integer getMinor() { |
132 |
return minorNumber; |
133 |
} |
134 |
|
135 |
/** |
136 |
* Returns the micro number. May return <code>null</code>. |
137 |
* |
138 |
* @return the micro number; may return <code>null</code> |
139 |
*/ |
140 |
@CheckForNull |
141 |
public Integer getMicro() { |
142 |
return microNumber; |
143 |
} |
144 |
|
145 |
/** |
146 |
* Returns the update. May return <code>null</code>. |
147 |
* |
148 |
* @return the update; may return <code>null</code> |
149 |
*/ |
150 |
@CheckForNull |
151 |
public Integer getUpdate() { |
152 |
return updateNumber; |
153 |
} |
154 |
|
155 |
/** |
156 |
* Returns the qualifier. May return <code>null</code>. |
157 |
* |
158 |
* @return the qualifier; may return <code>null</code> |
159 |
*/ |
160 |
@CheckForNull |
161 |
public String getQualifier() { |
162 |
return qualifier; |
163 |
} |
164 |
|
165 |
/** |
166 |
* {@inheritDoc} |
167 |
* <p> |
168 |
* Two versions are equal if and only if they have same major, minor, |
169 |
* micro, update number and qualifier. If the version does not conform to |
170 |
* notation the versions are equal only if the version strings exactly |
171 |
* matches. |
172 |
*/ |
173 |
@Override |
174 |
public boolean equals(Object obj) { |
175 |
if (obj == null) { |
176 |
return false; |
177 |
} |
178 |
if (getClass() != obj.getClass()) { |
179 |
return false; |
180 |
} |
181 |
final Version other = (Version) obj; |
182 |
|
183 |
// non conform |
184 |
if ((this.majorNumber == null && other.majorNumber != null) |
185 |
|| (this.majorNumber != null && other.majorNumber == null)) { |
186 |
return false; |
187 |
} else if (this.majorNumber == null && other.majorNumber == null) { |
188 |
return this.version.equals(other.version); |
189 |
} |
190 |
|
191 |
// standard |
192 |
if (this.majorNumber != other.majorNumber && (this.majorNumber == null || !this.majorNumber.equals(other.majorNumber))) { |
193 |
return false; |
194 |
} |
195 |
if (this.minorNumber != other.minorNumber && (this.minorNumber == null || !this.minorNumber.equals(other.minorNumber))) { |
196 |
return false; |
197 |
} |
198 |
if (this.microNumber != other.microNumber && (this.microNumber == null || !this.microNumber.equals(other.microNumber))) { |
199 |
return false; |
200 |
} |
201 |
if (this.updateNumber != other.updateNumber && (this.updateNumber == null || !this.updateNumber.equals(other.updateNumber))) { |
202 |
return false; |
203 |
} |
204 |
if ((this.qualifier == null) ? (other.qualifier != null) : !this.qualifier.equals(other.qualifier)) { |
205 |
return false; |
206 |
} |
207 |
return true; |
208 |
} |
209 |
|
210 |
/** |
211 |
* {@inheritDoc} |
212 |
* <p> |
213 |
* The implementation consistent with {@link #equals(Object)}. |
214 |
*/ |
215 |
@Override |
216 |
public int hashCode() { |
217 |
// non conform |
218 |
if (this.majorNumber == null) { |
219 |
return this.version.hashCode(); |
220 |
} |
221 |
|
222 |
// standard |
223 |
int hash = 7; |
224 |
hash = 97 * hash + (this.majorNumber != null ? this.majorNumber.hashCode() : 0); |
225 |
hash = 97 * hash + (this.minorNumber != null ? this.minorNumber.hashCode() : 0); |
226 |
hash = 97 * hash + (this.microNumber != null ? this.microNumber.hashCode() : 0); |
227 |
hash = 97 * hash + (this.updateNumber != null ? this.updateNumber.hashCode() : 0); |
228 |
hash = 97 * hash + (this.qualifier != null ? this.qualifier.hashCode() : 0); |
229 |
return hash; |
230 |
} |
231 |
|
232 |
public boolean isAboveOrEqual(Version other) { |
233 |
if (this.majorNumber == null || other.majorNumber == null) { |
234 |
return this.equals(other); |
235 |
} |
236 |
|
237 |
return compareTo(other) >= 0; |
238 |
} |
239 |
|
240 |
public boolean isBelowOrEqual(Version other) { |
241 |
if (this.majorNumber == null || other.majorNumber == null) { |
242 |
return this.equals(other); |
243 |
} |
244 |
|
245 |
return compareTo(other) <= 0; |
246 |
} |
247 |
|
248 |
@Override |
249 |
public String toString() { |
250 |
return version; |
251 |
} |
252 |
|
253 |
private int compareTo(Version o) { |
254 |
int comparison = compare(majorNumber, o.majorNumber); |
255 |
if (comparison != 0) { |
256 |
return comparison; |
257 |
} |
258 |
comparison = compare(minorNumber, o.minorNumber); |
259 |
if (comparison != 0) { |
260 |
return comparison; |
261 |
} |
262 |
comparison = compare(microNumber, o.microNumber); |
263 |
if (comparison != 0) { |
264 |
return comparison; |
265 |
} |
266 |
comparison = compare(updateNumber, o.updateNumber); |
267 |
if (comparison != 0) { |
268 |
return comparison; |
269 |
} |
270 |
return compare(qualifier, o.qualifier); |
271 |
} |
272 |
|
273 |
private static int compare(Integer o1, Integer o2) { |
274 |
if (o1 == null && o2 == null) { |
275 |
return 0; |
276 |
} |
277 |
if (o1 == null) { |
278 |
return Integer.valueOf(0).compareTo(o2); |
279 |
} |
280 |
if (o2 == null) { |
281 |
return o1.compareTo(Integer.valueOf(0)); |
282 |
} |
283 |
return o1.compareTo(o2); |
284 |
} |
285 |
|
286 |
private static int compare(String o1, String o2) { |
287 |
if (o1 == null && o2 == null) { |
288 |
return 0; |
289 |
} |
290 |
if (o1 == null) { |
291 |
return "".compareTo(o2); |
292 |
} |
293 |
if (o2 == null) { |
294 |
return o1.compareTo(""); |
295 |
} |
296 |
return o1.compareTo(o2); |
297 |
} |
298 |
|
299 |
} |