One of the more interesting utility classes in the .NET Framework is the Version class in the System namespace. It allows for easy management and comparisons of versions stored in the standard format MAJOR.MINOR.BUILD.REVISION. Java has not traditionally placed as much importance on versions in their manifests as the .NET Framework does, so perhaps that is why the JDK does not ship with an equivalent class for working with versions. I decided to do something about that. Presenting contains a class named Version in the net.sf.nvn.commons package that makes it easy to work with standard versions in Java. For example, to parse a version from a string requires a single line of code:

Version v = Version.parse("The embedded version is 4.0.0-SNAPSHOT and won't change");

The above code will create a initialize a new Version object with a MAJOR component of 4, a MINOR component of 0, and a BUILD component of 0. Because the REVISION component was not specified it will be initialized to the default value of 0. The new object can be used to retrieve information about how it was initialized as well:

// The following statement prints "3" because only 3 components were parsed from 
// the version, the MAJOR, MINOR, and BUILD component. The REVISION component 
// will be initialized to a default value of 0, but it was not one of the components
// parsed, and thus not counted.

// The following statement prints "The embedded version is ". This is the 
// text before the version in the string that was parsed.

// Prints "4"

// Prints "0"

// Prints "0"

// Prints "0"

// The following statement prints ".". This is the 
// text after the version in the string that was parsed.

// The toString() method is overridden to print the entire standard
// version, no matter how many components were parsed. The 
// following statement prints "".

// You can also specify how many components to print using the
// toString(int) method. The following statement prints "4".

// Prints "4.0"

// Prints "4.0.0"

// Prints ""

You don’t have to parse a version from a string to use this class. You can also instantiate a new Version object using its default constructor and then set its various components.

// Initialize a new version. 
Version v = new Version();

// Prints ""

// The following statement prints "0" because this Version object
// has not had any of its components directly initialized.


// The following statement prints "1" because the MAJOR
// component was initialized.


// The following statement prints "3" because the BUILD
// component was initialized. Although only two components
// have been directly initialized, MAJOR and BUILD, the 
// number of components include any higher-order
// elements in the version string. Thus, if the only component
// initialized after instantiating a new Version class was the
// REVISION component then the getNumberOfComponents()
// method would return a value of 4.

The Version class is also implements the Cloneable and Comparable interfaces. In addition to overriding the equals(object) and hashCode() methods, this makes it possible to clone a version and also do comparisons on them.

// Create a new version.
Version v1 = Version.parse("");

// Clone v1 into v2.
Version v2 = (Version) v1.clone();

// The following statement prints "false" because this is comparing pointers.
System.out.println(v1 == v2);

// The following statement prints "true" because the two versions are equal.

// Make v2 greater than v1.

// The following statement prints "false" because v1 is less than v2.

// The following statement prints "-1" (a negative integer) because v1 is 
// less than v2.

// The following statement prints "1" because v2 is greater than v1.

// Make v1 equal to v2.

// The following statement prints "0" because v2 is equal to v1.

I hope this class is able to help a lot of people who have shied away from working with versions in the past. Here is a link to the HEAD version of the class on SourceForge as well as its unit test class.

Hope this helps!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s