org.pfsw.text
Class Version

java.lang.Object
  extended by org.pfsw.text.Version
All Implemented Interfaces:
org.pfsw.bif.text.IStringRepresentation

public class Version
extends java.lang.Object
implements org.pfsw.bif.text.IStringRepresentation

This class provides a representation of version identifiers of pattern "x.y.z" in a way that they can be compared and sorted according to their real meaning and not just by simple string comparison.

The last element (i.e. most right) may contain non-numeric characters and will be compared as String. Such characters are limited to ascii letters, digits and '-' and '_'.

Examples:
3.1 > 3.0
3.1.1 > 3.1
2.4 < 10.0
0.11.2 > 0.1.22
1.4.7b > 1.4.7a
1.5.0_02 < 1.5.0_17
1.4.2_02 < 1.4.10_01


Field Summary
static java.lang.String SEPARATOR
          Contains the seperator between the version elements. (".")
static java.lang.String SPECIAL_CHARACTERS
          A definition of characters that are allowed in a version element.
 
Constructor Summary
protected Version()
          Initialize the new instance.
  Version(int major)
          Initialize the new instance with a major version.
  Version(int major, int minor)
          Initialize the new instance with a major and minor version.
  Version(int major, int minor, int micro)
          Initialize the new instance with a major, minor and micro version.
  Version(int major, int minor, int micro, java.lang.String qualifier)
          Initialize the new instance with a major, minor, micro version and a qualifier.
  Version(java.lang.String versionString)
          Initialize the new instance with a version string of type "x.y.z".
  Version(Version version)
          Initialize the new instance with the values from another a version.
 
Method Summary
protected  void addElement(int value)
           
 java.lang.String asString()
          Returns the version as string.
 int compareTo(java.lang.Object obj)
          Compares this object with the specified object for order.
 Version copy()
          Returns a new version object with the same value as this one.
 boolean equals(java.lang.Object obj)
          Returns true if this version is equal to the given object.
protected  org.pfsw.text.Version.VersionElement getElement(int index)
           
protected  java.util.List<org.pfsw.text.Version.VersionElement> getElements()
           
protected  int getIntValueOfElement(int index)
           
static Version getJavaBaseVersion()
          Returns the version of the current JVM with out the patch level.
static Version getJavaVersion()
          Returns the version of the current JVM.
protected  org.pfsw.text.Version.VersionElement getLastElement()
           
 int getMajorVersion()
          Returns the major version (the most left part) or -1 if this element is not available or is not numeric.
 int getMicroVersion()
          Returns the micro version (the third from left part) or -1 if this element is not available or is not numeric.
 int getMinorVersion()
          Returns the minor version (the second from left part) or -1 if this element is not available or is not numeric.
 java.lang.String[] getVersionElements()
          Returns an array where all sub-elements are contained.
 int hashCode()
          Returns a hash code
 boolean isEqualTo(java.lang.String strVersion)
          Returns true if this version is equal to the version represented by the given string.
 boolean isGreaterOrEqual(java.lang.String strVersion)
          Returns if this version is greater or equal compared to the given version.
 boolean isGreaterOrEqual(Version version)
          Returns if this version is greater or equal compared to the given version.
 boolean isGreaterThan(java.lang.String version)
          Returns if this version is greater than the given version.
 boolean isGreaterThan(Version version)
          Returns if this version is greater than the specified version.
 boolean isLessOrEqual(java.lang.String strVersion)
          Returns if this version is less or equal compared to the given version.
 boolean isLessOrEqual(Version version)
          Returns if this version is less or equal compared to the given version.
 boolean isLessThan(java.lang.String version)
          Returns if this version is less than the specified version.
 boolean isLessThan(Version version)
          Returns if this version is less than the given version.
 boolean isNumeric()
          Returns true if this version contains only positive numeric sub parts.
 boolean isValid()
          Returns true if this version contains only valid sub parts.
static boolean isValidVersion(java.lang.String str)
          Returns true if the given string represents a valid version.
protected  boolean needsSeparator(int elementIndex, org.pfsw.text.Version.VersionElement element)
           
protected  void parse(java.lang.String versionString)
           
protected  void setElements(java.util.List<org.pfsw.text.Version.VersionElement> newValue)
           
protected  StringUtil str()
           
 java.lang.String toString()
          Returns the string for this object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

SEPARATOR

public static final java.lang.String SEPARATOR
Contains the seperator between the version elements. (".")

See Also:
Constant Field Values

SPECIAL_CHARACTERS

public static final java.lang.String SPECIAL_CHARACTERS
A definition of characters that are allowed in a version element.

