intersect method

  1. @override
VersionConstraint intersect(
  1. VersionConstraint other
)
override

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

Implementation

@override
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 = min;
      intersectIncludeMin = includeMin;
    }

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

    if (intersectMin == null && intersectMax == null) {
      // Open range.
      return 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 VersionRange(
        min: intersectMin,
        max: intersectMax,
        includeMin: intersectIncludeMin,
        includeMax: intersectIncludeMax,
        alwaysIncludeMaxPreRelease: true);
  }

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