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,version
usually 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 ofJavaVersion
s will also differ.
-
-
Field Summary
Fields Modifier and Type Field Description static Pattern
SPEC_PATTERN
Pattern used to verify the specification version strings.static Pattern
VERSION_PATTERN
Pattern used to verify the version strings.static Pattern
VM_VERSION_PATTERN
Pattern used to verify the VM version strings.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(JavaVersion otherVersion)
Compares thisJavaVersion
with the designated one.boolean
equals(Object obj)
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.version
andjava.vm.version
.int
hashCode()
boolean
isFCS()
Returns whether (the version of) thisJavaVersion
is a FCS (first customer shipment) version, i.e. a final version (in contrast e.g.boolean
isSpecificationVersion(String specification)
Gets whether the specification version of thisJavaVersion
is equal to the designated specification version.boolean
isVendor(String vendor)
Returns whether the vendor of thisJavaVersion
is equal to the specified vendor.boolean
satisfiesMinimumSpecification(String minimumSpec)
Gets whether the specification version of thisJavaVersion
satisfies at least the designated specification version.boolean
satisfiesMinimumVersion(String minimumVersion)
Gets whether the version of thisJavaVersion
satisfies at least the designated version.boolean
satisfiesMinimumVmVersion(String minimumVmVersion)
Gets whether the VM version of thisJavaVersion
satisfies at least the designated VM version.boolean
satisfiesRange(String minimumVersion, String maximumVersion)
Gets whether the version of thisJavaVersion
lies between the specified lower and upper version limit, both boundaries included.boolean
satisfiesVersion(String versionString)
Gets whether the version of thisJavaVersion
satisfies the designated partial version.String
toString()
boolean
vmNameContains(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 newJavaVersion
for 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) thisJavaVersion
is 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 thisJavaVersion
is 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 thisJavaVersion
is equal to the designated specification version.- Parameters:
specification
- The string representation of the specification version the specification version of thisJavaVersion
has to satisfy.- Returns:
- Whether the specification version of this
JavaVersion
is equal to the designated specification version. - Throws:
IllegalArgumentException
- If the designated specification version string does not have a legal format, anIllegalArgumentException
will be thrown.
-
satisfiesMinimumSpecification
public boolean satisfiesMinimumSpecification(String minimumSpec)
Gets whether the specification version of thisJavaVersion
satisfies at least the designated specification version.- Parameters:
minimumSpec
- The minimum the specification version of thisJavaVersion
must be in order to satisfy.- Returns:
- Whether the specification version of this
JavaVersion
is the same as the designated specification version or higher. - Throws:
IllegalArgumentException
- If the designated specification version string does not have a legal format, anIllegalArgumentException
will be thrown.
-
satisfiesMinimumVersion
public boolean satisfiesMinimumVersion(String minimumVersion)
Gets whether the version of thisJavaVersion
satisfies at least the designated version.- Parameters:
minimumVersion
- The minimum the version of thisJavaVersion
must be in order to satisfy.- Returns:
- Whether the version of this
JavaVersion
is the same as the designated version or higher. - Throws:
IllegalArgumentException
- If the designated version string does not have a legal format, anIllegalArgumentException
will be thrown.
-
satisfiesVersion
public boolean satisfiesVersion(String versionString)
Gets whether the version of thisJavaVersion
satisfies 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 thisJavaVersion
has 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_22
satisfies the minimum version1.4
but it does not satisfy the version1.4
.- Parameters:
versionString
- The (partial) version that the version of thisJavaVersion
must be in order to satisfy.- Returns:
- Whether the version of this
JavaVersion
satisfies the designated version. - Throws:
IllegalArgumentException
- If the designated version string does not have a legal format, anIllegalArgumentException
will be thrown.
-
satisfiesRange
public boolean satisfiesRange(String minimumVersion, String maximumVersion)
Gets whether the version of thisJavaVersion
lies between the specified lower and upper version limit, both boundaries included.- Parameters:
minimumVersion
- The lower limit of the range in which the version of thisJavaVersion
must lie.maximumVersion
- The upper limit of the range in which the version of thisJavaVersion
must lie.- Returns:
- Whether the version of this
JavaVersion
lies 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, anIllegalArgumentException
will be thrown.
-
satisfiesMinimumVmVersion
public boolean satisfiesMinimumVmVersion(String minimumVmVersion)
Gets whether the VM version of thisJavaVersion
satisfies at least the designated VM version.- Parameters:
minimumVmVersion
- The minimum the VM version of thisJavaVersion
must be in order to satisfy.- Returns:
- Whether the VM version of this
JavaVersion
is the same as the designated VM version or higher. - Throws:
IllegalArgumentException
- If the designated VM version string does not have a legal format, anIllegalArgumentException
will 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
/64
orHotSpot
/OpenJDK
.
-
compareTo
public int compareTo(JavaVersion otherVersion)
Compares thisJavaVersion
with 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:
compareTo
in interfaceComparable<JavaVersion>
- Parameters:
otherVersion
- TheJavaVersion
to compare thisJavaVersion
to.- 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.version
andjava.vm.version
.- Returns:
- The version data of this Java VM.
-
-