Class Version

java.lang.Object
org.apache.juneau.commons.lang.Version
All Implemented Interfaces:
Comparable<Version>

public class Version extends Object implements Comparable<Version>
Represents a version string such as "1.2" or "1.2.3"

Used to compare version numbers.

Equality vs. matching:

equals(Object), hashCode() and compareTo(Version) are strict and mutually consistent — two versions are only equal when their full part sequences are identical (e.g. "1.2" is not equal to "1.2.3" or "1.2.0"). For the looser prefix/compatibility comparison (where trailing parts are ignored), use matches(Version).

  • Constructor Details

    • Version

      public Version(String value)
      Constructor
      Parameters:
      value - A string of the form "#.#..." where there can be any number of parts.
      Valid values:
      • "1.2"
      • "1.2.3"
      • "0.1"
      • ".1"
      Any parts that are not numeric are interpreted as Integer.MAX_VALUE
      Can be null or empty (treated as version "0").
  • Method Details

    • of

      public static Version of(String value)
      Static creator.
      Parameters:
      value - A string of the form "#.#..." where there can be any number of parts.
      Valid values:
      • "1.2"
      • "1.2.3"
      • "0.1"
      • ".1"

      Can be null.
      Returns:
      A new header bean, or null if the value is null.
    • compareTo

      public int compareTo(Version v)
      Specified by:
      compareTo in interface Comparable<Version>
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • getMaintenance

      Returns the maintenance version part (i.e. part at index 2).
      Returns:
      The version part, never null.
    • getMajor

      Returns the major version part (i.e. part at index 0).
      Returns:
      The version part, never null.
    • getMinor

      Returns the minor version part (i.e. part at index 1).
      Returns:
      The version part, never null.
    • getPart

      public Optional<Integer> getPart(int index)
      Returns the version part at the specified zero-indexed value.
      Parameters:
      index - The index of the version part.
      Returns:
      The version part, never null.
    • hashCode

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

      public boolean isAtLeast(Version v)
      Returns true if the specified version is at least this version.
      Example:

      assertTrue(Version.of("1.2").isAtLeast(Version.of("1"))); assertFalse(Version.of("1.2").isAtLeast(Version.of("2"))); assertTrue(Version.of("1.2").isAtLeast(Version.of("1.2.3"))); assertFalse(Version.of("1.2.0").isAtLeast(Version.of("1.2.3")));

      Parameters:
      v - The version to compare to. Must not be null.
      Returns:
      true if the specified version is at least this version.
    • isAtLeast

      public boolean isAtLeast(Version v, boolean exclusive)
      Returns true if the specified version is at least this version.
      Example:

      assertTrue(Version.of("1.2.3").isAtLeast(Version.of("1.2.3", false))); assertFalse(Version.of("1.2.3").isAtLeast(Version.of("1.2.3", true)));

      Parameters:
      v - The version to compare to. Must not be null.
      exclusive - Match down-to-version but not including.
      Returns:
      true if the specified version is at least this version.
    • isAtMost

      public boolean isAtMost(Version v)
      Returns true if the specified version is at most this version.
      Example:

      assertFalse(Version.of("1.2.3").isAtMost(Version.of("1"))); assertTrue(Version.of("1.2.3").isAtMost(Version.of("2"))); assertTrue(Version.of("1.2.3").isAtMost(Version.of("1.2"))); assertFalse(Version.of("1.2.3").isAtMost(Version.of("1.2.0")));

      Parameters:
      v - The version to compare to. Must not be null.
      Returns:
      true if the specified version is at most this version.
    • isAtMost

      public boolean isAtMost(Version v, boolean exclusive)
      Returns true if the specified version is at most this version.
      Example:

      assertTrue(Version.of("1.2.3").isAtMost(Version.of("1.2.3", false))); assertFalse(Version.of("1.2.3").isAtMost(Version.of("1.2.3", true)));

      Parameters:
      v - The version to compare to. Must not be null.
      exclusive - Match up-to-version but not including.
      Returns:
      true if the specified version is at most this version.
    • matches

      public boolean matches(Version v)
      Returns true if this version matches the specified version, comparing only the parts they have in common (i.e. one version is a prefix of the other).

      This is a looser comparison than equals(Object), which requires the full version part sequences to be identical. Two versions match when every part up to the length of the shorter version is identical; any trailing parts on the longer version are ignored. Unlike equals(Object), this relation is not consistent with hashCode() and must never be used as the basis for hash- or tree-based collection membership.

      Example:

      assertTrue(Version.of("1.2.3").matches(Version.of("1.2.3"))); assertTrue(Version.of("1.2.3").matches(Version.of("1.2"))); assertTrue(Version.of("1.2").matches(Version.of("1.2.3"))); assertFalse(Version.of("1.2.3").matches(Version.of("1.2.4")));

      Parameters:
      v - The version to compare to. Must not be null.
      Returns:
      true if this version matches the specified version on their common parts.
    • toString

      public String toString()
      Overrides:
      toString in class Object