Class VersionConstraint

java.lang.Object
org.cthing.versionparser.VersionConstraint

public class VersionConstraint extends Object
Represents zero (empty constraint) or more version ranges. A version range is a contiguous set of versions (e.g. version 1.0 inclusive through version 2.0 exclusive). Constraints can be weak or strong (the default). A weak constraint is one that can be ignored by a dependency resolution algorithm if necessary (e.g. Maven undecorated versions). Typically, an instance of this class is created by using a versioning scheme class to parse a version constraint string. The notation for version constraints is specific to a version scheme (e.g. Maven). Please refer to the Javadoc for the specific version scheme classes for information about the notation and the method to call to create an instance of this class.
  • Field Details

    • ANY

      public static final VersionConstraint ANY
      Constraint allowing any version.
    • EMPTY

      public static final VersionConstraint EMPTY
      Constraint not allowing any version.
  • Constructor Details

    • VersionConstraint

      public VersionConstraint(Version version)
      Constructs a strong version constraint consisting of a single version (i.e. the minimum version equals the maximum version inclusive at both ends).
      Parameters:
      version - Single version comprising the range
    • VersionConstraint

      public VersionConstraint(Version version, boolean weak)
      Constructs a version constraint consisting of a single version (i.e. the minimum version equals the maximum version inclusive at both ends).
      Parameters:
      version - Single version comprising the range
      weak - true if this constraint can be ignored by dependency resolution algorithms
    • VersionConstraint

      public VersionConstraint(@Nullable Version minVersion, @Nullable Version maxVersion, boolean minIncluded, boolean maxIncluded)
      Constructs a strong version range from the specified minimum and maximum versions.
      Parameters:
      minVersion - Version defining the lower bound of the range. Specify null if there is no lower bound.
      maxVersion - Version defining the upper bound of the range Specify null if there is no upper bound.
      minIncluded - true if the minimum version is included in the range. If the minimum version is null, this parameter must be set to false.
      maxIncluded - true if the maximum version is included in the range. If the maximum version is null, this parameter must be set to false.
    • VersionConstraint

      public VersionConstraint(@Nullable Version minVersion, @Nullable Version maxVersion, boolean minIncluded, boolean maxIncluded, boolean weak)
      Constructs a version range from the specified minimum and maximum versions.
      Parameters:
      minVersion - Version defining the lower bound of the range. Specify null if there is no lower bound.
      maxVersion - Version defining the upper bound of the range Specify null if there is no upper bound.
      minIncluded - true if the minimum version is included in the range. If the minimum version is null, this parameter must be set to false.
      maxIncluded - true if the maximum version is included in the range. If the maximum version is null, this parameter must be set to false.
      weak - true if this constraint can be ignored by dependency resolution algorithms
    • VersionConstraint

      public VersionConstraint(List<VersionRange> ranges)
      Constructs a strong constraint consisting of the union of version ranges.
      Parameters:
      ranges - Version ranges that comprise this union
    • VersionConstraint

      public VersionConstraint(List<VersionRange> ranges, boolean weak)
      Constructs a constraint consisting of the union of version ranges.
      Parameters:
      ranges - Version ranges that comprise this union
      weak - true if this constraint can be ignored by dependency resolution algorithms
  • Method Details

    • getRanges

      public List<VersionRange> getRanges()
      Obtains the version ranges comprising this constraint.
      Returns:
      Version ranges comprising this constraint. If the constraint is empty (i.e. contains no version ranges), an empty list is returned.
    • isEmpty

      public boolean isEmpty()
      Indicates whether this constraint does not contain any versions.
      Returns:
      true if this constraint does not contain any versions.
    • isNotEmpty

      public boolean isNotEmpty()
      Indicates whether this constraint contains any versions.
      Returns:
      true if this constraint contains at least one version.
    • isAny

      public boolean isAny()
      Indicates whether this constraint allows all versions.
      Returns:
      true if this constraint allows all versions.
    • isWeak

      public boolean isWeak()
      Indicates whether this constraint can be ignored by a dependency resolution algorithm. For example, Maven treats an undecorated dependency version constraint (e.g. 1.0.0) as "soft" (i.e. weak), meaning that during dependency mediation, the version can be replaced by a different version if necessary.
      Returns:
      true if this is a weak constraint.
    • isSingleVersion

      public boolean isSingleVersion()
      Indicates whether this constraint represents a single version (e.g. [4.0.0]).
      Returns:
      true if this constraint represents a single version.
    • allows

      public boolean allows(Version version)
      Indicates if the specified version is allowed by this constraint.
      Parameters:
      version - Version to test
      Returns:
      true if the specified version is allowed by this constraint.
    • allowsAll

      public boolean allowsAll(VersionConstraint other)
      Indicates if this constraint allows all versions that are allowed by the specified constraint.
      Parameters:
      other - Constraint defining the versions to be allowed by this constraint
      Returns:
      true if this constraint allows all versions allowed by the specified constraint.
    • allowsAny

      public boolean allowsAny(VersionConstraint other)
      Indicates if this constraint allows any of the versions allowed by the specified constraint.
      Parameters:
      other - Constraint defining the versions any of which are allowed by this constraint
      Returns:
      true if this constraint allows any of the versions allowed by the specified constraint.
    • intersect

      public VersionConstraint intersect(VersionConstraint other)
      Creates a constraint that only allows versions allowed by both this and the specified constraints. The following examples show the intersection of this constraint T and the specified constraint C:
       T = []                    C = []                    T ∩ C = []
       T = []                    C = (,)                   T ∩ C = []
       T = [1.5]                 C = []                    T ∩ C = []
       T = [1.5,2.0]             C = [1.7]                 T ∩ C = [1.7]
       T = [1.5,2.0]             C = [1.6,1.9]             T ∩ C = [1.6,1.9]
       T = [1.5,)                C = [1.2,1.8]             T ∩ C = [1.5,1.8]
       T = (,3.0)                C = (,1.8]                T ∩ C = (,1.8]
       T = [1.5,2.0)             C = [1.6],[1.8]           T ∩ C = [1.6],[1.8]
       T = [1.5,2.0),[3.0,4.0)   C = []                    T ∩ C = []
       T = [1.5,2.0),[3.0,4.0)   C = [1.6,1.6]             T ∩ C = [1.6,1.6]
       T = [1.5,2.0),[3.0,4.0)   C = [1.6,1.8]             T ∩ C = [1.6,1.8]
       T = [1.5,2.0),[3.0,4.0)   C = [1.6,1.8],[3.2,3.4]   T ∩ C = [1.6,1.8],[3.2,3.4]
       
      Parameters:
      other - Constraint to intersect with this constraint
      Returns:
      A new constraint that only allows versions allowed by both this and the specified constraints. Returns an empty constraint if there is no intersection between this and the specified constraints.
    • union

      public VersionConstraint union(VersionConstraint other)
      Creates a constraint that allows versions allowed by either this or the specified constraint. The following examples show the union of this constraint T and the specified constraint C:
       T = []                    C = []          T ∪ C = []
       T = []                    C= (,)          T ∪ C = (,)
       T = [1.5]                 C = []          T ∪ C = [1.5]
       T = [1.5]                 C = [1.5]       T ∪ C = [1.5]
       T = [1.5]                 C = [1.6]       T ∪ C = [1.5],[1.6]
       T = [1.5,1.7)             C = [1.6]       T ∪ C = [1.5,1.7)
       T = (1.5,)                C = [1.3,2.0]   T ∪ C = [1.3,)
       T = (,1.7]                C = [1.5,2.0]   T ∪ C = (,2.0]
       T = (,)                   C = [1.5,2.0]   T ∪ C = (,)
       T = [1.2,2.0)             C = (,)         T ∪ C = (,)
       T = (1.0,2.0)             C = (2.0,3.0)   T ∪ C = (1.0,2.0),(2.0,3.0)
       T = [1.5,2.0),[3.0,4.0)   C = [1.6,1.6]   T ∪ C = [1.5,2.0),[3.0,4.0)
       
      Parameters:
      other - Constraint to for a union with this constraint
      Returns:
      A new constraint that allows versions allowed by either this or the specified constraint.
    • difference

      public VersionConstraint difference(VersionConstraint other)
      Creates a constraint that allows versions allowed by this constraint but not by the specified constraint. The following examples show the difference between this constraint T and the specified constraint C:
       T = [1.5]                 C = []                    T - C = [1.5]
       T = [1.5]                 C = [1.5]                 T - C = []
       T = [1.2,2.0)             C = [1.5,1.5]             T - C = [1.2,1.5),(1.5,2.0)
       T = [1.2,2.0)             C = [1.2,1.2]             T - C = (1.2,2.0)
       T = [1.2,2.0)             C = (,)                   T - C = []
       T = [1.2,2.0)             C = [1.5,1.5],[1.7,1.7]   T - C = [1.2,1.5),(1.5,1.7),(1.7,2.0)
       T = [1.5],[2.0,3.0)       C = []                    T - C = [1.5],[2.0,3.0)
       T = [1.5],[2.0,3.0)       C = [1.5,1.5]             T - C = [2.0,3.0)
       T = [1.0,3.0),(4.0,6.0)   C = [2.0],[7.0,8.0)       T - C = [1.0,2.0),(2.0,3.0),(4.0,6.0)
       T = []                    C = []                    T - C = []
       T = []                    C = [1.2,2.0)             T - C = []
       
      Parameters:
      other - Constraint to form the difference with this constraint
      Returns:
      A new constraint that allows versions allowed by this constraint but not by the specified constraint.
    • complement

      public VersionConstraint complement()
      Creates a constraint that is the complement of this constraint. The following examples show the constraint C and its complement C':
       C = []                          C' = (,)
       C = [1.5]                       C' = (,1.5),(1.5,)
       C = [1.2,2.0)                   C' = (,1.2),[2.0,)
       C = [1.5],[2.0,3.0)             C' = (,1.5),(1.5,2.0),[3.0,)
       C = [1.5],[2.0,3.0),[4.0,7.0)   C' = (,1.5),(1.5,2.0),[3.0,4.0),[7.0,)
       
      Returns:
      Complement of this constraint.
    • 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