See Also:
Constant Field Values
Constructor Detail

Version

protected Version()
Initialize the new instance.


Version

public Version(java.lang.String versionString)
Initialize the new instance with a version string of type "x.y.z". The elements in the string separated by dots is not limited!


Version

public Version(Version version)
Initialize the new instance with the values from another a version.


Version

public Version(int major,
               int minor,
               int micro,
               java.lang.String qualifier)
Initialize the new instance with a major, minor, micro version and a qualifier. For any negative parameter value zero will be used instead!


Version

public Version(int major,
               int minor,
               int micro)
Initialize the new instance with a major, minor and micro version. For any negative parameter value zero will be used instead!


Version

public Version(int major,
               int minor)
Initialize the new instance with a major and minor version. For any negative parameter value zero will be used instead!


Version

public Version(int major)
Initialize the new instance with a major version. For a negative parameter value zero will be used instead!

Method Detail

isValidVersion

public static boolean isValidVersion(java.lang.String str)
Returns true if the given string represents a valid version.

Parameters:
str - The string to be checked if it is a valid version

getJavaVersion

public static Version getJavaVersion()
Returns the version of the current JVM.


getJavaBaseVersion

public static Version getJavaBaseVersion()
Returns the version of the current JVM with out the patch level. That is, for Java version "1.5.0_12" the version "1.5.0" will be returned.


isGreaterThan

public boolean isGreaterThan(Version version)
Returns if this version is greater than the specified version.


isLessThan

public boolean isLessThan(Version version)
Returns if this version is less than the given version.


isGreaterThan

public boolean isGreaterThan(java.lang.String version)
Returns if this version is greater than the given version.


isLessThan

public boolean isLessThan(java.lang.String version)
Returns if this version is less than the specified version.


isGreaterOrEqual

public boolean isGreaterOrEqual(Version version)
Returns if this version is greater or equal compared to the given version.


isLessOrEqual

public boolean isLessOrEqual(Version version)
Returns if this version is less or equal compared to the given version.


isGreaterOrEqual

public boolean isGreaterOrEqual(java.lang.String strVersion)
Returns if this version is greater or equal compared to the given version.


isLessOrEqual

public boolean isLessOrEqual(java.lang.String strVersion)
Returns if this version is less or equal compared to the given version.


isEqualTo

public boolean isEqualTo(java.lang.String strVersion)
Returns true if this version is equal to the version represented by the given string. If the strVersion is no valid version false will be returned.


equals

public boolean equals(java.lang.Object obj)
Returns true if this version is equal to the given object. The object must be of type Version or String.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Returns a hash code

Overrides:
hashCode in class java.lang.Object

asString

public java.lang.String asString()
Returns the version as string. Use this method to get the version string rather than toString(). The toString() might be changed in a future release to return a debug string of this object.

Specified by:
asString in interface org.pfsw.bif.text.IStringRepresentation

toString

public java.lang.String toString()
Returns the string for this object. Currently this is the same as asString(), but might change in the future. So use asString() instead!

Overrides:
toString in class java.lang.Object

copy

public Version copy()
Returns a new version object with the same value as this one.


getVersionElements

public java.lang.String[] getVersionElements()
Returns an array where all sub-elements are contained.


compareTo

public int compareTo(java.lang.Object obj)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Throws:
java.lang.IllegalArgumentException - if the specified object is not a Version

isNumeric

public boolean isNumeric()
Returns true if this version contains only positive numeric sub parts.


isValid

public boolean isValid()
Returns true if this version contains only valid sub parts.


getMajorVersion

public int getMajorVersion()
Returns the major version (the most left part) or -1 if this element is not available or is not numeric.


getMinorVersion

public int getMinorVersion()
Returns the minor version (the second from left part) or -1 if this element is not available or is not numeric.


getMicroVersion

public int getMicroVersion()
Returns the micro version (the third from left part) or -1 if this element is not available or is not numeric.


parse

protected void parse(java.lang.String versionString)

needsSeparator

protected boolean needsSeparator(int elementIndex,
                                 org.pfsw.text.Version.VersionElement element)

getIntValueOfElement

protected int getIntValueOfElement(int index)

addElement

protected void addElement(int value)

getLastElement

protected org.pfsw.text.Version.VersionElement getLastElement()

getElement

protected org.pfsw.text.Version.VersionElement getElement(int index)

getElements

protected java.util.List<org.pfsw.text.Version.VersionElement> getElements()

setElements

protected void setElements(java.util.List<org.pfsw.text.Version.VersionElement> newValue)

str

protected StringUtil str()