1 18 19 package org.osgi.framework; 20 21 import java.util.NoSuchElementException ; 22 import java.util.StringTokenizer ; 23 24 44 45 public class Version implements Comparable { 46 private final int major; 47 private final int minor; 48 private final int micro; 49 private final String qualifier; 50 private static final String SEPARATOR = "."; 52 56 public static final Version emptyVersion = new Version(0, 0, 0); 57 58 70 public Version(int major, int minor, int micro) { 71 this(major, minor, micro, null); 72 } 73 74 86 public Version(int major, int minor, int micro, String qualifier) { 87 if (qualifier == null) { 88 qualifier = ""; } 90 91 this.major = major; 92 this.minor = minor; 93 this.micro = micro; 94 this.qualifier = qualifier; 95 validate(); 96 } 97 98 120 public Version(String version) { 121 int major = 0; 122 int minor = 0; 123 int micro = 0; 124 String qualifier = ""; 126 try { 127 StringTokenizer st = new StringTokenizer (version, SEPARATOR, true); 128 major = Integer.parseInt(st.nextToken()); 129 130 if (st.hasMoreTokens()) { 131 st.nextToken(); minor = Integer.parseInt(st.nextToken()); 133 134 if (st.hasMoreTokens()) { 135 st.nextToken(); micro = Integer.parseInt(st.nextToken()); 137 138 if (st.hasMoreTokens()) { 139 st.nextToken(); qualifier = st.nextToken(); 141 142 if (st.hasMoreTokens()) { 143 throw new IllegalArgumentException ("invalid format"); } 145 } 146 } 147 } 148 } 149 catch (NoSuchElementException e) { 150 throw new IllegalArgumentException ("invalid format"); } 152 153 this.major = major; 154 this.minor = minor; 155 this.micro = micro; 156 this.qualifier = qualifier; 157 validate(); 158 } 159 160 166 private void validate() { 167 if (major < 0) { 168 throw new IllegalArgumentException ("negative major"); } 170 if (minor < 0) { 171 throw new IllegalArgumentException ("negative minor"); } 173 if (micro < 0) { 174 throw new IllegalArgumentException ("negative micro"); } 176 int length = qualifier.length(); 177 for (int i = 0; i < length; i++) { 178 if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-".indexOf(qualifier.charAt(i)) == -1) { throw new IllegalArgumentException ("invalid qualifier"); } 181 } 182 } 183 184 199 public static Version parseVersion(String version) { 200 if (version == null) { 201 return emptyVersion; 202 } 203 204 version = version.trim(); 205 if (version.length() == 0) { 206 return emptyVersion; 207 } 208 209 return new Version(version); 210 } 211 212 217 public int getMajor() { 218 return major; 219 } 220 221 226 public int getMinor() { 227 return minor; 228 } 229 230 235 public int getMicro() { 236 return micro; 237 } 238 239 244 public String getQualifier() { 245 return qualifier; 246 } 247 248 258 public String toString() { 259 String base = major + SEPARATOR + minor + SEPARATOR + micro; 260 if (qualifier.length() == 0) { return base; 262 } 263 else { 264 return base + SEPARATOR + qualifier; 265 } 266 } 267 268 273 public int hashCode() { 274 return (major << 24) + (minor << 16) + (micro << 8) 275 + qualifier.hashCode(); 276 } 277 278 291 public boolean equals(Object object) { 292 if (object == this) { return true; 294 } 295 296 if (!(object instanceof Version)) { 297 return false; 298 } 299 300 Version other = (Version) object; 301 return (major == other.major) && (minor == other.minor) 302 && (micro == other.micro) && qualifier.equals(other.qualifier); 303 } 304 305 330 public int compareTo(Object object) { 331 if (object == this) { return 0; 333 } 334 335 Version other = (Version) object; 336 337 int result = major - other.major; 338 if (result != 0) { 339 return result; 340 } 341 342 result = minor - other.minor; 343
|