Class JavaVersion
- java.lang.Object
-
- de.aristaflow.adept2.util.JavaVersion
-
- All Implemented Interfaces:
Comparable<JavaVersion>
public class JavaVersion extends Object implements Comparable<JavaVersion>
An instance of this class represents a certain version of a Java VM. The main purpose is to test if the current version satisfies certain version requirements. So the user can e.g. be informed to upgrade to a newer version (or has to expect problems otherwise) or different code can be executed depending on which version is available.
An instance of this class consists of a specification version, a version and a VM version. This is usually for instance 1.7, 1.7.1_11 and 24.11-b01. However, the numbering scheme may differ slightly between different vendors, especially concerning the VM version.
In the following,versionusually means the second part of the 3 version numbers not the complete triple.All numbers are parsed and compared by number. This allows for different amount of digits. For instance, comparing a 2-digit-number with a 3-digit-number may lead to a wrong result when using string comparison. If strings are part of the version, a string comparison will be used at the appropriate place, i. e. previous and following numbers are compared like other numbers.
The format of the version string is described on the following website: http://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html It also considers specific representations we found in different JVM implementations and versions.
Note: this class has a natural ordering that is inconsistent with equals. While
equals(Object)considers the vendor,compareTo(JavaVersion)does not. Usually this does not matter since the vendors use different VM version patterns so instances ofJavaVersions will also differ.
-
-
Field Summary
Fields Modifier and Type Field Description static PatternSPEC_PATTERNPattern used to verify the specification version strings.static PatternVERSION_PATTERNPattern used to verify the version strings.static PatternVM_VERSION_PATTERNPattern used to verify the VM version strings.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(JavaVersion otherVersion)Compares thisJavaVersionwith the designated one.booleanequals(Object obj)static JavaVersiongetCurrent()Returns the version data of the Java VM this method is executed in based on the system propertiesjava.vendor,java.specification.version,java.versionandjava.vm.version.inthashCode()booleanisFCS()Returns whether (the version of) thisJavaVersionis a FCS (first customer shipment) version, i.e.booleanisSpecificationVersion(String specification)Gets whether the specification version of thisJavaVersionis equal to the designated specification version.booleanisVendor(String vendor)Returns whether the vendor of thisJavaVersionis equal to the specified vendor.booleansatisfiesMinimumSpecification(String minimumSpec)Gets whether the specification version of thisJavaVersionsatisfies at least the designated specification version.booleansatisfiesMinimumVersion(String minimumVersion)Gets whether the version of thisJavaVersionsatisfies at least the designated version.booleansatisfiesMinimumVmVersion(String minimumVmVersion)Gets whether the VM version of thisJavaVersionsatisfies at least the designated VM version.booleansatisfiesRange(String minimumVersion, String maximumVersion)Gets whether the version of thisJavaVersionlies between the specified lower and upper version limit, both boundaries included.booleansatisfiesVersion(String versionString)Gets whether the version of thisJavaVersionsatisfies the designated partial version.StringtoString()booleanvmNameContains(String content)Gets whether the VM name contains the designated string (case-sensitive).
-
-
-
Field Detail
-
SPEC_PATTERN
public static final Pattern SPEC_PATTERN
Pattern used to verify the specification version strings. This is a mandatory number followed by a second optional number separated by..
-
VERSION_PATTERN
public static final Pattern VERSION_PATTERN
Pattern used to verify the version strings. This is similar to the specification pattern followed by another optional number separated by., a further number separated by_or., optionally followed by an arbitrary string separated by-.
-
VM_VERSION_PATTERN
public static final Pattern VM_VERSION_PATTERN
Pattern used to verify the VM version strings. This is similar to the version pattern followed by an optional number separated by-or+. The group next to last (the arbitrary string) must not start with a digit. At the end a group starting with-is optional. This is required for Linux distributions appending their name at the end.
For OpenJ9 the complete version may start withopenj9-.
-
-
Constructor Detail
-
JavaVersion
public JavaVersion(String vendor, String specification, String version, String vmVersion, String vmName)
Constructs a newJavaVersionfor the given vendor, specification version, version, VM version and VM name.- Parameters:
vendor- The vendor string of a Java VM.specification- The specification version string of a Java VM.version- The version string of a Java VM.vmVersion- The VM version string of a Java VM.vmName- The name a Java VM, e. g. Oracle or OpenJDK.- Throws:
IllegalArgumentException- If one of the designated strings does not have a legal format. Check the patterns in this class for the format.
-
-
Method Detail
-
isFCS
public boolean isFCS()
Returns whether (the version of) thisJavaVersionis a FCS (first customer shipment) version, i.e. a final version (in contrast e.g. to a beta or early access version). An FCS version has no dash in its version string.- Returns:
- Whether this version is a FCS version.
-
isVendor
public boolean isVendor(String vendor)
Returns whether the vendor of thisJavaVersionis equal to the specified vendor.- Parameters:
vendor- The specified vendor this vendor has to satisfy.- Returns:
- Whether this vendor is equal to the specified vendor.
-
isSpecificationVersion
public boolean isSpecificationVersion(String specification)
Gets whether the specification version of thisJavaVersionis equal to the designated specification version.- Parameters:
specification- The string representation of the specification version the specification version of thisJavaVersionhas to satisfy.- Returns:
- Whether the specification version of this
JavaVersionis equal to the designated specification version. - Throws:
IllegalArgumentException- If the designated specification version string does not have a legal format, anIllegalArgumentExceptionwill be thrown.
-
satisfiesMinimumSpecification
public boolean satisfiesMinimumSpecification(String minimumSpec)
Gets whether the specification version of thisJavaVersionsatisfies at least the designated specification version.- Parameters:
minimumSpec- The minimum the specification version of thisJavaVersionmust be in order to satisfy.- Returns:
- Whether the specification version of this
JavaVersionis the same as the designated specification version or higher. - Throws:
IllegalArgumentException- If the designated specification version string does not have a legal format, anIllegalArgumentExceptionwill be thrown.
-
satisfiesMinimumVersion
public boolean satisfiesMinimumVersion(String minimumVersion)
Gets whether the version of thisJavaVersionsatisfies at least the designated version.- Parameters:
minimumVersion- The minimum the version of thisJavaVersionmust be in order to satisfy.- Returns:
- Whether the version of this
JavaVersionis the same as the designated version or higher. - Throws:
IllegalArgumentException- If the designated version string does not have a legal format, anIllegalArgumentExceptionwill be thrown.
-
satisfiesVersion
public boolean satisfiesVersion(String versionString)
Gets whether the version of thisJavaVersionsatisfies the designated partial version. The designated version need not be fully qualified. Only the provided numbers will be compared. If the designated version is a non-FCS version, the version of thisJavaVersionhas to be at least the designated version. This being an FCS version also satisfies the condition.
This differs fromsatisfiesMinimumVersion(String)since the partial version has to match completely. For instance1.5.0_22satisfies the minimum version1.4but it does not satisfy the version1.4.- Parameters:
versionString- The (partial) version that the version of thisJavaVersionmust be in order to satisfy.- Returns:
- Whether the version of this
JavaVersionsatisfies the designated version. - Throws:
IllegalArgumentException- If the designated version string does not have a legal format, anIllegalArgumentExceptionwill be thrown.
-
satisfiesRange
public boolean satisfiesRange(String minimumVersion, String maximumVersion)
Gets whether the version of thisJavaVersionlies between the specified lower and upper version limit, both boundaries included.- Parameters:
minimumVersion- The lower limit of the range in which the version of thisJavaVersionmust lie.maximumVersion- The upper limit of the range in which the version of thisJavaVersionmust lie.- Returns:
- Whether the version of this
JavaVersionlies between the specified lower and upper version limit, both boundaries included. - Throws:
IllegalArgumentException- If one of the designated version strings does not have a legal format, anIllegalArgumentExceptionwill be thrown.
-
satisfiesMinimumVmVersion
public boolean satisfiesMinimumVmVersion(String minimumVmVersion)
Gets whether the VM version of thisJavaVersionsatisfies at least the designated VM version.- Parameters:
minimumVmVersion- The minimum the VM version of thisJavaVersionmust be in order to satisfy.- Returns:
- Whether the VM version of this
JavaVersionis the same as the designated VM version or higher. - Throws:
IllegalArgumentException- If the designated VM version string does not have a legal format, anIllegalArgumentExceptionwill be thrown.
-
vmNameContains
public boolean vmNameContains(String content)
Gets whether the VM name contains the designated string (case-sensitive). This allows for instance to distinguish 32 and 64 bit VMs as well as Oracle and Open JDK.- Parameters:
content- The string which to check for whether it is part of the VM name.- Returns:
- Whether the VM name contains the designated string (case-sensitive), e. g.
32/64orHotSpot/OpenJDK.
-
compareTo
public int compareTo(JavaVersion otherVersion)
Compares thisJavaVersionwith the designated one. This compares the specification version, the version and the VM version (in this order). Note that the version considers a non-FCS version to be less than the FCS version with the same number but no appendix. Strict string comparison would lead to the opposite result.- Specified by:
compareToin interfaceComparable<JavaVersion>- Parameters:
otherVersion- TheJavaVersionto compare thisJavaVersionto.- Returns:
-
getCurrent
public static JavaVersion getCurrent()
Returns the version data of the Java VM this method is executed in based on the system propertiesjava.vendor,java.specification.version,java.versionandjava.vm.version.- Returns:
- The version data of this Java VM.
-
-