Qwylt

java.lang.module
Class Version

java.lang.Object
  extended by java.lang.module.Version
All Implemented Interfaces:
java.lang.Comparable<Version>, Matcher<Version>

public class Version
extends java.lang.Object
implements Matcher<Version>, java.lang.Comparable<Version>

A canonical, syntax agnostic version number type that supports ordered comparison.

Instances may contain any number of positive numeric components and an optional 'preview' or 'update' string qualifier. An unqualified instance is considered a 'release' version. Qualifier strings may contain any of the posix visible characters (i.e. '!' through 'n').

During comparison:

Lexicographic qualifier comparison can result in unexpected ordering (e.g. "100" < "20"); therefore, version schemes should be mapped to use additional numeric components where possible.

Since:
Dec 10, 2008
Author:
Bryan Atsatt

Nested Class Summary
static class Version.Type
           
 
Field Summary
static Version MAX_VALUE
          An instance of the maximum value.
static Version MIN_VALUE
          An instance of the minimum value.
static Version ZERO
          An instance of the release version zero.
 
Method Summary
 int compareTo(Version version)
          Compare two Version objects.
 boolean equals(java.lang.Object obj)
          Compare two Version objects for equality.
 int getComponentAt(int index)
          Return the component at the specified index.
 int getComponentCount()
          Returns the number of components in this instance.
 java.lang.String getQualifier()
          Returns the qualifier, if any.
 Version.Type getType()
          Returns the type of this instance.
 int hashCode()
          Returns a hash code for this Version.
 boolean matches(Version version)
          Tests if the specified version is equal to this instance.
static Version newPreviewVersion(int[] components, java.lang.String qualifier)
          Create a new PREVIEW version.
static Version newReleaseVersion(int... components)
          Create a new RELEASE version.
static Version newUpdateVersion(int[] components, java.lang.String qualifier)
          Create a new UPDATE version.
static Version newVersion(Version.Type type, int[] components, java.lang.String qualifier)
          Create a new version.
 java.lang.String toString()
          Returns a String object representing this Version's value, in canonical form.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MIN_VALUE

public static final Version MIN_VALUE
An instance of the minimum value.


MAX_VALUE

public static final Version MAX_VALUE
An instance of the maximum value.


ZERO

public static Version ZERO
An instance of the release version zero.

Method Detail

newPreviewVersion

public static Version newPreviewVersion(int[] components,
                                        java.lang.String qualifier)
Create a new PREVIEW version.

Parameters:
components - The components.
qualifier - The qualifier.
Returns:
The new instance.

newReleaseVersion

public static Version newReleaseVersion(int... components)
Create a new RELEASE version.

Parameters:
components - The components.
Returns:
The new instance.

newUpdateVersion

public static Version newUpdateVersion(int[] components,
                                       java.lang.String qualifier)
Create a new UPDATE version.

Parameters:
components - The components.
qualifier - The qualifier.
Returns:
The new instance.

newVersion

public static Version newVersion(Version.Type type,
                                 int[] components,
                                 java.lang.String qualifier)
Create a new version.

Parameters:
type - The type.
components - The components.
qualifier - The qualifier. Must be null if type == RELEASE, otherwise must be non-zero length.
Returns:
The new instance.

getType

public Version.Type getType()
Returns the type of this instance.

Returns:
The type.

getComponentCount

public int getComponentCount()
Returns the number of components in this instance.

Returns:
The count.

getComponentAt

public int getComponentAt(int index)
Return the component at the specified index.

Parameters:
index - The index.
Returns:
The component.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of bounds.

getQualifier

public java.lang.String getQualifier()
Returns the qualifier, if any.

Returns:
The qualifier if type is PREVIEW or UPDATE, null if RELEASE;

compareTo

public int compareTo(Version version)
Compare two Version objects.

Specified by:
compareTo in interface java.lang.Comparable<Version>
Parameters:
version - the Version to be compared.
Returns:
the value 0 if the this Version is equal to the Version argument; a value less than 0 if this Version is less than the Version argument; and a value greater than 0 if this Version is greater than the Version argument.

equals

public boolean equals(java.lang.Object obj)
Compare two Version objects for equality. The result is true if and only if the argument is not null and is a Version object for which compareTo() returns 0.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object to compare with.
Returns:
whether or not two Version objects are equal.

hashCode

public int hashCode()
Returns a hash code for this Version.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this Version.

toString

public java.lang.String toString()
Returns a String object representing this Version's value, in canonical form.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the value of this Version in canonical form.

matches

public boolean matches(Version version)
Tests if the specified version is equal to this instance.

Specified by:
matches in interface Matcher<Version>
Parameters:
version - The target version.
Returns:
true if the specified version is equal to this instance; false otherwise.

Qwylt

Send comments or questions to Bryan Atsatt