1 18 19 package org.eclipse.pde.internal.build.tasks; 20 21 import java.util.NoSuchElementException ; 22 import java.util.StringTokenizer ; 23 24 43 44 public class Version implements Comparable { 45 private final int major; 46 private final int minor; 47 private final int micro; 48 private final String qualifier; 49 private static final String SEPARATOR = "."; 51 55 public static final Version emptyVersion = new Version(0, 0, 0); 56 57 69 public Version(int major, int minor, int micro) { 70 this(major, minor, micro, null); 71 } 72 73 85 public Version(int major, int minor, int micro, String qualifier) { 86 if (qualifier == null) { 87 qualifier = ""; } 89 90 this.major = major; 91 this.minor = minor; 92 this.micro = micro; 93 this.qualifier = qualifier; 94 validate(); 95 } 96 97 119 public Version(String version) { 120 int major = 0; 121 int minor = 0; 122 int micro = 0; 123 String qualifier = ""; 125 try { 126 StringTokenizer st = new StringTokenizer (version, SEPARATOR, true); 127 major = Integer.parseInt(st.nextToken()); 128 129 if (st.hasMoreTokens()) { 130 st.nextToken(); minor = Integer.parseInt(st.nextToken()); 132 133 if (st.hasMoreTokens()) { 134 st.nextToken(); micro = Integer.parseInt(st.nextToken()); 136 137 if (st.hasMoreTokens()) { 138 st.nextToken(); qualifier = st.nextToken(); 140 141 if (st.hasMoreTokens()) { 142 throw new IllegalArgumentException ("invalid format"); } 144 } 145 } 146 } 147 } 148 catch (NoSuchElementException e) { 149 throw new IllegalArgumentException ("invalid format"); } 151 152 this.major = major; 153 this.minor = minor; 154 this.micro = micro; 155 this.qualifier = qualifier; 156 validate(); 157 } 158 159 165 private void validate() { 166 if (major < 0) { 167 throw new IllegalArgumentException ("negative major"); } 169 if (minor < 0) { 170 throw new IllegalArgumentException ("negative minor"); } 172 if (micro < 0) { 173 throw new IllegalArgumentException ("negative micro"); } 175 int length = qualifier.length(); 176 for (int i = 0; i < length; i++) { 177 if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-".indexOf(qualifier.charAt(i)) == -1) { throw new IllegalArgumentException ("invalid qualifier"); } 180 } 181 } 182 183 198 public static Version parseVersion(String version) { 199 if (version == null) { 200 return emptyVersion; 201 } 202 203 version = version.trim(); 204 if (version.length() == 0) { 205 return emptyVersion; 206 } 207 208 return new Version(version); 209 } 210 211 216 public int getMajor() { 217 return major; 218 } 219 220 225 public int getMinor() { 226 return minor; 227 } 228 229 234 public int getMicro() { 235 return micro; 236 } 237 238 243 public String getQualifier() { 244 return qualifier; 245 } 246 247 257 public String toString() { 258 String base = major + SEPARATOR + minor + SEPARATOR + micro; 259 if (qualifier.length() == 0) { return base; 261 } 262 else { 263 return base + SEPARATOR + qualifier; 264 } 265 } 266 267 272 public int hashCode() { 273 return (major << 24) + (minor << 16) + (micro << 8) 274 + qualifier.hashCode(); 275 } 276 277 290 public boolean equals(Object object) { 291 if (object == this) { return true; 293 } 294 295 if (!(object instanceof Version)) { 296 return false; 297 } 298 299 Version other = (Version) object; 300 return (major == other.major) && (minor == other.minor) 301 && (micro == other.micro) && qualifier.equals(other.qualifier); 302 } 303 304 329 public int compareTo(Object object) { 330 if (object == this) { return 0; 332 } 333 334 Version other = (Version) object; 335 336 int result = major - other.major; 337 if (result != 0) { 338 return result; 339 } 340 341 result = minor - other.minor; 342 if (result != 0) { 343 return result; 344 } 345 346 result = micro - other.micro; 347 if (result != 0) { 348 return result; 349 } 350 351 return qualifier.compareTo(other.qualifier); 352 } 353 } 354 | Popular Tags |