1 11 package org.eclipse.osgi.service.resolver; 12 13 import java.util.StringTokenizer ; 14 import java.util.Vector ; 15 16 53 public final class Version implements Comparable { 54 55 private int major = 0; 56 private int minor = 0; 57 private int micro = 0; 58 private String qualifier = ""; private boolean inclusive = true; 60 61 private static final String SEPARATOR = "."; 63 public static Version emptyVersion = new Version(0, 0, 0); 64 public static Version maxVersion = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE); 65 66 71 public Version(Version version) { 72 this(version.major, version.minor, version.micro, version.qualifier, version.inclusive); 73 } 74 75 82 public Version(int major, int minor, int micro) { 83 this(major, minor, micro, null); 84 } 85 86 95 public Version(int major, int minor, int micro, String qualifier) throws IllegalArgumentException { 96 this(major, minor, micro, qualifier, true); 97 } 98 99 109 public Version(int major, int minor, int micro, String qualifier, boolean inclusive) throws IllegalArgumentException { 110 111 115 if (major < 0) 116 throw new IllegalArgumentException ("Negative major"); if (minor < 0) 118 throw new IllegalArgumentException ("Negative minor"); if (micro < 0) 120 throw new IllegalArgumentException ("Negative micro"); if (qualifier == null) 122 qualifier = ""; 124 this.major = major; 125 this.minor = minor; 126 this.micro = micro; 127 this.qualifier = verifyQualifier(qualifier); 128 this.inclusive = inclusive; 129 } 130 131 148 public Version(String versionId) { 149 this(versionId, true); 150 } 151 152 170 public Version(String versionId, boolean inclusive) { 171 if (versionId == null) 172 versionId = "0.0.0"; Object [] parts = parseVersion(versionId); 174 this.major = ((Integer ) parts[0]).intValue(); 175 this.minor = ((Integer ) parts[1]).intValue(); 176 this.micro = ((Integer ) parts[2]).intValue(); 177 this.qualifier = (String ) parts[3]; 178 this.inclusive = inclusive; 179 } 180 181 private static Object [] parseVersion(String versionId) { 182 if (versionId == null) 186 throw new IllegalArgumentException ("Null version string"); 188 String s = versionId.trim(); 189 if (s.equals("")) throw new IllegalArgumentException ("Empty version string"); if (s.startsWith(SEPARATOR)) 192 throw new IllegalArgumentException ("Invalid version format"); if (s.endsWith(SEPARATOR)) 194 throw new IllegalArgumentException ("Invalid version format"); if (s.indexOf(SEPARATOR + SEPARATOR) != -1) 196 throw new IllegalArgumentException ("Invalid version format"); 198 StringTokenizer st = new StringTokenizer (s, SEPARATOR); 199 Vector elements = new Vector (4); 200 201 while (st.hasMoreTokens()) 202 elements.addElement(st.nextToken()); 203 204 int elementSize = elements.size(); 205 206 if (elementSize <= 0) 207 throw new IllegalArgumentException ("Invalid version format (no token)"); if (elementSize > 4) 209 throw new IllegalArgumentException ("Invalid version format (more than 4 tokens)"); 211 int[] numbers = new int[3]; 212 try { 213 numbers[0] = Integer.parseInt((String ) elements.elementAt(0)); 214 if (numbers[0] < 0) 215 throw new IllegalArgumentException ("Negative major"); } catch (NumberFormatException nfe) { 217 throw new IllegalArgumentException ("Invalid major"); } 219 220 try { 221 if (elementSize >= 2) { 222 numbers[1] = Integer.parseInt((String ) elements.elementAt(1)); 223 if (numbers[1] < 0) 224 throw new IllegalArgumentException ("Negative minor"); } else 226 numbers[1] = 0; 227 } catch (NumberFormatException nfe) { 228 throw new IllegalArgumentException ("Invalid minor"); } 230 231 try { 232 if (elementSize >= 3) { 233 numbers[2] = Integer.parseInt((String ) elements.elementAt(2)); 234 if (numbers[2] < 0) 235 throw new IllegalArgumentException ("Invalid micro"); } else 237 numbers[2] = 0; 238 } catch (NumberFormatException nfe) { 239 throw new IllegalArgumentException ("Invalid micro"); } 241 242 Object [] result = new Object [4]; 244 result[0] = new Integer (numbers[0]); 245 result[1] = new Integer (numbers[1]); 246 result[2] = new Integer (numbers[2]); 247 if (elementSize >= 4) 248 result[3] = verifyQualifier((String ) elements.elementAt(3)); 249 else 250 result[3] = ""; return result; 252 } 253 254 261 public boolean equals(Object object) { 262 if (!(object instanceof Version)) 263 return false; 264 Version v = (Version) object; 265 return v.getMajorComponent() == major && v.getMinorComponent() == minor && v.getMicroComponent() == micro && v.getQualifierComponent().equals(qualifier); 266 } 267 268 273 public int hashCode() { 274 int code = major + minor + micro; if (qualifier.equals("")) return code; 277 else 278 return code + qualifier.hashCode(); 279 } 280 281 287 public int getMajorComponent() { 288 return major; 289 } 290 291 297 public int getMinorComponent() { 298 return minor; 299 } 300 301 307 public int getMicroComponent() { 308 return micro; 309 } 310 311 317 public String getQualifierComponent() { 318 return qualifier; 319 } 320 321 327 public boolean isInclusive() { 328 return inclusive; 329 } 330 331 351 public boolean matchGreaterOrEqualTo(Version id) { 352 if (id == null) 353 return false; 354 if (major > id.getMajorComponent()) 355 return true; 356 if ((major == id.getMajorComponent()) && (minor > id.getMinorComponent())) 357 return true; 358 if ((major == id.getMajorComponent()) && (minor == id.getMinorComponent()) && (micro > id.getMicroComponent())) 359 return true; 360 if ((major == id.getMajorComponent()) && (minor == id.getMinorComponent()) && (micro == id.getMicroComponent()) && (qualifier.compareTo(id.getQualifierComponent()) >= 0)) 361 return true; 362 else 363 return false; 364 } 365 366 385 public boolean matchMajor(Version id) { 386 if (id == null) 387 return false; 388 if (major != id.getMajorComponent()) 389 return false; 390 if (minor > id.getMinorComponent()) 391 return true; 392 if (minor < id.getMinorComponent()) 393 return false; 394 if (micro > id.getMicroComponent()) 395 return true; 396 if (micro < id.getMicroComponent()) 397 return false; 398 if (qualifier.compareTo(id.getQualifierComponent()) >= 0) 399 return true; 400 else 401 return false; 402 } 403 404 420 public boolean matchMinor(Version id) { 421 if (id == null) 422 return false; 423 if (major != id.getMajorComponent()) 424 return false; 425 if (minor != id.getMinorComponent()) 426 return false; 427 if (micro > id.getMicroComponent()) 428 return true; 429 if (micro < id.getMicroComponent()) 430 return false; 431 if (qualifier.compareTo(id.getQualifierComponent()) >= 0) 432 return true; 433 else 434 return false; 435 } 436 437 451 public boolean matchMicro(Version id) { 452 if (id == null) 453 return false; 454 if (major != id.getMajorComponent() || minor != id.getMinorComponent() || micro != id.getMicroComponent()) 455 return false; 456 if (qualifier.compareTo(id.getQualifierComponent()) >= 0) 457 return true; 458 else 459 return false; 460 } 461 462 474 public boolean matchQualifier(Version id) { 475 return equals(id); 476 } 477 478 487 public boolean isGreaterThan(Version id) { 488 489 if (id == null) { 490 if (major == 0 && minor == 0 && micro == 0 && qualifier.equals("")) return false; 492 else 493 return true; 494 } 495 496 if (major > id.getMajorComponent()) 497 return true; 498 if (major < id.getMajorComponent()) 499 return false; 500 if (minor > id.getMinorComponent()) 501 return true; 502 if (minor < id.getMinorComponent()) 503 return false; 504 if (micro > id.getMicroComponent()) 505 return true; 506 if (micro < id.getMicroComponent()) 507 return false; 508 if (qualifier.compareTo(id.getQualifierComponent()) > 0) 509 return true; 510 else 511 return false; 512 513 } 514 515 522 public String toString() { 523 String base = major + SEPARATOR + minor + SEPARATOR + micro; 524 if (qualifier.equals("")) return base; 527 else 528 return base + SEPARATOR + qualifier; 529 } 530 531 private static String verifyQualifier(String s) { 532 char[] chars = s.trim().toCharArray(); 533 boolean whitespace = false; 534 for (int i = 0; i < chars.length; i++) { 535 char c = chars[i]; 536 if (!(Character.isLetter(c) || Character.isDigit(c))) { 537 chars[i] = '-'; 538 whitespace = true; 539 } 540 } 541 return whitespace ? new String (chars) : s; 542 } 543 544 556 public int compareTo(Object o) { 557 if (!(o instanceof Version)) 558 throw new ClassCastException (); 559 560 if (equals(o)) 561 return 0; 562 563 if (isGreaterThan((Version) o)) 564 return 1; 565 566 return -1; 567 } 568 } | Popular Tags |