Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2004, 2012). All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.osgi.framework;
 
Version identifier for capabilities such as bundles and packages.

Version identifiers have four components.

  1. Major version. A non-negative integer.
  2. Minor version. A non-negative integer.
  3. Micro version. A non-negative integer.
  4. Qualifier. A text string. See Version(String) for the format of the qualifier string.

Version objects are immutable.

Version:
$Id: a0b5a865f7fbf2b3dcb77a13b2e99da0b64702bb $
Since:
1.3
Immutable:
 
 
 public class Version implements Comparable<Version> {
 	private final int			major;
 	private final int			minor;
 	private final int			micro;
 	private final String		qualifier;
 	private static final String	SEPARATOR".";
 	private transient String	versionString /* default to null */;
 	private transient int		hash /* default to 0 */;

The empty version "0.0.0".
 
 	public static final Version	emptyVersionnew Version(0, 0, 0);

Creates a version identifier from the specified numerical components.

The qualifier is set to the empty string.

Parameters:
major Major component of the version identifier.
minor Minor component of the version identifier.
micro Micro component of the version identifier.
Throws:
java.lang.IllegalArgumentException If the numerical components are negative.
 
 	public Version(int majorint minorint micro) {
 		this(majorminormicronull);
 	}

Creates a version identifier from the specified components.

Parameters:
major Major component of the version identifier.
minor Minor component of the version identifier.
micro Micro component of the version identifier.
qualifier Qualifier component of the version identifier. If null is specified, then the qualifier will be set to the empty string.
Throws:
java.lang.IllegalArgumentException If the numerical components are negative or the qualifier string is invalid.
 
 	public Version(int majorint minorint microString qualifier) {
 		if (qualifier == null) {
 			qualifier = "";
 		}
 
 		this. = major;
 		this. = minor;
 		this. = micro;
 		this. = qualifier;
 	}

Creates a version identifier from the specified string.

Version string grammar:

 version ::= major('.'minor('.'micro('.'qualifier)?)?)?
 major ::= digit+
 minor ::= digit+
 micro ::= digit+
 qualifier ::= (alpha|digit|'_'|'-')+
 digit ::= [0..9]
 alpha ::= [a..zA..Z]
 

Parameters:
version String representation of the version identifier. There must be no whitespace in the argument.
Throws:
java.lang.IllegalArgumentException If version is improperly formatted.
	public Version(String version) {
		int maj = 0;
		int min = 0;
		int mic = 0;
		String qual = "";
		try {
			StringTokenizer st = new StringTokenizer(versiontrue);
			maj = parseInt(st.nextToken(), version);
			if (st.hasMoreTokens()) { // minor
				st.nextToken(); // consume delimiter
				min = parseInt(st.nextToken(), version);
				if (st.hasMoreTokens()) { // micro
					st.nextToken(); // consume delimiter
					mic = parseInt(st.nextToken(), version);
					if (st.hasMoreTokens()) { // qualifier separator
						st.nextToken(); // consume delimiter
						qual = st.nextToken(""); // remaining string
						if (st.hasMoreTokens()) { // fail safe
							throw new IllegalArgumentException("invalid version \"" + version + "\": invalid format");
						}
					}
				}
			}
catch (NoSuchElementException e) {
			IllegalArgumentException iae = new IllegalArgumentException("invalid version \"" + version + "\": invalid format");
			iae.initCause(e);
			throw iae;
		}
		 = maj;
		 = min;
		 = mic;
		 = qual;
	}

Parse numeric component into an int.

Parameters:
value Numeric component
version Complete version string for exception message, if any
Returns:
int value of numeric component
	private static int parseInt(String valueString version) {
		try {
			return Integer.parseInt(value);
catch (NumberFormatException e) {
			IllegalArgumentException iae = new IllegalArgumentException("invalid version \"" + version + "\": non-numeric \"" + value + "\"");
			iae.initCause(e);
			throw iae;
		}
	}

Called by the Version constructors to validate the version components.

Throws:
java.lang.IllegalArgumentException If the numerical components are negative or the qualifier string is invalid.
	private void validate() {
		if ( < 0) {
			throw new IllegalArgumentException("invalid version \"" + toString0() + "\": negative number \"" +  + "\"");
		}
		if ( < 0) {
			throw new IllegalArgumentException("invalid version \"" + toString0() + "\": negative number \"" +  + "\"");
		}
		if ( < 0) {
			throw new IllegalArgumentException("invalid version \"" + toString0() + "\": negative number \"" +  + "\"");
		}
		for (char ch : .toCharArray()) {
			if (('A' <= ch) && (ch <= 'Z')) {
				continue;
			}
			if (('a' <= ch) && (ch <= 'z')) {
				continue;
			}
			if (('0' <= ch) && (ch <= '9')) {
				continue;
			}
			if ((ch == '_') || (ch == '-')) {
				continue;
			}
			throw new IllegalArgumentException("invalid version \"" + toString0() + "\": invalid qualifier \"" +  + "\"");
		}
	}

Parses a version identifier from the specified string.

See Version(String) for the format of the version string.

Parameters:
version String representation of the version identifier. Leading and trailing whitespace will be ignored.
Returns:
A Version object representing the version identifier. If version is null or the empty string then emptyVersion will be returned.
Throws:
java.lang.IllegalArgumentException If version is improperly formatted.
	public static Version parseVersion(String version) {
		if (version == null) {
			return ;
		}
		version = version.trim();
		if (version.length() == 0) {
			return ;
		}
		return new Version(version);
	}

Returns the major component of this version identifier.

Returns:
The major component.
	public int getMajor() {
		return ;
	}

Returns the minor component of this version identifier.

Returns:
The minor component.
	public int getMinor() {
		return ;
	}

Returns the micro component of this version identifier.

Returns:
The micro component.
	public int getMicro() {
		return ;
	}

Returns the qualifier component of this version identifier.

Returns:
The qualifier component.
	public String getQualifier() {
		return ;
	}

Returns the string representation of this version identifier.

The format of the version string will be major.minor.micro if qualifier is the empty string or major.minor.micro.qualifier otherwise.

Returns:
The string representation of this version identifier.
	public String toString() {
		return toString0();
	}

Internal toString behavior

Returns:
The string representation of this version identifier.
		if ( != null) {
		}
		int q = .length();
		StringBuffer result = new StringBuffer(20 + q);
		result.append();
		result.append();
		result.append();
		if (q > 0) {
		}
		return  = result.toString();
	}

Returns a hash code value for the object.

Returns:
An integer which is a hash code value for this object.
	public int hashCode() {
		if ( != 0) {
			return ;
		}
		int h = 31 * 17;
		h = 31 * h + ;
		h = 31 * h + ;
		h = 31 * h + ;
		h = 31 * h + .hashCode();
		return  = h;
	}

Compares this Version object to another object.

A version is considered to be equal to another version if the major, minor and micro components are equal and the qualifier component is equal (using String.equals).

Parameters:
object The Version object to be compared.
Returns:
true if object is a Version and is equal to this object; false otherwise.
	public boolean equals(Object object) {
		if (object == this) { // quicktest
			return true;
		}
		if (!(object instanceof Version)) {
			return false;
		}
		Version other = (Versionobject;
		return ( == other.major) && ( == other.minor) && ( == other.micro) && .equals(other.qualifier);
	}

Compares this Version object to another Version.

A version is considered to be less than another version if its major component is less than the other version's major component, or the major components are equal and its minor component is less than the other version's minor component, or the major and minor components are equal and its micro component is less than the other version's micro component, or the major, minor and micro components are equal and it's qualifier component is less than the other version's qualifier component (using String.compareTo).

A version is considered to be equal to another version if the major, minor and micro components are equal and the qualifier component is equal (using String.compareTo).

Parameters:
other The Version object to be compared.
Returns:
A negative integer, zero, or a positive integer if this version is less than, equal to, or greater than the specified Version object.
Throws:
java.lang.ClassCastException If the specified object is not a Version object.
	public int compareTo(Version other) {
		if (other == this) { // quicktest
			return 0;
		}
		int result =  - other.major;
		if (result != 0) {
			return result;
		}
		result =  - other.minor;
		if (result != 0) {
			return result;
		}
		result =  - other.micro;
		if (result != 0) {
			return result;
		}
		return .compareTo(other.qualifier);
	}
New to GrepCode? Check out our FAQ X