intersect method

VersionConstraint intersect (VersionConstraint other)

Returns a VersionConstraint that only allows Versions allowed by both this and other.


VersionConstraint intersect(VersionConstraint other) {
  if (other.isEmpty) return other;
  if (other is VersionUnion) return other.intersect(this);

  // A range and a Version just yields the version if it's in the range.
  if (other is Version) {
    return allows(other) ? other : VersionConstraint.empty;

  if (other is VersionRange) {
    // Intersect the two ranges.
    Version intersectMin;
    bool intersectIncludeMin;
    if (allowsLower(this, other)) {
      if (strictlyLower(this, other)) return VersionConstraint.empty;
      intersectMin = other.min;
      intersectIncludeMin = other.includeMin;
    } else {
      if (strictlyLower(other, this)) return VersionConstraint.empty;
      intersectMin = this.min;
      intersectIncludeMin = this.includeMin;

    Version intersectMax;
    bool intersectIncludeMax;
    if (allowsHigher(this, other)) {
      intersectMax = other.max;
      intersectIncludeMax = other.includeMax;
    } else {
      intersectMax = this.max;
      intersectIncludeMax = this.includeMax;

    if (intersectMin == null && intersectMax == null) {
      // Open range.
      return new VersionRange();

    // If the range is just a single version.
    if (intersectMin == intersectMax) {
      // Because we already verified that the lower range isn't strictly
      // lower, there must be some overlap.
      assert(intersectIncludeMin && intersectIncludeMax);
      return intersectMin;

    // If we got here, there is an actual range.
    return new VersionRange(
        min: intersectMin,
        max: intersectMax,
        includeMin: intersectIncludeMin,
        includeMax: intersectIncludeMax,
        alwaysIncludeMaxPreRelease: true);

  throw new ArgumentError('Unknown VersionConstraint type $other.');