1 11 package org.eclipse.pde.internal.core.util; 12 13 import org.eclipse.core.runtime.IStatus; 14 import org.eclipse.core.runtime.Status; 15 import org.eclipse.osgi.service.resolver.VersionRange; 16 import org.eclipse.pde.core.plugin.IMatchRules; 17 import org.eclipse.pde.internal.core.PDECore; 18 import org.eclipse.pde.internal.core.PDECoreMessages; 19 import org.osgi.framework.Version; 20 21 public class VersionUtil { 22 23 public static IStatus validateVersion(String versionString) { 24 try { 25 if (versionString != null) 26 new Version(versionString.trim()); 27 } catch (IllegalArgumentException e) { 28 return new Status(IStatus.ERROR, PDECore.PLUGIN_ID, IStatus.ERROR, 29 PDECoreMessages.BundleErrorReporter_InvalidFormatInBundleVersion, e); 30 } 31 return Status.OK_STATUS; 32 } 33 34 public static IStatus validateVersionRange(String versionRangeString) { 35 try { 36 new VersionRange(versionRangeString); 37 } catch (IllegalArgumentException e) { 38 return new Status(IStatus.ERROR, PDECore.PLUGIN_ID, IStatus.ERROR, 39 PDECoreMessages.BundleErrorReporter_invalidVersionRangeFormat, e); 40 } 41 return Status.OK_STATUS; 42 } 43 44 public static boolean compare(String id1, String version1, String id2, String version2, int match) { 45 if (!(id1.equals(id2))) 46 return false; 47 try { 48 Version v1 = Version.parseVersion(version1); 49 Version v2 = Version.parseVersion(version2); 50 51 switch (match) { 52 case IMatchRules.NONE : 53 case IMatchRules.COMPATIBLE : 54 return isCompatibleWith(v1, v2); 55 case IMatchRules.EQUIVALENT : 56 return isEquivalentTo(v1, v2); 57 case IMatchRules.PERFECT : 58 return v1.equals(v2); 59 case IMatchRules.GREATER_OR_EQUAL : 60 return isGreaterOrEqualTo(v1, v2); 61 } 62 } catch (RuntimeException e) { 63 } 64 return version1.equals(version2); 65 } 66 67 public static boolean isCompatibleWith(Version v1, Version v2) { 68 if (v1.getMajor() != v2.getMajor()) 69 return false; 70 if (v1.getMinor() > v2.getMinor()) 71 return true; 72 if (v1.getMinor() < v2.getMinor()) 73 return false; 74 if (v1.getMicro() > v2.getMicro()) 75 return true; 76 if (v1.getMicro() < v2.getMicro()) 77 return false; 78 return v1.getQualifier().compareTo(v2.getQualifier()) >= 0; 79 } 80 81 public static boolean isEquivalentTo(Version v1, Version v2) { 82 if (v1.getMajor() != v2.getMajor() || v1.getMinor() != v2.getMinor()) 83 return false; 84 if (v1.getMicro() > v2.getMicro()) 85 return true; 86 if (v1.getMicro() < v2.getMicro()) 87 return false; 88 return v1.getQualifier().compareTo(v2.getQualifier()) >= 0; 89 } 90 91 public static boolean isGreaterOrEqualTo(Version v1, Version v2) { 92 if (v1.getMajor() > v2.getMajor()) 93 return true; 94 if (v1.getMajor() == v2.getMajor()) { 95 if (v1.getMinor() > v2.getMinor()) 96 return true; 97 if (v1.getMinor() == v2.getMinor()) { 98 if (v1.getMicro() > v2.getMicro()) 99 return true; 100 if (v1.getMicro() == v2.getMicro()) 101 return v1.getQualifier().compareTo(v2.getQualifier()) >= 0; 102 } 103 } 104 return false; 105 } 106 107 public static int compareMacroMinorMicro(Version v1, Version v2) { 108 int result = v1.getMajor() - v2.getMajor(); 109 if (result != 0) 110 return result; 111 112 result = v1.getMinor() - v2.getMinor(); 113 if (result != 0) 114 return result; 115 116 result = v1.getMicro() - v2.getMicro(); 117 return result; 118 } 119 120 } 121 | Popular Tags |