software development

Version.java

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 Version.java.

Version.java 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.
System.out.println(v.getNumberOfComponents());

// The following statement prints "The embedded version is ". This is the 
// text before the version in the string that was parsed.
System.out.println(v.getPrefix());

// Prints "4"
System.out.println(v.getMajor());

// Prints "0"
System.out.println(v.getMinor());

// Prints "0"
System.out.println(v.getBuild());

// Prints "0"
System.out.println(v.getRevision());

// The following statement prints ".". This is the 
// text after the version in the string that was parsed.
System.out.println(v.getSuffix());

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

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

// Prints "4.0"
System.out.println(v.toString(2));

// Prints "4.0.0"
System.out.println(v.toString(3));

// Prints "4.0.0.0"
System.out.println(v.toString(4));

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 "0.0.0.0"
System.out.println(v.toString());

// The following statement prints "0" because this Version object
// has not had any of its components directly initialized.
System.out.println(v.getNumberOfComponents());

v.setMajor(3);

// The following statement prints "1" because the MAJOR
// component was initialized.
System.out.println(v.getNumberOfComponents());

v.setBuild(42);

// 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.
System.out.println(v.getNumberOfComponents());

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("7.5.3.1");

// 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.
System.out.println(v1.equals(v2));

// Make v2 greater than v1.
v2.setRevision(4);

// The following statement prints "false" because v1 is less than v2.
System.out.println(v1.equals(v2));

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

// The following statement prints "1" because v2 is greater than v1.
System.out.println(v2.compareTo(v1));

// Make v1 equal to v2.
v1.setRevision(4);

// The following statement prints "0" because v2 is equal to v1.
System.out.println(v2.compareTo(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!

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s