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 of JavaVersions will also differ.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Pattern
    Pattern used to verify the specification version strings.
    static final Pattern
    Pattern used to verify the version strings.
    static final Pattern
    Pattern used to verify the VM version strings.
  • Constructor Summary

    Constructors
    Constructor
    Description
    JavaVersion(String vendor, String specification, String version, String vmVersion, String vmName)
    Constructs a new JavaVersion for the given vendor, specification version, version, VM version and VM name.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    compareTo(JavaVersion otherVersion)
    Compares this JavaVersion with the designated one.
    boolean
     
    Returns the version data of the Java VM this method is executed in based on the system properties java.vendor, java.specification.version, java.version and java.vm.version.
    int
     
    boolean
    Returns whether (the version of) this JavaVersion is a FCS (first customer shipment) version, i.e.
    boolean
    Gets whether the specification version of this JavaVersion is equal to the designated specification version.
    boolean
    isVendor(String vendor)
    Returns whether the vendor of this JavaVersion is equal to the specified vendor.
    boolean
    Gets whether the specification version of this JavaVersion satisfies at least the designated specification version.
    boolean
    Gets whether the version of this JavaVersion satisfies at least the designated version.
    boolean
    Gets whether the VM version of this JavaVersion satisfies at least the designated VM version.
    boolean
    satisfiesRange(String minimumVersion, String maximumVersion)
    Gets whether the version of this JavaVersion lies between the specified lower and upper version limit, both boundaries included.
    boolean
    satisfiesVersion(String versionString)
    Gets whether the version of this JavaVersion satisfies the designated partial version.
     
    boolean
    Gets whether the VM name contains the designated string (case-sensitive).

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • 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 with openj9-.
  • Constructor Details

    • JavaVersion

      public JavaVersion(String vendor, String specification, String version, String vmVersion, String vmName)
      Constructs a new JavaVersion 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 Details

    • isFCS

      public boolean isFCS()
      Returns whether (the version of) this JavaVersion 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 this JavaVersion 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 this JavaVersion is equal to the designated specification version.
      Parameters:
      specification - The string representation of the specification version the specification version of this JavaVersion 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, an IllegalArgumentException will be thrown.
    • satisfiesMinimumSpecification

      public boolean satisfiesMinimumSpecification(String minimumSpec)
      Gets whether the specification version of this JavaVersion satisfies at least the designated specification version.
      Parameters:
      minimumSpec - The minimum the specification version of this JavaVersion 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, an IllegalArgumentException will be thrown.
    • satisfiesMinimumVersion

      public boolean satisfiesMinimumVersion(String minimumVersion)
      Gets whether the version of this JavaVersion satisfies at least the designated version.
      Parameters:
      minimumVersion - The minimum the version of this JavaVersion 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, an IllegalArgumentException will be thrown.
    • satisfiesVersion

      public boolean satisfiesVersion(String versionString)
      Gets whether the version of this JavaVersion 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 this JavaVersion has to be at least the designated version. This being an FCS version also satisfies the condition.
      This differs from satisfiesMinimumVersion(String) since the partial version has to match completely. For instance 1.5.0_22 satisfies the minimum version 1.4 but it does not satisfy the version 1.4.
      Parameters:
      versionString - The (partial) version that the version of this JavaVersion 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, an IllegalArgumentException will be thrown.
    • satisfiesRange

      public boolean satisfiesRange(String minimumVersion, String maximumVersion)
      Gets whether the version of this JavaVersion 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 this JavaVersion must lie.
      maximumVersion - The upper limit of the range in which the version of this JavaVersion 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, an IllegalArgumentException will be thrown.
    • satisfiesMinimumVmVersion

      public boolean satisfiesMinimumVmVersion(String minimumVmVersion)
      Gets whether the VM version of this JavaVersion satisfies at least the designated VM version.
      Parameters:
      minimumVmVersion - The minimum the VM version of this JavaVersion 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, an IllegalArgumentException 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 or HotSpot/OpenJDK.
    • compareTo

      public int compareTo(JavaVersion otherVersion)
      Compares this JavaVersion 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 interface Comparable<JavaVersion>
      Parameters:
      otherVersion - The JavaVersion to compare this JavaVersion to.
      Returns:
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getCurrent

      public static JavaVersion getCurrent()
      Returns the version data of the Java VM this method is executed in based on the system properties java.vendor, java.specification.version, java.version and java.vm.version.
      Returns:
      The version data of this Java VM.