1 11 package org.eclipse.core.runtime; 12 13 import java.util.StringTokenizer ; 14 import java.util.Vector ; 15 import org.eclipse.core.internal.runtime.CommonMessages; 16 import org.eclipse.core.internal.runtime.IRuntimeConstants; 17 import org.eclipse.osgi.util.NLS; 18 import org.osgi.framework.Version; 19 20 58 public final class PluginVersionIdentifier { 59 60 private Version version; 61 62 private static final String SEPARATOR = "."; 64 71 public PluginVersionIdentifier(int major, int minor, int service) { 72 this(major, minor, service, null); 73 } 74 75 84 public PluginVersionIdentifier(int major, int minor, int service, String qualifier) { 85 89 if (major < 0) 90 Assert.isTrue(false, NLS.bind(CommonMessages.parse_postiveMajor, major + SEPARATOR + minor + SEPARATOR + service + SEPARATOR + qualifier)); 91 if (minor < 0) 92 Assert.isTrue(false, NLS.bind(CommonMessages.parse_postiveMinor, major + SEPARATOR + minor + SEPARATOR + service + SEPARATOR + qualifier)); 93 if (service < 0) 94 Assert.isTrue(false, NLS.bind(CommonMessages.parse_postiveService, major + SEPARATOR + minor + SEPARATOR + service + SEPARATOR + qualifier)); 95 96 this.version = new Version(major, minor, service, qualifier); 97 } 98 99 117 public PluginVersionIdentifier(String versionId) { 118 Object [] parts = parseVersion(versionId); 119 version = new Version(((Integer ) parts[0]).intValue(), ((Integer ) parts[1]).intValue(), ((Integer ) parts[2]).intValue(), (String ) parts[3]); 120 } 121 122 131 public static IStatus validateVersion(String version) { 132 try { 133 parseVersion(version); 134 } catch (RuntimeException e) { 135 return new Status(IStatus.ERROR, IRuntimeConstants.PI_RUNTIME, IStatus.ERROR, e.getMessage(), e); 136 } 137 return Status.OK_STATUS; 138 } 139 140 private static Object [] parseVersion(String versionId) { 141 142 if (versionId == null) 146 Assert.isNotNull(null, CommonMessages.parse_emptyPluginVersion); 147 String s = versionId.trim(); 148 if (s.equals("")) Assert.isTrue(false, CommonMessages.parse_emptyPluginVersion); 150 if (s.startsWith(SEPARATOR)) 151 Assert.isTrue(false, NLS.bind(CommonMessages.parse_separatorStartVersion, s)); 152 if (s.endsWith(SEPARATOR)) 153 Assert.isTrue(false, NLS.bind(CommonMessages.parse_separatorEndVersion, s)); 154 if (s.indexOf(SEPARATOR + SEPARATOR) != -1) 155 Assert.isTrue(false, NLS.bind(CommonMessages.parse_doubleSeparatorVersion, s)); 156 157 StringTokenizer st = new StringTokenizer (s, SEPARATOR); 158 Vector elements = new Vector (4); 159 160 while (st.hasMoreTokens()) 161 elements.addElement(st.nextToken()); 162 163 int elementSize = elements.size(); 164 165 if (elementSize <= 0) 166 Assert.isTrue(false, NLS.bind(CommonMessages.parse_oneElementPluginVersion, s)); 167 if (elementSize > 4) 168 Assert.isTrue(false, NLS.bind(CommonMessages.parse_fourElementPluginVersion, s)); 169 170 int[] numbers = new int[3]; 171 try { 172 numbers[0] = Integer.parseInt((String ) elements.elementAt(0)); 173 if (numbers[0] < 0) 174 Assert.isTrue(false, NLS.bind(CommonMessages.parse_postiveMajor, s)); 175 } catch (NumberFormatException nfe) { 176 Assert.isTrue(false, NLS.bind(CommonMessages.parse_numericMajorComponent, s)); 177 } 178 179 try { 180 if (elementSize >= 2) { 181 numbers[1] = Integer.parseInt((String ) elements.elementAt(1)); 182 if (numbers[1] < 0) 183 Assert.isTrue(false, NLS.bind(CommonMessages.parse_postiveMinor, s)); 184 } else 185 numbers[1] = 0; 186 } catch (NumberFormatException nfe) { 187 Assert.isTrue(false, NLS.bind(CommonMessages.parse_numericMinorComponent, s)); 188 } 189 190 try { 191 if (elementSize >= 3) { 192 numbers[2] = Integer.parseInt((String ) elements.elementAt(2)); 193 if (numbers[2] < 0) 194 Assert.isTrue(false, NLS.bind(CommonMessages.parse_postiveService, s)); 195 } else 196 numbers[2] = 0; 197 } catch (NumberFormatException nfe) { 198 Assert.isTrue(false, NLS.bind(CommonMessages.parse_numericServiceComponent, s)); 199 } 200 201 Object [] result = new Object [4]; 203 result[0] = new Integer (numbers[0]); 204 result[1] = new Integer (numbers[1]); 205 result[2] = new Integer (numbers[2]); 206 if (elementSize >= 4) 207 result[3] = (String ) elements.elementAt(3); 208 else 209 result[3] = ""; return result; 211 } 212 213 220 public boolean equals(Object object) { 221 if (!(object instanceof PluginVersionIdentifier)) 222 return false; 223 PluginVersionIdentifier v = (PluginVersionIdentifier) object; 224 return version.equals(v.version); 225 } 226 227 232 public int hashCode() { 233 return version.hashCode(); 234 } 235 236 242 public int getMajorComponent() { 243 return version.getMajor(); 244 } 245 246 252 public int getMinorComponent() { 253 return version.getMinor(); 254 } 255 256 262 public int getServiceComponent() { 263 return version.getMicro(); 264 } 265 266 272 public String getQualifierComponent() { 273 return version.getQualifier(); 274 } 275 276 297 public boolean isGreaterOrEqualTo(PluginVersionIdentifier id) { 298 if (id == null) 299 return false; 300 if (getMajorComponent() > id.getMajorComponent()) 301 return true; 302 if ((getMajorComponent() == id.getMajorComponent()) && (getMinorComponent() > id.getMinorComponent())) 303 return true; 304 if ((getMajorComponent() == id.getMajorComponent()) && (getMinorComponent() == id.getMinorComponent()) && (getServiceComponent() > id.getServiceComponent())) 305 return true; 306 if ((getMajorComponent() == id.getMajorComponent()) && (getMinorComponent() == id.getMinorComponent()) && (getServiceComponent() == id.getServiceComponent()) && (getQualifierComponent().compareTo(id.getQualifierComponent()) >= 0)) 307 return true; 308 else 309 return false; 310 } 311 312 331 public boolean isCompatibleWith(PluginVersionIdentifier id) { 332 if (id == null) 333 return false; 334 if (getMajorComponent() != id.getMajorComponent()) 335 return false; 336 if (getMinorComponent() > id.getMinorComponent()) 337 return true; 338 if (getMinorComponent() < id.getMinorComponent()) 339 return false; 340 if (getServiceComponent() > id.getServiceComponent()) 341 return true; 342 if (getServiceComponent() < id.getServiceComponent()) 343 return false; 344 if (getQualifierComponent().compareTo(id.getQualifierComponent()) >= 0) 345 return true; 346 else 347 return false; 348 } 349 350 367 public boolean isEquivalentTo(PluginVersionIdentifier id) { 368 if (id == null) 369 return false; 370 if (getMajorComponent() != id.getMajorComponent()) 371 return false; 372 if (getMinorComponent() != id.getMinorComponent()) 373 return false; 374 if (getServiceComponent() > id.getServiceComponent()) 375 return true; 376 if (getServiceComponent() < id.getServiceComponent()) 377 return false; 378 if (getQualifierComponent().compareTo(id.getQualifierComponent()) >= 0) 379 return true; 380 else 381 return false; 382 } 383 384 397 public boolean isPerfect(PluginVersionIdentifier id) { 398 if (id == null) 399 return false; 400 if ((getMajorComponent() != id.getMajorComponent()) || (getMinorComponent() != id.getMinorComponent()) || (getServiceComponent() != id.getServiceComponent()) || (!getQualifierComponent().equals(id.getQualifierComponent()))) 401 return false; 402 else 403 return true; 404 } 405 406 415 public boolean isGreaterThan(PluginVersionIdentifier id) { 416 417 if (id == null) { 418 if (getMajorComponent() == 0 && getMinorComponent() == 0 && getServiceComponent() == 0 && getQualifierComponent().equals("")) return false; 420 else 421 return true; 422 } 423 424 if (getMajorComponent() > id.getMajorComponent()) 425 return true; 426 if (getMajorComponent() < id.getMajorComponent()) 427 return false; 428 if (getMinorComponent() > id.getMinorComponent()) 429 return true; 430 if (getMinorComponent() < id.getMinorComponent()) 431 return false; 432 if (getServiceComponent() > id.getServiceComponent()) 433 return true; 434 if (getServiceComponent() < id.getServiceComponent()) 435 return false; 436 if (getQualifierComponent().compareTo(id.getQualifierComponent()) > 0) 437 return true; 438 else 439 return false; 440 441 } 442 443 450 public String toString() { 451 return version.toString(); 452 } 453 454 } 455 | Popular Tags |