Class VersionRange

java.lang.Object
com.helger.commons.version.VersionRange
All Implemented Interfaces:
IComparable<VersionRange>, Comparable<VersionRange>

@Immutable public final class VersionRange extends Object implements IComparable<VersionRange>
This class represents a range of versions. Each range needs at least a lower bound but can as well have an upper bound. See OSGi v4 reference 3.2.5
Author:
Philip Helger
  • Field Details

    • DEFAULT_VERSION_RANGE_STRING

      public static final String DEFAULT_VERSION_RANGE_STRING
      Default version range string.
      See Also:
  • Constructor Details

    • VersionRange

      public VersionRange(@Nonnull Version aFloorVersion, @Nullable Version aCeilingVersion)
      Create a new version range depicted by two versions, assuming that both the floor and the ceiling version should be included meaning we have an inclusive interval.
      Parameters:
      aFloorVersion - the floor version of the range - may not be null
      aCeilingVersion - the ceiling version of the range - may be null
      Throws:
      IllegalArgumentException - if the floor version to be used is > the ceiling version or if the floor version is null.
    • VersionRange

      public VersionRange(@Nonnull Version aFloorVersion, boolean bIncludeFloorVersion, @Nullable Version aCeilingVersion, boolean bIncludeCeilingVersion)
      Create a new version range depicted by two versions.
      Parameters:
      aFloorVersion - the floor version of the range - may not be null
      bIncludeFloorVersion - if true, a >= comparison is used on the version number, else a > comparison is used
      aCeilingVersion - the ceiling version of the range - may be null
      bIncludeCeilingVersion - if true, a <= comparison is used on the version number, else a < comparison is used
      Throws:
      IllegalArgumentException - if the floor version to be used is > the ceiling version or if the floor version is null.
  • Method Details

    • parse

      @Nonnull public static VersionRange parse(@Nullable String sVersionString)
      Construct a version range object from a string.
      Examples:
      • [1.2.3, 4.5.6) -- 1.2.3 <= x < 4.5.6
      • [1.2.3, 4.5.6] -- 1.2.3 <= x <= 4.5.6
      • (1.2.3, 4.5.6) -- 1.2.3 < x < 4.5.6
      • (1.2.3, 4.5.6] -- 1.2.3 < x <= 4.5.6
      • 1.2.3 -- 1.2.3 <= x
      • [1.2.3 -- 1.2.3 <= x
      • (1.2.3 -- 1.2.3 < x
      • null -- 0.0.0 <= x
      • 1, 4 -- 1 <= x <= 4
      Parameters:
      sVersionString - the version range in a string format as depicted above
      Returns:
      The parsed VersionRange object
      Throws:
      IllegalArgumentException - if the floor version is < than the ceiling version
    • isIncludingFloor

      public boolean isIncludingFloor()
    • getFloorVersion

      @Nullable public Version getFloorVersion()
    • isIncludingCeil

      public boolean isIncludingCeil()
    • getCeilVersion

      @Nullable public Version getCeilVersion()
    • versionMatches

      public boolean versionMatches(@Nonnull Version rhs)
    • compareTo

      public int compareTo(@Nonnull VersionRange rhs)
      Compare this version range to another version range. Returns -1 if this is < than the passed version or +1 if this is > the passed version range
      Specified by:
      compareTo in interface Comparable<VersionRange>
      Parameters:
      rhs - the version range to compare to
      Returns:
      0 if the passed version range is equal to this version range
      -1 if the floor version of this is < than the floor version of the passed version range.
      -1 if the floor versions are equal but the ceiling version of this has a lower upper bound than the passed version range
      +1 if the floor version of this is > than the floor version of the passed version range.
      +1 if the floor versions are equal but the ceiling version of this has a higher upper bound than the passed version range
    • getAsString

      @Nonnull public String getAsString()
      Converts the version range to a string. The brackets whether floor or ceiling version should be included or not is always prepended and appended. If a ceiling version is present, the ceiling version is appended with a single comma as a delimiter.
      Example return: "[1.2.3,4.5.6)"
      Returns:
      The version range in a parseable string format.
    • getAsString

      @Nonnull public String getAsString(boolean bPrintZeroElements)
      Converts the version range to a string. The brackets whether floor or ceiling version should be included or not is always prepended and appended. If a ceiling version is present, the ceiling version is appended with a single comma as a delimiter.
      Example return: "[1.2.3,4.5.6)"
      Parameters:
      bPrintZeroElements - If true than trailing zeroes are printed, otherwise printed zeroes are not printed.
      Returns:
      Never null.
    • equals

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

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

      public String toString()
      Overrides:
      toString in class Object