Version.java

00001 /*
00002  * $Header: /cvshome/build/org.osgi.framework/src/org/osgi/framework/Version.java,v 1.16 2006/06/16 16:31:18 hargrave Exp $
00003  * 
00004  * Copyright (c) OSGi Alliance (2004, 2006). All Rights Reserved.
00005  * 
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *      http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 package org.osgi.framework;
00020 
00021 import java.util.NoSuchElementException;
00022 import java.util.StringTokenizer;
00023 
00044 public class Version implements Comparable {
00045         private final int                       major;
00046         private final int                       minor;
00047         private final int                       micro;
00048         private final String            qualifier;
00049         private static final String     SEPARATOR               = ".";                                  //$NON-NLS-1$
00050 
00055         public static final Version     emptyVersion    = new Version(0, 0, 0);
00056 
00069         public Version(int major, int minor, int micro) {
00070                 this(major, minor, micro, null);
00071         }
00072 
00085         public Version(int major, int minor, int micro, String qualifier) {
00086                 if (qualifier == null) {
00087                         qualifier = ""; //$NON-NLS-1$
00088                 }
00089 
00090                 this.major = major;
00091                 this.minor = minor;
00092                 this.micro = micro;
00093                 this.qualifier = qualifier;
00094                 validate();
00095         }
00096 
00119         public Version(String version) {
00120                 int major = 0;
00121                 int minor = 0;
00122                 int micro = 0;
00123                 String qualifier = ""; //$NON-NLS-1$
00124 
00125                 try {
00126                         StringTokenizer st = new StringTokenizer(version, SEPARATOR, true);
00127                         major = Integer.parseInt(st.nextToken());
00128 
00129                         if (st.hasMoreTokens()) {
00130                                 st.nextToken(); // consume delimiter
00131                                 minor = Integer.parseInt(st.nextToken());
00132 
00133                                 if (st.hasMoreTokens()) {
00134                                         st.nextToken(); // consume delimiter
00135                                         micro = Integer.parseInt(st.nextToken());
00136 
00137                                         if (st.hasMoreTokens()) {
00138                                                 st.nextToken(); // consume delimiter
00139                                                 qualifier = st.nextToken();
00140 
00141                                                 if (st.hasMoreTokens()) {
00142                                                         throw new IllegalArgumentException("invalid format"); //$NON-NLS-1$
00143                                                 }
00144                                         }
00145                                 }
00146                         }
00147                 }
00148                 catch (NoSuchElementException e) {
00149                         throw new IllegalArgumentException("invalid format"); //$NON-NLS-1$
00150                 }
00151 
00152                 this.major = major;
00153                 this.minor = minor;
00154                 this.micro = micro;
00155                 this.qualifier = qualifier;
00156                 validate();
00157         }
00158 
00165         private void validate() {
00166                 if (major < 0) {
00167                         throw new IllegalArgumentException("negative major"); //$NON-NLS-1$
00168                 }
00169                 if (minor < 0) {
00170                         throw new IllegalArgumentException("negative minor"); //$NON-NLS-1$
00171                 }
00172                 if (micro < 0) {
00173                         throw new IllegalArgumentException("negative micro"); //$NON-NLS-1$
00174                 }
00175                 int length = qualifier.length();
00176                 for (int i = 0; i < length; i++) {
00177                         if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-".indexOf(qualifier.charAt(i)) == -1) { //$NON-NLS-1$
00178                                 throw new IllegalArgumentException("invalid qualifier"); //$NON-NLS-1$
00179                         }
00180                 }
00181         }
00182 
00198         public static Version parseVersion(String version) {
00199                 if (version == null) {
00200                         return emptyVersion;
00201                 }
00202 
00203                 version = version.trim();
00204                 if (version.length() == 0) {
00205                         return emptyVersion;
00206                 }
00207 
00208                 return new Version(version);
00209         }
00210 
00216         public int getMajor() {
00217                 return major;
00218         }
00219 
00225         public int getMinor() {
00226                 return minor;
00227         }
00228 
00234         public int getMicro() {
00235                 return micro;
00236         }
00237 
00243         public String getQualifier() {
00244                 return qualifier;
00245         }
00246 
00257         public String toString() {
00258                 String base = major + SEPARATOR + minor + SEPARATOR + micro;
00259                 if (qualifier.length() == 0) { //$NON-NLS-1$
00260                         return base;
00261                 }
00262                 else {
00263                         return base + SEPARATOR + qualifier;
00264                 }
00265         }
00266 
00272         public int hashCode() {
00273                 return (major << 24) + (minor << 16) + (micro << 8)
00274                                 + qualifier.hashCode();
00275         }
00276 
00290         public boolean equals(Object object) {
00291                 if (object == this) { // quicktest
00292                         return true;
00293                 }
00294 
00295                 if (!(object instanceof Version)) {
00296                         return false;
00297                 }
00298 
00299                 Version other = (Version) object;
00300                 return (major == other.major) && (minor == other.minor)
00301                                 && (micro == other.micro) && qualifier.equals(other.qualifier);
00302         }
00303 
00329         public int compareTo(Object object) {
00330                 if (object == this) { // quicktest
00331                         return 0;
00332                 }
00333 
00334                 Version other = (Version) object;
00335 
00336                 int result = major - other.major;
00337                 if (result != 0) {
00338                         return result;
00339                 }
00340 
00341                 result = minor - other.minor;
00342                 if (result != 0) {
00343                         return result;
00344                 }
00345 
00346                 result = micro - other.micro;
00347                 if (result != 0) {
00348                         return result;
00349                 }
00350 
00351                 return qualifier.compareTo(other.qualifier);
00352         }
00353 }

Generated on Mon Jan 11 21:19:17 2010 for OpenMobileIS by  doxygen 1.5.4